2022-11-08 22:20:21 +00:00
|
|
|
// This file Copyright (C) 2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef> // size_t
|
2022-11-08 22:20:21 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <event2/event.h>
|
|
|
|
|
|
|
|
#include <libtransmission/timer.h>
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/timer-ev.h>
|
|
|
|
#include <libtransmission/utils-ev.h>
|
2022-11-08 22:20:21 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "gtest/gtest.h"
|
2022-11-08 22:20:21 +00:00
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
|
|
|
namespace libtransmission::test
|
|
|
|
{
|
|
|
|
|
|
|
|
class TimerTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
// setup + teardown to manage an event_base
|
|
|
|
|
|
|
|
void SetUp() override
|
|
|
|
{
|
|
|
|
::testing::Test::SetUp();
|
|
|
|
evbase_.reset(event_base_new());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override
|
|
|
|
{
|
|
|
|
evbase_.reset();
|
|
|
|
::testing::Test::TearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
static auto constexpr AsMSec = [](auto val)
|
|
|
|
{
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(val);
|
|
|
|
};
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
void sleepMsec(std::chrono::milliseconds msec)
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
EXPECT_FALSE(waitFor(
|
|
|
|
evbase_.get(),
|
|
|
|
[]() { return false; },
|
|
|
|
msec));
|
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
static void expectTime(
|
2022-11-08 22:20:21 +00:00
|
|
|
std::chrono::milliseconds expected,
|
|
|
|
std::chrono::milliseconds actual,
|
|
|
|
std::chrono::milliseconds allowed_deviation)
|
|
|
|
{
|
|
|
|
auto const lower_bound = expected - allowed_deviation;
|
|
|
|
EXPECT_LT(lower_bound, actual) << "lower_bound:" << lower_bound.count() << " actual:" << actual.count();
|
|
|
|
|
|
|
|
auto const upper_bound = expected + allowed_deviation;
|
|
|
|
EXPECT_LT(actual, upper_bound) << "actual:" << actual.count() << " upper_bound:" << upper_bound.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This checks that `actual` is in the bounds of [expected/2 ... expected*1.5]
|
|
|
|
// to confirm that the timer didn't kick too close to the previous or next interval.
|
2022-11-11 16:09:24 +00:00
|
|
|
static void expectInterval(std::chrono::milliseconds expected, std::chrono::milliseconds actual)
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
2022-11-11 16:09:24 +00:00
|
|
|
expectTime(expected, actual, expected / 2);
|
2022-11-08 22:20:21 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
[[nodiscard]] static auto currentTime()
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
return std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
evhelpers::evbase_unique_ptr evbase_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(TimerTest, canInstantiate)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, singleShotCallsCallback)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto called = false;
|
|
|
|
auto callback = [&called]()
|
|
|
|
{
|
|
|
|
called = true;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
static auto constexpr Interval = 100ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
waitFor(evbase_.get(), [&called] { return called; });
|
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, repeatingCallsCallback)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto called = false;
|
|
|
|
auto callback = [&called]()
|
|
|
|
{
|
|
|
|
called = true;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
static auto constexpr Interval = 100ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_repeating(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
waitFor(evbase_.get(), [&called] { return called; });
|
|
|
|
EXPECT_TRUE(called);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, singleShotHonorsInterval)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto called = false;
|
|
|
|
auto callback = [&called]()
|
|
|
|
{
|
|
|
|
called = true;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// run a single-shot timer
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const begin_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
static auto constexpr Interval = 100ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(Interval);
|
|
|
|
EXPECT_FALSE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(Interval, timer->interval());
|
|
|
|
waitFor(evbase_.get(), [&called] { return called; });
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const end_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// confirm that it kicked at the right interval
|
|
|
|
EXPECT_TRUE(called);
|
2022-11-11 16:09:24 +00:00
|
|
|
expectInterval(Interval, AsMSec(end_time - begin_time));
|
2022-11-08 22:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, repeatingHonorsInterval)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// start a repeating timer
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const begin_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
static auto constexpr Interval = 100ms;
|
|
|
|
static auto constexpr DesiredLoops = 3;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_repeating(Interval);
|
|
|
|
EXPECT_TRUE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(Interval, timer->interval());
|
|
|
|
waitFor(evbase_.get(), [&n_calls] { return n_calls >= DesiredLoops; });
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const end_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// confirm that it kicked the right number of times
|
2022-11-11 16:09:24 +00:00
|
|
|
expectInterval(Interval * DesiredLoops, AsMSec(end_time - begin_time));
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(DesiredLoops, n_calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, restartWithDifferentInterval)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
auto const test = [this, &n_calls, &timer](auto interval)
|
|
|
|
{
|
|
|
|
auto const next = n_calls + 1;
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const begin_time = currentTime();
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
waitFor(evbase_.get(), [&n_calls, next]() { return n_calls >= next; });
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const end_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
expectInterval(interval, AsMSec(end_time - begin_time));
|
2022-11-08 22:20:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
test(200ms);
|
2023-01-08 21:28:54 +00:00
|
|
|
test(400ms);
|
|
|
|
test(200ms);
|
2022-11-08 22:20:21 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 23:35:22 +00:00
|
|
|
TEST_F(TimerTest, DISABLED_restartWithSameInterval)
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
auto const test = [this, &n_calls, &timer](auto interval)
|
|
|
|
{
|
|
|
|
auto const next = n_calls + 1;
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const begin_time = currentTime();
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
waitFor(evbase_.get(), [&n_calls, next]() { return n_calls >= next; });
|
2022-11-11 16:09:24 +00:00
|
|
|
auto const end_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
|
2022-11-11 16:09:24 +00:00
|
|
|
expectInterval(interval, AsMSec(end_time - begin_time));
|
2022-11-08 22:20:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
test(timer->interval());
|
|
|
|
test(timer->interval());
|
|
|
|
test(timer->interval());
|
|
|
|
}
|
|
|
|
|
2023-02-10 23:35:22 +00:00
|
|
|
TEST_F(TimerTest, DISABLED_repeatingThenSingleShot)
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// start a repeating timer and confirm that it's running
|
2022-11-11 16:09:24 +00:00
|
|
|
auto begin_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
static auto constexpr RepeatingInterval = 100ms;
|
|
|
|
static auto constexpr DesiredLoops = 2;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_repeating(RepeatingInterval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(RepeatingInterval, timer->interval());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_TRUE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
waitFor(evbase_.get(), [&n_calls]() { return n_calls >= DesiredLoops; });
|
2022-11-11 16:09:24 +00:00
|
|
|
auto end_time = currentTime();
|
|
|
|
expectTime(RepeatingInterval * DesiredLoops, AsMSec(end_time - begin_time), RepeatingInterval / 2);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// now restart it as a single shot
|
|
|
|
auto const baseline = n_calls;
|
2022-11-11 16:09:24 +00:00
|
|
|
begin_time = currentTime();
|
2023-01-08 21:28:54 +00:00
|
|
|
static auto constexpr SingleShotInterval = 200ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(SingleShotInterval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(SingleShotInterval, timer->interval());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_FALSE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
waitFor(evbase_.get(), [&n_calls]() { return n_calls >= DesiredLoops + 1; });
|
2022-11-11 16:09:24 +00:00
|
|
|
end_time = currentTime();
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// confirm that the single shot interval was honored
|
2022-11-11 16:09:24 +00:00
|
|
|
expectInterval(SingleShotInterval, AsMSec(end_time - begin_time));
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// confirm that the timer only kicks once, since it was converted into single-shot
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(SingleShotInterval * 3);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(baseline + 1, n_calls);
|
|
|
|
}
|
|
|
|
|
2023-02-10 23:35:22 +00:00
|
|
|
TEST_F(TimerTest, DISABLED_singleShotStop)
|
2022-11-08 22:20:21 +00:00
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// start a single-shot timer
|
|
|
|
static auto constexpr Interval = 200ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_single_shot(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(Interval, timer->interval());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_FALSE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// wait half the interval, then stop the timer
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval / 2);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
timer->stop();
|
|
|
|
|
|
|
|
// wait until the timer has gone past.
|
|
|
|
// since we stopped it, callback should not have been called.
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, repeatingStop)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// start a repeating timer
|
|
|
|
static auto constexpr Interval = 200ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_repeating(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(Interval, timer->interval());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_TRUE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// wait half the interval, then stop the timer
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval / 2);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
timer->stop();
|
|
|
|
|
|
|
|
// wait until the timer has gone past.
|
|
|
|
// since we stopped it, callback should not have been called.
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TimerTest, destroyedTimersStop)
|
|
|
|
{
|
|
|
|
auto timer_maker = EvTimerMaker{ evbase_.get() };
|
|
|
|
auto timer = timer_maker.create();
|
|
|
|
EXPECT_TRUE(timer);
|
|
|
|
|
|
|
|
auto n_calls = size_t{ 0U };
|
|
|
|
auto callback = [&n_calls]()
|
|
|
|
{
|
|
|
|
++n_calls;
|
|
|
|
};
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->set_callback(callback);
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// start a repeating timer
|
|
|
|
static auto constexpr Interval = 200ms;
|
2023-05-06 04:11:05 +00:00
|
|
|
timer->start_repeating(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(Interval, timer->interval());
|
2023-05-06 04:11:05 +00:00
|
|
|
EXPECT_TRUE(timer->is_repeating());
|
2022-11-08 22:20:21 +00:00
|
|
|
|
|
|
|
// wait half the interval, then destroy the timer
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval / 2);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
timer.reset();
|
|
|
|
|
|
|
|
// wait until the timer has gone past.
|
|
|
|
// since we destroyed it, callback should not have been called.
|
2022-11-11 16:09:24 +00:00
|
|
|
sleepMsec(Interval);
|
2022-11-08 22:20:21 +00:00
|
|
|
EXPECT_EQ(0U, n_calls);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace libtransmission::test
|