Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
4d9fc806
Unverified
Commit
4d9fc806
authored
Mar 16, 2018
by
Luca Boccassi
Committed by
GitHub
Mar 16, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2999 from sigiesec/migrate-to-unity
Migrate further tests to unity
parents
8d0d4c76
6d89635f
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
278 additions
and
380 deletions
+278
-380
Makefile.am
Makefile.am
+6
-7
CMakeLists.txt
tests/CMakeLists.txt
+0
-1
test_immediate.cpp
tests/test_immediate.cpp
+98
-117
test_last_endpoint.cpp
tests/test_last_endpoint.cpp
+25
-15
test_poller.cpp
tests/test_poller.cpp
+0
-7
test_radio_dish.cpp
tests/test_radio_dish.cpp
+142
-99
test_udp.cpp
tests/test_udp.cpp
+0
-134
testutil_unity.hpp
tests/testutil_unity.hpp
+7
-0
No files found.
Makefile.am
View file @
4d9fc806
...
...
@@ -489,10 +489,12 @@ tests_test_connect_resolve_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_connect_resolve_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_immediate_SOURCES
=
tests/test_immediate.cpp
tests_test_immediate_LDADD
=
src/libzmq.la
tests_test_immediate_LDADD
=
src/libzmq.la
${
UNITY_LIBS
}
tests_test_immediate_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_last_endpoint_SOURCES
=
tests/test_last_endpoint.cpp
tests_test_last_endpoint_LDADD
=
src/libzmq.la
tests_test_last_endpoint_LDADD
=
src/libzmq.la
${
UNITY_LIBS
}
tests_test_last_endpoint_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_term_endpoint_SOURCES
=
tests/test_term_endpoint.cpp
tests_test_term_endpoint_LDADD
=
src/libzmq.la
...
...
@@ -853,7 +855,6 @@ test_apps += tests/test_poller \
tests/test_thread_safe
\
tests/test_timers
\
tests/test_radio_dish
\
tests/test_udp
\
tests/test_scatter_gather
\
tests/test_dgram
\
tests/test_app_meta
...
...
@@ -874,10 +875,8 @@ tests_test_timers_SOURCES = tests/test_timers.cpp
tests_test_timers_LDADD
=
src/libzmq.la
tests_test_radio_dish_SOURCES
=
tests/test_radio_dish.cpp
tests_test_radio_dish_LDADD
=
src/libzmq.la
tests_test_udp_SOURCES
=
tests/test_udp.cpp
tests_test_udp_LDADD
=
src/libzmq.la
tests_test_radio_dish_LDADD
=
src/libzmq.la
${
UNITY_LIBS
}
tests_test_radio_dish_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_scatter_gather_SOURCES
=
tests/test_scatter_gather.cpp
tests_test_scatter_gather_LDADD
=
src/libzmq.la
...
...
tests/CMakeLists.txt
View file @
4d9fc806
...
...
@@ -132,7 +132,6 @@ IF (ENABLE_DRAFTS)
test_client_server
test_timers
test_radio_dish
test_udp
test_scatter_gather
test_dgram
test_app_meta
...
...
tests/test_immediate.cpp
View file @
4d9fc806
...
...
@@ -28,10 +28,22 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int
main
(
void
)
#include <unity.h>
void
setUp
()
{
setup_test_context
();
}
void
tearDown
()
{
teardown_test_context
();
}
void
test_immediate_1
()
{
setup_test_environment
();
int
val
;
int
rc
;
char
buffer
[
16
];
...
...
@@ -44,47 +56,38 @@ int main (void)
// of the messages getting queued, as connect() creates a
// pipe immediately.
void
*
context
=
zmq_ctx_new
();
assert
(
context
);
void
*
to
=
zmq_socket
(
context
,
ZMQ_PULL
);
assert
(
to
);
void
*
to
=
test_context_socket
(
ZMQ_PULL
);
// Bind the one valid receiver
val
=
0
;
rc
=
zmq_setsockopt
(
to
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
));
assert
(
rc
==
0
);
rc
=
zmq_bind
(
to
,
"tcp://127.0.0.1:*"
);
assert
(
rc
==
0
);
rc
=
zmq_getsockopt
(
to
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
to
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
)));
bind_loopback_ipv4
(
to
,
my_endpoint
,
len
);
// Create a socket pushing to two endpoints - only 1 message should arrive.
void
*
from
=
zmq_socket
(
context
,
ZMQ_PUSH
);
assert
(
from
);
void
*
from
=
test_context_socket
(
ZMQ_PUSH
);
val
=
0
;
zmq_setsockopt
(
from
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
));
// This pipe will not connect
rc
=
zmq_connect
(
from
,
"tcp://localhost:5556"
);
assert
(
rc
==
0
);
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"
)
);
// This pipe will
rc
=
zmq_connect
(
from
,
my_endpoint
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
from
,
my_endpoint
));
msleep
(
SETTLE_TIME
);
// We send 10 messages, 5 should just get stuck in the queue
// for the not-yet-connected pipe
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
rc
=
zmq_send
(
from
,
"Hello"
,
5
,
0
);
assert
(
rc
==
5
);
send_string_expect_success
(
from
,
"Hello"
,
0
);
}
// We now consume from the connected pipe
// - we should see just 5
int
timeout
=
250
;
rc
=
zmq_setsockopt
(
to
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
to
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
))
);
int
seen
=
0
;
while
(
true
)
{
...
...
@@ -93,158 +96,136 @@ int main (void)
break
;
// Break when we didn't get a message
seen
++
;
}
assert
(
seen
==
5
);
rc
=
zmq_close
(
from
);
assert
(
rc
==
0
);
TEST_ASSERT_EQUAL_INT
(
5
,
seen
);
rc
=
zmq_close
(
to
);
assert
(
rc
==
0
);
test_context_socket_close
(
from
);
test_context_socket_close
(
to
);
}
rc
=
zmq_ctx_term
(
context
);
assert
(
rc
==
0
);
// TEST 2
void
test_immediate_2
()
{
// 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
// cause the pipe attachment to be delayed until the connection
// succeeds.
context
=
zmq_ctx_new
();
// Bind the valid socket
to
=
zmq_socket
(
context
,
ZMQ_PULL
);
assert
(
to
);
rc
=
zmq_bind
(
to
,
"tcp://127.0.0.1:*"
);
assert
(
rc
==
0
);
len
=
MAX_SOCKET_STRING
;
rc
=
zmq_getsockopt
(
to
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
);
assert
(
rc
==
0
);
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
);
val
=
0
;
rc
=
zmq_setsockopt
(
to
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
));
assert
(
rc
==
0
);
int
val
=
0
;
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
to
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
))
);
// Create a socket pushing to two endpoints - all messages should arrive.
from
=
zmq_socket
(
context
,
ZMQ_PUSH
);
assert
(
from
);
void
*
from
=
test_context_socket
(
ZMQ_PUSH
);
val
=
0
;
rc
=
zmq_setsockopt
(
from
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
from
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
))
);
// Set the key flag
val
=
1
;
rc
=
zmq_setsockopt
(
from
,
ZMQ_IMMEDIATE
,
&
val
,
sizeof
(
val
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
from
,
ZMQ_IMMEDIATE
,
&
val
,
sizeof
(
val
))
);
// Connect to the invalid socket
rc
=
zmq_connect
(
from
,
"tcp://localhost:5561"
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
from
,
"tcp://localhost:5561"
));
// Connect to the valid socket
rc
=
zmq_connect
(
from
,
my_endpoint
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
from
,
my_endpoint
));
// Send 10 messages, all should be routed to the connected pipe
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
rc
=
zmq_send
(
from
,
"Hello"
,
5
,
0
);
assert
(
rc
==
5
);
send_string_expect_success
(
from
,
"Hello"
,
0
);
}
rc
=
zmq_setsockopt
(
to
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
int
timeout
=
250
;
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
to
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
)));
seen
=
0
;
int
seen
=
0
;
while
(
true
)
{
rc
=
zmq_recv
(
to
,
&
buffer
,
sizeof
(
buffer
),
0
);
char
buffer
[
16
];
int
rc
=
zmq_recv
(
to
,
&
buffer
,
sizeof
(
buffer
),
0
);
if
(
rc
==
-
1
)
break
;
// Break when we didn't get a message
seen
++
;
}
assert
(
seen
==
10
);
rc
=
zmq_close
(
from
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
to
);
assert
(
rc
==
0
);
TEST_ASSERT_EQUAL_INT
(
10
,
seen
);
rc
=
zmq_ctx_term
(
context
);
assert
(
rc
==
0
);
test_context_socket_close
(
from
);
test_context_socket_close
(
to
);
}
// TEST 3
void
test_immediate_3
()
{
// This time we want to validate that the same blocking behaviour
// occurs with an existing connection that is broken. We will send
// messages to a connected pipe, disconnect and verify the messages
// block. Then we reconnect and verify messages flow again.
context
=
zmq_ctx_new
();
void
*
backend
=
test_context_socket
(
ZMQ_DEALER
);
void
*
frontend
=
test_context_socket
(
ZMQ_DEALER
);
void
*
backend
=
zmq_socket
(
context
,
ZMQ_DEALER
);
assert
(
backend
);
void
*
frontend
=
zmq_socket
(
context
,
ZMQ_DEALER
);
assert
(
frontend
);
int
zero
=
0
;
rc
=
zmq_setsockopt
(
backend
,
ZMQ_LINGER
,
&
zero
,
sizeof
(
zero
));
assert
(
rc
==
0
);
rc
=
zmq_setsockopt
(
frontend
,
ZMQ_LINGER
,
&
zero
,
sizeof
(
zero
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
backend
,
ZMQ_LINGER
,
&
zero
,
sizeof
(
zero
))
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
frontend
,
ZMQ_LINGER
,
&
zero
,
sizeof
(
zero
))
);
// Frontend connects to backend using IMMEDIATE
int
on
=
1
;
rc
=
zmq_setsockopt
(
frontend
,
ZMQ_IMMEDIATE
,
&
on
,
sizeof
(
on
));
assert
(
rc
==
0
);
rc
=
zmq_bind
(
backend
,
"tcp://127.0.0.1:*"
);
assert
(
rc
==
0
);
len
=
MAX_SOCKET_STRING
;
rc
=
zmq_getsockopt
(
backend
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
);
assert
(
rc
==
0
);
rc
=
zmq_connect
(
frontend
,
my_endpoint
);
assert
(
rc
==
0
);
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
));
// Ping backend to frontend so we know when the connection is up
rc
=
zmq_send
(
backend
,
"Hello"
,
5
,
0
);
assert
(
rc
==
5
);
rc
=
zmq_recv
(
frontend
,
buffer
,
255
,
0
);
assert
(
rc
==
5
);
send_string_expect_success
(
backend
,
"Hello"
,
0
);
recv_string_expect_success
(
frontend
,
"Hello"
,
0
);
// Send message from frontend to backend
rc
=
zmq_send
(
frontend
,
"Hello"
,
5
,
ZMQ_DONTWAIT
);
assert
(
rc
==
5
);
send_string_expect_success
(
frontend
,
"Hello"
,
ZMQ_DONTWAIT
);
rc
=
zmq_close
(
backend
);
assert
(
rc
==
0
);
test_context_socket_close
(
backend
);
// Give time to process disconnect
msleep
(
SETTLE_TIME
*
10
);
// Send a message, should fail
rc
=
zmq_send
(
frontend
,
"Hello"
,
5
,
ZMQ_DONTWAIT
);
assert
(
rc
==
-
1
);
TEST_ASSERT_FAILURE_ERRNO
(
EAGAIN
,
zmq_send
(
frontend
,
"Hello"
,
5
,
ZMQ_DONTWAIT
)
);
// Recreate backend socket
backend
=
zmq_socket
(
context
,
ZMQ_DEALER
);
assert
(
backend
);
rc
=
zmq_setsockopt
(
backend
,
ZMQ_LINGER
,
&
zero
,
sizeof
(
zero
));
assert
(
rc
==
0
);
rc
=
zmq_bind
(
backend
,
my_endpoint
);
assert
(
rc
==
0
);
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
));
// Ping backend to frontend so we know when the connection is up
rc
=
zmq_send
(
backend
,
"Hello"
,
5
,
0
);
assert
(
rc
==
5
);
rc
=
zmq_recv
(
frontend
,
buffer
,
255
,
0
);
assert
(
rc
==
5
);
send_string_expect_success
(
backend
,
"Hello"
,
0
);
recv_string_expect_success
(
frontend
,
"Hello"
,
0
);
// After the reconnect, should succeed
rc
=
zmq_send
(
frontend
,
"Hello"
,
5
,
ZMQ_DONTWAIT
);
assert
(
rc
==
5
);
send_string_expect_success
(
frontend
,
"Hello"
,
ZMQ_DONTWAIT
);
rc
=
zmq_close
(
backend
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
frontend
);
assert
(
rc
==
0
);
test_context_socket_close
(
backend
);
test_context_socket_close
(
frontend
);
}
rc
=
zmq_ctx_term
(
context
);
assert
(
rc
==
0
);
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
();
}
tests/test_last_endpoint.cpp
View file @
4d9fc806
...
...
@@ -28,6 +28,19 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
#include <unity.h>
void
setUp
()
{
setup_test_context
();
}
void
tearDown
()
{
teardown_test_context
();
}
static
void
do_bind_and_verify
(
void
*
s
,
const
char
*
endpoint
)
{
...
...
@@ -39,27 +52,24 @@ static void do_bind_and_verify (void *s, const char *endpoint)
assert
(
rc
==
0
&&
strcmp
(
reported
,
endpoint
)
==
0
);
}
int
main
(
void
)
void
test_last_endpoint
(
)
{
setup_test_environment
();
// Create the infrastructure
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
void
*
sb
=
zmq_socket
(
ctx
,
ZMQ_ROUTER
);
assert
(
sb
);
void
*
sb
=
test_context_socket
(
ZMQ_ROUTER
);
int
val
=
0
;
int
rc
=
zmq_setsockopt
(
sb
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
sb
,
ZMQ_LINGER
,
&
val
,
sizeof
(
val
))
);
do_bind_and_verify
(
sb
,
ENDPOINT_1
);
do_bind_and_verify
(
sb
,
ENDPOINT_2
);
rc
=
zmq
_close
(
sb
);
assert
(
rc
==
0
);
test_context_socket
_close
(
sb
);
}
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
int
main
(
void
)
{
setup_test_environment
();
return
0
;
UNITY_BEGIN
();
RUN_TEST
(
test_last_endpoint
);
return
UNITY_END
();
}
tests/test_poller.cpp
View file @
4d9fc806
...
...
@@ -446,13 +446,6 @@ TEST_CASE_FUNC_PARAM (call_poller_wait_all_empty_without_timeout_fails,
TEST_CASE_FUNC_PARAM
(
call_poller_wait_all_empty_with_timeout_fails
,
test_with_empty_poller
)
void
bind_loopback_ipv4
(
void
*
socket
,
char
*
my_endpoint
,
size_t
len
)
{
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_bind
(
socket
,
"tcp://127.0.0.1:*"
));
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_getsockopt
(
socket
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
));
}
void
test_poll_basic
()
{
// Create few sockets
...
...
tests/test_radio_dish.cpp
View file @
4d9fc806
...
...
@@ -28,165 +28,208 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
int
msg_send
(
zmq_msg_t
*
msg_
,
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
#include <unity.h>
void
setUp
()
{
setup_test_context
();
}
void
tearDown
()
{
int
rc
=
zmq_msg_init_size
(
msg_
,
strlen
(
body_
));
if
(
rc
!=
0
)
return
rc
;
teardown_test_context
();
}
memcpy
(
zmq_msg_data
(
msg_
),
body_
,
strlen
(
body_
));
void
msg_send_expect_success
(
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
{
zmq_msg_t
msg
;
const
size_t
len
=
strlen
(
body_
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_init_size
(
&
msg
,
len
));
rc
=
zmq_msg_set_group
(
msg_
,
group_
);
if
(
rc
!=
0
)
{
zmq_msg_close
(
msg_
);
return
rc
;
}
memcpy
(
zmq_msg_data
(
&
msg
),
body_
,
len
);
rc
=
zmq_msg_send
(
msg_
,
s_
,
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_set_group
(
&
msg
,
group_
)
);
zmq_msg_close
(
msg_
);
int
rc
=
zmq_msg_send
(
&
msg
,
s_
,
0
);
TEST_ASSERT_EQUAL_INT
((
int
)
len
,
rc
);
return
rc
;
// TODO isn't the msg closed by zmq_msg_send?
zmq_msg_close
(
&
msg
);
}
int
msg_recv_cmp
(
zmq_msg_t
*
msg_
,
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
void
msg_recv_cmp
(
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
{
int
rc
=
zmq_msg_init
(
msg_
);
if
(
rc
!=
0
)
return
-
1
;
int
recv_rc
=
zmq_msg_recv
(
msg_
,
s_
,
0
);
if
(
recv_rc
==
-
1
)
return
-
1
;
if
(
strcmp
(
zmq_msg_group
(
msg_
),
group_
)
!=
0
)
{
zmq_msg_close
(
msg_
);
return
-
1
;
}
char
*
body
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
zmq_msg_size
(
msg_
)
+
1
));
memcpy
(
body
,
zmq_msg_data
(
msg_
),
zmq_msg_size
(
msg_
));
body
[
zmq_msg_size
(
msg_
)]
=
'\0'
;
if
(
strcmp
(
body
,
body_
)
!=
0
)
{
zmq_msg_close
(
msg_
);
return
-
1
;
}
zmq_msg_close
(
msg_
);
free
(
body
);
return
recv_rc
;
zmq_msg_t
msg
;
const
size_t
len
=
strlen
(
body_
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_init
(
&
msg
));
int
recv_rc
=
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_recv
(
&
msg
,
s_
,
0
));
TEST_ASSERT_EQUAL_INT
(
len
,
recv_rc
);
TEST_ASSERT_EQUAL_STRING
(
group_
,
zmq_msg_group
(
&
msg
));
TEST_ASSERT_EQUAL_STRING_LEN
(
body_
,
zmq_msg_data
(
&
msg
),
len
);
zmq_msg_close
(
&
msg
);
}
int
main
(
void
)
void
test_leave_unjoined_fails
(
)
{
size_t
len
=
MAX_SOCKET_STRING
;
char
my_endpoint
[
MAX_SOCKET_STRING
];
setup_test_environment
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
void
*
radio
=
zmq_socket
(
ctx
,
ZMQ_RADIO
);
void
*
dish
=
zmq_socket
(
ctx
,
ZMQ_DISH
);
// Leaving a group which we didn't join
TEST_ASSERT_FAILURE_ERRNO
(
EINVAL
,
zmq_leave
(
dish
,
"Movies"
)
);
int
rc
=
zmq_bind
(
radio
,
"tcp://127.0.0.1:*"
);
assert
(
rc
==
0
);
rc
=
zmq_getsockopt
(
radio
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
);
assert
(
rc
==
0
);
test_context_socket_close
(
dish
);
}
// Leaving a group which we didn't join
rc
=
zmq_leave
(
dish
,
"Movies"
);
assert
(
rc
==
-
1
);
void
test_join_too_long_fails
()
{
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
// Joining too long group
char
too_long_group
[
ZMQ_GROUP_MAX_LENGTH
+
2
];
for
(
int
index
=
0
;
index
<
ZMQ_GROUP_MAX_LENGTH
+
2
;
index
++
)
too_long_group
[
index
]
=
'A'
;
too_long_group
[
ZMQ_GROUP_MAX_LENGTH
+
1
]
=
'\0'
;
rc
=
zmq_join
(
dish
,
too_long_group
);
assert
(
rc
==
-
1
);
TEST_ASSERT_FAILURE_ERRNO
(
EINVAL
,
zmq_join
(
dish
,
too_long_group
));
// Joining
rc
=
zmq_join
(
dish
,
"Movies"
);
assert
(
rc
==
0
);
test_context_socket_close
(
dish
);
}
void
test_join_twice_fails
()
{
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_join
(
dish
,
"Movies"
));
// Duplicate Joining
rc
=
zmq_join
(
dish
,
"Movies"
);
assert
(
rc
==
-
1
);
TEST_ASSERT_FAILURE_ERRNO
(
EINVAL
,
zmq_join
(
dish
,
"Movies"
));
test_context_socket_close
(
dish
);
}
void
test_radio_dish_tcp_poll
()
{
size_t
len
=
MAX_SOCKET_STRING
;
char
my_endpoint
[
MAX_SOCKET_STRING
];
void
*
radio
=
test_context_socket
(
ZMQ_RADIO
);
bind_loopback_ipv4
(
radio
,
my_endpoint
,
len
);
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
// Joining
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_join
(
dish
,
"Movies"
));
// Connecting
rc
=
zmq_connect
(
dish
,
my_endpoint
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
dish
,
my_endpoint
));
msleep
(
SETTLE_TIME
);
zmq_msg_t
msg
;
// This is not going to be sent as dish only subscribe to "Movies"
rc
=
msg_send
(
&
msg
,
radio
,
"TV"
,
"Friends"
);
assert
(
rc
==
7
);
msg_send_expect_success
(
radio
,
"TV"
,
"Friends"
);
// This is going to be sent to the dish
rc
=
msg_send
(
&
msg
,
radio
,
"Movies"
,
"Godfather"
);
assert
(
rc
==
9
);
msg_send_expect_success
(
radio
,
"Movies"
,
"Godfather"
);
// Check the correct message arrived
rc
=
msg_recv_cmp
(
&
msg
,
dish
,
"Movies"
,
"Godfather"
);
assert
(
rc
==
9
);
msg_recv_cmp
(
dish
,
"Movies"
,
"Godfather"
);
// Join group during connection optvallen
rc
=
zmq_join
(
dish
,
"TV"
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_join
(
dish
,
"TV"
));
zmq_sleep
(
1
);
// This should arrive now as we joined the group
rc
=
msg_send
(
&
msg
,
radio
,
"TV"
,
"Friends"
);
assert
(
rc
==
7
);
msg_send_expect_success
(
radio
,
"TV"
,
"Friends"
);
// Check the correct message arrived
rc
=
msg_recv_cmp
(
&
msg
,
dish
,
"TV"
,
"Friends"
);
assert
(
rc
==
7
);
msg_recv_cmp
(
dish
,
"TV"
,
"Friends"
);
// Leaving groupr
rc
=
zmq_leave
(
dish
,
"TV"
);
assert
(
rc
==
0
);
// Leaving group
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_leave
(
dish
,
"TV"
));
zmq_sleep
(
1
);
// This is not going to be sent as dish only subscribe to "Movies"
rc
=
msg_send
(
&
msg
,
radio
,
"TV"
,
"Friends"
);
assert
(
rc
==
7
);
msg_send_expect_success
(
radio
,
"TV"
,
"Friends"
);
// This is going to be sent to the dish
rc
=
msg_send
(
&
msg
,
radio
,
"Movies"
,
"Godfather"
);
assert
(
rc
==
9
);
msg_send_expect_success
(
radio
,
"Movies"
,
"Godfather"
);
// test zmq_poll with dish
zmq_pollitem_t
items
[]
=
{
{
radio
,
0
,
ZMQ_POLLIN
,
0
},
// read publications
{
dish
,
0
,
ZMQ_POLLIN
,
0
},
// read subscriptions
};
rc
=
zmq_poll
(
items
,
2
,
2000
);
assert
(
rc
==
1
);
assert
(
items
[
1
].
revents
==
ZMQ_POLLIN
);
int
rc
=
zmq_poll
(
items
,
2
,
2000
);
TEST_ASSERT_EQUAL_INT
(
1
,
rc
);
TEST_ASSERT_EQUAL_INT
(
ZMQ_POLLIN
,
items
[
1
].
revents
);
// Check the correct message arrived
rc
=
msg_recv_cmp
(
&
msg
,
dish
,
"Movies"
,
"Godfather"
);
assert
(
rc
==
9
);
msg_recv_cmp
(
dish
,
"Movies"
,
"Godfather"
);
test_context_socket_close
(
dish
);
test_context_socket_close
(
radio
);
}
void
test_dish_connect_fails
()
{
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
// Connecting dish should fail
TEST_ASSERT_FAILURE_ERRNO
(
ENOCOMPATPROTO
,
zmq_connect
(
dish
,
"udp://127.0.0.1:5556"
));
test_context_socket_close
(
dish
);
}
void
test_radio_bind_fails
()
{
void
*
radio
=
test_context_socket
(
ZMQ_RADIO
);
// Connecting dish should fail
// Bind radio should fail
TEST_ASSERT_FAILURE_ERRNO
(
ENOCOMPATPROTO
,
zmq_bind
(
radio
,
"udp://*:5556"
));
test_context_socket_close
(
radio
);
}
rc
=
zmq_close
(
dish
);
assert
(
rc
==
0
);
void
test_radio_dish_udp
()
{
void
*
radio
=
test_context_socket
(
ZMQ_RADIO
);
void
*
dish
=
test_context_socket
(
ZMQ_DISH
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_bind
(
dish
,
"udp://*:5556"
));
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
radio
,
"udp://127.0.0.1:5556"
));
msleep
(
SETTLE_TIME
);
rc
=
zmq_close
(
radio
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_join
(
dish
,
"TV"
));
msg_send_expect_success
(
radio
,
"TV"
,
"Friends"
);
msg_recv_cmp
(
dish
,
"TV"
,
"Friends"
);
test_context_socket_close
(
dish
);
test_context_socket_close
(
radio
);
}
int
main
(
void
)
{
setup_test_environment
();
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
UNITY_BEGIN
();
RUN_TEST
(
test_leave_unjoined_fails
);
RUN_TEST
(
test_join_too_long_fails
);
RUN_TEST
(
test_join_twice_fails
);
RUN_TEST
(
test_radio_bind_fails
);
RUN_TEST
(
test_dish_connect_fails
);
RUN_TEST
(
test_radio_dish_tcp_poll
);
RUN_TEST
(
test_radio_dish_udp
);
return
0
;
return
UNITY_END
()
;
}
tests/test_udp.cpp
deleted
100644 → 0
View file @
8d0d4c76
/*
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/>.
*/
#include "testutil.hpp"
int
msg_send
(
zmq_msg_t
*
msg_
,
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
{
int
rc
=
zmq_msg_init_size
(
msg_
,
strlen
(
body_
));
if
(
rc
!=
0
)
return
rc
;
memcpy
(
zmq_msg_data
(
msg_
),
body_
,
strlen
(
body_
));
rc
=
zmq_msg_set_group
(
msg_
,
group_
);
if
(
rc
!=
0
)
{
zmq_msg_close
(
msg_
);
return
rc
;
}
rc
=
zmq_msg_send
(
msg_
,
s_
,
0
);
zmq_msg_close
(
msg_
);
return
rc
;
}
int
msg_recv_cmp
(
zmq_msg_t
*
msg_
,
void
*
s_
,
const
char
*
group_
,
const
char
*
body_
)
{
int
rc
=
zmq_msg_init
(
msg_
);
if
(
rc
!=
0
)
return
-
1
;
int
recv_rc
=
zmq_msg_recv
(
msg_
,
s_
,
0
);
if
(
recv_rc
==
-
1
)
{
zmq_msg_close
(
msg_
);
return
-
1
;
}
if
(
strcmp
(
zmq_msg_group
(
msg_
),
group_
)
!=
0
)
{
zmq_msg_close
(
msg_
);
return
-
1
;
}
char
*
body
=
(
char
*
)
malloc
(
sizeof
(
char
)
*
(
zmq_msg_size
(
msg_
)
+
1
));
memcpy
(
body
,
zmq_msg_data
(
msg_
),
zmq_msg_size
(
msg_
));
body
[
zmq_msg_size
(
msg_
)]
=
'\0'
;
if
(
strcmp
(
body
,
body_
)
!=
0
)
{
zmq_msg_close
(
msg_
);
free
(
body
);
return
-
1
;
}
zmq_msg_close
(
msg_
);
free
(
body
);
return
recv_rc
;
}
int
main
(
void
)
{
setup_test_environment
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
zmq_msg_t
msg
;
void
*
radio
=
zmq_socket
(
ctx
,
ZMQ_RADIO
);
void
*
dish
=
zmq_socket
(
ctx
,
ZMQ_DISH
);
// Connecting dish should fail
int
rc
=
zmq_connect
(
dish
,
"udp://127.0.0.1:5556"
);
assert
(
rc
==
-
1
);
rc
=
zmq_bind
(
dish
,
"udp://*:5556"
);
assert
(
rc
==
0
);
// Bind radio should fail
rc
=
zmq_bind
(
radio
,
"udp://*:5556"
);
assert
(
rc
==
-
1
);
rc
=
zmq_connect
(
radio
,
"udp://127.0.0.1:5556"
);
assert
(
rc
==
0
);
msleep
(
SETTLE_TIME
);
rc
=
zmq_join
(
dish
,
"TV"
);
assert
(
rc
==
0
);
rc
=
msg_send
(
&
msg
,
radio
,
"TV"
,
"Friends"
);
assert
(
rc
!=
-
1
);
rc
=
msg_recv_cmp
(
&
msg
,
dish
,
"TV"
,
"Friends"
);
assert
(
rc
!=
-
1
);
rc
=
zmq_close
(
dish
);
assert
(
rc
==
0
);
rc
=
zmq_close
(
radio
);
assert
(
rc
==
0
);
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
return
0
;
}
tests/testutil_unity.hpp
View file @
4d9fc806
...
...
@@ -191,3 +191,10 @@ void *test_context_socket_close (void *socket)
internal_manage_test_sockets
(
socket
,
false
);
return
socket
;
}
void
bind_loopback_ipv4
(
void
*
socket
,
char
*
my_endpoint
,
size_t
len
)
{
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_bind
(
socket
,
"tcp://127.0.0.1:*"
));
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_getsockopt
(
socket
,
ZMQ_LAST_ENDPOINT
,
my_endpoint
,
&
len
));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment