Repository navigation
Expand file tree
/
Copy pathrun_loop_failure_test.cpp
More file actions
270 lines (229 loc) · 7.67 KB
/
Copy pathrun_loop_failure_test.cpp
File metadata and controls
270 lines (229 loc) · 7.67 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
#include <gtest/gtest.h>
import std;
import mcpplibs.cmp;
namespace {
std::atomic<bool> gFailAllocation { false };
std::atomic<int> gRejectedAllocation {};
} // namespace
void* operator new(std::size_t size) {
if (gFailAllocation.load(std::memory_order_relaxed)) {
gRejectedAllocation.fetch_add(1, std::memory_order_relaxed);
throw std::bad_alloc {};
}
if (auto* memory = std::malloc(size == 0 ? 1 : size)) {
return memory;
}
throw std::bad_alloc {};
}
void operator delete(void* memory) noexcept {
std::free(memory);
}
void operator delete(void* memory, std::size_t) noexcept {
std::free(memory);
}
namespace {
using namespace mcpplibs::cmp;
using Scheduler = RunLoop::Scheduler;
class AllocationFailure final {
public:
AllocationFailure() noexcept {
gFailAllocation.store(true, std::memory_order_relaxed);
}
~AllocationFailure() {
gFailAllocation.store(false, std::memory_order_relaxed);
}
};
Task<void> count_timer(
Scheduler scheduler,
Scheduler::TimePoint deadline,
int& completed) {
co_await scheduler.schedule_at(deadline);
++completed;
}
Task<int> drain_admitted_timers(Scheduler scheduler) {
TaskGroup group {};
int completed {};
const auto deadline = Scheduler::Clock::now() + std::chrono::milliseconds(100);
for (int index {}; index < 4'096; ++index) {
group.spawn(count_timer(scheduler, deadline, completed));
}
// join 帧在故障开启前创建,故障仅作用于已接纳工作的推进
auto joining = group.join();
AllocationFailure failure {};
co_await std::move(joining);
co_return completed;
}
Task<void> fail_timer_admission(Scheduler scheduler, bool& caught) {
AllocationFailure failure {};
try {
co_await scheduler.schedule_after(std::chrono::seconds(1));
} catch (const std::bad_alloc&) {
caught = true;
}
}
Task<void> queued_without_allocation(Scheduler scheduler, int& completed) {
AllocationFailure failure {};
co_await scheduler.schedule();
++completed;
co_await scheduler.schedule_after(Scheduler::Duration::zero());
++completed;
}
Task<void> return_from_worker(
ThreadPool::Scheduler worker,
Scheduler caller,
std::latch& entered,
std::binary_semaphore& release,
int& completed) {
co_await worker.schedule();
entered.count_down();
release.acquire();
co_await caller.schedule();
++completed;
}
Task<int> drain_mixed_children(Scheduler scheduler, ThreadPool::Scheduler worker) {
TaskGroup group {};
int completed {};
std::latch entered { 1 };
std::binary_semaphore release { 0 };
group.spawn(return_from_worker(worker, scheduler, entered, release, completed));
const auto deadline = Scheduler::Clock::now() + std::chrono::milliseconds(100);
for (int index {}; index < 4'096; ++index) {
group.spawn(count_timer(scheduler, deadline, completed));
}
auto joining = group.join();
entered.wait();
AllocationFailure failure {};
release.release();
co_await std::move(joining);
co_return completed;
}
Task<void> cancelled_timer(Scheduler scheduler, std::stop_token token, int& completed) {
try {
co_await scheduler.schedule_at(Scheduler::TimePoint::max(), token);
} catch (const OperationCancelled&) {
++completed;
}
}
Task<int> cancel_admitted_timers(Scheduler scheduler) {
TaskGroup group {};
std::stop_source stop {};
int completed {};
for (int index {}; index < 4'096; ++index) {
group.spawn(cancelled_timer(scheduler, stop.get_token(), completed));
}
auto joining = group.join();
AllocationFailure failure {};
stop.request_stop();
co_await std::move(joining);
co_return completed;
}
Task<int> value();
TEST(CmpRunLoopFailureTest, TimersAndExternalWorkersDrainWithoutAllocating) {
ThreadPool pool { 1 };
RunLoop loop {};
gRejectedAllocation.store(0);
EXPECT_EQ(loop.run(drain_mixed_children(
loop.get_scheduler(), pool.get_scheduler())), 4'097);
EXPECT_EQ(gRejectedAllocation.load(), 0);
}
TEST(CmpRunLoopFailureTest, CancellationAndPromotionDoNotAllocate) {
RunLoop loop {};
gRejectedAllocation.store(0);
EXPECT_EQ(loop.run(cancel_admitted_timers(loop.get_scheduler())), 4'096);
EXPECT_EQ(gRejectedAllocation.load(), 0);
EXPECT_EQ(loop.run(value()), 42);
}
Task<int> value() {
co_return 42;
}
Task<void> fail_admission_after_children(
Scheduler scheduler,
std::stop_source& stop,
std::latch& entered,
std::binary_semaphore& release,
bool& caught) {
entered.wait();
{
AllocationFailure failure {};
try {
co_await scheduler.schedule_at(Scheduler::TimePoint::max());
} catch (const std::bad_alloc&) {
caught = true;
}
}
stop.request_stop();
release.release();
}
Task<int> admission_failure_with_children(
Scheduler scheduler,
ThreadPool::Scheduler worker,
bool tuple,
bool& caught) {
int completed {};
std::stop_source stop {};
std::latch entered { 1 };
std::binary_semaphore release { 0 };
if (tuple) {
co_await when_all(
cancelled_timer(scheduler, stop.get_token(), completed),
cancelled_timer(scheduler, stop.get_token(), completed),
return_from_worker(worker, scheduler, entered, release, completed),
fail_admission_after_children(scheduler, stop, entered, release, caught));
} else {
TaskGroup group {};
auto joining = group.join();
group.spawn(cancelled_timer(scheduler, stop.get_token(), completed));
group.spawn(cancelled_timer(scheduler, stop.get_token(), completed));
group.spawn(return_from_worker(worker, scheduler, entered, release, completed));
group.spawn(fail_admission_after_children(scheduler, stop, entered, release, caught));
co_await std::move(joining);
}
co_return completed;
}
TEST(CmpRunLoopFailureTest, GroupAdmissionFailureDrainsAlreadyAcceptedTimersAndWorker) {
ThreadPool pool { 1 };
RunLoop loop {};
bool caught {};
gRejectedAllocation.store(0);
EXPECT_EQ(loop.run(admission_failure_with_children(
loop.get_scheduler(), pool.get_scheduler(), false, caught)), 3);
EXPECT_TRUE(caught);
EXPECT_EQ(gRejectedAllocation.load(), 1);
EXPECT_EQ(loop.run(value()), 42);
}
TEST(CmpRunLoopFailureTest, WhenAllAdmissionFailureDrainsAlreadyAcceptedTimersAndWorker) {
ThreadPool pool { 1 };
RunLoop loop {};
bool caught {};
gRejectedAllocation.store(0);
EXPECT_EQ(loop.run(admission_failure_with_children(
loop.get_scheduler(), pool.get_scheduler(), true, caught)), 3);
EXPECT_TRUE(caught);
EXPECT_EQ(gRejectedAllocation.load(), 1);
EXPECT_EQ(loop.run(value()), 42);
}
TEST(CmpRunLoopFailureTest, AdmittedTimersDrainWithoutAllocating) {
RunLoop loop {};
gRejectedAllocation.store(0);
EXPECT_EQ(loop.run(drain_admitted_timers(loop.get_scheduler())), 4'096);
EXPECT_EQ(gRejectedAllocation.load(), 0);
EXPECT_EQ(loop.run(value()), 42);
}
TEST(CmpRunLoopFailureTest, TimerAdmissionFailureIsCatchableAndReusable) {
RunLoop loop {};
bool caught {};
gRejectedAllocation.store(0);
loop.run(fail_timer_admission(loop.get_scheduler(), caught));
EXPECT_TRUE(caught);
EXPECT_EQ(gRejectedAllocation.load(), 1);
EXPECT_EQ(loop.run(value()), 42);
}
TEST(CmpRunLoopFailureTest, ReadyAndImmediateTimerQueuesDoNotAllocate) {
RunLoop loop {};
int completed {};
gRejectedAllocation.store(0);
loop.run(queued_without_allocation(loop.get_scheduler(), completed));
EXPECT_EQ(completed, 2);
EXPECT_EQ(gRejectedAllocation.load(), 0);
}
} // namespace