Repository navigation
Expand file tree
/
Copy pathschema_runtime_probe.cpp
More file actions
203 lines (184 loc) · 7.02 KB
/
Copy pathschema_runtime_probe.cpp
File metadata and controls
203 lines (184 loc) · 7.02 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
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/module.hpp>
#include <teaql/core/detail/model_json.hpp>
#include "fixture_value.hpp"
#include <iostream>
using namespace teaql;
struct Shared {
using allocator_type = Allocator;
Json config;
Vector<String> calls;
Shared(std::allocator_arg_t, Allocator a, const Json& j)
: config(std::allocator_arg, a, j), calls(a.resource()) {}
void note(const UserContext& context, std::string_view name) {
String entry(name, calls.get_allocator().resource());
entry += detail::is_generated_schema_bootstrap(context) ? ":bootstrap" : ":normal";
calls.push_back(std::move(entry));
}
};
static Shared& state(const UserContext& context) {
return const_cast<Shared&>(context.require_resource<Shared>().value().get());
}
struct Step {
using allocator_type = Allocator;
Memory r;
const UserContext& context;
String name;
std::uint64_t remaining;
bool fail;
Step(std::allocator_arg_t,
Allocator a,
const UserContext& context,
std::string_view name,
std::uint64_t remaining,
bool fail)
: r(a.resource()), context(context), name(name, r), remaining(remaining), fail(fail) {}
void note(std::string_view suffix) const {
String message(name, r);
message += suffix;
state(context).note(context, message);
}
std::optional<RuntimeSchemaResult> poll(WakeSignal& wake) {
note(".poll");
if (remaining) {
--remaining;
wake.notify();
return {};
}
if (fail) {
String message(name, r);
message += " failed";
return RuntimeSchemaResult::err(r, RuntimeError::schema(r, message));
}
return RuntimeSchemaResult::ok(r, {});
}
void finish_and_wait() noexcept {
note(".finish");
}
void cancel_and_wait() noexcept {
note(".cancel");
}
};
struct Provider : runtime::SchemaProvider {
AsyncOperation<RuntimeSchemaResult>& ensure_schema(const UserContext& context,
const SchemaInvocation&) const override {
auto& shared = state(context);
shared.note(context, "provider.create");
return make_operation<RuntimeSchemaResult, Step>(
context.memory_resource(),
context,
"provider",
std::get<std::uint64_t>(field(shared.config, "provider_delay").data),
text(field(shared.config, "fail")) == "provider");
}
};
struct Business : BusinessIdAllocator, BusinessIdSchemaContributor {
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 {
auto& shared = state(context);
shared.note(context, "business");
using R = Result<std::monostate, BusinessIdError>;
if (text(field(shared.config, "fail")) == "business") {
return R::err(r,
BusinessIdError(std::allocator_arg,
Allocator(r),
BusinessIdErrorCode::Allocation,
"business failed"));
}
return R::ok(r, {});
}
};
static GeneratedSchemaBootstrapFuture& bootstrap(const UserContext& context, unsigned index) {
auto& shared = state(context);
auto r = context.memory_resource();
char digit = char('0' + index);
String name("callback", r);
name += digit;
String create(name, r);
create += ".create";
shared.note(context, create);
auto& delays = std::get<Json::Array>(field(shared.config, "delays").data);
return make_operation<RuntimeSchemaResult, Step>(r,
context,
name,
std::get<std::uint64_t>(delays.at(index).data),
text(field(shared.config, "fail")) ==
std::string_view(&digit, 1));
}
static GeneratedSchemaBootstrapFuture& first(const UserContext& c) {
return bootstrap(c, 0);
}
static GeneratedSchemaBootstrapFuture& second(const UserContext& c) {
return bootstrap(c, 1);
}
static GeneratedSchemaBootstrapFuture& third(const UserContext& c) {
return bootstrap(c, 2);
}
static Json evaluate(UserContext& context, const Json& j) {
auto r = context.memory_resource();
auto& shared = context.make<Shared>(j);
context.insert_resource(context, shared);
auto& module = context.make<RuntimeModule>();
GeneratedSchemaBootstrap callbacks[]{first, second, third};
for (std::size_t i = 0; i < std::get<Json::Array>(field(j, "delays").data).size(); ++i) {
module.generated_schema_bootstrap(callbacks[i]);
}
module.apply_to(context);
if (std::get<bool>(field(j, "provider").data)) {
auto& provider = context.make_resource<Provider>();
context.set_schema_provider(context, provider);
}
if (std::get<bool>(field(j, "business").data)) {
auto& business = context.make_resource<Business>();
context.set_business_id_infrastructure(context, business);
}
shared.note(context, "before");
auto& operation = context.ensure_schema();
auto outcome = detail::model_json(r);
detail::model_put(outcome, "cancelled", true);
for (std::uint64_t i = 0; i < std::get<std::uint64_t>(field(j, "polls").data); ++i) {
auto* result = operation.poll();
if (!result) {
shared.note(context, "outside");
continue;
}
outcome = detail::model_json(r);
if (*result) {
detail::model_put(outcome, "ok", true);
} else {
detail::model_put(outcome, "message", result->error().message());
detail::model_put(outcome, "display", result->error().to_string(r));
}
break;
}
operation.cancel();
shared.note(context, "after");
auto out = detail::model_json(r);
Json::Array calls(r);
for (const auto& call : shared.calls) {
calls.emplace_back(call);
}
detail::model_put(out, "calls", calls);
detail::model_put(out, "result", outcome);
return out;
}
int main() {
GlobalContext global;
std::string line;
while (std::getline(std::cin, line)) {
auto context = global.make_user_context();
auto r = context->memory_resource();
auto j = Json::parse(r, line);
if (!j) {
throw std::runtime_error("invalid fixture JSON");
}
std::cout << evaluate(*context, *j).dump(r) << '\n';
}
}