test_hwm.cpp 9.86 KB
Newer Older
Mikko Koppanen's avatar
Mikko Koppanen committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Mikko Koppanen's avatar
Mikko Koppanen committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Mikko Koppanen's avatar
Mikko Koppanen committed
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
Mikko Koppanen's avatar
Mikko Koppanen committed
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.
Mikko Koppanen's avatar
Mikko Koppanen committed
25 26 27 28 29

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

30
#include "testutil.hpp"
31 32 33 34 35 36
#include "testutil_unity.hpp"

#include <unity.h>

void setUp ()
{
37
    setup_test_context ();
38 39 40 41
}

void tearDown ()
{
42
    teardown_test_context ();
43
}
Mikko Koppanen's avatar
Mikko Koppanen committed
44

45 46
const int MAX_SENDS = 10000;

47 48 49 50 51
enum TestType
{
    BIND_FIRST,
    CONNECT_FIRST
};
52

53
void test_defaults ()
54 55
{
    // Set up bind socket
56
    void *bind_socket = test_context_socket (ZMQ_PULL);
57
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
58 59

    // Set up connect socket
60
    void *connect_socket = test_context_socket (ZMQ_PUSH);
61
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
62 63 64

    // Send until we block
    int send_count = 0;
65 66
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
67 68
        ++send_count;

69
    msleep (SETTLE_TIME);
70

71 72 73 74 75
    // Now receive all sent messages
    int recv_count = 0;
    while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++recv_count;

76
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
77 78

    // Clean up
79 80
    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
81

82 83
    // Default values are 1000 on send and 1000 one receive, so 2000 total
    TEST_ASSERT_EQUAL_INT (2000, send_count);
84 85
}

86
int count_msg (int send_hwm, int recv_hwm, TestType testType)
Mikko Koppanen's avatar
Mikko Koppanen committed
87
{
88 89
    void *bind_socket;
    void *connect_socket;
90
    if (testType == BIND_FIRST) {
91
        // Set up bind socket
92
        bind_socket = test_context_socket (ZMQ_PULL);
93 94 95
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
          bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
96 97

        // Set up connect socket
98
        connect_socket = test_context_socket (ZMQ_PUSH);
99 100 101
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
          connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
102
    } else {
103
        // Set up connect socket
104
        connect_socket = test_context_socket (ZMQ_PUSH);
105 106 107
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
          connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
Mikko Koppanen's avatar
Mikko Koppanen committed
108

109
        // Set up bind socket
110
        bind_socket = test_context_socket (ZMQ_PULL);
111 112 113
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
          bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
        TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
114
    }
Mikko Koppanen's avatar
Mikko Koppanen committed
115

116 117
    // Send until we block
    int send_count = 0;
118 119
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
120 121 122 123 124 125 126
        ++send_count;

    // Now receive all sent messages
    int recv_count = 0;
    while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++recv_count;

127
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
128

129
    // Now it should be possible to send one more.
130
    send_string_expect_success (connect_socket, NULL, 0);
131 132

    //  Consume the remaining message.
133
    recv_string_expect_success (bind_socket, NULL, 0);
134

135
    // Clean up
136 137
    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
Mikko Koppanen's avatar
Mikko Koppanen committed
138

139 140 141 142 143
    return send_count;
}

int test_inproc_bind_first (int send_hwm, int recv_hwm)
{
144
    return count_msg (send_hwm, recv_hwm, BIND_FIRST);
145 146 147 148
}

int test_inproc_connect_first (int send_hwm, int recv_hwm)
{
149
    return count_msg (send_hwm, recv_hwm, CONNECT_FIRST);
150 151
}

152 153 154
int test_inproc_connect_and_close_first (int send_hwm, int recv_hwm)
{
    // Set up connect socket
155
    void *connect_socket = test_context_socket (ZMQ_PUSH);
156 157 158
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_SNDHWM,
                                               &send_hwm, sizeof (send_hwm)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
159 160 161

    // Send until we block
    int send_count = 0;
162 163
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
164 165 166
        ++send_count;

    // Close connect
167
    test_context_socket_close (connect_socket);
168 169

    // Set up bind socket
170
    void *bind_socket = test_context_socket (ZMQ_PULL);
171 172 173
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
174 175 176 177 178 179

    // Now receive all sent messages
    int recv_count = 0;
    while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++recv_count;

180
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
181 182

    // Clean up
183
    test_context_socket_close (bind_socket);
184 185 186 187

    return send_count;
}

