void test_null_timer_pointers () { void *timers = NULL; int rc = zmq_timers_destroy (&timers); assert (rc == -1 && errno == EFAULT); // TODO this currently triggers an access violation #if 0 rc = zmq_timers_destroy (NULL); assert (rc == -1 && errno == EFAULT); #endif const size_t dummy_interval = 100; const int dummy_timer_id = 1; rc = zmq_timers_add (timers, dummy_interval, &handler, NULL); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_add (&timers, dummy_interval, &handler, NULL); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_cancel (timers, dummy_timer_id); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_cancel (&timers, dummy_timer_id); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_set_interval (timers, dummy_timer_id, dummy_interval); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_set_interval (&timers, dummy_timer_id, dummy_interval); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_reset (timers, dummy_timer_id); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_reset (&timers, dummy_timer_id); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_timeout (timers); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_timeout (&timers); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_execute (timers); assert (rc == -1 && errno == EFAULT); rc = zmq_timers_execute (&timers); assert (rc == -1 && errno == EFAULT); }
int sleep_and_execute (void *timers_) { int timeout = zmq_timers_timeout (timers_); // Sleep methods are inaccurate, so we sleep in a loop until time arrived while (timeout > 0) { msleep (timeout); timeout = zmq_timers_timeout (timers_); } return zmq_timers_execute (timers_); }
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; }