forked from cppalliance/capy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_context.cpp
More file actions
500 lines (397 loc) · 12.1 KB
/
execution_context.cpp
File metadata and controls
500 lines (397 loc) · 12.1 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/capy
//
// Test that header file is self-contained.
#include <boost/capy/ex/execution_context.hpp>
#include <boost/capy/ex/recycling_memory_resource.hpp>
#include "test_helpers.hpp"
#include <atomic>
#include <thread>
#include <vector>
namespace boost {
namespace capy {
namespace {
// Simple service for basic tests
struct simple_service : execution_context::service
{
int value = 0;
explicit simple_service(execution_context&)
{
}
simple_service(execution_context&, int v)
: value(v)
{
}
void shutdown() override {}
};
// Service that tracks shutdown
struct tracking_service : execution_context::service
{
bool& shutdown_called;
explicit tracking_service(execution_context&, bool& flag)
: shutdown_called(flag)
{
}
void shutdown() override
{
shutdown_called = true;
}
};
// Base service type for key_type tests
struct base_service : execution_context::service
{
virtual int get_value() const = 0;
};
// Derived service with key_type
struct derived_service : base_service
{
using key_type = base_service;
int value;
explicit derived_service(execution_context&)
: value(42)
{
}
derived_service(execution_context&, int v)
: value(v)
{
}
int get_value() const override { return value; }
void shutdown() override {}
};
// Another derived service with same key_type
struct other_derived_service : base_service
{
using key_type = base_service;
explicit other_derived_service(execution_context&)
{
}
int get_value() const override { return 99; }
void shutdown() override {}
};
// Service with multiple constructor args
struct multi_arg_service : execution_context::service
{
int a;
std::string b;
double c;
multi_arg_service(execution_context&, int a_, std::string b_, double c_)
: a(a_), b(std::move(b_)), c(c_)
{
}
void shutdown() override {}
};
// Service that creates another service in constructor
struct nested_service : execution_context::service
{
explicit nested_service(execution_context& ctx)
{
// This should work - nested service creation
ctx.use_service<simple_service>();
}
void shutdown() override {}
};
} // namespace
struct execution_context_test
{
void
testConstruct()
{
// Basic construction and destruction
{
test_io_context ctx;
}
}
void
testHasService()
{
test_io_context ctx;
// Initially no services
BOOST_TEST(!ctx.has_service<simple_service>());
// After adding, has_service returns true
ctx.use_service<simple_service>();
BOOST_TEST(ctx.has_service<simple_service>());
}
void
testFindService()
{
test_io_context ctx;
// Initially returns nullptr
BOOST_TEST_EQ(ctx.find_service<simple_service>(), nullptr);
// After adding, returns pointer
ctx.use_service<simple_service>();
BOOST_TEST_NE(ctx.find_service<simple_service>(), nullptr);
}
void
testUseService()
{
test_io_context ctx;
// Creates service if not present
auto& svc1 = ctx.use_service<simple_service>();
BOOST_TEST(ctx.has_service<simple_service>());
// Returns same instance on subsequent calls
auto& svc2 = ctx.use_service<simple_service>();
BOOST_TEST_EQ(&svc1, &svc2);
}
void
testMakeService()
{
test_io_context ctx;
// Creates service with value
auto& svc = ctx.make_service<simple_service>(42);
BOOST_TEST_EQ(svc.value, 42);
// Throws if service already exists
BOOST_TEST_THROWS(
ctx.make_service<simple_service>(100),
std::invalid_argument);
// Original value unchanged
BOOST_TEST_EQ(ctx.find_service<simple_service>()->value, 42);
}
void
testMakeServiceMultipleArgs()
{
test_io_context ctx;
auto& svc = ctx.make_service<multi_arg_service>(
123, std::string("hello"), 3.14);
BOOST_TEST_EQ(svc.a, 123);
BOOST_TEST_EQ(svc.b, "hello");
BOOST_TEST_EQ(svc.c, 3.14);
}
void
testKeyType()
{
test_io_context ctx;
// Create derived service
auto& svc = ctx.make_service<derived_service>(100);
BOOST_TEST_EQ(svc.value, 100);
// Can find via derived type
BOOST_TEST(ctx.has_service<derived_service>());
BOOST_TEST_NE(ctx.find_service<derived_service>(), nullptr);
// Can find via base type (key_type)
BOOST_TEST(ctx.has_service<base_service>());
auto* base = ctx.find_service<base_service>();
BOOST_TEST_NE(base, nullptr);
BOOST_TEST_EQ(base->get_value(), 100);
// Cannot add another service with same key_type
BOOST_TEST_THROWS(
ctx.make_service<other_derived_service>(),
std::invalid_argument);
}
void
testKeyTypeUseService()
{
test_io_context ctx;
// use_service creates via derived type
auto& svc = ctx.use_service<derived_service>();
BOOST_TEST_EQ(svc.get_value(), 42);
// Can lookup via base type
BOOST_TEST(ctx.has_service<base_service>());
}
void
testShutdown()
{
bool shutdown_called = false;
{
test_io_context ctx;
ctx.make_service<tracking_service>(shutdown_called);
BOOST_TEST(!shutdown_called);
}
// Shutdown called when context destroyed
BOOST_TEST(shutdown_called);
}
void
testMultipleServices()
{
test_io_context ctx;
ctx.make_service<simple_service>(1);
ctx.make_service<multi_arg_service>(2, "test", 2.0);
BOOST_TEST(ctx.has_service<simple_service>());
BOOST_TEST(ctx.has_service<multi_arg_service>());
BOOST_TEST_EQ(ctx.find_service<simple_service>()->value, 1);
BOOST_TEST_EQ(ctx.find_service<multi_arg_service>()->a, 2);
}
void
testNestedServiceCreation()
{
test_io_context ctx;
// nested_service creates simple_service in its constructor
ctx.use_service<nested_service>();
// Both services should exist
BOOST_TEST(ctx.has_service<nested_service>());
BOOST_TEST(ctx.has_service<simple_service>());
}
void
testConcurrentAccess()
{
test_io_context ctx;
std::atomic<int> success_count{0};
constexpr int num_threads = 8;
std::vector<std::thread> threads;
threads.reserve(num_threads);
for(int i = 0; i < num_threads; ++i)
{
threads.emplace_back([&ctx, &success_count]{
// All threads try to use_service simultaneously
auto& svc = ctx.use_service<simple_service>();
(void)svc;
++success_count;
});
}
for(auto& t : threads)
t.join();
// All threads should succeed
BOOST_TEST_EQ(success_count.load(), num_threads);
// Only one service instance should exist
BOOST_TEST(ctx.has_service<simple_service>());
}
void
testTarget()
{
test_io_context ctx;
// Matching type returns non-null
auto* p = ctx.target<test_io_context>();
BOOST_TEST_NE(p, nullptr);
BOOST_TEST_EQ(p, &ctx);
// Const overload
execution_context const& cctx = ctx;
auto* cp = cctx.target<test_io_context>();
BOOST_TEST_NE(cp, nullptr);
BOOST_TEST_EQ(cp, &ctx);
// Wrong type returns nullptr
struct other_context : execution_context {};
BOOST_TEST_EQ(
ctx.target<other_context>(),
nullptr);
}
void
testGetFrameAllocator()
{
test_io_context ctx;
// Default returns non-null (recycling allocator)
auto* mr = ctx.get_frame_allocator();
BOOST_TEST_NE(mr, nullptr);
// Should be the recycling allocator
BOOST_TEST_EQ(mr, get_recycling_memory_resource());
}
void
testSetFrameAllocatorRawPointer()
{
test_io_context ctx;
// Create a custom memory resource
std::pmr::monotonic_buffer_resource custom;
// Set it
ctx.set_frame_allocator(&custom);
// Get returns our custom resource
BOOST_TEST_EQ(ctx.get_frame_allocator(), &custom);
}
void
testSetFrameAllocatorTemplate()
{
test_io_context ctx;
// Get default allocator for comparison
auto* default_mr = ctx.get_frame_allocator();
// Set using std::allocator
ctx.set_frame_allocator(std::allocator<int>{});
// Get returns non-null
auto* new_mr = ctx.get_frame_allocator();
BOOST_TEST_NE(new_mr, nullptr);
// Should be different from default
BOOST_TEST_NE(new_mr, default_mr);
}
void
testSlotLookupConsistency()
{
// Verify that find_service returns the same pointer
// whether from the slot fast path or linked list fallback.
test_io_context ctx;
auto& svc = ctx.make_service<simple_service>(77);
auto* p1 = ctx.find_service<simple_service>();
auto* p2 = ctx.find_service<simple_service>();
BOOST_TEST_NE(p1, nullptr);
BOOST_TEST_EQ(p1, p2);
BOOST_TEST_EQ(p1, &svc);
}
void
testSlotKeyTypeLookup()
{
// Verify slot lookup works for both concrete and key_type.
test_io_context ctx;
ctx.make_service<derived_service>(55);
auto* p1 = ctx.find_service<derived_service>();
BOOST_TEST_NE(p1, nullptr);
BOOST_TEST_EQ(p1->value, 55);
auto* p2 = ctx.find_service<base_service>();
BOOST_TEST_NE(p2, nullptr);
BOOST_TEST_EQ(p2->get_value(), 55);
// Both should point to the same object
BOOST_TEST_EQ(
static_cast<base_service*>(p1), p2);
}
void
testUseServiceSlotFastPath()
{
// Verify use_service fast path returns same instance.
test_io_context ctx;
auto& svc1 = ctx.use_service<simple_service>();
auto& svc2 = ctx.use_service<simple_service>();
BOOST_TEST_EQ(&svc1, &svc2);
}
void
testConcurrentUseServiceSlots()
{
// Stress test: many threads calling use_service simultaneously.
// All must get the same service instance.
test_io_context ctx;
constexpr int num_threads = 16;
std::atomic<simple_service*> results[num_threads] = {};
std::vector<std::thread> threads;
threads.reserve(num_threads);
for(int i = 0; i < num_threads; ++i)
{
threads.emplace_back([&ctx, &results, i]{
auto& svc = ctx.use_service<simple_service>();
results[i].store(&svc,
std::memory_order_relaxed);
});
}
for(auto& t : threads)
t.join();
auto* expected = results[0].load();
BOOST_TEST_NE(expected, nullptr);
for(int i = 1; i < num_threads; ++i)
BOOST_TEST_EQ(results[i].load(), expected);
}
void
run()
{
testConstruct();
testHasService();
testFindService();
testUseService();
testMakeService();
testMakeServiceMultipleArgs();
testKeyType();
testKeyTypeUseService();
testShutdown();
testMultipleServices();
testNestedServiceCreation();
testConcurrentAccess();
testTarget();
testGetFrameAllocator();
testSetFrameAllocatorRawPointer();
testSetFrameAllocatorTemplate();
testSlotLookupConsistency();
testSlotKeyTypeLookup();
testUseServiceSlotFastPath();
testConcurrentUseServiceSlots();
}
};
TEST_SUITE(
execution_context_test,
"boost.capy.execution_context");
} // capy
} // boost