void test_corner_cases () { void *timers = zmq_timers_new (); assert (timers); const size_t dummy_interval = SIZE_MAX; const int dummy_timer_id = 1; // attempt to cancel non-existent timer int rc = zmq_timers_cancel (timers, dummy_timer_id); assert (rc == -1 && errno == EINVAL); // attempt to set interval of non-existent timer rc = zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval); assert (rc == -1 && errno == EINVAL); // attempt to reset non-existent timer rc = zmq_timers_reset (timers, dummy_timer_id); assert (rc == -1 && errno == EINVAL); // attempt to add NULL handler rc = zmq_timers_add (timers, dummy_interval, NULL, NULL); assert (rc == -1 && errno == EFAULT); int timer_id = zmq_timers_add (timers, dummy_interval, handler, NULL); assert (timer_id != -1); // attempt to cancel timer twice // TODO should this case really be an error? canceling twice could be allowed rc = zmq_timers_cancel (timers, timer_id); assert (rc == 0); rc = zmq_timers_cancel (timers, timer_id); assert (rc == -1 && errno == EINVAL); // timeout without any timers active rc = zmq_timers_timeout (timers); assert (rc == -1); rc = zmq_timers_destroy (&timers); assert (rc == 0); }
int main (void) { setup_test_environment (); void *timers = zmq_timers_new (); assert (timers); bool timer_invoked = false; const unsigned long full_timeout = 100; void *const stopwatch = zmq_stopwatch_start (); int timer_id = zmq_timers_add (timers, full_timeout, handler, &timer_invoked); assert (timer_id); // Timer should not have been invoked yet int rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) { assert (!timer_invoked); } #endif // Wait half the time and check again long timeout = zmq_timers_timeout (timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < full_timeout) { assert (!timer_invoked); } #endif // Wait until the end rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // Wait half the time and check again timeout = zmq_timers_timeout (timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); #ifdef ZMQ_BUILD_DRAFT_API if (zmq_stopwatch_intermediate (stopwatch) < 2 * full_timeout) { assert (!timer_invoked); } #endif // Reset timer and wait half of the time left rc = zmq_timers_reset (timers, timer_id); assert (rc == 0); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); if (zmq_stopwatch_stop (stopwatch) < 2 * full_timeout) { assert (!timer_invoked); } // Wait until the end rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // reschedule rc = zmq_timers_set_interval (timers, timer_id, 50); assert (rc == 0); rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // cancel timer timeout = zmq_timers_timeout (timers); assert (rc != -1); rc = zmq_timers_cancel (timers, timer_id); assert (rc == 0); msleep (timeout * 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); rc = zmq_timers_destroy (&timers); assert (rc == 0); test_null_timer_pointers (); test_corner_cases (); return 0; }
int main (void) { setup_test_environment (); void* timers = zmq_timers_new (); assert (timers); bool timer_invoked = false; int timer_id = zmq_timers_add (timers, 100, handler, &timer_invoked); assert (timer_id); // Timer should be invoked yet int rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); // Wait half the time and check again long timeout = zmq_timers_timeout(timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); // Wait until the end rc = sleep_and_execute (timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // Wait half the time and check again timeout = zmq_timers_timeout (timers); assert (rc != -1); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); // Reset timer and wait half of the time left rc = zmq_timers_reset (timers, timer_id); assert (rc == 0); msleep (timeout / 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); // Wait until the end rc = sleep_and_execute(timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // reschedule rc = zmq_timers_set_interval (timers, timer_id, 50); assert (rc == 0); rc = sleep_and_execute(timers); assert (rc == 0); assert (timer_invoked); timer_invoked = false; // cancel timer timeout = zmq_timers_timeout (timers); assert (rc != -1); rc = zmq_timers_cancel (timers, timer_id); assert (rc == 0); msleep (timeout * 2); rc = zmq_timers_execute (timers); assert (rc == 0); assert (!timer_invoked); rc = zmq_timers_destroy (&timers); assert (rc == 0); test_null_timer_pointers (); test_corner_cases (); return 0; }