Skip to content

Commit 4d9fc80

Browse files
authored
Merge pull request #2999 from sigiesec/migrate-to-unity
Migrate further tests to unity
2 parents 8d0d4c7 + 6d89635 commit 4d9fc80

8 files changed

Lines changed: 278 additions & 380 deletions

Makefile.am

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,12 @@ tests_test_connect_resolve_LDADD = src/libzmq.la ${UNITY_LIBS}
489489
tests_test_connect_resolve_CPPFLAGS = ${UNITY_CPPFLAGS}
490490

491491
tests_test_immediate_SOURCES = tests/test_immediate.cpp
492-
tests_test_immediate_LDADD = src/libzmq.la
492+
tests_test_immediate_LDADD = src/libzmq.la ${UNITY_LIBS}
493+
tests_test_immediate_CPPFLAGS = ${UNITY_CPPFLAGS}
493494

494495
tests_test_last_endpoint_SOURCES = tests/test_last_endpoint.cpp
495-
tests_test_last_endpoint_LDADD = src/libzmq.la
496+
tests_test_last_endpoint_LDADD = src/libzmq.la ${UNITY_LIBS}
497+
tests_test_last_endpoint_CPPFLAGS = ${UNITY_CPPFLAGS}
496498

497499
tests_test_term_endpoint_SOURCES = tests/test_term_endpoint.cpp
498500
tests_test_term_endpoint_LDADD = src/libzmq.la
@@ -853,7 +855,6 @@ test_apps += tests/test_poller \
853855
tests/test_thread_safe \
854856
tests/test_timers \
855857
tests/test_radio_dish \
856-
tests/test_udp \
857858
tests/test_scatter_gather \
858859
tests/test_dgram \
859860
tests/test_app_meta
@@ -874,10 +875,8 @@ tests_test_timers_SOURCES = tests/test_timers.cpp
874875
tests_test_timers_LDADD = src/libzmq.la
875876

876877
tests_test_radio_dish_SOURCES = tests/test_radio_dish.cpp
877-
tests_test_radio_dish_LDADD = src/libzmq.la
878-
879-
tests_test_udp_SOURCES = tests/test_udp.cpp
880-
tests_test_udp_LDADD = src/libzmq.la
878+
tests_test_radio_dish_LDADD = src/libzmq.la ${UNITY_LIBS}
879+
tests_test_radio_dish_CPPFLAGS = ${UNITY_CPPFLAGS}
881880

882881
tests_test_scatter_gather_SOURCES = tests/test_scatter_gather.cpp
883882
tests_test_scatter_gather_LDADD = src/libzmq.la

tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ IF (ENABLE_DRAFTS)
132132
test_client_server
133133
test_timers
134134
test_radio_dish
135-
test_udp
136135
test_scatter_gather
137136
test_dgram
138137
test_app_meta

tests/test_immediate.cpp

Lines changed: 98 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,22 @@
2828
*/
2929

3030
#include "testutil.hpp"
31+
#include "testutil_unity.hpp"
3132

