| 1 | /* |
| 2 | Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #include "testutil.hpp" |
| 31 | #include "testutil_unity.hpp" |
| 32 | |
| 33 | SETUP_TEARDOWN_TESTCONTEXT |
| 34 | |
| 35 | // DEBUG shouldn't be defined in sources as it will cause a redefined symbol |
| 36 | // error when it is defined in the build configuration. It appears that the |
| 37 | // intent here is to semi-permanently disable DEBUG tracing statements, so the |
| 38 | // implementation is changed to accomodate that intent. |
| 39 | //#define DEBUG 0 |
| 40 | #define TRACE_ENABLED 0 |
| 41 | |
| 42 | void test_router_mandatory_hwm () |
| 43 | { |
| 44 | if (TRACE_ENABLED) |
| 45 | fprintf (stderr, "Staring router mandatory HWM test ...\n" ); |
| 46 | char my_endpoint[MAX_SOCKET_STRING]; |
| 47 | void *router = test_context_socket (ZMQ_ROUTER); |
| 48 | |
| 49 | // Configure router socket to mandatory routing and set HWM and linger |
| 50 | int mandatory = 1; |
| 51 | TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY, |
| 52 | &mandatory, sizeof (mandatory))); |
| 53 | int sndhwm = 1; |
| 54 | TEST_ASSERT_SUCCESS_ERRNO ( |
| 55 | zmq_setsockopt (router, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm))); |
| 56 | int linger = 1; |
| 57 | TEST_ASSERT_SUCCESS_ERRNO ( |
| 58 | zmq_setsockopt (router, ZMQ_LINGER, &linger, sizeof (linger))); |
| 59 | |
| 60 | bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint); |
| 61 | |
| 62 | // Create dealer called "X" and connect it to our router, configure HWM |
| 63 | void *dealer = test_context_socket (ZMQ_DEALER); |
| 64 | TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X" , 1)); |
| 65 | int rcvhwm = 1; |
| 66 | TEST_ASSERT_SUCCESS_ERRNO ( |
| 67 | zmq_setsockopt (dealer, ZMQ_RCVHWM, &rcvhwm, sizeof (rcvhwm))); |
| 68 | |
| 69 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint)); |
| 70 | |
| 71 | // Get message from dealer to know when connection is ready |
| 72 | send_string_expect_success (dealer, "Hello" , 0); |
| 73 | recv_string_expect_success (router, "X" , 0); |
| 74 | |
| 75 | int i; |
| 76 | const int buf_size = 65536; |
| 77 | const uint8_t buf[buf_size] = {0}; |
| 78 | // Send first batch of messages |
| 79 | for (i = 0; i < 100000; ++i) { |
| 80 | if (TRACE_ENABLED) |
| 81 | fprintf (stderr, "Sending message %d ...\n" , i); |
| 82 | const int rc = zmq_send (router, "X" , 1, ZMQ_DONTWAIT | ZMQ_SNDMORE); |
| 83 | if (rc == -1 && zmq_errno () == EAGAIN) |
| 84 | break; |
| 85 | TEST_ASSERT_EQUAL_INT (1, rc); |
| 86 | send_array_expect_success (router, buf, ZMQ_DONTWAIT); |
| 87 | } |
| 88 | // This should fail after one message but kernel buffering could |
| 89 | // skew results |
| 90 | TEST_ASSERT_LESS_THAN_INT (10, i); |
| 91 | msleep (1000); |
| 92 | // Send second batch of messages |
| 93 | for (; i < 100000; ++i) { |
| 94 | if (TRACE_ENABLED) |
| 95 | fprintf (stderr, "Sending message %d (part 2) ...\n" , i); |
| 96 | const int rc = zmq_send (router, "X" , 1, ZMQ_DONTWAIT | ZMQ_SNDMORE); |
| 97 | if (rc == -1 && zmq_errno () == EAGAIN) |
| 98 | break; |
| 99 | TEST_ASSERT_EQUAL_INT (1, rc); |
| 100 | send_array_expect_success (router, buf, ZMQ_DONTWAIT); |
| 101 | } |
| 102 | // This should fail after two messages but kernel buffering could |
| 103 | // skew results |
| 104 | TEST_ASSERT_LESS_THAN_INT (20, i); |
| 105 | |
| 106 | if (TRACE_ENABLED) |
| 107 | fprintf (stderr, "Done sending messages.\n" ); |
| 108 | |
| 109 | test_context_socket_close (router); |
| 110 | test_context_socket_close (dealer); |
| 111 | } |
| 112 | |
| 113 | int main () |
| 114 | { |
| 115 | setup_test_environment (); |
| 116 | |
| 117 | UNITY_BEGIN (); |
| 118 | RUN_TEST (test_router_mandatory_hwm); |
| 119 | return UNITY_END (); |
| 120 | } |
| 121 | |