test_inproc_connect.cpp 13.4 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2016 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 30 31

    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/>.
*/

#include "testutil.hpp"

32 33 34 35 36
static void pusher (void *ctx)
{
    // Connect first
    void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (connectSocket);
37
    int rc = zmq_connect (connectSocket, "inproc://sink");
38 39 40 41 42 43 44 45 46 47 48
    assert (rc == 0);

    // Queue up some data
    rc = zmq_send_const (connectSocket, "foobar", 6, 0);
    assert (rc == 6);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
static void simult_conn (void *payload)
{
    // Pull out arguments - context followed by endpoint string
    void* ctx   = (void*)((void**)payload)[0];
    char* endpt = (char*)((void**)payload)[1];

    // Connect
    void *connectSocket = zmq_socket (ctx, ZMQ_SUB);
    assert (connectSocket);
    int rc = zmq_connect (connectSocket, endpt);
    assert (rc == 0);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);
}

static void simult_bind (void *payload)
{
    // Pull out arguments - context followed by endpoint string
    void* ctx   = (void*)((void**)payload)[0];
    char* endpt = (char*)((void**)payload)[1];

    // Bind
    void *bindSocket = zmq_socket (ctx, ZMQ_PUB);
    assert (bindSocket);
    int rc = zmq_bind (bindSocket, endpt);
    assert (rc == 0);

    // Cleanup
    rc = zmq_close (bindSocket);
    assert (rc == 0);
}

83
void test_bind_before_connect ()
84 85 86 87 88 89 90
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    // Bind first
    void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (bindSocket);
91
    int rc = zmq_bind (bindSocket, "inproc://bbc");
92 93 94 95 96
    assert (rc == 0);

    // Now connect
    void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (connectSocket);
97
    rc = zmq_connect (connectSocket, "inproc://bbc");
98
    assert (rc == 0);
99

100 101 102 103 104 105 106 107
    // Queue up some data
    rc = zmq_send_const (connectSocket, "foobar", 6, 0);
    assert (rc == 6);

    // Read pending message
    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);
108
    rc = zmq_msg_recv (&msg, bindSocket, 0);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    assert (rc == 6);
    void *data = zmq_msg_data (&msg);
    assert (memcmp ("foobar", data, 6) == 0);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);

    rc = zmq_close (bindSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

124
void test_connect_before_bind ()
125 126 127 128 129 130 131
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    // Connect first
    void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (connectSocket);
132
    int rc = zmq_connect (connectSocket, "inproc://cbb");
133 134 135 136 137 138 139 140 141
    assert (rc == 0);

    // Queue up some data
    rc = zmq_send_const (connectSocket, "foobar", 6, 0);
    assert (rc == 6);

    // Now bind
    void *bindSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (bindSocket);
142
    rc = zmq_bind (bindSocket, "inproc://cbb");
143
    assert (rc == 0);
144

145 146 147 148
    // Read pending message
    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);
