0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

Lots of cleanups to self-tests

* Removed or truncated sleeps so the tests run faster
* Removed dependencies on zmq_utils
* Rewrote a few tests that were confusing
* Minor code cleanups
This commit is contained in:
Pieter Hintjens 2013-01-31 19:46:22 +01:00
parent c39cb0bde1
commit 963c6a8e2f
21 changed files with 242 additions and 335 deletions

View File

@ -1,25 +1,23 @@
/*
Copyright (c) 2012 Ian Barber
Copyright (c) 2012 Other contributors as noted in the AUTHORS file
Copyright (c) 2012 Ian Barber
Copyright (c) 2007-2013 iMatix Corporation
This file is part of 0MQ.
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ 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.
0MQ 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/>.
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 "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@ -31,12 +29,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
int main (void)
{
fprintf (stderr, "test_connect_delay running...\n");
int val;
int rc;
char buffer[16];
int seen = 0;
// TEST 1.
// First we're going to attempt to send messages to two
// pipes, one connected, the other not. We should see
@ -53,7 +48,7 @@ int main (void)
val = 0;
rc = zmq_setsockopt(to, ZMQ_LINGER, &val, sizeof(val));
assert (rc == 0);
rc = zmq_bind(to, "tcp://*:6555");
rc = zmq_bind (to, "tcp://*:6555");
assert (rc == 0);
// Create a socket pushing to two endpoints - only 1 message should arrive.
@ -71,26 +66,22 @@ int main (void)
// 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)
{
std::string message("message ");
message += ('0' + i);
rc = zmq_send (from, message.data(), message.size(), 0);
assert(rc >= 0);
for (int i = 0; i < 10; ++i) {
rc = zmq_send (from, "Hello", 5, 0);
assert (rc == 5);
}
// Sleep to allow the messages to be delivered
zmq_sleep (1);
// We now consume from the connected pipe
// - we should see just 5
seen = 0;
for (int i = 0; i < 10; ++i)
{
memset (&buffer, 0, sizeof(buffer));
rc = zmq_recv (to, &buffer, sizeof(buffer), ZMQ_DONTWAIT);
if( rc == -1)
break;
int timeout = 100;
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
int seen = 0;
while (true) {
rc = zmq_recv (to, &buffer, sizeof (buffer), 0);
if (rc == -1)
break; // Break when we didn't get a message
seen++;
}
assert (seen == 5);
@ -144,26 +135,21 @@ int main (void)
assert (rc == 0);
// Send 10 messages, all should be routed to the connected pipe
for (int i = 0; i < 10; ++i)
{
std::string message("message ");
message += ('0' + i);
rc = zmq_send (from, message.data(), message.size(), 0);
assert (rc >= 0);
for (int i = 0; i < 10; ++i) {
rc = zmq_send (from, "Hello", 5, 0);
assert (rc == 5);
}
rc = zmq_setsockopt (to, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
// Sleep to allow the messages to be delivered
zmq_sleep (1);
// Send 10 messages, all should arrive.
seen = 0;
for (int i = 0; i < 10; ++i)
{
memset(&buffer, 0, sizeof(buffer));
rc = zmq_recv (to, &buffer, sizeof(buffer), ZMQ_DONTWAIT);
// If there is a failed delivery, assert!
assert (rc != -1);
while (true) {
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);
@ -177,82 +163,75 @@ int main (void)
// TEST 3
// This time we want to validate that the same blocking behaviour
// occurs with an existing connection that is broken. We will send
// messaages to a connected pipe, disconnect and verify the messages
// messages to a connected pipe, disconnect and verify the messages
// block. Then we reconnect and verify messages flow again.
context = zmq_ctx_new();
void *context2 = zmq_ctx_new();
context = zmq_ctx_new ();
to = zmq_socket (context2, ZMQ_PULL);
assert (to);
rc = zmq_bind (to, "tcp://*:5560");
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);
val = 0;
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof(val));
// Frontend connects to backend using DELAY_ATTACH_ON_CONNECT
int on = 1;
rc = zmq_setsockopt (frontend, ZMQ_DELAY_ATTACH_ON_CONNECT, &on, sizeof (on));
assert (rc == 0);
rc = zmq_bind (backend, "tcp://*:5560");
assert (rc == 0);
rc = zmq_connect (frontend, "tcp://localhost:5560");
assert (rc == 0);
// Create a socket pushing
from = zmq_socket (context, ZMQ_PUSH);
assert (from);
// 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);
val = 0;
rc = zmq_setsockopt (from, ZMQ_LINGER, &val, sizeof(val));
assert (rc == 0);
val = 1;
rc = zmq_setsockopt (from, ZMQ_DELAY_ATTACH_ON_CONNECT, &val, sizeof(val));
// Send message from frontend to backend
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
rc = zmq_close (backend);
assert (rc == 0);
// Connect to the valid socket socket
rc = zmq_connect (from, "tcp://localhost:5560");
assert (rc == 0);
// Allow connections to stabilise
zmq_sleep(1);
// Send a message, should succeed
std::string message("message ");
rc = zmq_send (from, message.data(), message.size(), 0);
assert (rc >= 0);
rc = zmq_close (to);
assert (rc == 0);
rc = zmq_ctx_term(context2);
assert (rc == 0);
// Give time to process disconnect
zmq_sleep(1);
// Give time to process disconnect
// There's no way to do this except with a sleep
struct timespec t = { 0, 250 * 1000000 };
nanosleep (&t, NULL);
// Send a message, should fail
rc = zmq_send (from, message.data(), message.size(), ZMQ_DONTWAIT);
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == -1);
context2 = zmq_ctx_new();
to = zmq_socket (context2, ZMQ_PULL);
assert (to);
rc = zmq_bind (to, "tcp://*:5560");
// 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, "tcp://*:5560");
assert (rc == 0);
val = 0;
rc = zmq_setsockopt (to, ZMQ_LINGER, &val, sizeof(val));
assert (rc == 0);
// Allow connections to stabilise
zmq_sleep(1);
// 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);
// After the reconnect, should succeed
rc = zmq_send (from, message.data(), message.size(), 0);
assert (rc >= 0);
rc = zmq_send (frontend, "Hello", 5, ZMQ_DONTWAIT);
assert (rc == 5);
rc = zmq_close (to);
rc = zmq_close (backend);
assert (rc == 0);
rc = zmq_close (from);
rc = zmq_close (frontend);
assert (rc == 0);
rc = zmq_ctx_term(context);
assert (rc == 0);
rc = zmq_ctx_term(context2);
assert (rc == 0);
}

View File

@ -27,8 +27,6 @@
int main (void)
{
fprintf (stderr, "test_connect_resolve running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -28,14 +28,14 @@ int main(int argc, char** argv) {
size_t more_size = sizeof(more);
int iteration = 0;
while(1) {
while (1) {
zmq_pollitem_t items [] = {
{ subSocket, 0, ZMQ_POLLIN, 0 }, // read publications
{ pubSocket, 0, ZMQ_POLLIN, 0 }, // read subscriptions
{ subSocket, 0, ZMQ_POLLIN, 0 }, // read publications
{ pubSocket, 0, ZMQ_POLLIN, 0 }, // read subscriptions
};
zmq_poll(items, 2, 500);
int rc = zmq_poll (items, 2, 100);
if (items[1].revents & ZMQ_POLLIN) {
if (items [1].revents & ZMQ_POLLIN) {
while (1) {
zmq_msg_t msg;
zmq_msg_init (&msg);
@ -73,34 +73,29 @@ int main(int argc, char** argv) {
}
}
}
if (iteration == 1) {
zmq_connect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_connect: %s\n", zmq_strerror(errno));
//zmq_connect(subSocket, "tcp://127.0.0.1:30010") && printf("zmq_connect: %s\n", zmq_strerror(errno));
}
if (iteration == 4) {
zmq_disconnect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_disconnect(%d): %s\n", errno, zmq_strerror(errno));
//zmq_disconnect(subSocket, "tcp://127.0.0.1:30010") && printf("zmq_disconnect: %s\n", zmq_strerror(errno));
}
if (iteration == 10) {
if (iteration > 4 && rc == 0)
break;
}
zmq_msg_t channelEnvlp;
ZMQ_PREPARE_STRING(channelEnvlp, "foo", 3);
zmq_sendmsg(pubSocket, &channelEnvlp, ZMQ_SNDMORE) >= 0 || printf("zmq_sendmsg: %s\n",zmq_strerror(errno));
zmq_msg_send (&channelEnvlp, pubSocket, ZMQ_SNDMORE) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno));
zmq_msg_close(&channelEnvlp) && printf("zmq_msg_close: %s\n",zmq_strerror(errno));
zmq_msg_t message;
ZMQ_PREPARE_STRING(message, "this is foo!", 12);
zmq_sendmsg(pubSocket, &message, 0) >= 0 || printf("zmq_sendmsg: %s\n",zmq_strerror(errno));
zmq_msg_send (&message, pubSocket, 0) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno));
zmq_msg_close(&message) && printf("zmq_msg_close: %s\n",zmq_strerror(errno));
iteration++;
}
assert(publicationsReceived == 3);
assert(!isSubscribed);

View File

@ -19,13 +19,14 @@
*/
#include "../include/zmq.h"
#include <stdio.h>
#include "testutil.hpp"
#include <string.h>
#undef NDEBUG
#include <assert.h>
int main (void)
{
fprintf (stderr, "test_hwm running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -27,8 +27,6 @@
int main (void)
{
fprintf (stderr, "test_invalid_rep running...\n");
// Create REQ/ROUTER wiring.
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -20,7 +20,6 @@
*/
#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <pthread.h>
#include <string.h>
#include "testutil.hpp"
@ -34,27 +33,27 @@ static int rep_socket_events;
const char *addr;
extern "C"
// REQ socket monitor thread
static void *req_socket_monitor (void *ctx)
{
// REQ socket monitor thread
static void *req_socket_monitor (void *ctx)
{
zmq_event_t event;
int rc;
zmq_event_t event;
int rc;
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
rc = zmq_connect (s, "inproc://monitor.req");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_recvmsg (s, &msg, 0);
if (rc == -1 && zmq_errno() == ETERM) break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
rc = zmq_connect (s, "inproc://monitor.req");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, s, 0);
if (rc == -1 && zmq_errno() == ETERM)
break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
case ZMQ_EVENT_CONNECTED:
assert (event.data.connected.fd > 0);
assert (!strcmp (event.data.connected.addr, addr));
@ -81,34 +80,33 @@ extern "C"
assert (!strcmp (event.data.disconnected.addr, addr));
req_socket_events |= ZMQ_EVENT_DISCONNECTED;
break;
}
}
zmq_close (s);
return NULL;
}
zmq_close (s);
return NULL;
}
extern "C"
// 2nd REQ socket monitor thread
static void *req2_socket_monitor (void *ctx)
{
// 2nd REQ socket monitor thread
static void *req2_socket_monitor (void *ctx)
{
zmq_event_t event;
int rc;
zmq_event_t event;
int rc;
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
rc = zmq_connect (s, "inproc://monitor.req2");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_recvmsg (s, &msg, 0);
if (rc == -1 && zmq_errno() == ETERM) break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
rc = zmq_connect (s, "inproc://monitor.req2");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, s, 0);
if (rc == -1 && zmq_errno() == ETERM)
break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
case ZMQ_EVENT_CONNECTED:
assert (event.data.connected.fd > 0);
assert (!strcmp (event.data.connected.addr, addr));
@ -119,35 +117,33 @@ extern "C"
assert (!strcmp (event.data.closed.addr, addr));
req2_socket_events |= ZMQ_EVENT_CLOSED;
break;
}
}
zmq_close (s);
return NULL;
}
zmq_close (s);
return NULL;
}
extern "C"
// REP socket monitor thread
static void *rep_socket_monitor (void *ctx)
{
// REP socket monitor thread
static void *rep_socket_monitor (void *ctx)
{
zmq_event_t event;
int rc;
zmq_event_t event;
int rc;
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
void *s = zmq_socket (ctx, ZMQ_PAIR);
assert (s);
rc = zmq_connect (s, "inproc://monitor.rep");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_recvmsg (s, &msg, 0);
if (rc == -1 && zmq_errno() == ETERM) break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
rc = zmq_connect (s, "inproc://monitor.rep");
assert (rc == 0);
while (true) {
zmq_msg_t msg;
zmq_msg_init (&msg);
rc = zmq_msg_recv (&msg, s, 0);
if (rc == -1 && zmq_errno() == ETERM)
break;
assert (rc != -1);
memcpy (&event, zmq_msg_data (&msg), sizeof (event));
switch (event.event) {
case ZMQ_EVENT_LISTENING:
assert (event.data.listening.fd > 0);
assert (!strcmp (event.data.listening.addr, addr));
@ -173,12 +169,11 @@ extern "C"
assert (!strcmp (event.data.disconnected.addr, addr));
rep_socket_events |= ZMQ_EVENT_DISCONNECTED;
break;
}
zmq_msg_close (&msg);
}
zmq_close (s);
return NULL;
zmq_msg_close (&msg);
}
zmq_close (s);
return NULL;
}
int main (void)
@ -230,6 +225,8 @@ int main (void)
rc = zmq_connect (req, addr);
assert (rc == 0);
bounce (rep, req);
// 2nd REQ socket
req2 = zmq_socket (ctx, ZMQ_REQ);
assert (req2);
@ -243,17 +240,13 @@ int main (void)
rc = zmq_connect (req2, addr);
assert (rc == 0);
bounce (rep, req);
// Allow a window for socket events as connect can be async
zmq_sleep (1);
// Close the REP socket
rc = zmq_close (rep);
assert (rc == 0);
// Allow some time for detecting error states
zmq_sleep (1);
struct timespec t = { 0, 250 * 1000000 };
nanosleep (&t, NULL);
// Close the REQ socket
rc = zmq_close (req);
@ -263,9 +256,6 @@ int main (void)
rc = zmq_close (req2);
assert (rc == 0);
// Allow for closed or disconnected events to bubble up
zmq_sleep (1);
zmq_ctx_term (ctx);
// Expected REP socket events

