-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathasync_manual_reset_event_tests.cpp
96 lines (75 loc) · 1.87 KB
/
async_manual_reset_event_tests.cpp
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/async_manual_reset_event.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/sync_wait.hpp>
#include <cppcoro/when_all_ready.hpp>
#include "doctest/doctest.h"
TEST_SUITE_BEGIN("async_manual_reset_event");
TEST_CASE("default constructor initially not set")
{
cppcoro::async_manual_reset_event event;
CHECK(!event.is_set());
}
TEST_CASE("construct event initially set")
{
cppcoro::async_manual_reset_event event{ true };
CHECK(event.is_set());
}
TEST_CASE("set and reset")
{
cppcoro::async_manual_reset_event event;
CHECK(!event.is_set());
event.set();
CHECK(event.is_set());
event.set();
CHECK(event.is_set());
event.reset();
CHECK(!event.is_set());
event.reset();
CHECK(!event.is_set());
event.set();
CHECK(event.is_set());
}
TEST_CASE("await not set event")
{
cppcoro::async_manual_reset_event event;
auto createWaiter = [&](bool& flag) -> cppcoro::task<>
{
co_await event;
flag = true;
};
bool completed1 = false;
bool completed2 = false;
auto check = [&]() -> cppcoro::task<>
{
CHECK(!completed1);
CHECK(!completed2);
event.reset();
CHECK(!completed1);
CHECK(!completed2);
event.set();
CHECK(completed1);
CHECK(completed2);
co_return;
};
cppcoro::sync_wait(cppcoro::when_all_ready(
createWaiter(completed1),
createWaiter(completed2),
check()));
}
TEST_CASE("awaiting already set event doesn't suspend")
{
cppcoro::async_manual_reset_event event{ true };
auto createWaiter = [&]() -> cppcoro::task<>
{
co_await event;
};
// Should complete without blocking.
cppcoro::sync_wait(cppcoro::when_all_ready(
createWaiter(),
createWaiter()));
}
TEST_SUITE_END();