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

    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 40 41 42 43 44
    // TEST 1. 
    // 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
    // pipe immediately. 
    
45 46 47 48 49
    void *context = zmq_ctx_new();
    assert (context);
    void *to = zmq_socket(context, ZMQ_PULL);
    assert (to);

50
    // Bind the one valid receiver
51 52 53
    val = 0;
    rc = zmq_setsockopt(to, ZMQ_LINGER, &val, sizeof(val));
    assert (rc == 0);
54
    rc = zmq_bind (to, "tcp://127.0.0.1:6555");
55 56 57 58 59 60 61
    assert (rc == 0);

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

    val = 0;
62
    zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val));
63
    // This pipe will not connect
64 65
    rc = zmq_connect (from, "tcp://localhost:5556");
    assert (rc == 0);
66
    // This pipe will 
67
    rc = zmq_connect (from, "tcp://localhost:6555");
68 69
    assert (rc == 0);

70 71
    msleep (SETTLE_TIME);

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

79 80
    // We now consume from the connected pipe
    // - we should see just 5
81
    int timeout = 250;
82 83 84 85 86 87 88 89
    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)
            break;          //  Break when we didn't get a message
90 91 92 93 94 95 96 97 98 99
        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
100
    rc = zmq_ctx_term (context);
101 102
    assert (rc == 0);

103 104 105 106 107 108 109
    // TEST 2
    // 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 
    // cause the pipe attachment to be delayed until the connection
    // succeeds. 
110 111
    context = zmq_ctx_new();

112
    // Bind the valid socket
113 114
    to = zmq_socket (context, ZMQ_PULL);
    assert (to);
115
    rc = zmq_bind (to, "tcp://127.0.0.1:5560");
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    assert (rc == 0);

    val = 0;
    rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof(val));
    assert (rc == 0);

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

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

130
    // Set the key flag
131
    val = 1;
132
    rc = zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof(val));
133 134
    assert (rc == 0);

135
    // Connect to the invalid socket
136 137
    rc = zmq_connect (from, "tcp://localhost:5561");
    assert (rc == 0);
138
    // Connect to the valid socket
139 140 141
    rc = zmq_connect (from, "tcp://localhost:5560");
    assert (rc == 0);

142
    // Send 10 messages, all should be routed to the connected pipe
143 144 145
    for (int i = 0; i < 10; ++i) {
        rc = zmq_send (from, "Hello", 5, 0);
        assert (rc == 5);
146
    }
147 148 149
    rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
    assert (rc == 0);
    
150
    seen = 0;
151 152 153 154 155
    while (true) {
        rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
        if (rc == -1)
            break;          //  Break when we didn't get a message
        seen++;
156
    }
157
    assert (seen == 10);
158 159 160 161 162 163 164

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

    rc = zmq_close (to);
    assert (rc == 0);
    
Pieter Hintjens's avatar
Pieter Hintjens committed
165
    rc = zmq_ctx_term (context);
166 167
    assert (rc == 0);

168 169 170
    // TEST 3
    // This time we want to validate that the same blocking behaviour
    // occurs with an existing connection that is broken. We will send
171
    // messages to a connected pipe, disconnect and verify the messages
172
    // block. Then we reconnect and verify messages flow again.
173
    context = zmq_ctx_new ();
174

175 176 177 178 179 180
    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));
181
    assert (rc == 0);
182
    rc = zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero));
183 184
    assert (rc == 0);

185
    //  Frontend connects to backend using IMMEDIATE
186
    int on = 1;
187
    rc = zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on));
188
    assert (rc == 0);
189
    rc = zmq_bind (backend, "tcp://127.0.0.1:5560");
190
    assert (rc == 0);
191
    rc = zmq_connect (frontend, "tcp://localhost:5560");
192
    assert (rc == 0);
193 194 195 196 197 198

    //  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);
199
    
200 201 202
    // Send message from frontend to backend
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
    assert (rc == 5);
203
    
204
    rc = zmq_close (backend);
205
    assert (rc == 0);
206

207
    //  Give time to process disconnect
208
    msleep (SETTLE_TIME * 10);
209 210
    
    // Send a message, should fail
211
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
212
    assert (rc == -1);
213

214 215 216 217
    //  Recreate backend socket
    backend = zmq_socket (context, ZMQ_DEALER);
    assert (backend);
    rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero));
218
    assert (rc == 0);
219
    rc = zmq_bind (backend, "tcp://127.0.0.1:5560");
220 221 222 223 224 225 226 227
    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);

228
    // After the reconnect, should succeed
229 230
    rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
    assert (rc == 5);
231
    
232
    rc = zmq_close (backend);
233 234
    assert (rc == 0);
    
235
    rc = zmq_close (frontend);
236 237
    assert (rc == 0);

Pieter Hintjens's avatar
Pieter Hintjens committed
238
    rc = zmq_ctx_term (context);
239
    assert (rc == 0);
240
}