32-
int main (void)
33+
#include <unity.h>
34+
35+
void setUp ()
36+
{
37+
setup_test_context ();
38+
}
39+
40+
void tearDown ()
41+
{
42+
teardown_test_context ();
43+
}
44+
45+
void test_immediate_1 ()
3346
{
34-
setup_test_environment ();
3547
int val;
3648
int rc;
3749
char buffer[16];
@@ -44,47 +56,38 @@ int main (void)
4456
// of the messages getting queued, as connect() creates a
4557
// pipe immediately.
4658

47-
void *context = zmq_ctx_new ();
48-
assert (context);
49-
void *to = zmq_socket (context, ZMQ_PULL);
50-
assert (to);
59+
void *to = test_context_socket (ZMQ_PULL);
5160

5261
// Bind the one valid receiver
5362
val = 0;
54-
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val));
55-
assert (rc == 0);
56-
rc = zmq_bind (to, "tcp://127.0.0.1:*");
57-
assert (rc == 0);
58-
rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
59-
assert (rc == 0);
63+
TEST_ASSERT_SUCCESS_ERRNO (
64+
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
65+
bind_loopback_ipv4 (to, my_endpoint, len);
6066

6167
// Create a socket pushing to two endpoints - only 1 message should arrive.
62-
void *from = zmq_socket (context, ZMQ_PUSH);
63-
assert (from);
68+
void *from = test_context_socket (ZMQ_PUSH);
6469

6570
val = 0;
66-
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val));
67-
// This pipe will not connect
68-
rc = zmq_connect (from, "tcp://localhost:5556");
69-
assert (rc == 0);
71+
TEST_ASSERT_SUCCESS_ERRNO (
72+
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
73+
// This pipe will not connect (provided the ephemeral port is not 5556)
74+
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5556"));
7075
// This pipe will
71-
rc = zmq_connect (from, my_endpoint);
72-
assert (rc == 0);
76+
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
7377

7478
msleep (SETTLE_TIME);
7579

7680
// We send 10 messages, 5 should just get stuck in the queue
7781
// for the not-yet-connected pipe
7882
for (int i = 0; i < 10; ++i) {
79-
rc = zmq_send (from, "Hello", 5, 0);
80-
assert (rc == 5);
83+
send_string_expect_success (from, "Hello", 0);
8184
}
8285

8386
// We now consume from the connected pipe
8487
// - we should see just 5
8588
int timeout = 250;
86-
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
87-
assert (rc == 0);
89+
TEST_ASSERT_SUCCESS_ERRNO (
90+
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
8891

8992
int seen = 0;
9093
while (true) {
@@ -93,158 +96,136 @@ int main (void)
9396
break; // Break when we didn't get a message
9497
seen++;
9598
}
96-
assert (seen == 5);
97-
98-
rc = zmq_close (from);
99-
assert (rc == 0);
99+
TEST_ASSERT_EQUAL_INT (5, seen);
100100

101-
rc = zmq_close (to);
102-
assert (rc == 0);
101+
test_context_socket_close (from);
102+
test_context_socket_close (to);
103+
}
103104

104-
rc = zmq_ctx_term (context);
105-
assert (rc == 0);
106105

107-
// TEST 2
106+
void test_immediate_2 ()
107+
{
108108
// This time we will do the same thing, connect two pipes,
109109
// one of which will succeed in connecting to a bound
110110
// receiver, the other of which will fail. However, we will
111111
// also set the delay attach on connect flag, which should
112112
// cause the pipe attachment to be delayed until the connection
113113
// succeeds.
114-
context = zmq_ctx_new ();
115114

116115
// Bind the valid socket
117-
to = zmq_socket (context, ZMQ_PULL);
118-
assert (to);
119-
rc = zmq_bind (to, "tcp://127.0.0.1:*");
120-
assert (rc == 0);
121-
len = MAX_SOCKET_STRING;
122-
rc = zmq_getsockopt (to, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
123-
assert (rc == 0);
116+
void *to = test_context_socket (ZMQ_PULL);
117+
size_t len = MAX_SOCKET_STRING;
118+
char my_endpoint[MAX_SOCKET_STRING];
119+
bind_loopback_ipv4 (to, my_endpoint, len);
124120

125-
val = 0;
126-
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val));
127-
assert (rc == 0);
121+
int val = 0;
122+
TEST_ASSERT_SUCCESS_ERRNO (
123+
zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof (val)));
128124

129125
// Create a socket pushing to two endpoints - all messages should arrive.
130-
from = zmq_socket (context, ZMQ_PUSH);
131-
assert (from);
126+
void *from = test_context_socket (ZMQ_PUSH);
132127

133128
val = 0;
134-
rc = zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val));
135-
assert (rc == 0);
129+
TEST_ASSERT_SUCCESS_ERRNO (
130+
zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof (val)));
136131

137132
// Set the key flag
138133
val = 1;
139-
rc = zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val));
140-
assert (rc == 0);
134+
TEST_ASSERT_SUCCESS_ERRNO (
135+
zmq_setsockopt (from, ZMQ_IMMEDIATE, &val, sizeof (val)));
141136

142137
// Connect to the invalid socket
143-
rc = zmq_connect (from, "tcp://localhost:5561");
144-
assert (rc == 0);
138+
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, "tcp://localhost:5561"));
145139
// Connect to the valid socket
146-
rc = zmq_connect (from, my_endpoint);
147-
assert (rc == 0);
140+
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (from, my_endpoint));
148141

149142
// Send 10 messages, all should be routed to the connected pipe
150143
for (int i = 0; i < 10; ++i) {
151-
rc = zmq_send (from, "Hello", 5, 0);
152-
assert (rc == 5);
144+
send_string_expect_success (from, "Hello", 0);
153145
}
154-
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
155-
assert (rc == 0);
146+
int timeout = 250;
147+
TEST_ASSERT_SUCCESS_ERRNO (
148+
zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int)));
156149

157-
seen = 0;
150+
int seen = 0;
158151
while (true) {
159-
rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
152+
char buffer[16];
153+
int rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
160154
if (rc == -1)
161155
break; // Break when we didn't get a message
162156
seen++;
163157
}
164-
assert (seen == 10);
165-
166-
rc = zmq_close (from);
167-
assert (rc == 0);
168-
169-
rc = zmq_close (to);
170-
assert (rc == 0);
158+
TEST_ASSERT_EQUAL_INT (10, seen);
171159

172-
rc = zmq_ctx_term (context);
173-
assert (rc == 0);
160+
test_context_socket_close (from);
161+
test_context_socket_close (to);
162+
}
174163

