Repository navigation
Expand file tree
/
Copy pathasync_tests.cpp
More file actions
365 lines (324 loc) · 10.9 KB
/
Copy pathasync_tests.cpp
File metadata and controls
365 lines (324 loc) · 10.9 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <teaql/async/stream.hpp>
#include <teaql/runtime/context.hpp>
#include <atomic>
#include <array>
#include <iostream>
#include <latch>
#include <semaphore>
#include <thread>
using namespace teaql;
static void check(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
struct Counters {
int polls = 0, finished = 0, cancelled = 0, destroyed = 0;
};
struct Pulse {
Counters* counts;
bool fail = false;
std::optional<int> poll(WakeSignal& signal) {
++counts->polls;
if (fail) {
throw std::runtime_error("provider poll failure");
}
if (counts->polls == 1) {
signal.notify();
return {};
}
return 42;
}
void finish_and_wait() noexcept {
++counts->finished;
}
void cancel_and_wait() noexcept {
++counts->cancelled;
}
};
struct Chunks {
Counters* counts;
int index = 0;
bool pending = true;
std::optional<std::optional<int>> poll_next(WakeSignal& wake) {
++counts->polls;
if (pending) {
pending = false;
wake.notify();
return {};
}
if (index == 2) {
return std::optional<int>{};
}
pending = true;
return std::optional<int>{++index};
}
void finish_and_wait() noexcept {
++counts->finished;
}
void cancel_and_wait() noexcept {
++counts->cancelled;
}
};
struct ClosingProbe {
UserContext& context;
Counters& counts;
bool& rejected;
std::optional<int> poll(WakeSignal&) {
return {};
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {
++counts.cancelled;
context.close(); // Reentrant close must not free us.
try {
make_operation<int, Pulse>(context, &counts, false);
} catch (const std::logic_error&) {
rejected = true;
}
auto& diagnostic = context.make<String>("quiescence can retain its final diagnostic");
if (diagnostic.empty()) {
std::terminate();
}
}
};
struct Payload {
using allocator_type = Allocator;
String text;
bool* destroyed;
Payload(std::allocator_arg_t, Allocator a, bool& destroyed)
: text(10000, 'x', a.resource()), destroyed(&destroyed) {}
~Payload() {
*destroyed = true;
}
};
struct Worker {
UserContext& context;
Counters& counts;
bool& payload_destroyed;
Payload* payload = nullptr;
std::thread thread;
std::latch started{1};
std::binary_semaphore release{0};
std::atomic<bool> done = false;
bool read_before_destroy = false, released = false;
Worker(UserContext& context, Counters& counts, bool& destroyed)
: context(context), counts(counts), payload_destroyed(destroyed) {}
~Worker() {
if (thread.joinable()) {
std::terminate();
}
++counts.destroyed;
}
std::optional<int> poll(WakeSignal& wake) {
++counts.polls;
if (!payload) {
// Allocated after AsyncOperation's cleanup registration: ordinary
// LIFO destruction would release this before joining the worker.
payload = &context.make<Payload>(payload_destroyed);
thread = std::thread([this, &wake] {
started.count_down();
release.acquire();
read_before_destroy = !payload_destroyed && payload->text.size() == 10000 &&
payload->text.front() == 'x';
done.store(true, std::memory_order_release);
wake.notify();
});
started.wait();
return {};
}
if (!done.load(std::memory_order_acquire)) {
return {};
}
return 99;
}
void allow_completion() {
if (!released) {
released = true;
release.release();
}
}
void join() noexcept {
if (thread.joinable()) {
allow_completion();
thread.join();
}
}
void finish_and_wait() noexcept {
join();
++counts.finished;
}
void cancel_and_wait() noexcept {
join();
++counts.cancelled;
// Cancellation diagnostics remain allocatable during quiescence.
auto& message =
context.make<String>("cancelled after all native host-memory access stopped");
if (message.empty() || (payload && !read_before_destroy)) {
std::terminate();
}
}
};
static void operations() {
GlobalContext global;
auto user = global.make_user_context();
Counters counts;
auto& task = make_operation<int, Pulse>(*user, &counts, false);
check(counts.polls == 0, "operations are lazy");
check(task.wait() == 42 && task.status() == AsyncStatus::Ready,
"notify during poll cannot lose the wakeup");
check(*task.poll() == 42 && counts.polls == 2 && counts.finished == 1,
"ready operation is memoized and joined once");
task.cancel();
check(counts.cancelled == 0, "cancelling ready work cannot invent cancellation");
Counters abandoned;
auto& unused = make_operation<int, Pulse>(*user, &abandoned, false);
unused.cancel();
unused.cancel();
bool cancelled = false;
try {
unused.poll();
} catch (const OperationCancelled&) {
cancelled = true;
}
check(cancelled && abandoned.polls == 0 && abandoned.cancelled == 1,
"unpolled cancellation is explicit and idempotent");
Counters failures;
auto& failure = make_operation<int, Pulse>(*user, &failures, true);
for (int i = 0; i < 2; ++i) {
bool caught = false;
try {
failure.poll();
} catch (const std::runtime_error&) {
caught = true;
}
check(caught, "poll failure rethrows original exception");
}
check(failures.polls == 1 && failures.cancelled == 1 && failure.status() == AsyncStatus::Failed,
"failed operation quiesces once");
user->close();
check(counts.cancelled == 0 && abandoned.cancelled == 1 && failures.cancelled == 1,
"Context close does not repeat terminal hooks");
}
static void native_work() {
GlobalContext global;
auto user = global.make_user_context(128);
Counters counts;
bool destroyed = false;
auto& worker = user->make_resource<Worker>(*user, counts, destroyed);
auto& task = user->make<AsyncOperation<int>>(worker);
check(!task.poll() && !destroyed, "native work suspends with its host payload live");
user->close();
check(counts.cancelled == 1 && counts.finished == 0 && counts.destroyed == 1 && destroyed,
"Context close joins native work before destroying late allocations");
check(user->allocation_stats().live_blocks == 0,
"all arena blocks returned after native completion");
auto other = global.make_user_context();
Counters success;
bool success_destroyed = false;
auto& ready_worker = other->make_resource<Worker>(*other, success, success_destroyed);
auto& ready_task = other->make<AsyncOperation<int>>(ready_worker);
check(!ready_task.poll(), "native operation initially pending");
ready_worker.allow_completion();
check(ready_task.wait() == 99 && ready_worker.read_before_destroy && success.finished == 1,
"native wake completion joins before exposing the result");
other->close();
check(success.cancelled == 0 && success_destroyed,
"successful work stays successful on Context close");
}
static void streams() {
GlobalContext global;
auto user = global.make_user_context();
Counters counts;
auto& stream = make_stream<int, Chunks>(*user, &counts, 0, true);
auto* first = stream.wait_next();
auto* second = stream.wait_next();
check(first && second && *first == 1 && *second == 2,
"stream emits ordered Context-owned chunks");
check(!stream.wait_next() && !stream.wait_next() && *first == 1 && counts.finished == 1,
"EOF is stable and does not invalidate earlier chunks");
Counters early;
auto& cancelled = make_stream<int, Chunks>(*user, &early, 0, true);
check(cancelled.poll_next().kind == StreamPoll<int>::Kind::Pending,
"stream pending differs from EOF");
cancelled.cancel();
bool caught = false;
try {
cancelled.wait_next();
} catch (const OperationCancelled&) {
caught = true;
}
check(caught && early.cancelled == 1, "explicit stream cancellation");
user->close();
check(counts.cancelled == 0 && early.cancelled == 1, "stream terminal cleanup is idempotent");
}
struct FailingResource : std::pmr::memory_resource {
bool fail = false;
void* do_allocate(std::size_t n, std::size_t a) override {
if (fail) {
throw std::bad_alloc{};
}
return std::pmr::new_delete_resource()->allocate(n, a);
}
void do_deallocate(void* p, std::size_t n, std::size_t a) override {
std::pmr::new_delete_resource()->deallocate(p, n, a);
}
bool do_is_equal(const std::pmr::memory_resource& r) const noexcept override {
return this == &r;
}
};
using Large = std::array<int, 65536>;
struct AllocFailureState {
int& cancelled;
AllocFailureState(FailingResource& r, int& cancelled) : cancelled(cancelled) {
r.fail = true;
}
std::optional<Large> poll(WakeSignal&) {
return {};
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {
++cancelled;
}
};
static void failed_registration() {
FailingResource upstream;
GlobalContext global(&upstream, 128);
auto user = global.make_user_context(128);
int cancelled = 0;
bool caught = false;
try {
make_resource_operation<Large, AllocFailureState>(*user, upstream, cancelled);
} catch (const std::bad_alloc&) {
caught = true;
}
check(caught && cancelled == 1, "failed operation allocation quiesces already-created state");
upstream.fail = false;
user->close();
}
static void closing_registration() {
GlobalContext global;
auto user = global.make_user_context();
Counters counts;
bool rejected = false;
make_operation<int, ClosingProbe>(*user, *user, counts, rejected);
user->close();
check(rejected && counts.cancelled == 1 && counts.polls == 0 &&
user->allocation_stats().live_blocks == 0,
"quiescence rejects new work and safely tolerates reentrant close");
}
int main() {
try {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
operations();
native_work();
streams();
failed_registration();
closing_registration();
std::pmr::set_default_resource(previous);
std::cout << "Async Context contracts passed\n";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}