2023-07-25 11:53:09 +02:00
|
|
|
#include "mono_time.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-12-24 12:21:34 +01:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
2024-01-14 21:51:01 +01:00
|
|
|
#include "mem_test_util.hh"
|
|
|
|
|
2023-07-25 11:53:09 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
TEST(MonoTime, UnixTimeIncreasesOverTime)
|
|
|
|
{
|
2024-01-14 21:51:01 +01:00
|
|
|
Test_Memory mem;
|
2023-10-10 19:37:39 +02:00
|
|
|
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
|
2023-07-25 11:53:09 +02:00
|
|
|
ASSERT_NE(mono_time, nullptr);
|
|
|
|
|
|
|
|
mono_time_update(mono_time);
|
|
|
|
uint64_t const start = mono_time_get(mono_time);
|
|
|
|
|
|
|
|
while (start == mono_time_get(mono_time)) {
|
|
|
|
mono_time_update(mono_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t const end = mono_time_get(mono_time);
|
|
|
|
EXPECT_GT(end, start);
|
|
|
|
|
2023-10-10 19:37:39 +02:00
|
|
|
mono_time_free(mem, mono_time);
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MonoTime, IsTimeout)
|
|
|
|
{
|
2024-01-14 21:51:01 +01:00
|
|
|
Test_Memory mem;
|
2023-10-10 19:37:39 +02:00
|
|
|
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
|
2023-07-25 11:53:09 +02:00
|
|
|
ASSERT_NE(mono_time, nullptr);
|
|
|
|
|
|
|
|
uint64_t const start = mono_time_get(mono_time);
|
|
|
|
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 1));
|
|
|
|
|
|
|
|
while (start == mono_time_get(mono_time)) {
|
|
|
|
mono_time_update(mono_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(mono_time_is_timeout(mono_time, start, 1));
|
|
|
|
|
2023-10-10 19:37:39 +02:00
|
|
|
mono_time_free(mem, mono_time);
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
2023-12-24 12:21:34 +01:00
|
|
|
TEST(MonoTime, IsTimeoutReal)
|
|
|
|
{
|
2024-01-14 21:51:01 +01:00
|
|
|
Test_Memory mem;
|
2023-12-24 12:21:34 +01:00
|
|
|
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
|
|
|
|
ASSERT_NE(mono_time, nullptr);
|
|
|
|
|
|
|
|
uint64_t const start = mono_time_get(mono_time);
|
|
|
|
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5));
|
|
|
|
|
2024-01-09 16:39:05 +01:00
|
|
|
const uint64_t before_sleep = mono_time_get(mono_time);
|
2023-12-24 12:21:34 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
mono_time_update(mono_time);
|
2024-01-09 16:39:05 +01:00
|
|
|
const uint64_t after_sleep = mono_time_get(mono_time);
|
2023-12-24 12:21:34 +01:00
|
|
|
|
|
|
|
// should still not have timed out (5sec) after sleeping ~100ms
|
2024-01-09 16:39:05 +01:00
|
|
|
EXPECT_FALSE(mono_time_is_timeout(mono_time, start, 5))
|
|
|
|
<< "before sleep: " << before_sleep << ", after sleep: " << after_sleep;
|
2023-12-24 12:21:34 +01:00
|
|
|
|
|
|
|
mono_time_free(mem, mono_time);
|
|
|
|
}
|
|
|
|
|
2023-07-25 11:53:09 +02:00
|
|
|
TEST(MonoTime, CustomTime)
|
|
|
|
{
|
2024-01-14 21:51:01 +01:00
|
|
|
Test_Memory mem;
|
2023-10-10 19:37:39 +02:00
|
|
|
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
|
2023-07-25 11:53:09 +02:00
|
|
|
ASSERT_NE(mono_time, nullptr);
|
|
|
|
|
|
|
|
uint64_t test_time = current_time_monotonic(mono_time) + 42137;
|
|
|
|
|
|
|
|
mono_time_set_current_time_callback(
|
|
|
|
mono_time, [](void *user_data) { return *static_cast<uint64_t *>(user_data); }, &test_time);
|
|
|
|
mono_time_update(mono_time);
|
|
|
|
|
|
|
|
EXPECT_EQ(current_time_monotonic(mono_time), test_time);
|
|
|
|
|
|
|
|
uint64_t const start = mono_time_get(mono_time);
|
|
|
|
|
|
|
|
test_time += 7000;
|
|
|
|
|
|
|
|
mono_time_update(mono_time);
|
|
|
|
EXPECT_EQ(mono_time_get(mono_time) - start, 7);
|
|
|
|
|
|
|
|
EXPECT_EQ(current_time_monotonic(mono_time), test_time);
|
|
|
|
|
2023-10-10 19:37:39 +02:00
|
|
|
mono_time_free(mem, mono_time);
|
2023-07-25 11:53:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|