Test

Buy the Book
ccon.jpeg

This is a test.

/*
This test exposes a problem with timer handling in zloop whereby
an attempt to replace an old timer with a new one in a socket
event handler does not succeed.

The test below initialises a timer which should be replaced (before
it expired) when the reactor calls s_socket_event.

However, what happens instead is that the new timer, though
added, never fires.

The problem does not occur if the original timer is not deleted,
in which case, both timers fire at the correct time.

Compile: gcc -o timer timer.c -I/usr/local/include -lczmq -lzmq

*/

#include "czmq.h"

static int
s_fire_timer (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
printf ("Timer %p fired!\n", arg);
return 0;
}

static int
s_socket_event (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
zmsg_t *msg = zmsg_recv (item->socket);
zmsg_destroy (&msg);
printf ("Replacing timer with arg %p\n", item->socket);
zloop_timer_end (loop, item->socket);

// Adding a new timer with the same arg as the old one.
// This will never fire!
zloop_timer (loop, 1000, 1, s_fire_timer, item->socket);
return 0;
}

int
zloop_test (bool verbose)
{
int rc = 0;

zctx_t *ctx = zctx_new ();
assert (ctx);

void *output = zsocket_new (ctx, ZMQ_PAIR);
assert (output);
rc = zsocket_bind (output, "inproc://timer.test");
assert (rc == 0);
void *input = zsocket_new (ctx, ZMQ_PAIR);
assert (input);

rc = zsocket_connect (input, "inproc://timer.test");
assert (rc == 0);

zloop_t *loop = zloop_new ();

assert (loop);
zloop_set_verbose (loop, verbose);

// Create a timer which we're going to *try* to replace
// in the socket handler… (but it won't get replaced,
// and will fire eventually!)
printf ("Creating timer with arg %p\n", input);
zloop_timer (loop, 5000, 1, s_fire_timer, input);

zmq_pollitem_t poll_input = { input, 0, ZMQ_POLLIN };
rc = zloop_poller (loop, &poll_input, s_socket_event, input);
assert (rc == 0);

// Send a message to trigger the handler:
zstr_send (output, "Replace timer!");
zloop_start (loop);
zloop_destroy (&loop);
assert (loop == NULL);

zctx_destroy (&ctx);
return 0;
}

int main( void )
{
return zloop_test (true);
}

/*
This test exposes a problem with timer handling in zloop whereby
an attempt to replace an old timer with a new one in a socket
event handler does not succeed.

The test below initialises a timer which should be replaced (before
it expired) when the reactor calls s_socket_event.

However, what happens instead is that the new timer, though
added, never fires.

The problem does not occur if the original timer is not deleted,
in which case, both timers fire at the correct time.

Compile: gcc -o timer timer.c -I/usr/local/include -lczmq -lzmq

*/

#include "czmq.h"

static int
s_fire_timer (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
printf ("Timer %p fired!\n", arg);
return 0;
}

static int
s_socket_event (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
zmsg_t *msg = zmsg_recv (item->socket);
zmsg_destroy (&msg);
printf ("Replacing timer with arg %p\n", item->socket);
zloop_timer_end (loop, item->socket);

// Adding a new timer with the same arg as the old one.
// This will never fire!
zloop_timer (loop, 1000, 1, s_fire_timer, item->socket);
return 0;
}

int
zloop_test (bool verbose)
{
int rc = 0;

zctx_t *ctx = zctx_new ();
assert (ctx);

void *output = zsocket_new (ctx, ZMQ_PAIR);
assert (output);
rc = zsocket_bind (output, "inproc://timer.test");
assert (rc == 0);
void *input = zsocket_new (ctx, ZMQ_PAIR);
assert (input);

rc = zsocket_connect (input, "inproc://timer.test");
assert (rc == 0);

zloop_t *loop = zloop_new ();

assert (loop);
zloop_set_verbose (loop, verbose);

// Create a timer which we're going to *try* to replace
// in the socket handler… (but it won't get replaced,
// and will fire eventually!)
printf ("Creating timer with arg %p\n", input);
zloop_timer (loop, 5000, 1, s_fire_timer, input);

zmq_pollitem_t poll_input = { input, 0, ZMQ_POLLIN };
rc = zloop_poller (loop, &poll_input, s_socket_event, input);
assert (rc == 0);

// Send a message to trigger the handler:
zstr_send (output, "Replace timer!");
zloop_start (loop);
zloop_destroy (&loop);
assert (loop == NULL);

zctx_destroy (&ctx);
return 0;
}

int main( void )
{
return zloop_test (true);
}

/*
This test exposes a problem with timer handling in zloop whereby
an attempt to replace an old timer with a new one in a socket
event handler does not succeed.

The test below initialises a timer which should be replaced (before
it expired) when the reactor calls s_socket_event.

However, what happens instead is that the new timer, though
added, never fires.

The problem does not occur if the original timer is not deleted,
in which case, both timers fire at the correct time.

Compile: gcc -o timer timer.c -I/usr/local/include -lczmq -lzmq

*/

#include "czmq.h"

static int
s_fire_timer (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
printf ("Timer %p fired!\n", arg);
return 0;
}

static int
s_socket_event (zloop_t *loop, zmq_pollitem_t *item, void *arg)
{
zmsg_t *msg = zmsg_recv (item->socket);
zmsg_destroy (&msg);
printf ("Replacing timer with arg %p\n", item->socket);
zloop_timer_end (loop, item->socket);

// Adding a new timer with the same arg as the old one.
// This will never fire!
zloop_timer (loop, 1000, 1, s_fire_timer, item->socket);
return 0;
}

int
zloop_test (bool verbose)
{
int rc = 0;

zctx_t *ctx = zctx_new ();
assert (ctx);

void *output = zsocket_new (ctx, ZMQ_PAIR);
assert (output);
rc = zsocket_bind (output, "inproc://timer.test");
assert (rc == 0);
void *input = zsocket_new (ctx, ZMQ_PAIR);
assert (input);

rc = zsocket_connect (input, "inproc://timer.test");
assert (rc == 0);

zloop_t *loop = zloop_new ();

assert (loop);
zloop_set_verbose (loop, verbose);

// Create a timer which we're going to *try* to replace
// in the socket handler… (but it won't get replaced,
// and will fire eventually!)
printf ("Creating timer with arg %p\n", input);
zloop_timer (loop, 5000, 1, s_fire_timer, input);

zmq_pollitem_t poll_input = { input, 0, ZMQ_POLLIN };
rc = zloop_poller (loop, &poll_input, s_socket_event, input);
assert (rc == 0);

// Send a message to trigger the handler:
zstr_send (output, "Replace timer!");
zloop_start (loop);
zloop_destroy (&loop);
assert (loop == NULL);

zctx_destroy (&ctx);
return 0;
}

int main( void )
{
return zloop_test (true);
}