test_disconnect_inproc.cpp 5.01 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

    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 34
#include <string.h>

35
SETUP_TEARDOWN_TESTCONTEXT
36 37

/// Initialize a zeromq message with a given null-terminated string
38
#define ZMQ_PREPARE_STRING(msg, data, size)                                    \
39 40
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));                           \
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, size + 1));            \
41
    memcpy (zmq_msg_data (&msg), data, size + 1);
42

43 44
static int publicationsReceived = 0;
static bool isSubscribed = false;
45

46
void test_disconnect_inproc ()
47
{
48 49 50 51
    void *pub_socket = test_context_socket (ZMQ_XPUB);
    void *sub_socket = test_context_socket (ZMQ_SUB);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sub_socket, ZMQ_SUBSCRIBE, "foo", 3));
52

53 54
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_bind (pub_socket, "inproc://someInProcDescriptor"));
55

56
    int more;
57 58
    size_t more_size = sizeof (more);

59
    for (int iteration = 0;; ++iteration) {
60
        zmq_pollitem_t items[] = {
61 62
          {sub_socket, 0, ZMQ_POLLIN, 0}, // read publications
          {pub_socket, 0, ZMQ_POLLIN, 0}, // read subscriptions
63
        };
64
        int rc = zmq_poll (items, 2, 100);
65 66

        if (items[1].revents & ZMQ_POLLIN) {
67
            for (more = 1; more;) {
68 69
                zmq_msg_t msg;
                zmq_msg_init (&msg);
70
                TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, pub_socket, 0));
71
                char *buffer = (char *) zmq_msg_data (&msg);
72 73

                if (buffer[0] == 0) {
74
                    TEST_ASSERT_TRUE (isSubscribed);
75
                    isSubscribed = false;
76
                } else {
77
                    TEST_ASSERT_FALSE (isSubscribed);
78 79 80
                    isSubscribed = true;
                }

81 82 83
                TEST_ASSERT_SUCCESS_ERRNO (
                  zmq_getsockopt (pub_socket, ZMQ_RCVMORE, &more, &more_size));
                TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
84 85 86 87
            }
        }

        if (items[0].revents & ZMQ_POLLIN) {
88 89
            more = 1;
            for (more = 1; more;) {
90
                zmq_msg_t msg;
91 92 93 94 95
                TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
                TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, sub_socket, 0));
                TEST_ASSERT_SUCCESS_ERRNO (
                  zmq_getsockopt (sub_socket, ZMQ_RCVMORE, &more, &more_size));
                TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
96
            }
97
            publicationsReceived++;
98 99
        }
        if (iteration == 1) {
100 101
            TEST_ASSERT_SUCCESS_ERRNO (
              zmq_connect (sub_socket, "inproc://someInProcDescriptor"));
102
            msleep (SETTLE_TIME);
103 104
        }
        if (iteration == 4) {
105 106
            TEST_ASSERT_SUCCESS_ERRNO (
              zmq_disconnect (sub_socket, "inproc://someInProcDescriptor"));
107
        }
108
        if (iteration > 4 && rc == 0)
109
            break;
110

111 112
        zmq_msg_t channel_envlp;
        ZMQ_PREPARE_STRING (channel_envlp, "foo", 3);
113 114 115
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_msg_send (&channel_envlp, pub_socket, ZMQ_SNDMORE));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&channel_envlp));
116 117

        zmq_msg_t message;
118
        ZMQ_PREPARE_STRING (message, "this is foo!", 12);
119 120
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&message, pub_socket, 0));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&message));
121
    }
122 123
    TEST_ASSERT_EQUAL_INT (3, publicationsReceived);
    TEST_ASSERT_FALSE (isSubscribed);
124

125 126 127 128 129 130 131
    test_context_socket_close (pub_socket);
    test_context_socket_close (sub_socket);
}

int main (int, char **)
{
    setup_test_environment ();
132

133 134 135
    UNITY_BEGIN ();
    RUN_TEST (test_disconnect_inproc);
    return UNITY_END ();
136
}