test_hwm.cpp 8.97 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"
Mikko Koppanen's avatar
Mikko Koppanen committed
31

32 33 34 35
const int MAX_SENDS = 10000;

enum TestType { BIND_FIRST, CONNECT_FIRST };

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
int test_defaults ()
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);
    int rc;

    // Set up bind socket
    void *bind_socket = zmq_socket (ctx, ZMQ_PULL);
    assert (bind_socket);
    rc = zmq_bind (bind_socket, "inproc://a");
    assert (rc == 0);

    // Set up connect socket
    void *connect_socket = zmq_socket (ctx, ZMQ_PUSH);
    assert (connect_socket);
    rc = zmq_connect (connect_socket, "inproc://a");
    assert (rc == 0);

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

60 61
    msleep (SETTLE_TIME);
    
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    // Now receive all sent messages
    int recv_count = 0;
    while (zmq_recv (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++recv_count;

    assert (send_count == recv_count);

    // Clean up
    rc = zmq_close (connect_socket);
    assert (rc == 0);

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

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

    return send_count;
}

82
int count_msg (int send_hwm, int recv_hwm, TestType testType)
Mikko Koppanen's avatar
Mikko Koppanen committed
83
{
84
    void *ctx = zmq_ctx_new ();
85
    assert (ctx);
86
    int rc;
87

88 89 90
    void *bind_socket;
    void *connect_socket;
    if (testType == BIND_FIRST)
Mikko Koppanen's avatar
Mikko Koppanen committed
91
    {
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        // Set up bind socket
        bind_socket = zmq_socket (ctx, ZMQ_PULL);
        assert (bind_socket);
        rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
        assert (rc == 0);
        rc = zmq_bind (bind_socket, "inproc://a");
        assert (rc == 0);

        // Set up connect socket
        connect_socket = zmq_socket (ctx, ZMQ_PUSH);
        assert (connect_socket);
        rc = zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
        assert (rc == 0);
        rc = zmq_connect (connect_socket, "inproc://a");
        assert (rc == 0);
107
    }
108 109 110 111 112 113 114 115 116
    else
    {
        // Set up connect socket
        connect_socket = zmq_socket (ctx, ZMQ_PUSH);
        assert (connect_socket);
        rc = zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
        assert (rc == 0);
        rc = zmq_connect (connect_socket, "inproc://a");
        assert (rc == 0);
Mikko Koppanen's avatar
Mikko Koppanen committed
117

118 119 120 121 122 123
        // Set up bind socket
        bind_socket = zmq_socket (ctx, ZMQ_PULL);
        assert (bind_socket);
        rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
        assert (rc == 0);
        rc = zmq_bind (bind_socket, "inproc://a");
124 125
        assert (rc == 0);
    }
Mikko Koppanen's avatar
Mikko Koppanen committed
126

127 128 129 130 131 132 133 134 135 136 137 138
    // Send until we block
    int send_count = 0;
    while (send_count < MAX_SENDS && zmq_send (connect_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
        ++send_count;

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

    assert (send_count == recv_count);

139
    // Now it should be possible to send one more.
140
    rc = zmq_send (connect_socket, NULL, 0, 0);
141 142 143
    assert (rc == 0);

    //  Consume the remaining message.
144
    rc = zmq_recv (bind_socket, NULL, 0, 0);
145 146
    assert (rc == 0);

147 148
    // Clean up
    rc = zmq_close (connect_socket);
149 150
    assert (rc == 0);

151
    rc = zmq_close (bind_socket);
152 153
    assert (rc == 0);

154
    rc = zmq_ctx_term (ctx);
155
    assert (rc == 0);
Mikko Koppanen's avatar
Mikko Koppanen committed
156

157 158 159 160 161 162 163 164 165 166 167 168 169
    return send_count;
}

int test_inproc_bind_first (int send_hwm, int recv_hwm)
{
    return count_msg(send_hwm, recv_hwm, BIND_FIRST);
}

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

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
int test_inproc_connect_and_close_first (int send_hwm, int recv_hwm)
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);
    int rc;

    // Set up connect socket
    void *connect_socket = zmq_socket (ctx, ZMQ_PUSH);
    assert (connect_socket);
    rc = zmq_setsockopt (connect_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
    assert (rc == 0);
    rc = zmq_connect (connect_socket, "inproc://a");
    assert (rc == 0);

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

    // Close connect
    rc = zmq_close (connect_socket);
    assert (rc == 0);

    // Set up bind socket
    void *bind_socket = zmq_socket (ctx, ZMQ_PULL);
    assert (bind_socket);
    rc = zmq_setsockopt (bind_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
    assert (rc == 0);
    rc = zmq_bind (bind_socket, "inproc://a");
    assert (rc == 0);

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

    assert (send_count == recv_count);

    // Clean up
    rc = zmq_close (bind_socket);
    assert (rc == 0);

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

    return send_count;
}

218
int test_inproc_bind_and_close_first (int send_hwm, int /* recv_hwm */)
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
{
    void *ctx = zmq_ctx_new ();
    assert (ctx);
    int rc;

    // Set up bind socket
    void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
    assert (bind_socket);
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &send_hwm, sizeof (send_hwm));
    assert (rc == 0);
    rc = zmq_bind (bind_socket, "inproc://a");
    assert (rc == 0);

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

    // Close bind
    rc = zmq_close (bind_socket);
    assert (rc == 0);

    /* Can't currently do connect without then wiring up a bind as things hang, this needs top be fixed.
    // Set up connect socket
    void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
    assert (connect_socket);
    rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &recv_hwm, sizeof (recv_hwm));
    assert (rc == 0);
    rc = zmq_connect (connect_socket, "inproc://a");
    assert (rc == 0);

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

    assert (send_count == recv_count);
    */

    // Clean up
    //rc = zmq_close (connect_socket);
    //assert (rc == 0);

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

    return send_count;
}

268 269 270
int main (void)
{
    setup_test_environment();
271
    
272 273
    int count;

274 275 276 277
    // Default values are 1000 on send and 1000 one receive, so 2000 total
    count = test_defaults ();
    assert (count == 2000);

278 279 280 281 282 283
    // Infinite send and receive buffer
    count = test_inproc_bind_first (0, 0);
    assert (count == MAX_SENDS);
    count = test_inproc_connect_first (0, 0);
    assert (count == MAX_SENDS);

284
    // Infinite receive buffer
285 286 287 288 289
    count = test_inproc_bind_first (1, 0);
    assert (count == MAX_SENDS);
    count = test_inproc_connect_first (1, 0);
    assert (count == MAX_SENDS);

290
    // Infinite send buffer
291 292 293 294 295
    count = test_inproc_bind_first (0, 1);
    assert (count == MAX_SENDS);
    count = test_inproc_connect_first (0, 1);
    assert (count == MAX_SENDS);

296
    // Send and recv buffers hwm 1, so total that can be queued is 2
297 298 299 300 301
    count = test_inproc_bind_first (1, 1);
    assert (count == 2);
    count = test_inproc_connect_first (1, 1);
    assert (count == 2);

302 303 304 305 306 307 308 309 310
    // Send hwm of 1, send before bind so total that can be queued is 1
    count = test_inproc_connect_and_close_first (1, 0);
    assert (count == 1);

    // 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?
    count = test_inproc_bind_and_close_first (1, 0);
    //assert (count == 1);

311
    return 0;
Mikko Koppanen's avatar
Mikko Koppanen committed
312
}