test_spec_dealer.cpp 7.78 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

    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"
31
#include "testutil_unity.hpp"
32

33 34 35 36 37 38 39 40 41
void setUp ()
{
    setup_test_context ();
}

void tearDown ()
{
    teardown_test_context ();
}
42

43 44 45

// SHALL route outgoing messages to available peers using a round-robin
// strategy.
46
void test_round_robin_out (const char *bind_address_)
47
{
48
    void *dealer = test_context_socket (ZMQ_DEALER);
49

50
    char connect_address[MAX_SOCKET_STRING];
51 52
    test_bind (dealer, bind_address_, connect_address,
               sizeof (connect_address));
53

54
    const size_t services = 5;
55
    void *rep[services];
56
    for (size_t peer = 0; peer < services; ++peer) {
57
        rep[peer] = test_context_socket (ZMQ_REP);
58

59
        int timeout = 250;
60 61
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_setsockopt (rep[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
62

63
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (rep[peer], connect_address));
64 65
    }

66
    // Wait for connections.
67
    msleep (SETTLE_TIME);
68

69 70
    // Send all requests
    for (size_t i = 0; i < services; ++i)
71 72 73 74 75 76
        s_send_seq (dealer, 0, "ABC", SEQ_END);

    // Expect every REP got one message
    zmq_msg_t msg;
    zmq_msg_init (&msg);

77
    for (size_t peer = 0; peer < services; ++peer)
78
        s_recv_seq (rep[peer], "ABC", SEQ_END);
79

80
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
81

82
    test_context_socket_close_zero_linger (dealer);
83

84
    for (size_t peer = 0; peer < services; ++peer)
85
        test_context_socket_close_zero_linger (rep[peer]);
86 87
}

88 89
// SHALL receive incoming messages from its peers using a fair-queuing
// strategy.
90
void test_fair_queue_in (const char *bind_address_)
91
{
92
    void *receiver = test_context_socket (ZMQ_DEALER);
93

94
    int timeout = 250;
95 96
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (receiver, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
97

98
    char connect_address[MAX_SOCKET_STRING];
99
    test_bind (receiver, bind_address_, connect_address,
100
               sizeof (connect_address));
101

102
    const size_t services = 5;
103
    void *senders[services];
104
    for (size_t peer = 0; peer < services; ++peer) {
105
        senders[peer] = test_context_socket (ZMQ_DEALER);
106

107 108
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_setsockopt (senders[peer], ZMQ_RCVTIMEO, &timeout, sizeof (int)));
109

110 111
        TEST_ASSERT_SUCCESS_ERRNO (
          zmq_connect (senders[peer], connect_address));
112 113 114
    }

    zmq_msg_t msg;
115
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
116

117
    s_send_seq (senders[0], "A", SEQ_END);
118 119
    s_recv_seq (receiver, "A", SEQ_END);

120
    s_send_seq (senders[0], "A", SEQ_END);
121 122
    s_recv_seq (receiver, "A", SEQ_END);

123
    // send our requests
124
    for (size_t peer = 0; peer < services; ++peer)
125
        s_send_seq (senders[peer], "B", SEQ_END);
126

127
    // Wait for data.
128
    msleep (SETTLE_TIME);
129

130
    // handle the requests
131
    for (size_t peer = 0; peer < services; ++peer)
132
        s_recv_seq (receiver, "B", SEQ_END);
133

134
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
135

136
    test_context_socket_close_zero_linger (receiver);
137

138
    for (size_t peer = 0; peer < services; ++peer)
139
        test_context_socket_close_zero_linger (senders[peer]);
140 141
}

142 143 144
// SHALL create a double queue when a peer connects to it. If this peer
// disconnects, the DEALER socket SHALL destroy its double queue and SHALL
// discard any messages it contains.
145
void test_destroy_queue_on_disconnect (const char *bind_address_)
146
{
147
    void *a = test_context_socket (ZMQ_DEALER);
148

149
    char connect_address[MAX_SOCKET_STRING];
150
    test_bind (a, bind_address_, connect_address, sizeof (connect_address));
151

152
    void *b = test_context_socket (ZMQ_DEALER);
153

154
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
155 156

    // Send a message in both directions
157 158
    s_send_seq (a, "ABC", SEQ_END);
    s_send_seq (b, "DEF", SEQ_END);
159

160
    TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (b, connect_address));
161 162

    // Disconnect may take time and need command processing.
163
    zmq_pollitem_t poller[2] = {{a, 0, 0, 0}, {b, 0, 0, 0}};
164 165
    TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (poller, 2, 100));
166 167 168 169 170

    // No messages should be available, sending should fail.
    zmq_msg_t msg;
    zmq_msg_init (&msg);

171
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (a, 0, 0, ZMQ_DONTWAIT));
172

173
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
174 175

    // After a reconnect of B, the messages should still be gone
176
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (b, connect_address));
177

178
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, a, ZMQ_DONTWAIT));
179

180
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_msg_recv (&msg, b, ZMQ_DONTWAIT));
181

182
    TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
183

184 185
    test_context_socket_close_zero_linger (a);
    test_context_socket_close_zero_linger (b);
186 187
}

188
// SHALL block on sending, or return a suitable error, when it has no connected peers.
189
void test_block_on_send_no_peers (const char *bind_address_)
190
{
191
    void *sc = test_context_socket (ZMQ_DEALER);
192

193
    int timeout = 250;
194 195
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc, ZMQ_SNDTIMEO, &timeout, sizeof (timeout)));
196

197 198
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, ZMQ_DONTWAIT));
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (sc, 0, 0, 0));
199

200
    test_context_socket_close (sc);
201 202
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216
#define TEST_CASES(name, bind_address)                                         \
    void test_round_robin_out_##name ()                                        \
    {                                                                          \
        test_round_robin_out (bind_address);                                   \
    }                                                                          \
    void test_fair_queue_in_##name () { test_fair_queue_in (bind_address); }   \
    void test_block_on_send_no_peers_##name ()                                 \
    {                                                                          \
        test_block_on_send_no_peers (bind_address);                            \
    }

TEST_CASES (inproc, "inproc://a")
TEST_CASES (tcp, "tcp://127.0.0.1:*")

217
int main (void)
218
{
219
    setup_test_environment ();
220

221
    UNITY_BEGIN ();
222

223 224 225
    RUN_TEST (test_round_robin_out_inproc);
    RUN_TEST (test_fair_queue_in_inproc);
    RUN_TEST (test_block_on_send_no_peers_inproc);
226

227 228 229
    RUN_TEST (test_round_robin_out_tcp);
    RUN_TEST (test_fair_queue_in_tcp);
    RUN_TEST (test_block_on_send_no_peers_tcp);
230

231 232
    // TODO *** Test disabled until libzmq does this properly ***
    // test_destroy_queue_on_disconnect (ctx);
233

234
    return UNITY_END ();
235
}