test_sockopt_hwm.cpp 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file

    This file is part of libzmq, the ZeroMQ core engine in C++.

    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
    (at your option) any later version.

    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.

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

32 33
const int MAX_SENDS = 10000;

34
void test_change_before_connected ()
35
{
36
    int rc;
37
    void *ctx = zmq_ctx_new ();
38

39 40
    void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
    void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
41

42
    int val = 2;
43 44 45 46
    rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
    assert (rc == 0);
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
    assert (rc == 0);
47

48 49
    zmq_connect (connect_socket, "inproc://a");
    zmq_bind (bind_socket, "inproc://a");
50

51
    size_t placeholder = sizeof (val);
52
    val = 0;
53 54 55
    rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
    assert (rc == 0);
    assert (val == 2);
56 57

    int send_count = 0;
58 59
    while (send_count < MAX_SENDS
           && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
60
        ++send_count;
61

62
    assert (send_count == 4);
63

64 65 66
    zmq_close (bind_socket);
    zmq_close (connect_socket);
    zmq_ctx_term (ctx);
67
}
68

69
void test_change_after_connected ()
70
{
71
    int rc;
72
    void *ctx = zmq_ctx_new ();
73

74 75
    void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
    void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
76

77
    int val = 1;
78 79 80 81
    rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
    assert (rc == 0);
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
    assert (rc == 0);
82

83 84
    zmq_connect (connect_socket, "inproc://a");
    zmq_bind (bind_socket, "inproc://a");
85

86
    val = 5;
87 88
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &val, sizeof (val));
    assert (rc == 0);
89

90
    size_t placeholder = sizeof (val);
91
    val = 0;
92 93 94
    rc = zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &val, &placeholder);
    assert (rc == 0);
    assert (val == 5);
95

96
    int send_count = 0;
97 98
    while (send_count < MAX_SENDS
           && zmq_send (bind_socket, NULL, 0, ZMQ_DONTWAIT) == 0)
99
        ++send_count;
100

101
    assert (send_count == 6);
102

103 104 105
    zmq_close (bind_socket);
    zmq_close (connect_socket);
    zmq_ctx_term (ctx);
106
}
107

108 109 110 111 112 113 114 115 116 117 118
int send_until_wouldblock (void *socket)
{
    int send_count = 0;
    while (send_count < MAX_SENDS
           && zmq_send (socket, &send_count, sizeof (send_count), ZMQ_DONTWAIT)
                == sizeof (send_count)) {
        ++send_count;
    }
    return send_count;
}

119 120 121
int test_fill_up_to_hwm (void *socket, int sndhwm)
{
    int send_count = send_until_wouldblock (socket);
122
    fprintf (stderr, "sndhwm==%i, send_count==%i\n", sndhwm, send_count);
123 124 125 126
    assert (send_count <= sndhwm + 1 && send_count > (sndhwm / 10));
    return send_count;
}

127
void test_decrease_when_full ()
128 129
{
    int rc;
130
    void *ctx = zmq_ctx_new ();
131

132 133
    void *bind_socket = zmq_socket (ctx, ZMQ_PUSH);
    void *connect_socket = zmq_socket (ctx, ZMQ_PULL);
134 135

    int val = 1;
136 137
    rc = zmq_setsockopt (connect_socket, ZMQ_RCVHWM, &val, sizeof (val));
    assert (rc == 0);
138

139 140 141
    int sndhwm = 100;
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
    assert (rc == 0);
142

143 144
    zmq_bind (bind_socket, "inproc://a");
    zmq_connect (connect_socket, "inproc://a");
145 146

    // Fill up to hwm
147
    int send_count = test_fill_up_to_hwm (bind_socket, sndhwm);
148

149 150
    // Decrease snd hwm
    sndhwm = 70;
151 152
    rc = zmq_setsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
    assert (rc == 0);
153

154
    int sndhwm_read = 0;
155 156 157 158 159
    size_t sndhwm_read_size = sizeof (sndhwm_read);
    rc =
      zmq_getsockopt (bind_socket, ZMQ_SNDHWM, &sndhwm_read, &sndhwm_read_size);
    assert (rc == 0);
    assert (sndhwm_read == sndhwm);
160 161

    msleep (SETTLE_TIME);
162 163 164 165

    // Read out all data (should get up to previous hwm worth so none were dropped)
    int read_count = 0;
    int read_data = 0;
166 167 168 169
    while (
      read_count < MAX_SENDS
      && zmq_recv (connect_socket, &read_data, sizeof (read_data), ZMQ_DONTWAIT)
           == sizeof (read_data)) {
170
        assert (read_count == read_data);
171 172 173
        ++read_count;
    }

174
    assert (read_count == send_count);
175 176

    // Give io thread some time to catch up
177
    msleep (SETTLE_TIME);
178 179

    // Fill up to new hwm
180
    test_fill_up_to_hwm (bind_socket, sndhwm);
181

182 183 184
    zmq_close (bind_socket);
    zmq_close (connect_socket);
    zmq_ctx_term (ctx);
185 186
}

187

188
int main ()
189
{
190 191 192
    test_change_before_connected ();
    test_change_after_connected ();
    test_decrease_when_full ();
193
}