-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathasync_latch_tests.cpp
More file actions
113 lines (102 loc) · 2.07 KB
/
async_latch_tests.cpp
File metadata and controls
113 lines (102 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
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/async_latch.hpp>
#include <cppcoro/single_consumer_event.hpp>
#include <cppcoro/task.hpp>
#include <cppcoro/when_all_ready.hpp>
#include <cppcoro/sync_wait.hpp>
#include <ostream>
#include "doctest/cppcoro_doctest.h"
TEST_SUITE_BEGIN("async_latch");
using namespace cppcoro;
TEST_CASE("latch constructed with zero count is initially ready")
{
async_latch latch(0);
CHECK(latch.is_ready());
}
TEST_CASE("latch constructed with negative count is initially ready")
{
async_latch latch(-3);
CHECK(latch.is_ready());
}
TEST_CASE("count_down and is_ready")
{
async_latch latch(3);
CHECK(!latch.is_ready());
latch.count_down();
CHECK(!latch.is_ready());
latch.count_down();
CHECK(!latch.is_ready());
latch.count_down();
CHECK(latch.is_ready());
}
TEST_CASE("count_down by n")
{
async_latch latch(5);
latch.count_down(3);
CHECK(!latch.is_ready());
latch.count_down(2);
CHECK(latch.is_ready());
}
TEST_CASE("single awaiter")
{
async_latch latch(2);
bool after = false;
sync_wait(when_all_ready(
[&]() -> task<>
{
co_await latch;
after = true;
}(),
[&]() -> task<>
{
CHECK(!after);
latch.count_down();
CHECK(!after);
latch.count_down();
CHECK(after);
co_return;
}()
));
}
TEST_CASE("multiple awaiters")
{
async_latch latch(2);
bool after1 = false;
bool after2 = false;
bool after3 = false;
sync_wait(when_all_ready(
[&]() -> task<>
{
co_await latch;
after1 = true;
}(),
[&]() -> task<>
{
co_await latch;
after2 = true;
}(),
[&]() -> task<>
{
co_await latch;
after3 = true;
}(),
[&]() -> task<>
{
CHECK(!after1);
CHECK(!after2);
CHECK(!after3);
latch.count_down();
CHECK(!after1);
CHECK(!after2);
CHECK(!after3);
latch.count_down();
CHECK(after1);
CHECK(after2);
CHECK(after3);
co_return;
}()));
}
TEST_SUITE_END();