test_spec_pushpull.cpp 7.77 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ 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.

    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"

22 23 24
const char *bind_address = 0;
const char *connect_address = 0;

25 26 27 28 29
void test_push_round_robin_out (void *ctx)
{
    void *push = zmq_socket (ctx, ZMQ_PUSH);
    assert (push);

30
    int rc = zmq_bind (push, bind_address);
31 32
    assert (rc == 0);

33 34 35 36 37
    const size_t services = 5;
    void *pulls [services];
    for (size_t peer = 0; peer < services; ++peer) {
        pulls [peer] = zmq_socket (ctx, ZMQ_PULL);
        assert (pulls [peer]);
38 39

        int timeout = 100;
40
        rc = zmq_setsockopt (pulls [peer], ZMQ_RCVTIMEO, &timeout, sizeof (int));
41 42
        assert (rc == 0);

43
        rc = zmq_connect (pulls [peer], connect_address);
44 45 46
        assert (rc == 0);
    }

47 48 49 50
    // Wait for connections.
    rc = zmq_poll (0, 0, 100);
    assert (rc == 0);

51
    // Send 2N messages
52
    for (size_t peer = 0; peer < services; ++peer)
53
        s_send_seq (push, "ABC", SEQ_END);
54
    for (size_t peer = 0; peer < services; ++peer)
55 56 57
        s_send_seq (push, "DEF", SEQ_END);

    // Expect every PULL got one of each
58 59 60
    for (size_t peer = 0; peer < services; ++peer) {
        s_recv_seq (pulls [peer], "ABC", SEQ_END);
        s_recv_seq (pulls [peer], "DEF", SEQ_END);
61 62
    }

63
    close_zero_linger (push);
64

65 66
    for (size_t peer = 0; peer < services; ++peer)
        close_zero_linger (pulls [peer]);
67 68 69 70

    // Wait for disconnects.
    rc = zmq_poll (0, 0, 100);
    assert (rc == 0);
71 72 73 74 75 76 77
}

void test_pull_fair_queue_in (void *ctx)
{
    void *pull = zmq_socket (ctx, ZMQ_PULL);
    assert (pull);

78
    int rc = zmq_bind (pull, bind_address);
79 80
    assert (rc == 0);

81 82 83
    const size_t services = 5;
    void *pushs [services];
    for (size_t peer = 0; peer < services; ++peer)
84
    {
85 86
        pushs [peer] = zmq_socket (ctx, ZMQ_PUSH);
        assert (pushs [peer]);
87

88
        rc = zmq_connect (pushs [peer], connect_address);
89 90 91
        assert (rc == 0);
    }

92 93 94 95 96 97 98
    // Wait for connections.
    rc = zmq_poll (0, 0, 100);
    assert (rc == 0);

    int first_half = 0;
    int second_half = 0;

99
    // Send 2N messages
100
    for (size_t peer = 0; peer < services; ++peer) {
101 102
        char *str = strdup("A");

103 104 105
        str [0] += peer;
        s_send_seq (pushs [peer], str, SEQ_END);
        first_half += str [0];
106

107 108 109
        str [0] += services;
        s_send_seq (pushs [peer], str, SEQ_END);
        second_half += str [0];
110

111 112 113
        free (str);
    }

114 115 116 117 118 119 120 121 122
    // Wait for data.
    rc = zmq_poll (0, 0, 100);
    assert (rc == 0);

    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);

    // Expect to pull one from each first
123
    for (size_t peer = 0; peer < services; ++peer) {
124 125 126
        rc = zmq_msg_recv (&msg, pull, 0);
        assert (rc == 2);
        const char *str = (const char *)zmq_msg_data (&msg);
127
        first_half -= str [0];
128
    }
129
    assert (first_half == 0);
130

131
    // And then get the second batch
132
    for (size_t peer = 0; peer < services; ++peer) {
133 134 135
        rc = zmq_msg_recv (&msg, pull, 0);
        assert (rc == 2);
        const char *str = (const char *)zmq_msg_data (&msg);
136
        second_half -= str [0];
137 138 139 140
    }
    assert (second_half == 0);

    rc = zmq_msg_close (&msg);
141 142
    assert (rc == 0);

143 144
    close_zero_linger (pull);

145 146
    for (size_t peer = 0; peer < services; ++peer)
        close_zero_linger (pushs [peer]);
147 148 149 150

    // Wait for disconnects.
    rc = zmq_poll (0, 0, 100);
    assert (rc == 0);
