Repository navigation
Expand file tree
/
Copy pathschema_runtime_tests.cpp
More file actions
508 lines (461 loc) · 20.1 KB
/
Copy pathschema_runtime_tests.cpp
File metadata and controls
508 lines (461 loc) · 20.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
501
502
503
504
505
506
507
508
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/module.hpp>
#include <iostream>
#include <type_traits>
using namespace teaql;
static void check(bool b, const char* text) {
if (!b) {
throw std::runtime_error(text);
}
}
template <class F>
static void rejected(F f, const char* text) {
bool caught = false;
try {
f();
} catch (const std::logic_error&) {
caught = true;
}
check(caught, text);
}
static_assert(!std::is_default_constructible_v<SchemaInvocation>);
static_assert(!std::is_copy_constructible_v<SchemaInvocation>);
static_assert(!std::is_same_v<runtime::SchemaProvider, SchemaProvider>);
struct Control {
using allocator_type = Allocator;
Vector<String> calls;
bool nested = false, ready = true, fail_provider = false, fail_business = false,
fail_bootstrap = false, throw_bootstrap = false;
UserContext* sibling = nullptr;
const UserContext* isolated = nullptr;
AsyncOperation<RuntimeSchemaResult>* child = nullptr;
Control(std::allocator_arg_t, Allocator a) : calls(a.resource()) {}
void note(std::string_view name, const UserContext& context) {
String entry(name, calls.get_allocator().resource());
entry += detail::is_generated_schema_bootstrap(context) ? ":bootstrap" : ":normal";
calls.push_back(std::move(entry));
}
};
struct Step {
using allocator_type = Allocator;
Memory r;
const UserContext& context;
Control& control;
bool bootstrap;
Step(std::allocator_arg_t,
Allocator a,
const UserContext& context,
Control& control,
bool bootstrap)
: r(a.resource()), context(context), control(control), bootstrap(bootstrap) {}
std::optional<RuntimeSchemaResult> poll(WakeSignal&) {
control.note(bootstrap ? "callback.poll" : "provider.poll", context);
if (!control.ready) {
return {};
}
if (bootstrap && control.throw_bootstrap) {
throw std::runtime_error("fixture panic");
}
if (bootstrap ? control.fail_bootstrap : control.fail_provider) {
return RuntimeSchemaResult::err(
r, RuntimeError::schema(r, bootstrap ? "bootstrap failed" : "provider failed"));
}
return RuntimeSchemaResult::ok(r, {});
}
void finish_and_wait() noexcept {
control.note(bootstrap ? "callback.finish" : "provider.finish", context);
}
void cancel_and_wait() noexcept {
control.note(bootstrap ? "callback.cancel" : "provider.cancel", context);
}
};
struct Provider : runtime::SchemaProvider {
Control& control;
explicit Provider(Control& control) : control(control) {}
AsyncOperation<RuntimeSchemaResult>& ensure_schema(const UserContext& context,
const SchemaInvocation&) const override {
control.note("provider.create", context);
auto r = context.memory_resource();
auto& operation = make_operation<RuntimeSchemaResult, Step>(r, context, control, false);
control.child = &operation;
return operation;
}
};
struct Business : BusinessIdAllocator, BusinessIdSchemaContributor {
Control& control;
explicit Business(Control& control) : control(control) {}
Result<BusinessIdAllocation, BusinessIdError> allocate(Memory r,
const BusinessIdPlan&) const override {
return Result<BusinessIdAllocation, BusinessIdError>::err(
r,
BusinessIdError(
std::allocator_arg, Allocator(r), BusinessIdErrorCode::Allocation, "unused"));
}
Result<std::monostate, BusinessIdError>
ensure_schema(Memory r, const UserContext& context) const override {
control.note("business", context);
if (control.fail_business) {
return Result<std::monostate, BusinessIdError>::err(
r,
BusinessIdError(std::allocator_arg,
Allocator(r),
BusinessIdErrorCode::Allocation,
"business failed"));
}
return Result<std::monostate, BusinessIdError>::ok(r, {});
}
};
static GeneratedSchemaBootstrapFuture& bootstrap(const UserContext& context) {
auto& control =
context.require_resource<Control>().value().get(); // tracked state is a test resource
auto& mutable_control = const_cast<Control&>(control);
mutable_control.note("callback.create", context);
if (control.sibling) {
check(detail::is_generated_schema_bootstrap(*control.sibling),
"same Global context observes inherited callback scope");
}
if (control.isolated) {
check(!detail::is_generated_schema_bootstrap(*control.isolated),
"different Global does not inherit bootstrap mode");
}
auto r = context.memory_resource();
auto& operation = make_operation<RuntimeSchemaResult, Step>(r, context, mutable_control, true);
mutable_control.child = &operation;
return operation;
}
static GeneratedSchemaBootstrapFuture& second(const UserContext& context) {
auto& control = const_cast<Control&>(context.require_resource<Control>().value().get());
control.note("second", context);
return bootstrap(context);
}
struct Fixture {
GlobalContext global;
std::unique_ptr<UserContext> user = global.make_user_context();
Control& control = global.make<Control>();
Provider& provider = global.make_resource<Provider>(control);
Business& business = global.make_resource<Business>(control);
RuntimeModule& module = global.make<RuntimeModule>();
Fixture() {
user->insert_resource(global, control);
user->set_schema_provider(global, provider);
user->set_business_id_infrastructure(global, business);
module.generated_schema_bootstrap(bootstrap).generated_schema_bootstrap(second);
module.apply_to(*user);
}
};
static void pipeline() {
Fixture f;
auto& operation = f.user->ensure_schema();
check(f.control.calls.empty(), "schema initialization is lazy");
rejected(
[&] {
f.module.apply_to(*f.user);
},
"pending immutable schema borrow rejects module replacement");
rejected(
[&] {
f.user->set_schema_provider(f.global, f.provider);
},
"pending borrow rejects provider replacement");
check(bool(operation.wait()), "pipeline succeeds");
Vector<String> expected(f.global.memory_resource());
for (auto text : {"provider.create:normal",
"provider.poll:normal",
"provider.finish:normal",
"business:normal",
"callback.create:bootstrap",
"callback.poll:bootstrap",
"callback.finish:bootstrap",
"second:bootstrap",
"callback.create:bootstrap",
"callback.poll:bootstrap",
"callback.finish:bootstrap"}) {
expected.emplace_back(text);
}
check(f.control.calls == expected,
"provider then business contributor then sequential bootstraps");
check(!detail::is_generated_schema_bootstrap(*f.user), "ready resets dynamic mode");
f.module.apply_to(*f.user);
for (int failure = 0; failure < 3; ++failure) {
f.control.calls.clear();
f.control.fail_provider = failure == 0;
f.control.fail_business = failure == 1;
f.control.fail_bootstrap = failure == 2;
auto& result = f.user->ensure_schema().wait();
check(!result, "errors short circuit");
if (failure == 0) {
check(f.control.calls.size() == 3, "provider failure skips contributor and callbacks");
}
if (failure == 1) {
check(f.control.calls.size() == 4 &&
result.error().kind() == RuntimeError::Kind::Schema &&
result.error().message() == "Allocation: business failed",
"business display maps to Schema error before callbacks");
}
if (failure == 2) {
check(f.control.calls.size() == 7, "first callback failure skips second");
}
check(!detail::is_generated_schema_bootstrap(*f.user), "failure resets mode");
}
}
static GeneratedSchemaBootstrapFuture& root_bootstrap(const UserContext& context) {
check(detail::is_generated_schema_bootstrap(context),
"active root initialization runs inside the bootstrap scope");
check(bool(context.set_generated_bootstrap_active_root("Tenant", 42)),
"bootstrap assigns the active root while schema initialization borrows Context");
check(bool(context.set_generated_bootstrap_active_root("Tenant", 42)),
"same bootstrap root assignment is idempotent");
auto conflict = context.set_generated_bootstrap_active_root("Tenant", 43);
check(!conflict && conflict.error().kind() == RuntimeError::Kind::Schema &&
conflict.error().message() == "active root already set to tenant:42",
"different bootstrap root retains the canonical source conflict");
auto invalid = context.set_generated_bootstrap_active_root("", 0);
check(!invalid && invalid.error().kind() == RuntimeError::Kind::Schema &&
invalid.error().message() == "invalid generated active root",
"invalid bootstrap root retains source validation precedence");
check(context.require_active_root("Tenant").value().get().id == 42,
"bootstrap can read the installed typed root immediately");
return bootstrap(context);
}
static void bootstrap_root_installation() {
Fixture f;
auto& module = f.global.make<RuntimeModule>();
module.generated_schema_bootstrap(root_bootstrap);
module.apply_to(*f.user);
check(!f.user->require_active_root("Tenant"), "root is absent before the first schema poll");
auto& operation = f.user->ensure_schema();
rejected(
[&] {
f.user->with_active_root("Tenant", 42);
},
"ordinary root assignment cannot mutate a pending schema Context");
check(bool(operation.wait()), "schema callback installs the generated active root");
check(f.user->require_active_root("Tenant").value().get().id == 42,
"installed root survives schema completion");
check(bool(f.user->ensure_schema().wait()), "repeated bootstrap retains the same root");
check(!detail::is_generated_schema_bootstrap(*f.user), "root installation restores scope");
f.user->close();
rejected(
[&] {
f.user->set_generated_bootstrap_active_root("Tenant", 42);
},
"generated root initialization still rejects a closed Context");
}
static void cancellation_and_interleaving() {
Fixture f;
auto sibling = f.global.make_user_context();
GlobalContext isolated;
auto other = isolated.make_user_context();
f.control.sibling = sibling.get();
f.control.isolated = other.get();
f.control.ready = false;
auto& first = f.user->ensure_schema();
check(!first.poll(), "provider can suspend");
auto ticket = first.wake_signal().ticket();
f.control.child->wake_signal().notify();
check(first.wake_signal().ticket() != ticket, "provider wake forwards to outer operation");
first.cancel();
check(f.control.calls.back() == "provider.cancel:normal",
"provider cancellation is not bootstrap");
f.control.calls.clear();
f.control.ready = true;
auto& second_op =
f.user->ensure_schema(); // provider will be ready; suspend via an alternate callback
f.control.ready = false;
check(!second_op.poll(), "second provider suspends");
f.control.ready = true;
check(second_op.poll() != nullptr, "resume finishes all callbacks");
f.control.calls.clear();
f.control.throw_bootstrap = true;
auto& panicking = f.user->ensure_schema();
bool caught = false;
try {
panicking.poll();
} catch (const std::runtime_error&) {
caught = true;
}
check(caught && !detail::is_generated_schema_bootstrap(*f.user),
"callback exception restores scope");
check(f.control.calls.back() == "callback.cancel:bootstrap",
"panicking callback cancellation retains mode");
f.control.throw_bootstrap = false;
f.module.apply_to(*f.user);
}
static GeneratedSchemaBootstrapFuture& pending_bootstrap(const UserContext& context) {
auto& control = const_cast<Control&>(context.require_resource<Control>().value().get());
control.ready = false;
return bootstrap(context);
}
static void closing() {
Fixture f;
auto& module = f.global.make<RuntimeModule>();
module.generated_schema_bootstrap(pending_bootstrap);
module.apply_to(*f.user);
auto& operation = f.user->ensure_schema();
check(!operation.poll(), "bootstrap suspends");
check(!detail::is_generated_schema_bootstrap(*f.user),
"pending bootstrap does not elevate ordinary operations");
auto ticket = operation.wake_signal().ticket();
f.control.child->wake_signal().notify();
check(operation.wake_signal().ticket() != ticket, "bootstrap wake forwards");
auto sibling = f.global.make_user_context();
check(!detail::is_generated_schema_bootstrap(*sibling), "interleaved sibling stays normal");
f.user->close();
check(f.control.calls.back() == "callback.cancel:bootstrap",
"Context close cancels descendant in bootstrap scope before arena destruction");
check(!detail::is_generated_schema_bootstrap(*sibling), "close resets Global mode");
rejected(
[&] {
f.user->ensure_schema();
},
"closed Context rejects initialization");
}
static GeneratedSchemaBootstrapFuture& nested_bootstrap(const UserContext& context) {
auto& control = const_cast<Control&>(context.require_resource<Control>().value().get());
if (!control.nested) {
control.nested = true;
return context.ensure_schema();
}
control.ready = false;
return bootstrap(context);
}
static void nested_close() {
Fixture f;
auto& module = f.global.make<RuntimeModule>();
module.generated_schema_bootstrap(nested_bootstrap);
module.apply_to(*f.user);
check(!f.user->ensure_schema().poll(), "nested schema can suspend");
check(f.control.calls.at(4) == "provider.create:bootstrap",
"nested provider inherits outer bootstrap scope");
auto sibling = f.global.make_user_context();
check(!detail::is_generated_schema_bootstrap(*sibling), "nested pending restores outer scope");
f.user->close();
check(f.control.calls.back() == "callback.cancel:bootstrap" &&
!detail::is_generated_schema_bootstrap(*sibling),
"nested cancellation restores dynamic scope in stack order");
}
static void module_and_lifetimes() {
GlobalContext global;
auto source = global.make_user_context();
auto& module = source->make<RuntimeModule>();
EntityDescriptor descriptor(std::allocator_arg, Allocator(source->memory_resource()), "Order");
descriptor.property("name", DataType::Text);
module.descriptor(std::move(descriptor));
auto target = module.into_context(global);
source.reset();
check(target->entity("Order") && target->has_entity_data_service("Order"),
"module materializes independent Context metadata and registry");
auto& result = target->ensure_schema().wait();
check(!result && result.error().message() == "missing schema provider",
"missing provider returns exact lazy Schema error");
auto& empty = global.make<RuntimeModule>();
empty.apply_to(*target);
check(!target->entity("Order") && !target->has_entity_data_service("Order"),
"applying module replaces registration");
auto sibling = global.make_user_context();
auto& state = target->make<Control>();
auto& provider = target->make_resource<Provider>(state);
rejected(
[&] {
sibling->set_schema_provider(*target, provider);
},
"cannot borrow sibling-owned provider");
}
struct ForeignSchemaProvider final : runtime::SchemaProvider {
GlobalContext& owner;
bool foreign_provider = true;
mutable unsigned cancelled = 0;
mutable bool saw_live_context = false, bootstrap_cancel = false;
mutable AsyncOperation<RuntimeSchemaResult>* child = nullptr;
explicit ForeignSchemaProvider(GlobalContext& owner) : owner(owner) {}
struct Work {
const ForeignSchemaProvider& provider;
const UserContext& context;
std::optional<RuntimeSchemaResult> poll(WakeSignal&) {
return {};
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {
++provider.cancelled;
provider.saw_live_context = context.trace_id() == "live-schema-context";
provider.bootstrap_cancel = detail::is_generated_schema_bootstrap(context);
}
};
AsyncOperation<RuntimeSchemaResult>& foreign_work(const UserContext& context) const {
child = &make_operation<RuntimeSchemaResult, Work>(owner, Work{*this, context});
return *child;
}
AsyncOperation<RuntimeSchemaResult>& ensure_schema(const UserContext& context,
const SchemaInvocation&) const override {
if (foreign_provider) {
return foreign_work(context);
}
struct Ready {
Memory owner;
std::optional<RuntimeSchemaResult> poll(WakeSignal&) {
return RuntimeSchemaResult::ok(owner, {});
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {}
};
return make_operation<RuntimeSchemaResult, Ready>(context.memory_resource(),
Ready{context.memory_resource()});
}
};
static GeneratedSchemaBootstrapFuture& foreign_schema_bootstrap(const UserContext& context) {
return context.require_resource<ForeignSchemaProvider>().value().get().foreign_work(context);
}
static void foreign_schema_children_are_quiesced() {
for (bool bootstrap : {false, true}) {
GlobalContext global;
auto& provider = global.make_resource<ForeignSchemaProvider>(global);
provider.foreign_provider = !bootstrap;
auto user = global.make_user_context();
user->set_trace_id("live-schema-context");
user->set_schema_provider(global, provider);
if (bootstrap) {
user->insert_resource(global, provider);
auto& module = global.make<RuntimeModule>();
module.generated_schema_bootstrap(foreign_schema_bootstrap);
module.apply_to(*user);
}
bool failed = false;
try {
(void)user->ensure_schema().poll();
} catch (const std::invalid_argument&) {
failed = true;
}
const bool quiesced = provider.cancelled == 1 && provider.saw_live_context &&
provider.bootstrap_cancel == bootstrap &&
provider.child->status() == AsyncStatus::Cancelled;
if (!quiesced) {
provider.child->cancel();
}
check(failed && quiesced,
"foreign schema child stops before rejection with the correct bootstrap scope");
check(!detail::is_generated_schema_bootstrap(*user),
"foreign schema rejection restores activation");
auto& replacement = global.make_resource<Provider>(global.make<Control>());
user->set_schema_provider(global, replacement);
user.reset();
check(provider.cancelled == 1 && global.live_user_bytes() == 0,
"foreign schema rejection releases Context borrow without deferred child access");
}
}
int main() {
try {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
pipeline();
bootstrap_root_installation();
cancellation_and_interleaving();
closing();
nested_close();
module_and_lifetimes();
foreign_schema_children_are_quiesced();
std::pmr::set_default_resource(previous);
std::cout << "Schema runtime contracts passed\n";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}