test_immediate.cpp 7.75 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

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

30
#include "testutil.hpp"
31

32
int main (void)
33
{
34
    setup_test_environment ();
35 36 37
    int val;
    int rc;
    char buffer[16];
38 39
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
40
    // TEST 1.
41 42 43 44
    // First we're going to attempt to send messages to two
    // pipes, one connected, the other not. We should see
    // the PUSH load balancing to both pipes, and hence half
    // of the messages getting queued, as connect() creates a
45 46 47
    // pipe immediately.

    void *context = zmq_ctx_new ();
48
    assert (context);
49
    void *to = zmq_socket (context, ZMQ_PULL);
50 51
    assert (to);

52
    // Bind the one valid receiver
53
    val = 0;
54
    rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val));
55
    assert (rc == 0);
56 57 58
    rc = zmq_bind (to, "tcp://127.0.0.1:*");
    assert (rc == 0);
    rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
59 60 61 62
    assert (rc == 0);

    // Create a socket pushing to two endpoints - only 1 message should arrive.
    void *from = zmq_socket (context, ZMQ_PUSH);
63
    assert (from);
64 65

    val = 0;
66
    zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val));
67
    // This pipe will not connect
68 69
    rc = zmq_connect (from, "tcp://localhost:5556");
    assert (rc == 0);
70
    // This pipe will
71
    rc = zmq_connect (from, my_endpoint);
72 73
    assert (rc == 0);

74 75
    msleep (SETTLE_TIME);

76 77
    // We send 10 messages, 5 should just get stuck in the queue
    // for the not-yet-connected pipe
78 79 80
    for (int i = 0; i < 10; ++i) {
        rc = zmq_send (from, "Hello", 5, 0);
        assert (rc == 5);
81 82
    }

83 84
    // We now consume from the connected pipe
    // - we should see just 5
85
    int timeout = 250;
86 87 88 89 90 91 92
    rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
    assert (rc == 0);

    int seen = 0;
    while (true) {
        rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
        if (rc == -1)
93
            break; //  Break when we didn't get a message
94 95 96 97 98 99 100 101 102 103
        seen++;
    }
    assert (seen == 5);

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

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

Pieter Hintjens's avatar
Pieter Hintjens committed
104
    rc = zmq_ctx_term (context);
105 106
    assert (rc == 0);

107
    // TEST 2
108 109 110 111
    // This time we will do the same thing, connect two pipes,
    // one of which will succeed in connecting to a bound
    // receiver, the other of which will fail. However, we will
    // also set the delay attach on connect flag, which should
112
    // cause the pipe attachment to be delayed until the connection
113 114
    // succeeds.
    context = zmq_ctx_new ();
115

116
    // Bind the valid socket
117 118
    to = zmq_socket (context, ZMQ_PULL);
    assert (to);
119 120 121 122
    rc = zmq_bind (to, "tcp://127.0.0.1:*");
    assert (rc == 0);
    len = MAX_SOCKET_STRING;
    rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
123 124 125
    assert (rc == 0);

    val = 0;
126
    rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val));
127 128 129 130 131 132 133
    assert (rc == 0);

    // Create a socket pushing to two endpoints - all messages should arrive.
    from = zmq_socket (context, ZMQ_PUSH);
    assert (from);

    val = 0;
134
    rc = zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val));
135 136
    assert (rc == 0);

137
    // Set the key flag
138
    val = 1;
139
    rc = zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val));
140 141
    assert (rc == 0);

142
    // Connect to the invalid socket
143 144
    rc = zmq_connect (from, "tcp://localhost:5561");
    assert (rc == 0);
145
    // Connect to the valid socket
146
    rc = zmq_connect (from, my_endpoint);
147 148
    assert (rc == 0);

149
    // Send 10 messages, all should be routed to the connected pipe
150 151 152
    for (int i = 0; i < 10; ++i) {
        rc = zmq_send (from, "Hello", 5, 0);
        assert (rc == 5);
153
    }
154 155
    rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
    assert (rc == 0);
156

157
    seen = 0;
158 159 160
    while (true) {
        rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
        if (rc == -1)
161
            break; //  Break when we didn't get a message
162
        seen++;
163
    }
164
    assert (seen == 10);
165 166 167 168 169 170

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

    rc = zmq_close (to);
    assert (rc == 0);
171

Pieter Hintjens's avatar
Pieter Hintjens committed
172
    rc = zmq_ctx_term (context);
173 174
    assert (rc == 0);

175 176 177
    // TEST 3
    // This time we want to validate that the same blocking behaviour
    // occurs with an existing connection that is broken. We will send
178
    // messages to a connected pipe, disconnect and verify the messages
179
    // block. Then we reconnect and verify messages flow again.
180
    context = zmq_ctx_new ();
181

182 183 184 185 186 187
    void *backend = zmq_socket (context, ZMQ_DEALER);
    assert (backend);
    void *frontend = zmq_socket (context, ZMQ_DEALER);
    assert (frontend);
    int zero = 0;
    rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero));
188
    assert (rc == 0);
189
    rc = zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero));
190 191
    assert (rc == 0);

192
    //  Frontend connects to backend using IMMEDIATE
193
    int on = 1;
194
    rc = zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on));
195
    assert (rc == 0);
196 197 198 199
    rc = zmq_bind (backend, "tcp://127.0.0.1:*");
    assert (rc == 0);
    len = MAX_SOCKET_STRING;
    rc = zmq_getsockopt (backend, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
200
    assert (rc == 0);
201
    rc = zmq_connect (frontend, my_endpoint);
202
    assert (rc == 0);
203 204 205 206 207 208

    //  Ping backend to frontend so we know when the connection is up
    rc = zmq_send (backend, "Hello", 5, 0);
    assert (rc == 5);
    rc = zmq_recv (frontend, buffer, 255, 0);
    assert (rc == 5);
209

210 211 212
    // Send message from frontend to backend
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
    assert (rc == 5);
213

214
    rc = zmq_close (backend);
215
    assert (rc == 0);
216

217
    //  Give time to process disconnect
218
    msleep (SETTLE_TIME * 10);
219

220
    // Send a message, should fail
221
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
222
    assert (rc == -1);
223

224 225 226 227
    //  Recreate backend socket
    backend = zmq_socket (context, ZMQ_DEALER);
    assert (backend);
    rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero));
228
    assert (rc == 0);
229
    rc = zmq_bind (backend, my_endpoint);
230 231 232 233 234 235 236 237
    assert (rc == 0);

    //  Ping backend to frontend so we know when the connection is up
    rc = zmq_send (backend, "Hello", 5, 0);
    assert (rc == 5);
    rc = zmq_recv (frontend, buffer, 255, 0);
    assert (rc == 5);

238
    // After the reconnect, should succeed
239 240
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
    assert (rc == 5);
241

242
    rc = zmq_close (backend);
243
    assert (rc == 0);
244

245
    rc = zmq_close (frontend);
246 247
    assert (rc == 0);

Pieter Hintjens's avatar
Pieter Hintjens committed
248
    rc = zmq_ctx_term (context);
249
    assert (rc == 0);
250
}