test_immediate.cpp 7.56 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

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

30
#include "testutil.hpp"
31
#include "testutil_unity.hpp"
32

33 34 35 36 37 38 39 40 41 42 43 44 45
#include <unity.h>

void setUp ()
{
    setup_test_context ();
}

void tearDown ()
{
    teardown_test_context ();
}

void test_immediate_1 ()
46 47 48 49
{
    int val;
    int rc;
    char buffer[16];
50 51
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
52
    // TEST 1.
53 54 55 56
    // First we're going to attempt to send messages to two
    // pipes, one connected, the other not. We should see
    // the PUSH load balancing to both pipes, and hence half
    // of the messages getting queued, as connect() creates a
57 58
    // pipe immediately.

59
    void *to = test_context_socket (ZMQ_PULL);
60

61
    // Bind the one valid receiver
62
    val = 0;
63 64 65
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
    bind_loopback_ipv4 (to, my_endpoint, len);
66 67

    // Create a socket pushing to two endpoints - only 1 message should arrive.
68
    void *from = test_context_socket (ZMQ_PUSH);
69 70

    val = 0;
71 72 73 74
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
    // This pipe will not connect (provided the ephemeral port is not 5556)
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5556"));
75
    // This pipe will
76
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
77

78 79
    msleep (SETTLE_TIME);

80 81
    // We send 10 messages, 5 should just get stuck in the queue
    // for the not-yet-connected pipe
82
    for (int i = 0; i < 10; ++i) {
83
        send_string_expect_success (from, "Hello", 0);
84 85
    }

86 87
    // We now consume from the connected pipe
    // - we should see just 5
88
    int timeout = 250;
89 90
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
91 92 93 94 95

    int seen = 0;
    while (true) {
        rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
        if (rc == -1)
96
            break; //  Break when we didn't get a message
97 98
        seen++;
    }
99
    TEST_ASSERT_EQUAL_INT (5, seen);
100

101 102 103
    test_context_socket_close (from);
    test_context_socket_close (to);
}
104 105


106 107
void test_immediate_2 ()
{
108 109 110 111
    // This time we will do the same thing, connect two pipes,
    // one of which will succeed in connecting to a bound
    // receiver, the other of which will fail. However, we will
    // also set the delay attach on connect flag, which should
112
    // cause the pipe attachment to be delayed until the connection
113
    // succeeds.
114

115
    // Bind the valid socket
116 117 118 119
    void *to = test_context_socket (ZMQ_PULL);
    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (to, my_endpoint, len);
120

121 122 123
    int val = 0;
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
124 125

    // Create a socket pushing to two endpoints - all messages should arrive.
126
    void *from = test_context_socket (ZMQ_PUSH);
127 128

    val = 0;
129 130
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
131

132
    // Set the key flag
133
    val = 1;
134 135
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val)));
136

137
    // Connect to the invalid socket
138
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5561"));
139
    // Connect to the valid socket
140
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
141

142
    // Send 10 messages, all should be routed to the connected pipe
143
    for (int i = 0; i < 10; ++i) {
144
        send_string_expect_success (from, "Hello", 0);
145
    }
146 147 148
    int timeout = 250;
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
149

150
    int seen = 0;
151
    while (true) {
152 153
        char buffer[16];
        int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
154
        if (rc == -1)
155
            break; //  Break when we didn't get a message
156
        seen++;
157
    }
158
    TEST_ASSERT_EQUAL_INT (10, seen);
159

160 161 162
    test_context_socket_close (from);
    test_context_socket_close (to);
}
163

164 165
void test_immediate_3 ()
{
166 167
    // This time we want to validate that the same blocking behaviour
    // occurs with an existing connection that is broken. We will send
168
    // messages to a connected pipe, disconnect and verify the messages
169
    // block. Then we reconnect and verify messages flow again.
170 171
    void *backend = test_context_socket (ZMQ_DEALER);
    void *frontend = test_context_socket (ZMQ_DEALER);
172

173
    int zero = 0;
174 175 176 177
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)));
178

179
    //  Frontend connects to backend using IMMEDIATE
180
    int on = 1;
181 182 183 184 185 186 187 188
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on)));

    size_t len = MAX_SOCKET_STRING;
    char my_endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (backend, my_endpoint, len);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, my_endpoint));
189 190

    //  Ping backend to frontend so we know when the connection is up
191 192
    send_string_expect_success (backend, "Hello", 0);
    recv_string_expect_success (frontend, "Hello", 0);
193

194
    // Send message from frontend to backend
195
    send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
196

197
    test_context_socket_close (backend);
198

199
    //  Give time to process disconnect
200
    msleep (SETTLE_TIME * 10);
201

202
    // Send a message, should fail
203 204
    TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
                               zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT));
205

206
    //  Recreate backend socket
207 208 209 210
    backend = test_context_socket (ZMQ_DEALER);
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, my_endpoint));
211 212

    //  Ping backend to frontend so we know when the connection is up
213 214
    send_string_expect_success (backend, "Hello", 0);
    recv_string_expect_success (frontend, "Hello", 0);
215

216
    // After the reconnect, should succeed
217
    send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
218

219 220 221
    test_context_socket_close (backend);
    test_context_socket_close (frontend);
}
222

223 224 225 226 227 228 229 230
int main (void)
{
    setup_test_environment ();
    UNITY_BEGIN ();
    RUN_TEST (test_immediate_1);
    RUN_TEST (test_immediate_2);
    RUN_TEST (test_immediate_3);
    return UNITY_END ();
231
}