149
    rc = zmq_msg_recv (&msg, bindSocket, 0);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    assert (rc == 6);
    void *data = zmq_msg_data (&msg);
    assert (memcmp ("foobar", data, 6) == 0);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);

    rc = zmq_close (bindSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

165
void test_connect_before_bind_pub_sub ()
166 167 168 169 170 171 172
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    // Connect first
    void *connectSocket = zmq_socket (ctx, ZMQ_PUB);
    assert (connectSocket);
173
    int rc = zmq_connect (connectSocket, "inproc://cbbps");
174 175 176 177 178 179 180 181 182 183 184
    assert (rc == 0);

    // Queue up some data, this will be dropped
    rc = zmq_send_const (connectSocket, "before", 6, 0);
    assert (rc == 6);

    // Now bind
    void *bindSocket = zmq_socket (ctx, ZMQ_SUB);
    assert (bindSocket);
    rc = zmq_setsockopt (bindSocket, ZMQ_SUBSCRIBE, "", 0);
    assert (rc == 0);
185
    rc = zmq_bind (bindSocket, "inproc://cbbps");
186
    assert (rc == 0);
187

188
    // Wait for pub-sub connection to happen
189
    msleep (SETTLE_TIME);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

    // Queue up some data, this not will be dropped
    rc = zmq_send_const (connectSocket, "after", 6, 0);
    assert (rc == 6);

    // Read pending message
    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);
    rc = zmq_msg_recv (&msg, bindSocket, 0);
    assert (rc == 6);
    void *data = zmq_msg_data (&msg);
    assert (memcmp ("after", data, 5) == 0);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);

    rc = zmq_close (bindSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
void test_connect_before_bind_ctx_term ()
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    for (int i = 0; i < 20; ++i) {
        // Connect first
        void *connectSocket = zmq_socket (ctx, ZMQ_ROUTER);
        assert (connectSocket);

        char ep[20];
        sprintf(ep, "inproc://cbbrr%d", i);
        int rc = zmq_connect (connectSocket, ep);
        assert (rc == 0);

        // Cleanup
        rc = zmq_close (connectSocket);
        assert (rc == 0);
    }

    int rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

239
void test_multiple_connects ()
240 241 242 243 244 245
{
    const unsigned int no_of_connects = 10;
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    int rc;
246
    void *connectSocket [no_of_connects];
247 248 249 250 251 252

    // Connect first
    for (unsigned int i = 0; i < no_of_connects; ++i)
    {
        connectSocket [i] = zmq_socket (ctx, ZMQ_PUSH);
        assert (connectSocket [i]);
253
        rc = zmq_connect (connectSocket [i], "inproc://multiple");
254 255 256 257 258 259 260 261 262 263
        assert (rc == 0);

        // Queue up some data
        rc = zmq_send_const (connectSocket [i], "foobar", 6, 0);
        assert (rc == 6);
    }

    // Now bind
    void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
    assert (bindSocket);
264
    rc = zmq_bind (bindSocket, "inproc://multiple");
265
    assert (rc == 0);
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    for (unsigned int i = 0; i < no_of_connects; ++i)
    {
        // Read pending message
        zmq_msg_t msg;
        rc = zmq_msg_init (&msg);
        assert (rc == 0);
        rc = zmq_msg_recv (&msg, bindSocket, 0);
        assert (rc == 6);
        void *data = zmq_msg_data (&msg);
        assert (memcmp ("foobar", data, 6) == 0);
    }

    // Cleanup
    for (unsigned int i = 0; i < no_of_connects; ++i)
    {
        rc = zmq_close (connectSocket [i]);
        assert (rc == 0);
    }

    rc = zmq_close (bindSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

293
void test_multiple_threads ()
294
{
295
    const unsigned int no_of_threads = 30;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    int rc;
    void *threads [no_of_threads];

    // Connect first
    for (unsigned int i = 0; i < no_of_threads; ++i)
    {
        threads [i] = zmq_threadstart (&pusher, ctx);
    }

    // Now bind
    void *bindSocket = zmq_socket (ctx, ZMQ_PULL);
    assert (bindSocket);
311
    rc = zmq_bind (bindSocket, "inproc://sink");
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    assert (rc == 0);

    for (unsigned int i = 0; i < no_of_threads; ++i)
    {
        // Read pending message
        zmq_msg_t msg;
        rc = zmq_msg_init (&msg);
        assert (rc == 0);
        rc = zmq_msg_recv (&msg, bindSocket, 0);
        assert (rc == 6);
        void *data = zmq_msg_data (&msg);
        assert (memcmp ("foobar", data, 6) == 0);
    }

    // Cleanup
    for (unsigned int i = 0; i < no_of_threads; ++i)
    {
        zmq_threadclose (threads [i]);
    }

    rc = zmq_close (bindSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
void test_simultaneous_connect_bind_threads ()
{
    const unsigned int no_of_times = 50;
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    void *threads[no_of_times*2];
    void *thr_args[no_of_times][2];
    char endpts[no_of_times][20];

    // Set up thread arguments: context followed by endpoint string
    for (unsigned int i = 0; i < no_of_times; ++i)
    {
        thr_args[i][0] = (void*) ctx;
        thr_args[i][1] = (void*) endpts[i];
        sprintf (endpts[i], "inproc://foo_%d", i);
    }

    // Spawn all threads as simultaneously as possible
    for (unsigned int i = 0; i < no_of_times; ++i)
    {
        threads[i*2+0] = zmq_threadstart (&simult_conn, (void*)thr_args[i]);
        threads[i*2+1] = zmq_threadstart (&simult_bind, (void*)thr_args[i]);
    }

    // Close all threads
    for (unsigned int i = 0; i < no_of_times; ++i)
    {
        zmq_threadclose (threads[i*2+0]);
        zmq_threadclose (threads[i*2+1]);
    }

    int rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

375
void test_identity ()
376 377 378 379
{
    //  Create the infrastructure
    void *ctx = zmq_ctx_new ();
    assert (ctx);
380

381 382
    void *sc = zmq_socket (ctx, ZMQ_DEALER);
    assert (sc);
383

384
    int rc = zmq_connect (sc, "inproc://identity");
385
    assert (rc == 0);
386

387 388
    void *sb = zmq_socket (ctx, ZMQ_ROUTER);
    assert (sb);
389

390
    rc = zmq_bind (sb, "inproc://identity");
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    assert (rc == 0);

    //  Send 2-part message.
    rc = zmq_send (sc, "A", 1, ZMQ_SNDMORE);
    assert (rc == 1);
    rc = zmq_send (sc, "B", 1, 0);
    assert (rc == 1);

    //  Identity comes first.
    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);
    rc = zmq_msg_recv (&msg, sb, 0);
    assert (rc >= 0);
    int more = zmq_msg_more (&msg);
    assert (more == 1);

    //  Then the first part of the message body.
    rc = zmq_msg_recv (&msg, sb, 0);
    assert (rc == 1);
    more = zmq_msg_more (&msg);
    assert (more == 1);

    //  And finally, the second part of the message body.
    rc = zmq_msg_recv (&msg, sb, 0);
    assert (rc == 1);
    more = zmq_msg_more (&msg);
    assert (more == 0);

    //  Deallocate the infrastructure.
    rc = zmq_close (sc);
    assert (rc == 0);
423

424 425
    rc = zmq_close (sb);
    assert (rc == 0);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

void test_connect_only ()
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    void *connectSocket = zmq_socket (ctx, ZMQ_PUSH);
    assert (connectSocket);
    int rc = zmq_connect (connectSocket, "inproc://a");
    assert (rc == 0);

    rc = zmq_close (connectSocket);
    assert (rc == 0);

444 445 446 447
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

void test_unbind ()
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    // Bind and unbind socket 1
    void *bindSocket1 = zmq_socket (ctx, ZMQ_PAIR);
    assert (bindSocket1);
    int rc = zmq_bind (bindSocket1, "inproc://unbind");
    assert (rc == 0);
    zmq_unbind (bindSocket1, "inproc://unbind");
    assert (rc == 0);

    // Bind socket 2
    void *bindSocket2 = zmq_socket (ctx, ZMQ_PAIR);
    assert (bindSocket2);
    rc = zmq_bind (bindSocket2, "inproc://unbind");
    assert (rc == 0);

    // Now connect
    void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (connectSocket);
    rc = zmq_connect (connectSocket, "inproc://unbind");
    assert (rc == 0);

    // Queue up some data
    rc = zmq_send_const (connectSocket, "foobar", 6, 0);
    assert (rc == 6);

    // Read pending message
    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);
    rc = zmq_msg_recv (&msg, bindSocket2, 0);
    assert (rc == 6);
    void *data = zmq_msg_data (&msg);
    assert (memcmp ("foobar", data, 6) == 0);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);
    rc = zmq_close (bindSocket1);
    assert (rc == 0);
    rc = zmq_close (bindSocket2);
    assert (rc == 0);
    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
void test_shutdown_during_pend ()
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    // Connect first
    void *connectSocket = zmq_socket (ctx, ZMQ_PAIR);
    assert (connectSocket);
    int rc = zmq_connect (connectSocket, "inproc://cbb");
    assert (rc == 0);

    zmq_ctx_shutdown (ctx);

    // Cleanup
    rc = zmq_close (connectSocket);
    assert (rc == 0);

    rc = zmq_ctx_term (ctx);
    assert (rc == 0);
}

519 520
int main (void)
{
521
    setup_test_environment ();
522

523 524 525
    test_bind_before_connect ();
    test_connect_before_bind ();
    test_connect_before_bind_pub_sub ();
526
    test_connect_before_bind_ctx_term ();
527 528
    test_multiple_connects ();
    test_multiple_threads ();
529
    test_simultaneous_connect_bind_threads ();
530
    test_identity ();
531
    test_connect_only ();
532
    test_unbind ();
533
    test_shutdown_during_pend ();
534

535
    return 0;
536
}