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
e07a6a69
Commit
e07a6a69
authored
Aug 17, 2018
by
Simon Giesecke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Problem: test_spec_router not yet using unity
Solution: migrate to unity
parent
ab22aeb8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
95 deletions
+84
-95
Makefile.am
Makefile.am
+2
-1
test_spec_router.cpp
tests/test_spec_router.cpp
+82
-94
No files found.
Makefile.am
View file @
e07a6a69
...
...
@@ -592,7 +592,8 @@ tests_test_spec_dealer_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_spec_dealer_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_spec_router_SOURCES
=
tests/test_spec_router.cpp
tests_test_spec_router_LDADD
=
src/libzmq.la
tests_test_spec_router_LDADD
=
src/libzmq.la
${
UNITY_LIBS
}
tests_test_spec_router_CPPFLAGS
=
${
UNITY_CPPFLAGS
}
tests_test_spec_pushpull_SOURCES
=
tests/test_spec_pushpull.cpp
tests_test_spec_pushpull_LDADD
=
src/libzmq.la
...
...
tests/test_spec_router.cpp
View file @
e07a6a69
...
...
@@ -28,50 +28,59 @@
*/
#include "testutil.hpp"
#include "testutil_unity.hpp"
const
char
*
bind_address
=
0
;
char
connect_address
[
MAX_SOCKET_STRING
];
#include <unity.h>
void
test_fair_queue_in
(
void
*
ctx_
)
void
setUp
(
)
{
void
*
receiver
=
zmq_socket
(
ctx_
,
ZMQ_ROUTER
);
assert
(
receiver
);
setup_test_context
();
}
void
tearDown
()
{
teardown_test_context
();
}
// SHALL receive incoming messages from its peers using a fair-queuing
// strategy.
void
test_fair_queue_in
(
const
char
*
bind_address
)
{
char
connect_address
[
MAX_SOCKET_STRING
];
void
*
receiver
=
test_context_socket
(
ZMQ_ROUTER
);
int
timeout
=
250
;
int
rc
=
zmq_setsockopt
(
receiver
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
receiver
,
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
))
);
rc
=
zmq_bind
(
receiver
,
bind_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_bind
(
receiver
,
bind_address
));
size_t
len
=
MAX_SOCKET_STRING
;
rc
=
zmq_getsockopt
(
receiver
,
ZMQ_LAST_ENDPOINT
,
connect_address
,
&
len
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_getsockopt
(
receiver
,
ZMQ_LAST_ENDPOINT
,
connect_address
,
&
len
)
);
const
unsigned
char
services
=
5
;
void
*
senders
[
services
];
for
(
unsigned
char
peer
=
0
;
peer
<
services
;
++
peer
)
{
senders
[
peer
]
=
zmq_socket
(
ctx_
,
ZMQ_DEALER
);
assert
(
senders
[
peer
]);
senders
[
peer
]
=
test_context_socket
(
ZMQ_DEALER
);
rc
=
zmq_setsockopt
(
senders
[
peer
],
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
senders
[
peer
],
ZMQ_RCVTIMEO
,
&
timeout
,
sizeof
(
int
)));
char
*
str
=
strdup
(
"A"
);
str
[
0
]
+=
peer
;
rc
=
zmq_setsockopt
(
senders
[
peer
],
ZMQ_ROUTING_ID
,
str
,
2
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
senders
[
peer
],
ZMQ_ROUTING_ID
,
str
,
2
)
);
free
(
str
);
rc
=
zmq_connect
(
senders
[
peer
],
connect_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
senders
[
peer
],
connect_address
)
);
}
msleep
(
SETTLE_TIME
);
zmq_msg_t
msg
;
rc
=
zmq_msg_init
(
&
msg
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_init
(
&
msg
));
s_send_seq
(
senders
[
0
],
"M"
,
SEQ_END
);
s_recv_seq
(
receiver
,
"A"
,
"M"
,
SEQ_END
);
...
...
@@ -87,56 +96,53 @@ void test_fair_queue_in (void *ctx_)
sum
+=
'A'
+
peer
;
}
assert
(
sum
==
services
*
'A'
+
services
*
(
services
-
1
)
/
2
);
TEST_ASSERT_EQUAL_INT
(
services
*
'A'
+
services
*
(
services
-
1
)
/
2
,
sum
);
// handle N requests
for
(
unsigned
char
peer
=
0
;
peer
<
services
;
++
peer
)
{
rc
=
zmq_msg_recv
(
&
msg
,
receiver
,
0
);
assert
(
rc
==
2
);
const
char
*
id
=
(
const
char
*
)
zmq_msg_data
(
&
msg
);
TEST_ASSERT_EQUAL_INT
(
2
,
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_recv
(
&
msg
,
receiver
,
0
))
);
const
char
*
id
=
static_cast
<
const
char
*>
(
zmq_msg_data
(
&
msg
)
);
sum
-=
id
[
0
];
s_recv_seq
(
receiver
,
"M"
,
SEQ_END
);
}
assert
(
sum
==
0
);
TEST_ASSERT_EQUAL_INT
(
0
,
sum
);
rc
=
zmq_msg_close
(
&
msg
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_close
(
&
msg
));
close_zero_linger
(
receiver
);
test_context_socket_
close_zero_linger
(
receiver
);
for
(
size_t
peer
=
0
;
peer
<
services
;
++
peer
)
close_zero_linger
(
senders
[
peer
]);
test_context_socket_
close_zero_linger
(
senders
[
peer
]);
// Wait for disconnects.
msleep
(
SETTLE_TIME
);
}
void
test_destroy_queue_on_disconnect
(
void
*
ctx_
)
// SHALL create a double queue when a peer connects to it. If this peer
// disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
// discard any messages it contains.
void
test_destroy_queue_on_disconnect
(
const
char
*
bind_address
)
{
void
*
a
=
zmq_socket
(
ctx_
,
ZMQ_ROUTER
);
assert
(
a
);
void
*
a
=
test_context_socket
(
ZMQ_ROUTER
);
int
enabled
=
1
;
int
rc
=
zmq_setsockopt
(
a
,
ZMQ_ROUTER_MANDATORY
,
&
enabled
,
sizeof
(
enabled
));
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
a
,
ZMQ_ROUTER_MANDATORY
,
&
enabled
,
sizeof
(
enabled
)));
rc
=
zmq_bind
(
a
,
bind_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_bind
(
a
,
bind_address
));
size_t
len
=
MAX_SOCKET_STRING
;
rc
=
zmq_getsockopt
(
a
,
ZMQ_LAST_ENDPOINT
,
connect_address
,
&
len
);
assert
(
rc
==
0
);
char
connect_address
[
MAX_SOCKET_STRING
];
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_getsockopt
(
a
,
ZMQ_LAST_ENDPOINT
,
connect_address
,
&
len
));
void
*
b
=
zmq_socket
(
ctx_
,
ZMQ_DEALER
);
assert
(
b
);
void
*
b
=
test_context_socket
(
ZMQ_DEALER
);
rc
=
zmq_setsockopt
(
b
,
ZMQ_ROUTING_ID
,
"B"
,
2
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_setsockopt
(
b
,
ZMQ_ROUTING_ID
,
"B"
,
2
));
rc
=
zmq_connect
(
b
,
connect_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
b
,
connect_address
));
// Wait for connection.
msleep
(
SETTLE_TIME
);
...
...
@@ -145,75 +151,57 @@ void test_destroy_queue_on_disconnect (void *ctx_)
s_send_seq
(
a
,
"B"
,
"ABC"
,
SEQ_END
);
s_send_seq
(
b
,
"DEF"
,
SEQ_END
);
rc
=
zmq_disconnect
(
b
,
connect_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_disconnect
(
b
,
connect_address
));
// Disconnect may take time and need command processing.
zmq_pollitem_t
poller
[
2
]
=
{{
a
,
0
,
0
,
0
},
{
b
,
0
,
0
,
0
}};
rc
=
zmq_poll
(
poller
,
2
,
100
);
assert
(
rc
==
0
);
rc
=
zmq_poll
(
poller
,
2
,
100
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_poll
(
poller
,
2
,
100
));
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_poll
(
poller
,
2
,
100
));
// No messages should be available, sending should fail.
zmq_msg_t
msg
;
zmq_msg_init
(
&
msg
);
rc
=
zmq_send
(
a
,
"B"
,
2
,
ZMQ_SNDMORE
|
ZMQ_DONTWAIT
);
assert
(
rc
==
-
1
);
assert
(
errno
==
EHOSTUNREACH
);
TEST_ASSERT_FAILURE_ERRNO
(
EHOSTUNREACH
,
zmq_send
(
a
,
"B"
,
2
,
ZMQ_SNDMORE
|
ZMQ_DONTWAIT
));
rc
=
zmq_msg_recv
(
&
msg
,
a
,
ZMQ_DONTWAIT
);
assert
(
rc
==
-
1
);
assert
(
errno
==
EAGAIN
);
TEST_ASSERT_FAILURE_ERRNO
(
EAGAIN
,
zmq_msg_recv
(
&
msg
,
a
,
ZMQ_DONTWAIT
));
// After a reconnect of B, the messages should still be gone
rc
=
zmq_connect
(
b
,
connect_address
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_connect
(
b
,
connect_address
));
rc
=
zmq_msg_recv
(
&
msg
,
a
,
ZMQ_DONTWAIT
);
assert
(
rc
==
-
1
);
assert
(
errno
==
EAGAIN
);
TEST_ASSERT_FAILURE_ERRNO
(
EAGAIN
,
zmq_msg_recv
(
&
msg
,
a
,
ZMQ_DONTWAIT
));
rc
=
zmq_msg_recv
(
&
msg
,
b
,
ZMQ_DONTWAIT
);
assert
(
rc
==
-
1
);
assert
(
errno
==
EAGAIN
);
TEST_ASSERT_FAILURE_ERRNO
(
EAGAIN
,
zmq_msg_recv
(
&
msg
,
b
,
ZMQ_DONTWAIT
));
rc
=
zmq_msg_close
(
&
msg
);
assert
(
rc
==
0
);
TEST_ASSERT_SUCCESS_ERRNO
(
zmq_msg_close
(
&
msg
));
close_zero_linger
(
a
);
close_zero_linger
(
b
);
test_context_socket_
close_zero_linger
(
a
);
test_context_socket_
close_zero_linger
(
b
);
// Wait for disconnects.
msleep
(
SETTLE_TIME
);
}
#define TEST_SUITE(name, bind_address) \
void test_fair_queue_in_##name () { test_fair_queue_in (bind_address); } \
void test_destroy_queue_on_disconnect_##name () \
{ \
test_destroy_queue_on_disconnect (bind_address); \
}
int
main
(
void
)
TEST_SUITE
(
inproc
,
"inproc://a"
)
TEST_SUITE
(
tcp
,
"tcp://127.0.0.1:*"
)
int
main
()
{
setup_test_environment
();
void
*
ctx
=
zmq_ctx_new
();
assert
(
ctx
);
const
char
*
binds
[]
=
{
"inproc://a"
,
"tcp://127.0.0.1:*"
};
for
(
int
transport
=
0
;
transport
<
2
;
++
transport
)
{
bind_address
=
binds
[
transport
];
// SHALL receive incoming messages from its peers using a fair-queuing
// strategy.
test_fair_queue_in
(
ctx
);
// SHALL create a double queue when a peer connects to it. If this peer
// disconnects, the ROUTER socket SHALL destroy its double queue and SHALL
// discard any messages it contains.
// *** Test disabled until libzmq does this properly ***
// test_destroy_queue_on_disconnect (ctx);
}
int
rc
=
zmq_ctx_term
(
ctx
);
assert
(
rc
==
0
);
return
0
;
UNITY_BEGIN
();
RUN_TEST
(
test_fair_queue_in_tcp
);
RUN_TEST
(
test_fair_queue_in_inproc
);
// TODO commented out until libzmq implements this properly
// RUN_TEST (test_destroy_queue_on_disconnect_tcp);
// RUN_TEST (test_destroy_queue_on_disconnect_inproc);
return
UNITY_END
();
}
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