test_router_mandatory.cpp 8.7 KB
Newer Older
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 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
25 26 27 28 29

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "testutil.hpp"
31 32
#include "testutil_unity.hpp"

33
#include <string.h>
34

35
SETUP_TEARDOWN_TESTCONTEXT
36

37
#ifdef ZMQ_BUILD_DRAFT_API
38
bool send_msg_to_peer_if_ready (void *router_, const char *peer_routing_id_)
39
{
40
    int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
41 42
      zmq_socket_get_peer_state (router_, peer_routing_id_, 1),
      peer_routing_id_);
43
    if (rc & ZMQ_POLLOUT) {
44
        send_string_expect_success (router_, peer_routing_id_,
45
                                    ZMQ_SNDMORE | ZMQ_DONTWAIT);
46
        send_string_expect_success (router_, "Hello", ZMQ_DONTWAIT);
47 48 49 50 51 52 53

        return true;
    }
    return false;
}
#endif

54 55 56
void test_get_peer_state ()
{
#ifdef ZMQ_BUILD_DRAFT_API
57
    void *router = test_context_socket (ZMQ_ROUTER);
58 59

    int mandatory = 1;
60 61
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
                                               &mandatory, sizeof (mandatory)));
62

63
    const char *my_endpoint = "inproc://test_get_peer_state";
64
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, my_endpoint));
65

66 67
    void *dealer1 = test_context_socket (ZMQ_DEALER);
    void *dealer2 = test_context_socket (ZMQ_DEALER);
68 69

    //  Lower HWMs to allow doing the test with fewer messages
70 71 72 73 74 75 76
    const int hwm = 100;
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (router, ZMQ_SNDHWM, &hwm, sizeof (int)));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer1, ZMQ_RCVHWM, &hwm, sizeof (int)));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer2, ZMQ_RCVHWM, &hwm, sizeof (int)));
77

78 79
    const char *dealer1_routing_id = "X";
    const char *dealer2_routing_id = "Y";
80 81

    //  Name dealer1 "X" and connect it to our router
82 83 84
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer1, my_endpoint));
85

86
    //  Name dealer2 "Y" and connect it to our router
87 88 89
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer2, my_endpoint));
90

91
    //  Get message from both dealers to know when connection is ready
92 93 94 95 96 97 98
    send_string_expect_success (dealer1, "Hello", 0);
    recv_string_expect_success (router, dealer1_routing_id, 0);
    recv_string_expect_success (router, "Hello", 0);

    send_string_expect_success (dealer2, "Hello", 0);
    recv_string_expect_success (router, dealer2_routing_id, 0);
    recv_string_expect_success (router, "Hello", 0);
99

100
    void *poller = zmq_poller_new ();
101
    TEST_ASSERT_NOT_NULL (poller);
102 103

    //  Poll on router and dealer1, but not on dealer2
104 105 106 107
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
108

109 110
    const unsigned int count = 10000;
    const unsigned int event_size = 2;
111
    bool dealer2_blocked = false;
112
    unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
113
    zmq_poller_event_t events[event_size];
114
    for (unsigned int iteration = 0; iteration < count; ++iteration) {
115 116
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_poller_wait_all (poller, events, event_size, -1));
117
        for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
118 119 120
            const zmq_poller_event_t &current_event = events[event_no];
            if (current_event.socket == router
                && current_event.events & ZMQ_POLLOUT) {
121
                if (send_msg_to_peer_if_ready (router, dealer1_routing_id))
122
                    ++dealer1_sent;
123

124
                if (send_msg_to_peer_if_ready (router, dealer2_routing_id))
125
                    ++dealer2_sent;
126
                else
127
                    dealer2_blocked = true;
128
            }
129 130
            if (current_event.socket == dealer1
                && current_event.events & ZMQ_POLLIN) {
131
                recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
132 133
                int more;
                size_t more_size = sizeof (more);
134 135 136
                TEST_ASSERT_SUCCESS_ERRNO (
                  zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
                TEST_ASSERT_FALSE (more);
137 138

                ++dealer1_received;
139 140 141 142
            }
            // never read from dealer2, so its pipe becomes full eventually
        }
    }
143
    printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
144
            dealer1_sent, dealer2_sent, dealer1_received);
145
    TEST_ASSERT_TRUE (dealer2_blocked);
146
    zmq_poller_destroy (&poller);
147

148 149 150
    test_context_socket_close (router);
    test_context_socket_close (dealer1);
    test_context_socket_close (dealer2);
151 152 153
#endif
}

154 155 156
void test_get_peer_state_corner_cases ()
{
#ifdef ZMQ_BUILD_DRAFT_API
157
    const char peer_routing_id[] = "foo";
158 159

    //  call get_peer_state with NULL socket
160 161
    int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
                                        strlen (peer_routing_id));
162 163
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno);
164

165 166
    void *dealer = test_context_socket (ZMQ_DEALER);
    void *router = test_context_socket (ZMQ_ROUTER);
167 168

    //  call get_peer_state with a non-ROUTER socket
169 170
    rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
                                    strlen (peer_routing_id));
171 172
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (ENOTSUP, errno);
173

174
    //  call get_peer_state for an unknown routing id
175 176
    rc = zmq_socket_get_peer_state (router, peer_routing_id,
                                    strlen (peer_routing_id));
177 178
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
179

180 181
    test_context_socket_close (router);
    test_context_socket_close (dealer);
182 183 184
#endif
}

185
void test_basic ()
186
{
187
    char my_endpoint[MAX_SOCKET_STRING];
188
    void *router = test_context_socket (ZMQ_ROUTER);
189
    bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint);
190

191 192
    //  Send a message to an unknown peer with the default setting
    //  This will not report any error
193 194
    send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
    send_string_expect_success (router, "DATA", 0);
195

196 197
    //  Send a message to an unknown peer with mandatory routing
    //  This will fail
198
    int mandatory = 1;
199 200 201 202 203
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
                                               &mandatory, sizeof (mandatory)));
    int rc = zmq_send (router, "UNKNOWN", 7, ZMQ_SNDMORE);
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
204

205
    //  Create dealer called "X" and connect it to our router
206
    void *dealer = test_context_socket (ZMQ_DEALER);
207 208
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
209

210
    //  Get message from dealer to know when connection is ready
211 212
    send_string_expect_success (dealer, "Hello", 0);
    recv_string_expect_success (router, "X", 0);
213

214 215
    //  Send a message to connected dealer now
    //  It should work
216 217
    send_string_expect_success (router, "X", ZMQ_SNDMORE);
    send_string_expect_success (router, "Hello", 0);
218

219 220
    test_context_socket_close (router);
    test_context_socket_close (dealer);
221 222 223 224 225 226
}

int main (void)
{
    setup_test_environment ();

227 228 229 230
    UNITY_BEGIN ();
    RUN_TEST (test_basic);
    RUN_TEST (test_get_peer_state);
    RUN_TEST (test_get_peer_state_corner_cases);
231

232
    return UNITY_END ();
233
}