Repository navigation
Expand file tree
/
Copy pathruntime_registry_tests.cpp
More file actions
242 lines (226 loc) · 9.91 KB
/
Copy pathruntime_registry_tests.cpp
File metadata and controls
242 lines (226 loc) · 9.91 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
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/registry_debug.hpp>
#include <iostream>
using namespace teaql;
static void check(bool ok, const char* message) {
if (!ok) {
throw std::runtime_error(message);
}
}
template <class F>
static void rejected(F&& f, const char* message) {
bool failed = false;
try {
f();
} catch (const std::logic_error&) {
failed = true;
}
check(failed, message);
}
static EntityDescriptor descriptor(Memory r, std::string_view name, std::string_view table) {
EntityDescriptor e(std::allocator_arg, Allocator(r), name);
e.table(table);
e.property("long_property_name_for_allocation", DataType::Text).with_max_length(100);
e.relation("long_relation_name_for_allocation", "OtherEntity").to_many();
e.audit_mask_fields.emplace_back("long_secret_field_for_allocation");
e.data_service.emplace("a long data service name for allocation", r);
return e;
}
static void registration_and_domains() {
GlobalContext global;
auto user = global.make_user_context();
auto sibling = global.make_user_context();
auto r = user->memory_resource();
auto g = global.memory_resource();
auto& metadata = user->make<InMemoryMetadataStore>();
auto& registry = user->make<InMemoryEntityRegistry>();
check(!user->entity("Item") && user->all_entities().empty() &&
!user->has_entity_data_service("Item"),
"unconfigured Context metadata has source empty defaults");
check(user->require_entity("Missing").error().to_string(r) ==
"missing entity descriptor: Missing",
"require_entity returns typed missing descriptor error");
metadata.register_entity(descriptor(g, "Zoo", "zoo_table"));
metadata.register_descriptor(descriptor(g, "Item", "old_table"));
registry.with_entity("RegisteredOnly").with_entity("RegisteredOnly");
user->with_metadata(*user, metadata).with_entity_registry(*user, registry);
check(
user->has_entity_data_service("Item") && user->has_entity_data_service("RegisteredOnly") &&
!user->entity("RegisteredOnly"),
"service availability is registry OR metadata; registry does not manufacture descriptors");
{
auto all = user->all_entities();
check(all.size() == 2 && all[0]->name == "Item" && all[1]->name == "Zoo",
"metadata follows sorted descriptor names");
}
const SchemaProvider& schema = metadata;
auto* snapshot = schema.get_entity("Item");
check(snapshot && snapshot != metadata.entity("Item"),
"data-service schema provider takes an independent descriptor snapshot");
metadata.register_entity(descriptor(r, "Item", "replacement_table"));
check(snapshot->table_name == "old_table" &&
user->entity("Item")->table_name == "replacement_table",
"schema snapshot remains stable across metadata replacement");
check(snapshot->properties[0].name.get_allocator().resource() == r,
"registration clones nested descriptor buffers into store domain");
rejected(
[&] {
global.make<ResourceBorrow<const EntityDescriptor>>(
user->require_entity("Item").value());
},
"required User descriptor cannot become a Global borrow");
rejected(
[&] {
sibling->set_metadata(*user, metadata);
},
"sibling metadata retention is rejected");
rejected(
[&] {
user->set_metadata(global, metadata);
},
"registration validates actual metadata allocator");
rejected(
[&] {
metadata.all_entities(g);
},
"pointer array cannot smuggle User descriptors into Global");
auto& metadata_copy = global.make<InMemoryMetadataStore>(metadata);
auto& registry_copy = global.make<InMemoryEntityRegistry>(registry);
auto& snapshot_copy = global.make<EntityDescriptor>(*snapshot);
auto& assigned = global.make<InMemoryMetadataStore>();
assigned = detail::clone_in(r, metadata);
user->set_metadata(global, metadata_copy);
auto& borrowed =
global.make<ResourceBorrow<const EntityDescriptor>>(user->require_entity("Item").value());
bool readable = false, replacement_rejected = false;
struct ClosingCheck {
UserContext& context;
GlobalContext& global;
InMemoryMetadataStore& metadata;
bool& readable;
bool& rejected;
};
ClosingCheck evidence{*user, global, metadata_copy, readable, replacement_rejected};
Arena::Quiescence stop;
stop.attach(*dynamic_cast<Arena*>(r), &evidence, [](void* raw) noexcept {
auto& e = *static_cast<ClosingCheck*>(raw);
e.readable = e.context.entity("Item") != nullptr;
try {
e.context.set_metadata(e.global, e.metadata);
} catch (const std::logic_error&) {
e.rejected = true;
}
});
user.reset();
check(readable && replacement_rejected,
"quiescence can read metadata but cannot replace services before pending work stops");
check(borrowed.get().table_name == "replacement_table" &&
snapshot_copy.table_name == "old_table",
"explicit Global copies and Global borrows survive User close");
check(metadata_copy.entity("Item")->properties[0].name.get_allocator().resource() == g &&
metadata_copy.entity("Item")->relations[0].target_entity == "OtherEntity" &&
assigned.entity("Zoo")->memory_resource() == g,
"metadata clone and move assignment preserve nested allocation domains");
check(registry_copy.contains("RegisteredOnly"), "registry deep copy survives source close");
auto next = global.make_user_context();
Ref<String> retained_diagnostic;
std::size_t expected_size = 0;
{
auto metadata_debug = debug_string(next->memory_resource(), metadata_copy);
auto registry_debug = debug_string(next->memory_resource(), registry_copy);
check(
metadata_debug.find("replacement_table") != String::npos &&
metadata_debug.find("OtherEntity") != String::npos &&
metadata_debug.get_allocator().resource() == next->memory_resource() &&
registry_debug ==
"InMemoryEntityRegistry { entities: {\"RegisteredOnly\": \"RegisteredOnly\"} }",
"Global registry diagnostics survive source User deletion and retain source map shape");
retained_diagnostic = next->make_global<String>(metadata_debug);
expected_size = metadata_debug.size();
}
next.reset();
auto reader = global.make_user_context();
const auto& retained_text = reader->get(reader->rebind_global(retained_diagnostic));
check(retained_text.size() == expected_size && retained_text.get_allocator().resource() == g,
"explicit Global diagnostic copy survives its formatting User deletion");
sibling->set_metadata(global, metadata_copy);
sibling->close();
rejected(
[&] {
sibling->all_entities();
},
"closed Context refuses metadata access");
rejected(
[&] {
sibling->set_entity_registry(global, registry_copy);
},
"closed Context refuses registry replacement");
}
struct LoggingStore final : MetadataStore {
Memory resource;
bool capture = true, debug = false;
mutable unsigned calls = 0;
mutable std::size_t count = 0;
explicit LoggingStore(Memory r) : resource(r) {}
Memory memory_resource() const noexcept override {
return resource;
}
const EntityDescriptor* entity(std::string_view) const override {
return nullptr;
}
Vector<const EntityDescriptor*> all_entities(Memory r) const override {
return Vector<const EntityDescriptor*>(r);
}
bool capture_execution_metadata() const override {
return capture;
}
bool capture_query_debug() const override {
return debug;
}
void record_metadata_log(const ExecutionMetadata& m) const override {
++calls;
count = m.result_count.value_or(0);
}
};
static void observers() {
GlobalContext global;
auto user = global.make_user_context();
auto r = user->memory_resource();
auto g = global.memory_resource();
auto& defaults = user->make<InMemoryMetadataStore>();
check(defaults.capture_execution_metadata() && defaults.capture_query_debug(),
"metadata capture defaults remain enabled");
auto& local = user->make_resource<LoggingStore>(r);
check(local.query_diagnostic_observer(r).has_value(),
"query observer depends on capture_metadata even when debug is off");
local.capture = false;
check(!local.query_diagnostic_observer(r) && local.mutation_diagnostic_observer(r).has_value(),
"mutation observer remains present when metadata capture is disabled");
auto local_callback = local.mutation_diagnostic_observer(r);
(*local_callback)(ExecutionMetadata::unrecorded_query(r, 11));
check(local.calls == 1 && local.count == 11, "observer forwards the actual execution metadata");
rejected(
[&] {
global.make<ExecutionObserver>(*local_callback);
},
"allocator-aware callback cloning cannot extend a User metadata store");
auto& shared = global.make_resource<LoggingStore>(g);
auto callback = shared.query_diagnostic_observer(r);
auto& retained = global.make<ExecutionObserver>(*callback);
user.reset();
retained(ExecutionMetadata::unrecorded_query(g, 17));
check(shared.calls == 1 && shared.count == 17,
"Global store observer may be explicitly retained after User close");
}
int main() {
try {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
registration_and_domains();
observers();
std::pmr::set_default_resource(previous);
std::cout << "Runtime registry contracts passed\n";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}