forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvs_concrt_ptp_work.cpp
More file actions
69 lines (58 loc) · 2.03 KB
/
vs_concrt_ptp_work.cpp
File metadata and controls
69 lines (58 loc) · 2.03 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
//
// Author : github.com/luncliff (luncliff@gmail.com)
// License : CC BY 4.0
//
#include <coroutine/concrt.h>
#include <array>
#include <CppUnitTest.h>
using namespace coro;
using namespace concrt;
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
class ptp_work_test : public TestClass<ptp_work_test> {
static auto set_after_sleep(HANDLE ev, uint32_t ms) -> no_return {
co_await ptp_work{}; // move to background thread ...
Sleep(ms);
// if failed, print error message
if (SetEvent(ev) == 0) {
auto ec = GetLastError();
auto m = system_category().message(ec);
Logger::WriteMessage(m.c_str());
}
}
array<HANDLE, 10> events{};
TEST_METHOD_INITIALIZE(create_events) {
for (auto& e : events) {
e = CreateEventEx(nullptr, nullptr, //
CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
Assert::IsTrue(e != NULL);
if (e) // if statement because of C6387
ResetEvent(e);
}
}
TEST_METHOD_CLEANUP(close_events) {
for (auto e : events)
CloseHandle(e);
}
TEST_METHOD(ptp_work_wait_one_event) {
HANDLE& e1 = events[0];
auto ms = rand() & 0b1111; // at most 16 ms
set_after_sleep(e1, ms);
Sleep(3);
// issue: CI environment runs slowly, so too short timeout might fail
// wait for 200 ms
auto ec = WaitForSingleObjectEx(e1, 200, true);
Assert::IsTrue(ec == WAIT_OBJECT_0);
}
TEST_METHOD(ptp_work_wait_multiple_event) {
for (auto e : events) {
auto ms = rand() & 0b1111; // at most 16 ms
set_after_sleep(e, ms);
}
Sleep(3);
// issue: CI environment runs slowly, so too short timeout might fail
// wait for 300 ms
auto ec = WaitForMultipleObjectsEx(events.max_size(), events.data(),
true, 300, true);
Assert::IsTrue(ec == WAIT_OBJECT_0);
}
};