-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreadTest.cpp
More file actions
216 lines (174 loc) · 5.25 KB
/
ThreadTest.cpp
File metadata and controls
216 lines (174 loc) · 5.25 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "pch.h"
#include <kf/Thread.h>
namespace
{
struct ThreadContext
{
bool started = false;
};
void threadProc(void* context)
{
static_cast<ThreadContext*>(context)->started = true;
PsTerminateSystemThread(STATUS_SUCCESS);
}
struct TestObject
{
ThreadContext m_threadContext;
NTSTATUS run()
{
m_threadContext.started = true;
return STATUS_SUCCESS;
}
};
}
SCENARIO("kf::Thread basic lifecycle")
{
GIVEN("A default constructed thread")
{
kf::Thread thread;
WHEN("join() is called without start()")
{
thread.join();
THEN("it should be a no-op")
{
REQUIRE(true);
}
}
WHEN("start() is used with threadProc")
{
ThreadContext context;
const NTSTATUS status = thread.start(&threadProc, &context);
THEN("start() should succeed")
{
REQUIRE_NT_SUCCESS(status);
}
THEN("join() waits for completion")
{
thread.join();
REQUIRE(context.started);
}
}
WHEN("start() is used with lambda")
{
ThreadContext context;
NTSTATUS status = thread.start([](void* context)
{
static_cast<ThreadContext*>(context)->started = true;
PsTerminateSystemThread(STATUS_SUCCESS);
},
&context);
THEN("start() should succeed")
{
REQUIRE_NT_SUCCESS(status);
}
thread.join();
THEN("lambda should run")
{
REQUIRE(context.started);
}
}
WHEN("start() is used with object member routine")
{
TestObject obj;
const NTSTATUS status = thread.start<&TestObject::run>(&obj);
THEN("start() should succeed")
{
REQUIRE_NT_SUCCESS(status);
}
thread.join();
THEN("member routine should run")
{
REQUIRE(obj.m_threadContext.started);
}
}
WHEN("calling join twice after start")
{
REQUIRE_NT_SUCCESS(thread.start([](void*) { PsTerminateSystemThread(STATUS_SUCCESS); }, nullptr));
thread.join();
thread.join();
THEN("it's safe to call join() multiple times")
{
REQUIRE(true);
}
}
WHEN("thread object is destroyed while running")
{
ThreadContext context;
{
kf::Thread local;
REQUIRE_NT_SUCCESS(local.start(&threadProc, &context));
}
THEN("destructor should join and thread should have run")
{
REQUIRE(context.started);
}
}
WHEN("starting a thread with a long working function")
{
ThreadContext context;
REQUIRE_NT_SUCCESS(thread.start([](void* context)
{
LARGE_INTEGER oneMillisecond{ .QuadPart = -10'000LL }; // 1ms
KeDelayExecutionThread(KernelMode, false, &oneMillisecond);
static_cast<ThreadContext*>(context)->started = true;
PsTerminateSystemThread(STATUS_SUCCESS);
},
&context));
THEN("join() waits for completion")
{
thread.join();
REQUIRE(context.started);
}
}
}
}
SCENARIO("kf::Thread move constructor and move assignment")
{
GIVEN("A started thread")
{
ThreadContext context;
kf::Thread thread1;
REQUIRE_NT_SUCCESS(thread1.start(&threadProc, &context));
WHEN("Thread is move-constructed")
{
kf::Thread thread2(std::move(thread1));
THEN("moved-to object should be able to join")
{
thread2.join();
REQUIRE(context.started);
}
THEN("moved-from object join() should be safe")
{
thread1.join();
REQUIRE(context.started);
}
}
}
GIVEN("Two threads where left-hand side already owns a running thread")
{
ThreadContext context1;
ThreadContext context2;
kf::Thread thread1;
kf::Thread thread2;
REQUIRE_NT_SUCCESS(thread1.start(&threadProc, &context1));
REQUIRE_NT_SUCCESS(thread2.start(&threadProc, &context2));
WHEN("Move-assigning thread1 = std::move(thread2)")
{
thread1 = std::move(thread2);
THEN("thread1 should join the thread it now owns")
{
thread1.join();
REQUIRE(context2.started);
}
THEN("thread2 should be in a valid empty state")
{
thread2.join();
REQUIRE(true);
}
THEN("the original thread from thread1 should have executed")
{
REQUIRE(context1.started);
}
}
}
}