Repository navigation
Expand file tree
/
Copy pathtransaction_provider_error_tests.cpp
More file actions
234 lines (197 loc) · 9.03 KB
/
Copy pathtransaction_provider_error_tests.cpp
File metadata and controls
234 lines (197 loc) · 9.03 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
#include <teaql/runtime/transaction.hpp>
#include "../../tests/support/move_only_error_fixture.hpp"
#include <iostream>
using namespace teaql;
struct Counts {
unsigned errors = 0, commits = 0, rollbacks = 0, abandoned = 0;
};
enum class Failure { Begin, Query, Mutation, Commit, Rollback };
template <class T>
struct NativeFailure {
using allocator_type = Allocator;
Memory owner;
Counts& counts;
const char* message;
NativeFailure(std::allocator_arg_t, Allocator a, Counts& counts, const char* message)
: owner(a.resource()), counts(counts), message(message) {}
std::optional<Result<T, MoveOnlyError>> poll(WakeSignal&) {
return Result<T, MoveOnlyError>::err(
owner, MoveOnlyError(std::allocator_arg, Allocator(owner), message, counts.errors));
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {}
};
struct NativeTx : DataServiceTransaction<MoveOnlyError> {
using allocator_type = Allocator;
Counts& counts;
Failure failure;
bool active = true;
NativeTx(std::allocator_arg_t, Allocator a, Counts& counts, Failure failure)
: DataServiceTransaction<MoveOnlyError>(a.resource()), counts(counts), failure(failure) {}
~NativeTx() override {
cancel();
}
DataServiceCapabilities capabilities() const override {
return {};
}
DataServiceOperationResult<QueryResult, MoveOnlyError>&
query_transaction(Memory r, QueryRequest) override {
return make_operation<Result<QueryResult, MoveOnlyError>, NativeFailure<QueryResult>>(
r, counts, "query failed");
}
DataServiceOperationResult<MutationResult, MoveOnlyError>&
mutate_transaction(Memory r, MutationRequest) override {
return make_operation<Result<MutationResult, MoveOnlyError>, NativeFailure<MutationResult>>(
r, counts, "mutation failed");
}
void cancel_transaction() noexcept override {
if (active) {
active = false;
++counts.abandoned;
}
}
struct Terminal {
using allocator_type = Allocator;
Memory owner;
NativeTx& tx;
bool commit;
Terminal(std::allocator_arg_t, Allocator a, NativeTx& tx, bool commit)
: owner(a.resource()), tx(tx), commit(commit) {}
std::optional<Result<std::monostate, MoveOnlyError>> poll(WakeSignal&) {
if ((commit && tx.failure == Failure::Commit) ||
(!commit && tx.failure == Failure::Rollback)) {
return Result<std::monostate, MoveOnlyError>::err(
owner,
MoveOnlyError(std::allocator_arg,
Allocator(owner),
commit ? "commit failed" : "rollback failed",
tx.counts.errors));
}
tx.active = false;
return Result<std::monostate, MoveOnlyError>::ok(owner, {});
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {
tx.cancel();
}
};
DataServiceOperationResult<std::monostate, MoveOnlyError>&
commit_transaction(Memory r) override {
++counts.commits;
return make_operation<Result<std::monostate, MoveOnlyError>, Terminal>(r, *this, true);
}
DataServiceOperationResult<std::monostate, MoveOnlyError>&
rollback_transaction(Memory r) override {
++counts.rollbacks;
return make_operation<Result<std::monostate, MoveOnlyError>, Terminal>(r, *this, false);
}
};
struct NativeProvider : TransactionExecutor<MoveOnlyError> {
Counts& counts;
Failure failure;
NativeProvider(Counts& counts, Failure failure) : counts(counts), failure(failure) {}
DataServiceCapabilities capabilities() const override {
return {};
}
struct Begin {
using allocator_type = Allocator;
Memory owner;
NativeProvider& provider;
Begin(std::allocator_arg_t, Allocator a, NativeProvider& provider)
: owner(a.resource()), provider(provider) {}
std::optional<Result<TransactionHandle<MoveOnlyError>, MoveOnlyError>> poll(WakeSignal&) {
if (provider.failure == Failure::Begin) {
return Result<TransactionHandle<MoveOnlyError>, MoveOnlyError>::err(
owner,
MoveOnlyError(std::allocator_arg,
Allocator(owner),
"begin failed",
provider.counts.errors));
}
auto& tx = detail::async_arena(owner).make_resource<NativeTx>(provider.counts,
provider.failure);
return Result<TransactionHandle<MoveOnlyError>, MoveOnlyError>::ok(
owner, TransactionHandle<MoveOnlyError>(std::allocator_arg, Allocator(owner), tx));
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {}
};
DataServiceOperationResult<TransactionHandle<MoveOnlyError>, MoveOnlyError>&
begin(Memory r) override {
return make_operation<Result<TransactionHandle<MoveOnlyError>, MoveOnlyError>, Begin>(
r, *this);
}
};
struct Ready {
using allocator_type = Allocator;
Memory owner;
bool fail;
Ready(std::allocator_arg_t, Allocator a, bool fail) : owner(a.resource()), fail(fail) {}
std::optional<Result<unsigned, RuntimeError>> poll(WakeSignal&) {
if (fail) {
return Result<unsigned, RuntimeError>::err(
owner, RuntimeError::event(owner, "business failed"));
}
return Result<unsigned, RuntimeError>::ok(owner, 17);
}
void finish_and_wait() noexcept {}
void cancel_and_wait() noexcept {}
};
static void check(bool value, const char* message) {
if (!value) {
throw std::runtime_error(message);
}
}
int main() {
std::pmr::set_default_resource(std::pmr::null_memory_resource());
for (auto failure :
{Failure::Begin, Failure::Query, Failure::Mutation, Failure::Commit, Failure::Rollback}) {
GlobalContext global;
auto user = global.make_user_context();
Counts counts;
auto executor = user->make_resource_ref<NativeProvider>(counts, failure);
user->insert_resource(executor);
using Output = Result<unsigned, RuntimeError>;
auto& work = user->execute_in_transaction<NativeProvider, unsigned>(
[failure](const TransactionScope<NativeProvider>& scope) -> AsyncOperation<Output>& {
auto owner = scope.context().memory_resource();
if (failure == Failure::Query) {
SelectQuery description(std::allocator_arg, Allocator(owner), "Item");
description.comment_text.emplace("inspect provider failure fixture", owner);
description.purpose_text.emplace("verify transaction error propagation", owner);
QueryRequest query(
std::allocator_arg, Allocator(owner), description, false, false);
const auto& result = scope.query(std::move(query)).wait();
check(!result &&
result.error().to_string(owner) == "transaction error: query failed",
"Move-only query error maps without copying");
} else if (failure == Failure::Mutation) {
InsertCommand command(std::allocator_arg, Allocator(owner), "Item");
const auto& result =
scope
.mutate(MutationRequest(std::allocator_arg,
Allocator(owner),
std::move(command),
"verify transaction provider failure"))
.wait();
check(!result && result.error().to_string(owner) ==
"transaction error: mutation failed",
"Move-only mutation error maps without copying");
}
return make_operation<Output, Ready>(owner, failure == Failure::Rollback);
});
const auto& result = work.wait();
check(bool(result) == (failure == Failure::Query || failure == Failure::Mutation),
"Provider error mapping preserves scope completion behavior");
if (failure == Failure::Rollback) {
check(
result.error().to_string(user->memory_resource()) ==
"transaction error: operation failed (entity event error: business failed); rollback also failed (rollback failed)",
"Combined rollback error preserves both messages");
}
user.reset();
check(counts.errors == 1 && global.live_user_bytes() == 0,
"Each native move-only error is destroyed exactly once before User reclamation");
}
std::cout << "Move-only transaction provider error contracts pass\n";
}