151 152 153 154 155 156 157 158
}

void test_push_block_on_send_no_peers (void *ctx)
{
    void *sc = zmq_socket (ctx, ZMQ_PUSH);
    assert (sc);

    int timeout = 100;
159
    int rc = zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout));
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    assert (rc == 0);

    rc = zmq_send (sc, 0, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    rc = zmq_send (sc, 0, 0, 0);
    assert (rc == -1);
    assert (errno == EAGAIN);

    rc = zmq_close (sc);
    assert (rc == 0);
}

void test_destroy_queue_on_disconnect (void *ctx)
{
    void *A = zmq_socket (ctx, ZMQ_PUSH);
    assert (A);

    int hwm = 1;
180
    int rc = zmq_setsockopt (A, ZMQ_SNDHWM, &hwm, sizeof (hwm));
181 182
    assert (rc == 0);

183
    rc = zmq_bind (A, bind_address);
184 185 186 187 188
    assert (rc == 0);

    void *B = zmq_socket (ctx, ZMQ_PULL);
    assert (B);

189
    rc = zmq_setsockopt (B, ZMQ_RCVHWM, &hwm, sizeof (hwm));
190 191
    assert (rc == 0);

192
    rc = zmq_connect (B, connect_address);
193 194 195 196 197 198 199 200 201 202 203 204
    assert (rc == 0);

    // Send two messages, one should be stuck in A's outgoing queue, the other
    // arrives at B.
    s_send_seq (A, "ABC", SEQ_END);
    s_send_seq (A, "DEF", SEQ_END);

    // Both queues should now be full, indicated by A blocking on send.
    rc = zmq_send (A, 0, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

205
    rc = zmq_disconnect (B, connect_address);
206 207 208
    assert (rc == 0);

    // Disconnect may take time and need command processing.
209
    zmq_pollitem_t poller [2] = { { A, 0, 0, 0 }, { B, 0, 0, 0 } };
210 211
    rc = zmq_poll (poller, 2, 100);
    assert (rc == 0);
212 213
    rc = zmq_poll (poller, 2, 100);
    assert (rc == 0);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

    zmq_msg_t msg;
    rc = zmq_msg_init (&msg);
    assert (rc == 0);

    // Can't receive old data on B.
    rc = zmq_msg_recv (&msg, B, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    // Sending fails.
    rc = zmq_send (A, 0, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    // Reconnect B
230
    rc = zmq_connect (B, connect_address);
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    assert (rc == 0);

    // Still can't receive old data on B.
    rc = zmq_msg_recv (&msg, B, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    // two messages should be sendable before the queues are filled up.
    s_send_seq (A, "ABC", SEQ_END);
    s_send_seq (A, "DEF", SEQ_END);

    rc = zmq_send (A, 0, 0, ZMQ_DONTWAIT);
    assert (rc == -1);
    assert (errno == EAGAIN);

    rc = zmq_msg_close (&msg);
    assert (rc == 0);

249 250
    close_zero_linger (A);
    close_zero_linger (B);
251

252 253
    // Wait for disconnects.
    rc = zmq_poll (0, 0, 100);
254 255 256
    assert (rc == 0);
}

257
int main (void)
258
{
259
    setup_test_environment();
260 261 262
    void *ctx = zmq_ctx_new ();
    assert (ctx);

263
    const char *binds [] = { "inproc://a", "tcp://127.0.0.1:5555" };
264
    const char *connects [] = { "inproc://a", "tcp://localhost:5555" };
265

266 267 268
    for (int transport = 0; transport < 2; ++transport) {
        bind_address = binds [transport];
        connect_address = connects [transport];
269

270 271 272
        // PUSH: SHALL route outgoing messages to connected peers using a
        // round-robin strategy.
        test_push_round_robin_out (ctx);
273

274 275 276
        // PULL: SHALL receive incoming messages from its peers using a fair-queuing
        // strategy.
        test_pull_fair_queue_in (ctx);
277

278 279 280 281 282 283 284
        // PUSH: SHALL block on sending, or return a suitable error, when it has no
        // available peers.
        test_push_block_on_send_no_peers (ctx);

        // PUSH and PULL: SHALL create this queue when a peer connects to it. If
        // this peer disconnects, the socket SHALL destroy its queue and SHALL
        // discard any messages it contains.
285 286
        // *** Test disabled until libzmq does this properly ***
        // test_destroy_queue_on_disconnect (ctx);
287
    }
288 289 290 291 292 293

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

    return 0 ;
}