175-
// TEST 3
164+
void test_immediate_3 ()
165+
{
176166
// This time we want to validate that the same blocking behaviour
177167
// occurs with an existing connection that is broken. We will send
178168
// messages to a connected pipe, disconnect and verify the messages
179169
// block. Then we reconnect and verify messages flow again.
180-
context = zmq_ctx_new ();
170+
void *backend = test_context_socket (ZMQ_DEALER);
171+
void *frontend = test_context_socket (ZMQ_DEALER);
181172

182-
void *backend = zmq_socket (context, ZMQ_DEALER);
183-
assert (backend);
184-
void *frontend = zmq_socket (context, ZMQ_DEALER);
185-
assert (frontend);
186173
int zero = 0;
187-
rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero));
188-
assert (rc == 0);
189-
rc = zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero));
190-
assert (rc == 0);
174+
TEST_ASSERT_SUCCESS_ERRNO (
175+
zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
176+
TEST_ASSERT_SUCCESS_ERRNO (
177+
zmq_setsockopt (frontend, ZMQ_LINGER, &zero, sizeof (zero)));
191178

192179
// Frontend connects to backend using IMMEDIATE
193180
int on = 1;
194-
rc = zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on));
195-
assert (rc == 0);
196-
rc = zmq_bind (backend, "tcp://127.0.0.1:*");
197-
assert (rc == 0);
198-
len = MAX_SOCKET_STRING;
199-
rc = zmq_getsockopt (backend, ZMQ_LAST_ENDPOINT, my_endpoint, &len);
200-
assert (rc == 0);
201-
rc = zmq_connect (frontend, my_endpoint);
202-
assert (rc == 0);
181+
TEST_ASSERT_SUCCESS_ERRNO (
182+
zmq_setsockopt (frontend, ZMQ_IMMEDIATE, &on, sizeof (on)));
183+
184+
size_t len = MAX_SOCKET_STRING;
185+
char my_endpoint[MAX_SOCKET_STRING];
186+
bind_loopback_ipv4 (backend, my_endpoint, len);
187+
188+
TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (frontend, my_endpoint));
203189

204190
// Ping backend to frontend so we know when the connection is up
205-
rc = zmq_send (backend, "Hello", 5, 0);
206-
assert (rc == 5);
207-
rc = zmq_recv (frontend, buffer, 255, 0);
208-
assert (rc == 5);
191+
send_string_expect_success (backend, "Hello", 0);
192+
recv_string_expect_success (frontend, "Hello", 0);
209193

210194
// Send message from frontend to backend
211-
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
212-
assert (rc == 5);
195+
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
213196

214-
rc = zmq_close (backend);
215-
assert (rc == 0);
197+
test_context_socket_close (backend);
216198

217199
// Give time to process disconnect
218200
msleep (SETTLE_TIME * 10);
219201

220202
// Send a message, should fail
221-
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
222-
assert (rc == -1);
203+
TEST_ASSERT_FAILURE_ERRNO (EAGAIN,
204+
zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT));
223205

224206
// Recreate backend socket
225-
backend = zmq_socket (context, ZMQ_DEALER);
226-
assert (backend);
227-
rc = zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero));
228-
assert (rc == 0);
229-
rc = zmq_bind (backend, my_endpoint);
230-
assert (rc == 0);
207+
backend = test_context_socket (ZMQ_DEALER);
208+
TEST_ASSERT_SUCCESS_ERRNO (
209+
zmq_setsockopt (backend, ZMQ_LINGER, &zero, sizeof (zero)));
210+
TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (backend, my_endpoint));
231211

232212
// Ping backend to frontend so we know when the connection is up
233-
rc = zmq_send (backend, "Hello", 5, 0);
234-
assert (rc == 5);
235-
rc = zmq_recv (frontend, buffer, 255, 0);
236-
assert (rc == 5);
213+
send_string_expect_success (backend, "Hello", 0);
214+
recv_string_expect_success (frontend, "Hello", 0);
237215

238216
// After the reconnect, should succeed
239-
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
240-
assert (rc == 5);
217+
send_string_expect_success (frontend, "Hello", ZMQ_DONTWAIT);
241218

242-
rc = zmq_close (backend);
243-
assert (rc == 0);
244-
245-
rc = zmq_close (frontend);
246-
assert (rc == 0);
219+
test_context_socket_close (backend);
220+
test_context_socket_close (frontend);
221+
}
247222

248-
rc = zmq_ctx_term (context);
249-
assert (rc == 0);
223+
int main (void)
224+
{
225+
setup_test_environment ();
226+
UNITY_BEGIN ();
227+
RUN_TEST (test_immediate_1);
228+
RUN_TEST (test_immediate_2);
229+
RUN_TEST (test_immediate_3);
230+
return UNITY_END ();
250231
}

0 commit comments

Comments
 (0)