188
int test_inproc_bind_and_close_first (int send_hwm, int /* recv_hwm */)
189 190
{
    void *ctx = zmq_ctx_new ();
191
    TEST_ASSERT_NOT_NULL (ctx);
192 193 194

    // Set up bind socket
    void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
195 196 197 198
    TEST_ASSERT_NOT_NULL (bind_socket);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
199 200 201

    // Send until we block
    int send_count = 0;
202 203
    while (send_count < MAX_SENDS
           && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
204 205 206
        ++send_count;

    // Close bind
207
    TEST_ASSERT_SUCCESS_ERRNO (zmq_close (bind_socket));
208

209
    /* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
210 211
    // Set up connect socket
    void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
212 213 214
    TEST_ASSERT_NOT_NULL(connect_socket);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
215 216 217 218 219 220

    // Now receive all sent messages
    int recv_count = 0;
    while (zmq_recv (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++recv_count;

221
    TEST_ASSERT_EQUAL_INT(send_count, recv_count);
222 223 224
    */

    // Clean up
225
    //TEST_ASSERT_SUCCESS_ERRNO (zmq_close (connect_socket));
226

227
    TEST_ASSERT_SUCCESS_ERRNO (zmq_ctx_term (ctx));
228 229 230 231

    return send_count;
}

232
void test_infinite_both_inproc_bind_first ()
233
{
234 235 236
    int count = test_inproc_bind_first (0, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
237

238 239 240 241 242
void test_infinite_both_inproc_connect_first ()
{
    int count = test_inproc_connect_first (0, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
243

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
void test_infinite_receive_inproc_bind_first ()
{
    int count = test_inproc_bind_first (1, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}

void test_infinite_receive_inproc_connect_first ()
{
    int count = test_inproc_connect_first (1, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}

void test_infinite_send_inproc_bind_first ()
{
    int count = test_inproc_bind_first (0, 1);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}

void test_infinite_send_inproc_connect_first ()
{
    int count = test_inproc_connect_first (0, 1);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
267

268 269 270 271 272 273 274 275
void test_finite_both_bind_first ()
{
    // Send and recv buffers hwm 1, so total that can be queued is 2
    int count = test_inproc_bind_first (1, 1);
    TEST_ASSERT_EQUAL_INT (2, count);
}
void test_finite_both_connect_first ()
{
276
    // Send and recv buffers hwm 1, so total that can be queued is 2
277 278 279
    int count = test_inproc_connect_first (1, 1);
    TEST_ASSERT_EQUAL_INT (2, count);
}
280

281 282
void test_infinite_recv_connect_and_close_first ()
{
283
    // Send hwm of 1, send before bind so total that can be queued is 1
284 285 286
    int count = test_inproc_connect_and_close_first (1, 0);
    TEST_ASSERT_EQUAL_INT (1, count);
}
287

288 289
void test_infinite_recv_bind_and_close_first ()
{
290 291
    // Send hwm of 1, send from bind side before connect so total that can be queued should be 1,
    // however currently all messages get thrown away before the connect.  BUG?
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    /*int count = */ test_inproc_bind_and_close_first (1, 0);
    // TEST_ASSERT_EQUAL_INT (1, count);
}

int main (void)
{
    setup_test_environment ();

    UNITY_BEGIN ();
    RUN_TEST (test_defaults);

    RUN_TEST (test_infinite_both_inproc_bind_first);
    RUN_TEST (test_infinite_both_inproc_connect_first);

    RUN_TEST (test_infinite_receive_inproc_bind_first);
    RUN_TEST (test_infinite_receive_inproc_connect_first);

    RUN_TEST (test_infinite_send_inproc_bind_first);
    RUN_TEST (test_infinite_send_inproc_connect_first);

    RUN_TEST (test_finite_both_bind_first);
    RUN_TEST (test_finite_both_connect_first);

    RUN_TEST (test_infinite_recv_connect_and_close_first);
    RUN_TEST (test_infinite_recv_bind_and_close_first);
317

318
    return UNITY_END ();
Mikko Koppanen's avatar
Mikko Koppanen committed
319
}