test_router_mandatory.cpp 8.9 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 33 34 35 36
#include "testutil_unity.hpp"

#include <unity.h>

void setUp ()
{
37
    setup_test_context ();
38 39 40 41
}

void tearDown ()
{
42
    teardown_test_context ();
43
}
44

45
#ifdef ZMQ_BUILD_DRAFT_API
46
bool send_msg_to_peer_if_ready (void *router_, const char *peer_routing_id_)
47
{
48
    int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
49 50
      zmq_socket_get_peer_state (router_, peer_routing_id_, 1),
      peer_routing_id_);
51
    if (rc & ZMQ_POLLOUT) {
52
        send_string_expect_success (router_, peer_routing_id_,
53
                                    ZMQ_SNDMORE | ZMQ_DONTWAIT);
54
        send_string_expect_success (router_, "Hello", ZMQ_DONTWAIT);
55 56 57 58 59 60 61

        return true;
    }
    return false;
}
#endif

62 63 64
void test_get_peer_state ()
{
#ifdef ZMQ_BUILD_DRAFT_API
65
    void *router = test_context_socket (ZMQ_ROUTER);
66 67

    int mandatory = 1;
68 69
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_ROUTER_MANDATORY,
                                               &mandatory, sizeof (mandatory)));
70

71
    const char *my_endpoint = "inproc://test_get_peer_state";
72
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, my_endpoint));
73

74 75
    void *dealer1 = test_context_socket (ZMQ_DEALER);
    void *dealer2 = test_context_socket (ZMQ_DEALER);
76 77

    //  Lower HWMs to allow doing the test with fewer messages
78 79 80 81 82 83 84
    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)));
85

86 87
    const char *dealer1_routing_id = "X";
    const char *dealer2_routing_id = "Y";
88 89

    //  Name dealer1 "X" and connect it to our router
90 91 92
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer1, ZMQ_ROUTING_ID, dealer1_routing_id, 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer1, my_endpoint));
93

94
    //  Name dealer2 "Y" and connect it to our router
95 96 97
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (dealer2, ZMQ_ROUTING_ID, dealer2_routing_id, 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer2, my_endpoint));
98

99
    //  Get message from both dealers to know when connection is ready
100 101 102 103 104 105 106
    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);
107

108
    void *poller = zmq_poller_new ();
109
    TEST_ASSERT_NOT_NULL (poller);
110 111

    //  Poll on router and dealer1, but not on dealer2
112 113 114 115
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_poller_add (poller, router, NULL, ZMQ_POLLOUT));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_poller_add (poller, dealer1, NULL, ZMQ_POLLIN));
116

117 118
    const unsigned int count = 10000;
    const unsigned int event_size = 2;
119
    bool dealer2_blocked = false;
120
    unsigned int dealer1_sent = 0, dealer2_sent = 0, dealer1_received = 0;
121
    zmq_poller_event_t events[event_size];
122
    for (unsigned int iteration = 0; iteration < count; ++iteration) {
123 124
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_poller_wait_all (poller, events, event_size, -1));
125
        for (unsigned int event_no = 0; event_no < event_size; ++event_no) {
126 127 128
            const zmq_poller_event_t &current_event = events[event_no];
            if (current_event.socket == router
                && current_event.events & ZMQ_POLLOUT) {
129
                if (send_msg_to_peer_if_ready (router, dealer1_routing_id))
130
                    ++dealer1_sent;
131

132
                if (send_msg_to_peer_if_ready (router, dealer2_routing_id))
133
                    ++dealer2_sent;
134
                else
135
                    dealer2_blocked = true;
136
            }
137 138
            if (current_event.socket == dealer1
                && current_event.events & ZMQ_POLLIN) {
139
                recv_string_expect_success (dealer1, "Hello", ZMQ_DONTWAIT);
140 141
                int more;
                size_t more_size = sizeof (more);
142 143 144
                TEST_ASSERT_SUCCESS_ERRNO (
                  zmq_getsockopt (dealer1, ZMQ_RCVMORE, &more, &more_size));
                TEST_ASSERT_FALSE (more);
145 146

                ++dealer1_received;
147 148 149 150
            }
            // never read from dealer2, so its pipe becomes full eventually
        }
    }
151
    printf ("dealer1_sent = %u, dealer2_sent = %u, dealer1_received = %u\n",
152
            dealer1_sent, dealer2_sent, dealer1_received);
153
    TEST_ASSERT_TRUE (dealer2_blocked);
154
    zmq_poller_destroy (&poller);
155

156 157 158
    test_context_socket_close (router);
    test_context_socket_close (dealer1);
    test_context_socket_close (dealer2);
159 160 161
#endif
}

162 163 164
void test_get_peer_state_corner_cases ()
{
#ifdef ZMQ_BUILD_DRAFT_API
165
    const char peer_routing_id[] = "foo";
166 167

    //  call get_peer_state with NULL socket
168 169
    int rc = zmq_socket_get_peer_state (NULL, peer_routing_id,
                                        strlen (peer_routing_id));
170 171
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (ENOTSOCK, errno);
172

173 174
    void *dealer = test_context_socket (ZMQ_DEALER);
    void *router = test_context_socket (ZMQ_ROUTER);
175 176

    //  call get_peer_state with a non-ROUTER socket
177 178
    rc = zmq_socket_get_peer_state (dealer, peer_routing_id,
                                    strlen (peer_routing_id));
179 180
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (ENOTSUP, errno);
181

182
    //  call get_peer_state for an unknown routing id
183 184
    rc = zmq_socket_get_peer_state (router, peer_routing_id,
                                    strlen (peer_routing_id));
185 186
    TEST_ASSERT_EQUAL_INT (-1, rc);
    TEST_ASSERT_EQUAL_INT (EHOSTUNREACH, errno);
187

188 189
    test_context_socket_close (router);
    test_context_socket_close (dealer);
190 191 192
#endif
}

193
void test_basic ()
194
{
195 196
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
197
    void *router = test_context_socket (ZMQ_ROUTER);
198

199 200 201
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (router, "tcp://127.0.0.1:*"));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_getsockopt (router, ZMQ_LAST_ENDPOINT, my_endpoint, &len));
202

203 204
    //  Send a message to an unknown peer with the default setting
    //  This will not report any error
205 206
    send_string_expect_success (router, "UNKNOWN", ZMQ_SNDMORE);
    send_string_expect_success (router, "DATA", 0);
207

208 209
    //  Send a message to an unknown peer with mandatory routing
    //  This will fail
210
    int mandatory = 1;
211 212 213 214 215
    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);
216

217
    //  Create dealer called "X" and connect it to our router
218
    void *dealer = test_context_socket (ZMQ_DEALER);
219 220
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (dealer, ZMQ_ROUTING_ID, "X", 1));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
221

222
    //  Get message from dealer to know when connection is ready
223 224
    send_string_expect_success (dealer, "Hello", 0);
    recv_string_expect_success (router, "X", 0);
225

226 227
    //  Send a message to connected dealer now
    //  It should work
228 229
    send_string_expect_success (router, "X", ZMQ_SNDMORE);
    send_string_expect_success (router, "Hello", 0);
230

231 232
    test_context_socket_close (router);
    test_context_socket_close (dealer);
233 234 235 236 237 238
}

int main (void)
{
    setup_test_environment ();

239 240 241 242
    UNITY_BEGIN ();
    RUN_TEST (test_basic);
    RUN_TEST (test_get_peer_state);
    RUN_TEST (test_get_peer_state_corner_cases);
243

244
    return UNITY_END ();
245
}