test_router_mandatory.cpp 8.89 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 49
    int rc = TEST_ASSERT_SUCCESS_MESSAGE_ERRNO (
      zmq_socket_get_peer_state (router, peer_routing_id, 1), peer_routing_id);
50
    if (rc & ZMQ_POLLOUT) {
51 52 53
        send_string_expect_success (router, peer_routing_id,
                                    ZMQ_SNDMORE | ZMQ_DONTWAIT);
        send_string_expect_success (router, "Hello", ZMQ_DONTWAIT);
54 55 56 57 58 59 60

        return true;
    }
    return false;
}
#endif

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

198 199 200
    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));
201

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

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

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

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

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

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

int main (void)
{
    setup_test_environment ();

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

243
    return UNITY_END ();
244
}