From 82d7fdc8f9fb61fd8eee565e0a34f5f94b0585db Mon Sep 17 00:00:00 2001 From: o9000 Date: Thu, 28 Dec 2017 10:42:09 +0100 Subject: [PATCH] Timer: more tests --- src/util/timer.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/util/timer.c b/src/util/timer.c index efe4c60..c3f9a10 100644 --- a/src/util/timer.c +++ b/src/util/timer.c @@ -544,6 +544,11 @@ typedef struct { bool add; int add_value_ms; int add_interval_ms; + bool stop_other; + bool change_other; + int change_other_value_ms; + int change_other_interval_ms; + timeout *other; } TimeoutContainer; void container_callback(void *arg) { @@ -557,6 +562,12 @@ void container_callback(void *arg) { add_timeout(container->add_value_ms, container->add_interval_ms, container_callback, arg, NULL); container->add = false; } + if (container->stop_other) + stop_timeout(container->other); + else if (container->change_other) { + change_timeout(&container->other, container->change_other_value_ms, container->change_other_interval_ms, container_callback, arg); + container->change_other = false; + } } TEST(add_timeout_simple) { @@ -800,6 +811,37 @@ TEST(stop_timeout_simple_inside_callback) { ASSERT_EQUAL(container.triggered, 1); } +TEST(stop_timeout_simple_other_inside_callback) { + u_int64_t origin = 2134523; + TimeoutContainer container; + bzero(&container, sizeof(container)); + int triggered_other = 0; + + set_mock_time_ms(origin + 0); + + container.stop_other = true; + container.timeout = add_timeout(100, 0, container_callback, &container, NULL); + container.other = add_timeout(200, 0, trigger_callback, &triggered_other, NULL); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 0); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 100); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 1); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 200); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 1); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 300); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 1); + ASSERT_EQUAL(triggered_other, 0); +} + TEST(change_timeout_simple) { u_int64_t origin = 2134523; int triggered = 0; @@ -903,6 +945,38 @@ TEST(change_timeout_simple_inside_callback) { ASSERT_EQUAL(container.triggered, 3); } +TEST(change_timeout_simple_other_inside_callback) { + u_int64_t origin = 2134523; + TimeoutContainer container; + bzero(&container, sizeof(container)); + int triggered_other = 0; + + set_mock_time_ms(origin + 0); + + container.change_other = true; + container.change_other_value_ms = 100; + container.timeout = add_timeout(100, 0, container_callback, &container, NULL); + container.other = add_timeout(1000, 0, trigger_callback, &triggered_other, NULL); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 0); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 100); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 1); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 200); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 2); + ASSERT_EQUAL(triggered_other, 0); + + set_mock_time_ms(origin + 200); + handle_expired_timers(); + ASSERT_EQUAL(container.triggered, 2); + ASSERT_EQUAL(triggered_other, 0); +} + TEST(add_change_two_timeout_simple_inside_callback) { u_int64_t origin = 2134523; TimeoutContainer container;