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