-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConditionVariableTest.cpp
More file actions
292 lines (255 loc) · 8.1 KB
/
ConditionVariableTest.cpp
File metadata and controls
292 lines (255 loc) · 8.1 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "pch.h"
#include <kf/ConditionVariable.h>
#include <kf/Thread.h>
#include <kf/EResource.h>
namespace
{
struct Context
{
kf::EResource* resource = nullptr;
kf::ConditionVariable* cv = nullptr;
LONG counter = 0;
};
void delay()
{
LARGE_INTEGER interval;
interval.QuadPart = -10'000; // 1ms
KeDelayExecutionThread(KernelMode, FALSE, &interval);
}
}
SCENARIO("kf::ConditionVariable")
{
GIVEN("ConditionVariable without timeout and 1 thread")
{
WHEN("waitFor: timeout is nullptr and cv is notified")
{
kf::EResource resource;
kf::ConditionVariable cv;
kf::Thread thread;
thread.start([](void* context) {
auto cv = static_cast<kf::ConditionVariable*>(context);
delay();
cv->notifyOne();
}, &cv);
auto status = cv.waitFor(resource, nullptr);
THEN("waitFor should return success")
{
REQUIRE(status == kf::ConditionVariable::Status::Success);
}
}
WHEN("waitFor: timeout is set to 0")
{
kf::EResource resource;
kf::ConditionVariable cv;
LARGE_INTEGER timeout{ 0 };
auto status = cv.waitFor(resource, &timeout);
THEN("waitFor should return Status::Timeout")
{
REQUIRE(status == kf::ConditionVariable::Status::Timeout);
}
}
WHEN("waitFor: timeout is set to 1ms")
{
kf::EResource resource;
kf::ConditionVariable cv;
LARGE_INTEGER timeout;
timeout.QuadPart = -10'000; // 1ms
auto status = cv.waitFor(resource, &timeout);
THEN("waitFor should return Status::Timeout")
{
REQUIRE(status == kf::ConditionVariable::Status::Timeout);
}
}
}
GIVEN("ConditionVariable wait with predicate immediately true")
{
kf::EResource resource;
kf::ConditionVariable cv;
LONG counter = 1;
cv.wait(resource, [&]() { return counter > 0; });
THEN("Wait should return immediately")
{
REQUIRE(counter == 1);
}
}
GIVEN("ConditionVariable without timeout and 2 Threads waiting for condition variable")
{
kf::EResource resource;
kf::ConditionVariable cv;
Context ctx{ &resource, &cv };
kf::Thread thread1;
kf::Thread thread2;
thread1.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() {
return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
thread2.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() {
return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
delay();
WHEN("Predicate is true and called notifyAll")
{
ctx.counter++;
cv.notifyAll();
thread1.join();
thread2.join();
THEN("Both threads should wake up and increment counter")
{
REQUIRE(ctx.counter == 3);
}
}
}
GIVEN("ConditionVariable without timeout and 1 thread waiting for predicate")
{
kf::EResource resource;
kf::ConditionVariable cv;
Context ctx{ &resource, &cv };
kf::Thread thread;
WHEN("Thread is notified")
{
thread.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
delay();
THEN("Thread should be waiting for predicate")
{
REQUIRE(ctx.counter == 0);
}
ctx.counter++;
cv.notifyOne();
thread.join();
THEN("Predicate become true and thread should wake up")
{
REQUIRE(ctx.counter == 2);
}
}
}
GIVEN("ConditionVariable with 2 waiting threads and notifyOne is called")
{
kf::EResource resource;
kf::ConditionVariable cv;
Context ctx{ &resource, &cv };
kf::Thread thread1;
kf::Thread thread2;
thread1.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
thread2.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
delay();
ctx.counter++;
cv.notifyOne();
delay();
THEN("Only one thread should wake up and increment counter")
{
REQUIRE(ctx.counter == 2);
}
cv.notifyOne();
thread1.join();
thread2.join();
THEN("After second notifyOne both threads should finish")
{
REQUIRE(ctx.counter == 3);
}
}
GIVEN("ConditionVariable::waitFor with predicate and timeout")
{
WHEN("Timeout is nullptr and predicate is true")
{
kf::EResource resource;
kf::ConditionVariable cv;
kf::Thread thread;
int counter = 1;
thread.start([](void* context) {
auto cv = static_cast<kf::ConditionVariable*>(context);
delay();
cv->notifyOne();
}, &cv);
THEN("waitFor should return true")
{
REQUIRE(cv.waitFor(resource, nullptr, [&]() { return counter > 0; }));
}
}
WHEN("Timeout is set to 0 and predicate is false")
{
kf::EResource resource;
kf::ConditionVariable cv;
LARGE_INTEGER timeout{ 0 };
int counter = 0;
THEN("waitFor should return false")
{
REQUIRE(!cv.waitFor(resource, &timeout, [&]() { return counter > 0; }));
}
}
WHEN("Timeout is set to 1ms and predicate is false")
{
kf::EResource resource;
kf::ConditionVariable cv;
LARGE_INTEGER timeout;
timeout.QuadPart = -10'000; // 1ms
int counter = 0;
THEN("waitFor should return false")
{
REQUIRE(!cv.waitFor(resource, &timeout, [&]() { return counter > 0; }));
}
}
}
GIVEN("ConditionVariable with no waiters")
{
kf::EResource resource;
kf::ConditionVariable cv;
WHEN("notifyAll is called without waiters")
{
cv.notifyAll();
THEN("Nothing should happen")
{
}
}
WHEN("notifyOne is called without waiters")
{
cv.notifyOne();
THEN("Nothing should happen")
{
}
}
}
GIVEN("ConditionVariable with 1 thread and multiple notifyOne calls")
{
kf::EResource resource;
kf::ConditionVariable cv;
Context ctx{ &resource, &cv };
kf::Thread thread;
thread.start([](void* context) {
auto ctx = static_cast<Context*>(context);
ctx->cv->wait(*ctx->resource, [&]() { return ctx->counter > 0; });
ctx->counter++;
ctx->resource->release();
}, &ctx);
delay();
ctx.counter++;
cv.notifyOne();
cv.notifyOne();
thread.join();
THEN("Thread should wake only once and increment counter")
{
REQUIRE(ctx.counter == 2);
}
}
}