forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stream_disconnect.cpp
265 lines (223 loc) · 10.5 KB
/
test_stream_disconnect.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
218
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
/*
Copyright (c) 2007-2017 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/>.
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <string.h>
SETUP_TEARDOWN_TESTCONTEXT
static const int SERVER = 0;
static const int CLIENT = 1;
struct test_message_t
{
int turn;
const char *text;
};
// NOTE: messages are sent without null terminator.
const test_message_t dialog[] = {
{CLIENT, "i can haz cheez burger?"},
{SERVER, "y u no disonnect?"},
{CLIENT, ""},
};
const int steps = sizeof (dialog) / sizeof (dialog[0]);
bool has_more (void *socket_)
{
int more = 0;
size_t more_size = sizeof (more);
int rc = zmq_getsockopt (socket_, ZMQ_RCVMORE, &more, &more_size);
if (rc != 0)
return false;
return more != 0;
}
void test_stream_disconnect ()
{
size_t len = MAX_SOCKET_STRING;
char bind_endpoint[MAX_SOCKET_STRING];
char connect_endpoint[MAX_SOCKET_STRING];
void *sockets[2];
sockets[SERVER] = test_context_socket (ZMQ_STREAM);
int enabled = 1;
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
sockets[SERVER], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sockets[SERVER], "tcp://0.0.0.0:*"));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sockets[SERVER], ZMQ_LAST_ENDPOINT, bind_endpoint, &len));
// Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
#ifdef ZMQ_HAVE_WINDOWS
sprintf (connect_endpoint, "tcp://127.0.0.1:%s",
strrchr (bind_endpoint, ':') + 1);
#else
strcpy (connect_endpoint, bind_endpoint);
#endif
sockets[CLIENT] = test_context_socket (ZMQ_STREAM);
TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
sockets[CLIENT], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)));
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sockets[CLIENT], connect_endpoint));
// wait for connect notification
// Server: Grab the 1st frame (peer routing id).
zmq_msg_t peer_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&peer_frame, sockets[SERVER], 0));
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
TEST_ASSERT_TRUE (has_more (sockets[SERVER]));
// Server: Grab the 2nd frame (actual payload).
zmq_msg_t data_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&data_frame, sockets[SERVER], 0));
TEST_ASSERT_EQUAL_INT (0, zmq_msg_size (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
// Client: Grab the 1st frame (peer routing id).
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&peer_frame, sockets[CLIENT], 0));
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
TEST_ASSERT_TRUE (has_more (sockets[CLIENT]));
// Client: Grab the 2nd frame (actual payload).
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&data_frame, sockets[CLIENT], 0));
TEST_ASSERT_EQUAL_INT (0, zmq_msg_size (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
// Send initial message.
char blob_data[256];
size_t blob_size = sizeof (blob_data);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_getsockopt (sockets[CLIENT], ZMQ_ROUTING_ID, blob_data, &blob_size));
TEST_ASSERT_GREATER_THAN (0, blob_size);
zmq_msg_t msg;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init_size (&msg, blob_size));
memcpy (zmq_msg_data (&msg), blob_data, blob_size);
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_size (&msg, strlen (dialog[0].text)));
memcpy (zmq_msg_data (&msg), dialog[0].text, strlen (dialog[0].text));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
// TODO: make sure this loop doesn't loop forever if something is wrong
// with the test (or the implementation).
int step = 0;
while (step < steps) {
// Wait until something happens.
zmq_pollitem_t items[] = {
{sockets[SERVER], 0, ZMQ_POLLIN, 0},
{sockets[CLIENT], 0, ZMQ_POLLIN, 0},
};
TEST_ASSERT_SUCCESS_ERRNO (zmq_poll (items, 2, 100));
// Check for data received by the server.
if (items[SERVER].revents & ZMQ_POLLIN) {
TEST_ASSERT_EQUAL_INT (CLIENT, dialog[step].turn);
// Grab the 1st frame (peer routing id).
zmq_msg_t peer_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_recv (&peer_frame, sockets[SERVER], 0));
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
TEST_ASSERT_TRUE (has_more (sockets[SERVER]));
// Grab the 2nd frame (actual payload).
zmq_msg_t data_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_recv (&data_frame, sockets[SERVER], 0));
// Make sure payload matches what we expect.
const char *const data =
static_cast<const char *> (zmq_msg_data (&data_frame));
const size_t size = zmq_msg_size (&data_frame);
// 0-length frame is a disconnection notification. The server
// should receive it as the last step in the dialogue.
if (size == 0) {
++step;
TEST_ASSERT_EQUAL_INT (steps, step);
} else {
TEST_ASSERT_EQUAL_INT (strlen (dialog[step].text), size);
TEST_ASSERT_EQUAL_STRING_LEN (dialog[step].text, data, size);
++step;
TEST_ASSERT_LESS_THAN_INT (steps, step);
// Prepare the response.
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_size (&data_frame, strlen (dialog[step].text)));
memcpy (zmq_msg_data (&data_frame), dialog[step].text,
zmq_msg_size (&data_frame));
// Send the response.
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&peer_frame, sockets[SERVER], ZMQ_SNDMORE));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&data_frame, sockets[SERVER], ZMQ_SNDMORE));
}
// Release resources.
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
}
// Check for data received by the client.
if (items[CLIENT].revents & ZMQ_POLLIN) {
TEST_ASSERT_EQUAL_INT (SERVER, dialog[step].turn);
// Grab the 1st frame (peer routing id).
zmq_msg_t peer_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_recv (&peer_frame, sockets[CLIENT], 0));
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&peer_frame));
TEST_ASSERT_TRUE (has_more (sockets[CLIENT]));
// Grab the 2nd frame (actual payload).
zmq_msg_t data_frame;
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_recv (&data_frame, sockets[CLIENT], 0));
TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&data_frame));
// Make sure payload matches what we expect.
const char *const data =
static_cast<const char *> (zmq_msg_data (&data_frame));
const size_t size = zmq_msg_size (&data_frame);
TEST_ASSERT_EQUAL_INT (strlen (dialog[step].text), size);
TEST_ASSERT_EQUAL_STRING_LEN (dialog[step].text, data, size);
++step;
// Prepare the response (next line in the dialog).
TEST_ASSERT_LESS_THAN_INT (steps, step);
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_init_size (&data_frame, strlen (dialog[step].text)));
memcpy (zmq_msg_data (&data_frame), dialog[step].text,
zmq_msg_size (&data_frame));
// Send the response.
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&peer_frame, sockets[CLIENT], ZMQ_SNDMORE));
TEST_ASSERT_SUCCESS_ERRNO (
zmq_msg_send (&data_frame, sockets[CLIENT], ZMQ_SNDMORE));
// Release resources.
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_frame));
TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&data_frame));
}
}
TEST_ASSERT_EQUAL_INT (steps, step);
test_context_socket_close (sockets[CLIENT]);
test_context_socket_close (sockets[SERVER]);
}
int main (int, char **)
{
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_stream_disconnect);
return UNITY_END ();
}