View File

@ -53,21 +53,21 @@ int main (void)
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_recvmsg (sb, &msg, 0);
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc >= 0);
int more = zmq_msg_get (&msg, ZMQ_MORE);
int more = zmq_msg_more (&msg);
assert (more == 1);
// Then the first part of the message body.
rc = zmq_recvmsg (sb, &msg, 0);
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc == 1);
more = zmq_msg_get (&msg, ZMQ_MORE);
more = zmq_msg_more (&msg);
assert (more == 1);
// And finally, the second part of the message body.
rc = zmq_recvmsg (sb, &msg, 0);
rc = zmq_msg_recv (&msg, sb, 0);
assert (rc == 1);
more = zmq_msg_get (&msg, ZMQ_MORE);
more = zmq_msg_more (&msg);
assert (more == 0);
// Deallocate the infrastructure.

View File

@ -23,8 +23,6 @@
int main (void)
{
fprintf (stderr, "test_pair_inproc running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -23,8 +23,6 @@
int main (void)
{
fprintf (stderr, "test_pair_ipc running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -24,8 +24,6 @@
int main (void)
{
fprintf (stderr, "test_pair_tcp running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2007-2013 iMatix Corporation
Copyright (c) 2007-2012 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
@ -18,10 +18,11 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <zmq.h>
#include <assert.h>
#include "../include/zmq.h"
#include <string.h>
#include <stdbool.h>
#undef NDEBUG
#include <assert.h>
// ZMTP protocol greeting structure
@ -44,7 +45,6 @@ static zmtp_greeting_t greeting
int main (void)
{
fprintf (stderr, "test_raw_sock running...\n");
int rc;
// Set up our context and sockets

View File

@ -28,8 +28,6 @@
int main (void)
{
fprintf (stderr, "test_reqrep_device running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);
@ -66,13 +64,13 @@ int main (void)
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_recvmsg (router, &msg, 0);
rc = zmq_msg_recv (&msg, router, 0);
assert (rc >= 0);
int rcvmore;
size_t sz = sizeof (rcvmore);
rc = zmq_getsockopt (router, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
rc = zmq_sendmsg (dealer, &msg, rcvmore ? ZMQ_SNDMORE : 0);
rc = zmq_msg_send (&msg, dealer, rcvmore? ZMQ_SNDMORE: 0);
assert (rc >= 0);
}
@ -104,12 +102,12 @@ int main (void)
zmq_msg_t msg;
rc = zmq_msg_init (&msg);
assert (rc == 0);
rc = zmq_recvmsg (dealer, &msg, 0);
rc = zmq_msg_recv (&msg, dealer, 0);
assert (rc >= 0);
int rcvmore;
rc = zmq_getsockopt (dealer, ZMQ_RCVMORE, &rcvmore, &sz);
assert (rc == 0);
rc = zmq_sendmsg (router, &msg, rcvmore ? ZMQ_SNDMORE : 0);
rc = zmq_msg_send (&msg, router, rcvmore? ZMQ_SNDMORE: 0);
assert (rc >= 0);
}

View File

@ -23,8 +23,6 @@
int main (void)
{
fprintf (stderr, "test_reqrep_inproc running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -23,8 +23,6 @@
int main (void)
{
fprintf (stderr, "test_reqrep_ipc running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -24,8 +24,6 @@
int main (void)
{
fprintf (stderr, "test_reqrep_tcp running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);

View File

@ -19,7 +19,6 @@
*/
#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <stdio.h>
#include <string.h>
#undef NDEBUG

View File

@ -55,8 +55,6 @@ int main (void)
int rc;
pthread_t threads [THREAD_COUNT];
fprintf (stderr, "test_shutdown_stress running...\n");
for (j = 0; j != 10; j++) {
// Check the shutdown with many parallel I/O threads.

View File

@ -20,20 +20,17 @@
*/
#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <stdio.h>
#include <time.h>
#undef NDEBUG
#include <assert.h>
int main (void)
{
fprintf (stderr, "test_sub_forward running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);
// First, create an intermediate device.
// First, create an intermediate device
void *xpub = zmq_socket (ctx, ZMQ_XPUB);
assert (xpub);
int rc = zmq_bind (xpub, "tcp://127.0.0.1:5560");
@ -43,13 +40,13 @@ int main (void)
rc = zmq_bind (xsub, "tcp://127.0.0.1:5561");
assert (rc == 0);
// Create a publisher.
// Create a publisher
void *pub = zmq_socket (ctx, ZMQ_PUB);
assert (pub);
rc = zmq_connect (pub, "tcp://127.0.0.1:5561");
assert (rc == 0);
// Create a subscriber.
// Create a subscriber
void *sub = zmq_socket (ctx, ZMQ_SUB);
assert (sub);
rc = zmq_connect (sub, "tcp://127.0.0.1:5560");
@ -59,27 +56,28 @@ int main (void)
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
assert (rc == 0);
// Pass the subscription upstream through the device.
// Pass the subscription upstream through the device
char buff [32];
rc = zmq_recv (xpub, buff, sizeof (buff), 0);
assert (rc >= 0);
rc = zmq_send (xsub, buff, rc, 0);
assert (rc >= 0);
// Wait a bit till the subscription gets to the publisher.
zmq_sleep (1);
// Wait a bit till the subscription gets to the publisher
struct timespec t = { 0, 250 * 1000000 };
nanosleep (&t, NULL);
// Send an empty message.
// Send an empty message
rc = zmq_send (pub, NULL, 0, 0);
assert (rc == 0);
// Pass the message downstream through the device.
// Pass the message downstream through the device
rc = zmq_recv (xsub, buff, sizeof (buff), 0);
assert (rc >= 0);
rc = zmq_send (xpub, buff, rc, 0);
assert (rc >= 0);
// Receive the message in the subscriber.
// Receive the message in the subscriber
rc = zmq_recv (sub, buff, sizeof (buff), 0);
assert (rc == 0);

View File

@ -20,9 +20,9 @@
*/
#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <string.h>
#include <unistd.h>
#include <time.h>
#undef NDEBUG
#include <assert.h>
@ -33,8 +33,6 @@ int main (void)
char buf[32];
const char *ep = "tcp://127.0.0.1:5560";
fprintf (stderr, "unbind endpoint test running...\n");
// Create infrastructure.
void *ctx = zmq_ctx_new ();
assert (ctx);
@ -47,24 +45,25 @@ int main (void)
rc = zmq_connect (pull, ep);
assert (rc == 0);
// Pass one message through to ensure the connection is established.
// Pass one message through to ensure the connection is established
rc = zmq_send (push, "ABC", 3, 0);
assert (rc == 3);
rc = zmq_recv (pull, buf, sizeof (buf), 0);
assert (rc == 3);
// Unbind the lisnening endpoint
// Unbind the listening endpoint
rc = zmq_unbind (push, ep);
assert (rc == 0);
// Let events some time
zmq_sleep (1);
// Allow unbind to settle
struct timespec t = { 0, 250 * 1000000 };
nanosleep (&t, NULL);
// Check that sending would block (there's no outbound connection).
// Check that sending would block (there's no outbound connection)
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);
assert (rc == -1 && zmq_errno () == EAGAIN);
// Clean up.
// Clean up
rc = zmq_close (pull);
assert (rc == 0);
rc = zmq_close (push);
@ -72,10 +71,7 @@ int main (void)
rc = zmq_ctx_term (ctx);
assert (rc == 0);
// Now the other way round.
fprintf (stderr, "disconnect endpoint test running...\n");
// Create infrastructure.
// Create infrastructure
ctx = zmq_ctx_new ();
assert (ctx);
push = zmq_socket (ctx, ZMQ_PUSH);
@ -93,12 +89,12 @@ int main (void)
rc = zmq_recv (pull, buf, sizeof (buf), 0);
assert (rc == 3);
// Disconnect the bound endpoint
// Disconnect the bound endpoint
rc = zmq_disconnect (push, ep);
assert (rc == 0);
// Let events some time
zmq_sleep (1);
// Allow disconnect to settle
nanosleep (&t, NULL);
// Check that sending would block (there's no inbound connections).
rc = zmq_send (push, "ABC", 3, ZMQ_DONTWAIT);

View File

@ -1,4 +1,5 @@
/*
Copyright (c) 2007-2013 iMatix Corporation
Copyright (c) 2010-2011 250bpm s.r.o.
Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
@ -19,101 +20,69 @@
*/
#include "../include/zmq.h"
#include "../include/zmq_utils.h"
#include <pthread.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#undef NDEBUG
#include <assert.h>
extern "C"
{
void *worker(void *ctx)
{
// Worker thread connects after delay of 1 second. Then it waits
// for 1 more second, so that async connect has time to succeed.
zmq_sleep (1);
void *sc = zmq_socket (ctx, ZMQ_PUSH);
assert (sc);
int rc = zmq_connect (sc, "inproc://timeout_test");
assert (rc == 0);
zmq_sleep (1);
rc = zmq_close (sc);
assert (rc == 0);
return NULL;
}
}
int main (void)
{
fprintf (stderr, "test_timeo running...\n");
void *ctx = zmq_ctx_new ();
assert (ctx);
// Create a disconnected socket.
void *sb = zmq_socket (ctx, ZMQ_PULL);
assert (sb);
int rc = zmq_bind (sb, "inproc://timeout_test");
void *frontend = zmq_socket (ctx, ZMQ_DEALER);
assert (frontend);
int rc = zmq_bind (frontend, "inproc://timeout_test");
assert (rc == 0);
// Check whether non-blocking recv returns immediately.
char buf [] = "12345678ABCDEFGH12345678abcdefgh";
rc = zmq_recv (sb, buf, 32, ZMQ_DONTWAIT);
// Receive on disconnected socket returns immediately
char buffer [32];
rc = zmq_recv (frontend, buffer, 32, ZMQ_DONTWAIT);
assert (rc == -1);
assert (zmq_errno() == EAGAIN);
// Check whether recv timeout is honoured.
int timeout = 500;
size_t timeout_size = sizeof timeout;
rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size);
// Check whether receive timeout is honored
int timeout = 250;
rc = zmq_setsockopt (frontend, ZMQ_RCVTIMEO, &timeout, sizeof (int));
assert (rc == 0);
void *watch = zmq_stopwatch_start ();
rc = zmq_recv (sb, buf, 32, 0);
struct timeval before, after;
gettimeofday (&before, NULL);
rc = zmq_recv (frontend, buffer, 32, 0);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
unsigned long elapsed = zmq_stopwatch_stop (watch);
assert (elapsed > 440000 && elapsed < 550000);
gettimeofday (&after, NULL);
// Check whether connection during the wait doesn't distort the timeout.
timeout = 2000;
rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size);
long elapsed = (long)
((after.tv_sec * 1000 + after.tv_usec / 1000)
- (before.tv_sec * 1000 + before.tv_usec / 1000));
assert (elapsed > 200 && elapsed < 300);
// Check that normal message flow works as expected
void *backend = zmq_socket (ctx, ZMQ_DEALER);
assert (backend);
rc = zmq_connect (backend, "inproc://timeout_test");
assert (rc == 0);
pthread_t thread;
rc = pthread_create (&thread, NULL, worker, ctx);
assert (rc == 0);
watch = zmq_stopwatch_start ();
rc = zmq_recv (sb, buf, 32, 0);
assert (rc == -1);
assert (zmq_errno () == EAGAIN);
elapsed = zmq_stopwatch_stop (watch);
assert (elapsed > 1900000 && elapsed < 2100000);
rc = pthread_join (thread, NULL);
rc = zmq_setsockopt (backend, ZMQ_SNDTIMEO, &timeout, sizeof (int));
assert (rc == 0);
// Check that timeouts don't break normal message transfer.
void *sc = zmq_socket (ctx, ZMQ_PUSH);
assert (sc);
rc = zmq_setsockopt(sb, ZMQ_RCVTIMEO, &timeout, timeout_size);
assert (rc == 0);
rc = zmq_setsockopt(sb, ZMQ_SNDTIMEO, &timeout, timeout_size);
assert (rc == 0);
rc = zmq_connect (sc, "inproc://timeout_test");
assert (rc == 0);
rc = zmq_send (sc, buf, 32, 0);
assert (rc == 32);
rc = zmq_recv (sb, buf, 32, 0);
assert (rc == 32);
rc = zmq_send (backend, "Hello", 5, 0);
assert (rc == 5);
rc = zmq_recv (frontend, buffer, 32, 0);
assert (rc == 5);
// Clean-up.
rc = zmq_close (sc);
// Clean-up
rc = zmq_close (backend);
assert (rc == 0);
rc = zmq_close (sb);
rc = zmq_close (frontend);
assert (rc == 0);
rc = zmq_ctx_term (ctx);
assert (rc == 0);
return 0 ;
}

View File

@ -24,11 +24,11 @@
#include "../include/zmq.h"
#include <string.h>
#undef NDEBUG
#include <assert.h>
inline void bounce (void *sb, void *sc)
static void
bounce (void *sb, void *sc)
{
const char *content = "12345678ABCDEFGH12345678abcdefgh";