test_stream_timeout.cpp 7.88 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 30 31 32 33 34 35

    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"

//  Read one event off the monitor socket; return value and address
//  by reference, if not null, and event number by value. Returns -1
//  in case of error.

36
static int get_monitor_event (void *monitor, int *value, char **address)
37 38
{
    //  First frame in message contains event number and value
39
    zmq_msg_t msg;
40 41
    zmq_msg_init (&msg);
    if (zmq_msg_recv (&msg, monitor, 0) == -1)
42
        return -1; //  Interruped, presumably
43
    assert (zmq_msg_more (&msg));
44

45 46 47 48 49 50 51 52
    uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
    uint16_t event = *(uint16_t *) (data);
    if (value)
        *value = *(uint32_t *) (data + 2);

    //  Second frame in message contains event address
    zmq_msg_init (&msg);
    if (zmq_msg_recv (&msg, monitor, 0) == -1)
53
        return -1; //  Interruped, presumably
54
    assert (!zmq_msg_more (&msg));
55

56 57 58 59 60
    if (address) {
        uint8_t *data = (uint8_t *) zmq_msg_data (&msg);
        size_t size = zmq_msg_size (&msg);
        *address = (char *) malloc (size + 1);
        memcpy (*address, data, size);
61
        *address[size] = 0;
62 63 64 65
    }
    return event;
}

66
static void test_stream_handshake_timeout_accept (void)
67 68
{
    int rc;
69 70
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    //  Set up our context and sockets
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    //  We use this socket in raw mode, to make a connection and send nothing
    void *stream = zmq_socket (ctx, ZMQ_STREAM);
    assert (stream);

    int zero = 0;
    rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);

    //  We'll be using this socket to test TCP stream handshake timeout
    void *dealer = zmq_socket (ctx, ZMQ_DEALER);
    assert (dealer);
    rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);
    int val, tenth = 100;
90
    size_t vsize = sizeof (val);
91 92 93 94

    // check for the expected default handshake timeout value - 30 sec
    rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
    assert (rc == 0);
95
    assert (vsize == sizeof (val));
96 97 98 99
    assert (val == 30000);
    // make handshake timeout faster - 1/10 sec
    rc = zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth));
    assert (rc == 0);
100
    vsize = sizeof (val);
101 102 103
    // make sure zmq_setsockopt changed the value
    rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
    assert (rc == 0);
104
    assert (vsize == sizeof (val));
105 106 107 108 109 110 111
    assert (val == tenth);

    //  Create and connect a socket for collecting monitor events on dealer
    void *dealer_mon = zmq_socket (ctx, ZMQ_PAIR);
    assert (dealer_mon);

    rc = zmq_socket_monitor (dealer, "inproc://monitor-dealer",
112 113
                             ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED
                               | ZMQ_EVENT_ACCEPTED);
114 115 116 117 118 119 120
    assert (rc == 0);

    //  Connect to the inproc endpoint so we'll get events
    rc = zmq_connect (dealer_mon, "inproc://monitor-dealer");
    assert (rc == 0);

    // bind dealer socket to accept connection from non-sending stream socket
121 122 123 124 125 126
    rc = zmq_bind (dealer, "tcp://127.0.0.1:*");
    assert (rc == 0);
    rc = zmq_getsockopt (dealer, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
    assert (rc == 0);

    rc = zmq_connect (stream, my_endpoint);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    assert (rc == 0);

    // we should get ZMQ_EVENT_ACCEPTED and then ZMQ_EVENT_DISCONNECTED
    int event = get_monitor_event (dealer_mon, NULL, NULL);
    assert (event == ZMQ_EVENT_ACCEPTED);
    event = get_monitor_event (dealer_mon, NULL, NULL);
    assert (event == ZMQ_EVENT_DISCONNECTED);

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

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

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

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

148
static void test_stream_handshake_timeout_connect (void)
149 150
{
    int rc;
151 152
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
153 154 155 156 157 158 159 160 161 162 163 164

    //  Set up our context and sockets
    void *ctx = zmq_ctx_new ();
    assert (ctx);

    //  We use this socket in raw mode, to accept a connection and send nothing
    void *stream = zmq_socket (ctx, ZMQ_STREAM);
    assert (stream);

    int zero = 0;
    rc = zmq_setsockopt (stream, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);
165 166 167
    rc = zmq_bind (stream, "tcp://127.0.0.1:*");
    assert (rc == 0);
    rc = zmq_getsockopt (stream, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
168 169 170 171 172 173 174 175
    assert (rc == 0);

    //  We'll be using this socket to test TCP stream handshake timeout
    void *dealer = zmq_socket (ctx, ZMQ_DEALER);
    assert (dealer);
    rc = zmq_setsockopt (dealer, ZMQ_LINGER, &zero, sizeof (zero));
    assert (rc == 0);
    int val, tenth = 100;
176
    size_t vsize = sizeof (val);
177 178 179 180

    // check for the expected default handshake timeout value - 30 sec
    rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
    assert (rc == 0);
181
    assert (vsize == sizeof (val));
182 183 184 185
    assert (val == 30000);
    // make handshake timeout faster - 1/10 sec
    rc = zmq_setsockopt (dealer, ZMQ_HANDSHAKE_IVL, &tenth, sizeof (tenth));
    assert (rc == 0);
186
    vsize = sizeof (val);
187 188 189
    // make sure zmq_setsockopt changed the value
    rc = zmq_getsockopt (dealer, ZMQ_HANDSHAKE_IVL, &val, &vsize);
    assert (rc == 0);
190
    assert (vsize == sizeof (val));
191 192 193 194 195 196 197
    assert (val == tenth);

    //  Create and connect a socket for collecting monitor events on dealer
    void *dealer_mon = zmq_socket (ctx, ZMQ_PAIR);
    assert (dealer_mon);

    rc = zmq_socket_monitor (dealer, "inproc://monitor-dealer",
198 199
                             ZMQ_EVENT_CONNECTED | ZMQ_EVENT_DISCONNECTED
                               | ZMQ_EVENT_ACCEPTED);
200 201 202 203 204 205 206
    assert (rc == 0);

    //  Connect to the inproc endpoint so we'll get events
    rc = zmq_connect (dealer_mon, "inproc://monitor-dealer");
    assert (rc == 0);

    // connect dealer socket to non-sending stream socket
207
    rc = zmq_connect (dealer, my_endpoint);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    assert (rc == 0);

    // we should get ZMQ_EVENT_CONNECTED and then ZMQ_EVENT_DISCONNECTED
    int event = get_monitor_event (dealer_mon, NULL, NULL);
    assert (event == ZMQ_EVENT_CONNECTED);
    event = get_monitor_event (dealer_mon, NULL, NULL);
    assert (event == ZMQ_EVENT_DISCONNECTED);

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

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

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

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

int main (void)
{
231
    setup_test_environment ();
232 233 234
    test_stream_handshake_timeout_accept ();
    test_stream_handshake_timeout_connect ();
}