test_hwm.cpp 9.78 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
#include "testutil_unity.hpp"

33
SETUP_TEARDOWN_TESTCONTEXT
Mikko Koppanen's avatar
Mikko Koppanen committed
34

35 36
const int MAX_SENDS = 10000;

37 38 39 40 41
enum TestType
{
    BIND_FIRST,
    CONNECT_FIRST
};
42

43
void test_defaults ()
44 45
{
    // Set up bind socket
46
    void *bind_socket = test_context_socket (ZMQ_PULL);
47
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
48 49

    // Set up connect socket
50
    void *connect_socket = test_context_socket (ZMQ_PUSH);
51
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
52 53 54

    // Send until we block
    int send_count = 0;
55 56
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
57 58
        ++send_count;

59
    msleep (SETTLE_TIME);
60

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

66
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
67 68

    // Clean up
69 70
    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
71

72 73
    // Default values are 1000 on send and 1000 one receive, so 2000 total
    TEST_ASSERT_EQUAL_INT (2000, send_count);
74 75
}

76
int count_msg (int send_hwm_, int recv_hwm_, TestType test_type_)
Mikko Koppanen's avatar
Mikko Koppanen committed
77
{
78 79
    void *bind_socket;
    void *connect_socket;
80
    if (test_type_ == BIND_FIRST) {
81
        // Set up bind socket
82
        bind_socket = test_context_socket (ZMQ_PULL);
83
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
84
          bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
85
        TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
86 87

        // Set up connect socket
88
        connect_socket = test_context_socket (ZMQ_PUSH);
89
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
90
          connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
91
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
92 93 94 95

        //  we must wait for the connect to succeed here, unfortunately we don't
        //  have monitoring events for inproc, so we just hope SETTLE_TIME suffices
        msleep (SETTLE_TIME);
96
    } else {
97
        // Set up connect socket
98
        connect_socket = test_context_socket (ZMQ_PUSH);
99
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
100
          connect_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
101
        TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
Mikko Koppanen's avatar
Mikko Koppanen committed
102

103
        // Set up bind socket
104
        bind_socket = test_context_socket (ZMQ_PULL);
105
        TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
106
          bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
107
        TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
108
    }
Mikko Koppanen's avatar
Mikko Koppanen committed
109

110 111
    // Send until we block
    int send_count = 0;
112 113
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
114 115 116 117 118 119 120
        ++send_count;

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

121
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
122

123
    // Now it should be possible to send one more.
124
    send_string_expect_success (connect_socket, NULL, 0);
125 126

    //  Consume the remaining message.
127
    recv_string_expect_success (bind_socket, NULL, 0);
128

129
    // Clean up
130 131
    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
Mikko Koppanen's avatar
Mikko Koppanen committed
132

133 134 135
    return send_count;
}

136
int test_inproc_bind_first (int send_hwm_, int recv_hwm_)
137
{
138
    return count_msg (send_hwm_, recv_hwm_, BIND_FIRST);
139 140
}

141
int test_inproc_connect_first (int send_hwm_, int recv_hwm_)
142
{
143
    return count_msg (send_hwm_, recv_hwm_, CONNECT_FIRST);
144 145
}

146
int test_inproc_connect_and_close_first (int send_hwm_, int recv_hwm_)
147 148
{
    // Set up connect socket
149
    void *connect_socket = test_context_socket (ZMQ_PUSH);
150
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (connect_socket, ZMQ_SNDHWM,
151
                                               &send_hwm_, sizeof (send_hwm_)));
152
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, "inproc://a"));
153 154 155

    // Send until we block
    int send_count = 0;
156 157
    while (send_count < MAX_SENDS
           && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
158 159 160
        ++send_count;

    // Close connect
161
    test_context_socket_close (connect_socket);
162 163

    // Set up bind socket
164
    void *bind_socket = test_context_socket (ZMQ_PULL);
165
    TEST_ASSERT_SUCCESS_ERRNO (
166
      zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm_, sizeof (recv_hwm_)));
167
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
168 169 170 171 172 173

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

174
    TEST_ASSERT_EQUAL_INT (send_count, recv_count);
175 176

    // Clean up
177
    test_context_socket_close (bind_socket);
178 179 180 181

    return send_count;
}

182
int test_inproc_bind_and_close_first (int send_hwm_, int /* recv_hwm */)
183 184
{
    // Set up bind socket
185
    void *bind_socket = test_context_socket (ZMQ_PUSH);
186
    TEST_ASSERT_SUCCESS_ERRNO (
187
      zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm_, sizeof (send_hwm_)));
188
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (bind_socket, "inproc://a"));
189 190 191

    // Send until we block
    int send_count = 0;
192 193
    while (send_count < MAX_SENDS
           && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
194 195 196
        ++send_count;

    // Close bind
197
    test_context_socket_close (bind_socket);
198

199
    /* TODO Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
200
    // Set up connect socket
201
    void *connect_socket = test_context_socket (ZMQ_PULL);
202 203
    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"));
204 205 206 207 208 209

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

210
    TEST_ASSERT_EQUAL_INT(send_count, recv_count);
211 212 213
    */

    // Clean up
214
    //test_context_socket_close (connect_socket);
215 216 217 218

    return send_count;
}

219
void test_infinite_both_inproc_bind_first ()
220
{
221 222 223
    int count = test_inproc_bind_first (0, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
224

225 226 227 228 229
void test_infinite_both_inproc_connect_first ()
{
    int count = test_inproc_connect_first (0, 0);
    TEST_ASSERT_EQUAL_INT (MAX_SENDS, count);
}
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
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);
}
254

255 256 257 258 259 260 261 262
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 ()
{
263
    // Send and recv buffers hwm 1, so total that can be queued is 2
264 265 266
    int count = test_inproc_connect_first (1, 1);
    TEST_ASSERT_EQUAL_INT (2, count);
}
267

268 269
void test_infinite_recv_connect_and_close_first ()
{
270
    // Send hwm of 1, send before bind so total that can be queued is 1
271 272 273
    int count = test_inproc_connect_and_close_first (1, 0);
    TEST_ASSERT_EQUAL_INT (1, count);
}
274

275 276
void test_infinite_recv_bind_and_close_first ()
{
277 278
    // 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?
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    /*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);
304

305
    return UNITY_END ();
Mikko Koppanen's avatar
Mikko Koppanen committed
306
}