Пример #1
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;
}
Пример #2
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;
}