Skip to content

Commit 02873cf

Browse files
author
Vlada Kanivets
committed
add tests for TimerThread
1 parent 83448bc commit 02873cf

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ wdk_add_driver(kf-test WINVER NTDDI_WIN10 STL
5959
AutoSpinLockTest.cpp
6060
EResourceSharedLockTest.cpp
6161
RecursiveAutoSpinLockTest.cpp
62+
TimerThreadTest.cpp
6263
)
6364

6465
target_link_libraries(kf-test kf::kf kmtest::kmtest)

test/TimerThreadTest.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "pch.h"
2+
#include <kf/TimerThread.h>
3+
4+
namespace
5+
{
6+
void delay(LONGLONG time)
7+
{
8+
LARGE_INTEGER interval;
9+
interval.QuadPart = -time;
10+
KeDelayExecutionThread(KernelMode, FALSE, &interval);
11+
}
12+
}
13+
14+
SCENARIO("TimerThread callback execution")
15+
{
16+
constexpr auto k100Microsecond = 1'000; // 100 microseconds
17+
constexpr auto kMillisecond = 10'000;
18+
19+
GIVEN("TimerThread and a callback incrementing a counter")
20+
{
21+
kf::TimerThread timer;
22+
LONG counter = 0;
23+
24+
LARGE_INTEGER period;
25+
period.QuadPart = k100Microsecond;
26+
27+
REQUIRE_NT_SUCCESS(timer.start(period, [&counter]() {
28+
InterlockedIncrement(&counter);
29+
}));
30+
31+
WHEN("Waiting enough time for multiple callbacks")
32+
{
33+
delay(5 * kMillisecond); // 5ms
34+
35+
THEN("Counter should have been incremented at least greater than 1")
36+
{
37+
REQUIRE(counter > 1);
38+
}
39+
}
40+
41+
WHEN("Stopping the TimerThread via join")
42+
{
43+
delay(5 * kMillisecond); // 5 ms
44+
timer.join();
45+
int valueAfterJoin = counter;
46+
delay(10 * kMillisecond); // 10 ms
47+
48+
THEN("Counter should not increase after join")
49+
{
50+
REQUIRE(counter == valueAfterJoin);
51+
}
52+
}
53+
}
54+
55+
GIVEN("TimerThread in a scope and a callback incrementing a counter")
56+
{
57+
LONG counter = 0;
58+
59+
{
60+
kf::TimerThread timer;
61+
LARGE_INTEGER period;
62+
period.QuadPart = k100Microsecond;
63+
64+
REQUIRE_NT_SUCCESS(timer.start(period, [&counter]() {
65+
InterlockedIncrement(&counter);
66+
}));
67+
68+
delay(5 * kMillisecond);
69+
REQUIRE(counter > 1);
70+
}
71+
72+
int valueAfterScope = counter;
73+
74+
WHEN("TimerThread goes out of scope")
75+
{
76+
delay(10 * kMillisecond); // 10 ms
77+
78+
THEN("Counter should not increase any more")
79+
{
80+
REQUIRE(counter == valueAfterScope);
81+
}
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)