orders: the fold is kept in memory, and a reference names one order
The ledger was folded from disk on every call: every order page, every /financials request, every reconciler sweep, and every two seconds for the mailer. Each event found its order by walking the orders so far, so a fold was quadratic. A ledger of 40 000 orders took 11.9 s to fold, anyone can add orders, and /financials is public and uncached. Orders are now found through an index, which makes a fold one pass (0.33 s for the same ledger), and the fold is held in memory. Every call stats the file, and a different size or modification time means fold it again, so an append by the CLI from another process and an edit by hand are seen on the next call without a restart. What this process appends itself is applied to the held fold by the same function that folds a line from disk, provided the file grew by exactly what was written; if it grew by more, another writer's line is in there and the file is folded again. With 40 000 orders /financials takes about 1 ms and an order page 0.3 ms. /financials sums from the held fold through LedgerSales and no longer copies every order to do it. The file stays the ledger and the held fold a reading of it. It costs 3 to 4 KB of memory an order, and it cannot see a change that leaves both the size and the modification time as they were. One thing the writer does differently. A file that ends mid-line, after a crash during a write, now gets its line break before the next event. The next event used to be written onto the end of the broken line and was skipped with it, so a crash cost two events, not one. The reference a payer quotes is the first six hex digits of the token: 24 bits, where two orders in a few thousand share one often enough to plan for, and a transfer quoting it would pay for both. Checkout now mints its token through NewUniqueOrderToken, which reads every reference the ledger has ever held, lapsed and cancelled orders included, and holds the reference while the checkout is in flight. ShouldFoldTheOrderLedger compares the held fold with a fresh one after appends of every kind, an outside append, a same-size edit, a replaced file and a torn tail. It hands colliding draws to the mint, since the real generator collides once in sixteen million. And it races one outside line against a stream of this process's own appends, which fails within a few rounds when the size check is removed. Found in the security review of 2026-11-26. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
This commit is contained in:
parent
21d34b13ed
commit
06ec1c525c
4 changed files with 810 additions and 112 deletions
|
|
@ -116,9 +116,10 @@ Server::OrderRecord Sample(std::string token, std::string email) {
|
|||
|
||||
// ── buyer free text cannot leave its field ────────────────────────────
|
||||
//
|
||||
// Form::ValidateCheckout length-limits name/street/postal/city and nothing
|
||||
// more, so quotes, backslashes and newlines reach CreateOrder's formatter
|
||||
// exactly as they were typed. JsonEscape is the only thing between them and
|
||||
// Form::ValidateCheckout length-limits name/street/postal/city and holds each
|
||||
// to one line, and nothing more, so quotes and backslashes reach CreateOrder's
|
||||
// formatter exactly as they were typed (and a newline still can, from any
|
||||
// caller that is not the form). JsonEscape is the only thing between them and
|
||||
// the record format.
|
||||
void BuyerTextStaysInItsField() {
|
||||
FreshLedger("escaping");
|
||||
|
|
@ -560,6 +561,427 @@ void ATransitionKeepsWhatItDoesNotName() {
|
|||
|
||||
} // namespace
|
||||
|
||||
// ── a reference names one order ───────────────────────────────────────
|
||||
//
|
||||
// The reference is what a payer quotes and what a transfer is matched on, and
|
||||
// it is only the first six hex digits of the token: 24 bits, where the token
|
||||
// has 128. Two orders sharing one would both be paid for by a transfer that
|
||||
// quotes it. So checkout mints its token through NewUniqueOrderToken, which
|
||||
// reads every reference the ledger has ever held.
|
||||
void AReferenceNamesOneOrder() {
|
||||
FreshLedger("references");
|
||||
|
||||
Check(!Server::ReferenceInUse("CC-ABCDEF"),
|
||||
"references: none is in use in an empty ledger");
|
||||
|
||||
const std::string token = "abcdef0123456789abcdef0123456789";
|
||||
Check(Server::CreateOrder(Sample(token, "ada@example.org")),
|
||||
"references: an order is written");
|
||||
Check(Server::ReferenceInUse("CC-ABCDEF"),
|
||||
"references: a stored order's reference is in use");
|
||||
// The same reduction the transfer matcher applies: what settles as one
|
||||
// reference is one reference here.
|
||||
Check(Server::ReferenceInUse("cc abcdef"),
|
||||
"references: however it is spelled");
|
||||
Check(!Server::ReferenceInUse("CC-ABCDE0"),
|
||||
"references: one character different is a different reference");
|
||||
Check(!Server::ReferenceInUse(""), "references: an empty one is nobody's");
|
||||
|
||||
// Lapsed and cancelled orders keep theirs. The payment for an old order
|
||||
// stays in the bank's history quoting it, so handing the reference to a
|
||||
// new order would hand that order somebody else's money.
|
||||
Check(Server::AppendOrderStatus(token, "cancelled", "2026-08-16T09:00:00Z"),
|
||||
"references: the order is cancelled");
|
||||
Check(Server::ReferenceInUse("CC-ABCDEF"),
|
||||
"references: a cancelled order still holds its reference");
|
||||
|
||||
// The uniqueness check itself. From the real generator a collision is one
|
||||
// draw in sixteen million, so checks that only draw from it pass with
|
||||
// the check deleted (a reviewer deleted it, and they did). Here the draws
|
||||
// are handed in. A draw that lands on a reference the LEDGER holds is thrown away,
|
||||
// one that lands on a reference being HELD for a checkout in flight is
|
||||
// thrown away, and a run of nothing but collisions ends in a refusal
|
||||
// after a bounded number of draws rather than in a duplicate or a loop.
|
||||
{
|
||||
const std::string stored = "abcdef99999999999999999999999999"; // CC-ABCDEF, in the ledger
|
||||
const std::string first = "0a0b0c11111111111111111111111111"; // CC-0A0B0C, free
|
||||
const std::string again = "0a0b0c22222222222222222222222222"; // CC-0A0B0C, held by then
|
||||
const std::string second = "0d0e0f33333333333333333333333333"; // CC-0D0E0F, free
|
||||
|
||||
int draws = 0;
|
||||
const auto fromLedger = Server::NewUniqueOrderToken([&] {
|
||||
return ++draws == 1 ? stored : first;
|
||||
});
|
||||
Check(fromLedger && *fromLedger == first,
|
||||
"references: a draw that collides with a stored order is thrown away",
|
||||
fromLedger ? *fromLedger : "nothing minted");
|
||||
Check(draws == 2, "references: and exactly one more is drawn for it",
|
||||
std::format("{} draws", draws));
|
||||
|
||||
draws = 0;
|
||||
const auto fromHeld = Server::NewUniqueOrderToken([&] {
|
||||
return ++draws == 1 ? again : second;
|
||||
});
|
||||
Check(fromHeld && *fromHeld == second,
|
||||
"references: a draw that collides with a reference being held is thrown away",
|
||||
fromHeld ? *fromHeld : "nothing minted");
|
||||
|
||||
draws = 0;
|
||||
const auto none = Server::NewUniqueOrderToken([&] {
|
||||
++draws;
|
||||
return stored;
|
||||
});
|
||||
Check(!none.has_value(),
|
||||
"references: nothing but collisions is refused, not minted",
|
||||
none ? *none : "");
|
||||
Check(draws == Server::kUniqueTokenDraws,
|
||||
"references: after the bounded number of draws",
|
||||
std::format("{} draws", draws));
|
||||
}
|
||||
|
||||
// A minted token is well formed, never lands on a reference the ledger
|
||||
// holds, and its reference is HELD from the moment it is handed out:
|
||||
// checkout asks the payment rail for its link between minting and
|
||||
// storing, and a second checkout minting in that gap must not be handed
|
||||
// the same reference.
|
||||
std::set<std::string> minted;
|
||||
for (int i = 0; i < 64; ++i) {
|
||||
const std::optional<std::string> fresh = Server::NewUniqueOrderToken();
|
||||
Check(fresh.has_value(), "references: a token can be minted");
|
||||
if (!fresh) break;
|
||||
Check(IsOrderToken(*fresh), "references: a minted token has the token shape",
|
||||
*fresh);
|
||||
const std::string reference = Server::ReferenceFromToken(*fresh);
|
||||
Check(reference != "CC-ABCDEF",
|
||||
"references: a minted token does not reuse a stored reference", reference);
|
||||
Check(minted.insert(reference).second,
|
||||
"references: no two minted tokens share a reference", reference);
|
||||
Check(Server::ReferenceInUse(reference),
|
||||
"references: a minted reference is held before its order is stored",
|
||||
reference);
|
||||
}
|
||||
|
||||
// Storing the order moves the reference from held to the ledger, and it
|
||||
// is in use either way.
|
||||
if (!minted.empty()) {
|
||||
const std::optional<std::string> fresh = Server::NewUniqueOrderToken();
|
||||
if (fresh) {
|
||||
Check(Server::CreateOrder(Sample(*fresh, "grace@example.org")),
|
||||
"references: a minted order is written");
|
||||
Check(Server::ReferenceInUse(Server::ReferenceFromToken(*fresh)),
|
||||
"references: and its reference is in use from the ledger");
|
||||
Check(Server::FindOrder(*fresh).has_value(),
|
||||
"references: the order reads back under its token");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── the fold is kept, and the file still wins ─────────────────────────
|
||||
//
|
||||
// The order store holds the folded ledger in memory and folds it again only
|
||||
// when the file changes. That is what keeps an order page or /financials from
|
||||
// costing a parse of the whole history, and it is only acceptable if the held
|
||||
// fold can never drift from the file. Three things can move the file under a
|
||||
// running server, and each has to be seen on the very next call: the CLI
|
||||
// appending from another process, an operator editing a field by hand, and
|
||||
// the file being replaced outright. And what THIS process appends is applied
|
||||
// to the held fold directly, so it has to come out identical to a fold of the
|
||||
// file it was just written to.
|
||||
bool SameOrders(const std::vector<Server::OrderRecord>& a,
|
||||
const std::vector<Server::OrderRecord>& b) {
|
||||
if (a.size() != b.size()) return false;
|
||||
for (std::size_t i = 0; i < a.size(); ++i) {
|
||||
const Server::OrderRecord& x = a[i];
|
||||
const Server::OrderRecord& y = b[i];
|
||||
if (x.token != y.token || x.reference != y.reference || x.status != y.status
|
||||
|| x.updatedAt != y.updatedAt || x.paidVia != y.paidVia
|
||||
|| x.paidAt != y.paidAt || x.invoiceNumber != y.invoiceNumber
|
||||
|| x.invoicedAt != y.invoicedAt
|
||||
|| x.confirmationSentAt != y.confirmationSentAt
|
||||
|| x.buyer.name != y.buyer.name || x.buyer.email != y.buyer.email
|
||||
|| x.totalMinor != y.totalMinor || x.donation != y.donation) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void TheHeldFoldFollowsTheFile() {
|
||||
const fs::path led = FreshLedger("held-fold");
|
||||
const std::string a = "aaaa0000aaaa0000aaaa0000aaaa0000";
|
||||
const std::string b = "bbbb1111bbbb1111bbbb1111bbbb1111";
|
||||
|
||||
Check(Server::CreateOrder(Sample(a, "ada@example.org")), "held: an order is written");
|
||||
Check(Server::FindOrder(a).has_value(), "held: and read back"); // now held
|
||||
|
||||
// Another process appends. `--orders FILE --mark-paid TOKEN` does exactly
|
||||
// this while the server runs, and the mailer must see the paid order on
|
||||
// its next sweep without anyone restarting anything.
|
||||
AppendRaw(led, std::format(
|
||||
R"({{"type":"status","at":"2026-08-15T10:00:00Z","id":"{}","status":"paid","via":"sepa"}})", a));
|
||||
{
|
||||
const auto o = Server::FindOrder(a);
|
||||
Check(o && o->status == "paid" && o->paidVia == "sepa",
|
||||
"held: an event another process appended is seen on the next call",
|
||||
o ? o->status : "order missing");
|
||||
}
|
||||
|
||||
// An edit by hand that keeps the file's size, which is what blanking a
|
||||
// name in place is. Size cannot show it, so the modification time has to.
|
||||
// The time is SET here rather than waited for, so the check does not
|
||||
// depend on how finely the filesystem under the scratch directory
|
||||
// happens to stamp a write made a moment after the last one.
|
||||
{
|
||||
std::string text = ReadAll(led);
|
||||
const std::size_t at = text.find("Ada Lovelace");
|
||||
Check(at != std::string::npos, "held: the ledger holds the name as written");
|
||||
if (at != std::string::npos) {
|
||||
const fs::file_time_type stamped = fs::last_write_time(led);
|
||||
text.replace(at, 12, "xxxxxxxxxxxx");
|
||||
{
|
||||
std::ofstream out(led, std::ios::trunc | std::ios::binary);
|
||||
out << text;
|
||||
}
|
||||
fs::last_write_time(led, stamped + std::chrono::seconds(2));
|
||||
const auto o = Server::FindOrder(a);
|
||||
Check(o && o->buyer.name == "xxxxxxxxxxxx",
|
||||
"held: an in-place edit that keeps the size is seen by its time",
|
||||
o ? o->buyer.name : "order missing");
|
||||
}
|
||||
}
|
||||
|
||||
// The file replaced by a different one, as a restored backup would be.
|
||||
WriteLedger(led, {
|
||||
R"({"type":"order","at":"2026-08-15T09:30:00Z","id":"bbbb1111bbbb1111bbbb1111bbbb1111","ref":"CC-BBBB11","email":"grace@example.org","total_minor":4200})",
|
||||
});
|
||||
Check(!Server::FindOrder(a).has_value(),
|
||||
"held: an order the file no longer has is gone from the fold");
|
||||
Check(Server::FindOrder(b).has_value(), "held: and the file's own order is there");
|
||||
Check(Server::ReferenceInUse("CC-BBBB11") && !Server::ReferenceInUse("CC-AAAA00"),
|
||||
"held: the references in use follow the file too");
|
||||
|
||||
// This process's own appends, one of each event, are applied to the held
|
||||
// fold as they are written. Dropping what is held and folding the file
|
||||
// from disk has to give the same orders, field for field.
|
||||
Check(Server::AppendOrderStatus(b, "paid", "2026-08-15T11:00:00Z", "ideal"),
|
||||
"held: a status event is appended");
|
||||
Check(Server::AssignInvoiceNumber(b, "2026-08-15T11:00:01Z").has_value(),
|
||||
"held: an invoice event is appended");
|
||||
Check(Server::AppendOrderNotified(b, "2026-08-15T11:00:02Z"),
|
||||
"held: a notified event is appended");
|
||||
Check(Server::CreateOrder(Sample(a, "ada@example.org")),
|
||||
"held: an order event is appended");
|
||||
Check(Server::AppendOrderStatus(a, "cancelled", "2026-08-15T12:00:00Z"),
|
||||
"held: a second order's status is appended");
|
||||
{
|
||||
const std::vector<Server::OrderRecord> held = Server::ListOrders();
|
||||
const Server::SalesSummary heldSales = Server::LedgerSales();
|
||||
Server::SetOrdersPath(led); // drops what is held; the next call folds the file
|
||||
const std::vector<Server::OrderRecord> fresh = Server::ListOrders();
|
||||
Check(held.size() == 2 && fresh.size() == 2,
|
||||
"held: both orders are in the fold either way",
|
||||
std::format("{} held, {} from disk", held.size(), fresh.size()));
|
||||
Check(SameOrders(held, fresh),
|
||||
"held: the fold kept across this process's appends is the fold of the file");
|
||||
const Server::SalesSummary summed = Server::SummarizeSales(fresh);
|
||||
Check(heldSales.count == summed.count && heldSales.totalMinor == summed.totalMinor
|
||||
&& heldSales.donationCount == summed.donationCount
|
||||
&& heldSales.donationsMinor == summed.donationsMinor
|
||||
&& summed.count == 1 && summed.totalMinor == 4200,
|
||||
"held: LedgerSales is SummarizeSales over the ledger",
|
||||
std::format("{} sale(s), {}", heldSales.count, heldSales.totalMinor));
|
||||
}
|
||||
|
||||
// A line cut short by a crash, with no line break after it. The reader
|
||||
// has always skipped such a line. What it did NOT survive was the next
|
||||
// append: written straight onto the end of the broken line, the new
|
||||
// event became part of it and was skipped along with it, so a crash cost
|
||||
// two events and the second could be somebody's "paid". The writer now
|
||||
// starts a line of its own.
|
||||
{
|
||||
{
|
||||
std::ofstream out(led, std::ios::app | std::ios::binary);
|
||||
out << R"({"type":"status","at":"2026-08-15T13:00:00Z","id":")" << b
|
||||
<< R"(","sta)";
|
||||
}
|
||||
Check(Server::AppendOrderStatus(b, "shipped", "2026-08-15T14:00:00Z"),
|
||||
"torn: an event is appended after a line cut short");
|
||||
const auto o = Server::FindOrder(b);
|
||||
Check(o && o->status == "shipped",
|
||||
"torn: the event after a broken line is not lost with it",
|
||||
o ? o->status : "order missing");
|
||||
Server::SetOrdersPath(led);
|
||||
const auto again = Server::FindOrder(b);
|
||||
Check(again && again->status == "shipped",
|
||||
"torn: and a fold from disk reads it the same",
|
||||
again ? again->status : "order missing");
|
||||
}
|
||||
|
||||
// The index. Orders are found through a map from token to position, not
|
||||
// by walking the list, so what has to hold is that every event lands on
|
||||
// its own order and on no other, whatever came between.
|
||||
{
|
||||
FreshLedger("held-many");
|
||||
constexpr int kOrders = 300;
|
||||
std::vector<std::string> tokens;
|
||||
for (int i = 0; i < kOrders; ++i) {
|
||||
tokens.push_back(std::format("{:08x}{:024x}", i * 7919 + 1, 0));
|
||||
Server::CreateOrder(Sample(tokens.back(), std::format("u{}@example.org", i)));
|
||||
}
|
||||
for (int i = 0; i < kOrders; i += 3) {
|
||||
Server::AppendOrderStatus(tokens[i], "paid", "2026-08-15T15:00:00Z", "sepa");
|
||||
}
|
||||
int right = 0;
|
||||
for (int i = 0; i < kOrders; ++i) {
|
||||
const auto o = Server::FindOrder(tokens[i]);
|
||||
const std::string_view want = (i % 3 == 0) ? "paid" : "awaiting_payment";
|
||||
if (o && o->token == tokens[i] && o->status == want
|
||||
&& o->buyer.email == std::format("u{}@example.org", i)) {
|
||||
++right;
|
||||
}
|
||||
}
|
||||
Check(right == kOrders,
|
||||
"index: every order is found under its own token, with its own events",
|
||||
std::format("{} of {}", right, kOrders));
|
||||
Check(Server::ListOrders().size() == static_cast<std::size_t>(kOrders),
|
||||
"index: the list holds each order once");
|
||||
Check(!Server::FindOrder(std::string(32, 'f')).has_value(),
|
||||
"index: a token no order has finds nothing");
|
||||
}
|
||||
}
|
||||
|
||||
// ── appends from several threads, and from outside, at once ───────────
|
||||
//
|
||||
// The server appends from the reconciler, the mailer and every request
|
||||
// thread, while the CLI may append from another process. An append by this
|
||||
// process is applied to the held fold directly, which is only right if the
|
||||
// file grew by exactly what was written: if another writer's line landed in
|
||||
// the same instant, it is in the file and not in the fold, and the fold has
|
||||
// to be redone. Nothing sequential can show that, so this races them. It can
|
||||
// only fail when something is wrong (when all is well the held fold equals
|
||||
// the file whatever the interleaving), and a reviewer who removed the
|
||||
// "grew by exactly what was written" check saw it fail.
|
||||
void AppendsFromManyWritersAllLand() {
|
||||
const fs::path led = FreshLedger("held-threads");
|
||||
constexpr int kThreads = 3;
|
||||
constexpr int kRounds = 150;
|
||||
|
||||
// The outside writer's orders, created before the race so its status
|
||||
// events always have an order to land on.
|
||||
std::vector<std::string> outside;
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
outside.push_back(std::format("e{:07x}{:024x}", i, 0));
|
||||
Server::CreateOrder(Sample(outside.back(), "outside@example.org"));
|
||||
}
|
||||
|
||||
std::atomic<int> appended{0};
|
||||
std::atomic<int> unseen{0};
|
||||
{
|
||||
std::vector<std::jthread> writers;
|
||||
for (int t = 0; t < kThreads; ++t) {
|
||||
writers.emplace_back([&, t] {
|
||||
std::vector<std::string> mine;
|
||||
for (int i = 0; i < kRounds; ++i) {
|
||||
if (mine.empty() || i % 5 == 0) {
|
||||
mine.push_back(std::format("{:x}{:07x}{:024x}", t, i, 0));
|
||||
if (!Server::CreateOrder(Sample(mine.back(), "t@example.org"))) continue;
|
||||
++appended;
|
||||
if (!Server::FindOrder(mine.back())) ++unseen;
|
||||
} else {
|
||||
const std::string& token = mine[static_cast<std::size_t>(i) % mine.size()];
|
||||
const std::string at = std::format("T{}-{}", t, i);
|
||||
if (!Server::AppendOrderStatus(token, "paid", at, "sepa")) continue;
|
||||
++appended;
|
||||
const auto o = Server::FindOrder(token);
|
||||
if (!o || o->updatedAt != at) ++unseen;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
// The other process: whole lines written straight to the file, the
|
||||
// way `--orders FILE --mark-shipped` writes them.
|
||||
writers.emplace_back([&] {
|
||||
for (int i = 0; i < kRounds; ++i) {
|
||||
const std::string& token = outside[static_cast<std::size_t>(i) % outside.size()];
|
||||
const std::string at = std::format("X-{}", i);
|
||||
AppendRaw(led, std::format(
|
||||
R"({{"type":"status","at":"{}","id":"{}","status":"shipped"}})", at, token));
|
||||
const auto o = Server::FindOrder(token);
|
||||
if (!o || o->updatedAt != at) ++unseen;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Check(unseen.load() == 0,
|
||||
"threads: every event is in the fold on the call after it was appended",
|
||||
std::format("{} not seen", unseen.load()));
|
||||
|
||||
const std::vector<Server::OrderRecord> held = Server::ListOrders();
|
||||
const std::string text = ReadAll(led);
|
||||
const auto lines = std::ranges::count(text, '\n');
|
||||
const auto expected = 6 + appended.load() + kRounds;
|
||||
Check(lines == expected, "threads: the file holds one line for every append",
|
||||
std::format("{} lines, {} appends", lines, expected));
|
||||
|
||||
Server::SetOrdersPath(led); // drop what is held and fold the file itself
|
||||
Check(SameOrders(held, Server::ListOrders()),
|
||||
"threads: the fold held through the race is the fold of the file");
|
||||
|
||||
// The case the race above almost never produces, and the one that
|
||||
// matters. There, the outside writer reads after every line it writes,
|
||||
// which waits for the ledger lock, so its next line tends to land while
|
||||
// the other threads are still waking up: before their append has looked
|
||||
// at the file, where the ordinary "the file changed, fold again" covers
|
||||
// it. (That race passed with the size check removed, which is how this
|
||||
// was found.)
|
||||
//
|
||||
// What needs the size check is ONE outside line landing in the middle of
|
||||
// a run of this process's own appends, with nothing from outside after
|
||||
// it. An append takes the file's stamp when it is done; if it took it
|
||||
// without noticing that the file grew by more than it wrote, the outside
|
||||
// line is under that stamp, in the file and not in the fold, and every
|
||||
// later own append keeps it that way. So each round here is a fresh
|
||||
// ledger, two threads appending without pause, and a single outside line
|
||||
// dropped in at a different moment each time, and the fold held at the
|
||||
// end is compared with the file. Correct code cannot fail this however
|
||||
// the threads fall; code without the check fails it within a few rounds.
|
||||
constexpr int kRaces = 120;
|
||||
int stale = 0;
|
||||
for (int race = 0; race < kRaces; ++race) {
|
||||
const fs::path p = FreshLedger("held-race");
|
||||
const std::string solo = std::format("f{:07x}{:024x}", race, 0);
|
||||
Server::CreateOrder(Sample(solo, "outside@example.org"));
|
||||
std::atomic<int> started{0};
|
||||
{
|
||||
std::vector<std::jthread> writers;
|
||||
for (int t = 0; t < 2; ++t) {
|
||||
writers.emplace_back([&, t] {
|
||||
const std::string mine = std::format("{:x}{:07x}{:024x}", t, race, 0);
|
||||
Server::CreateOrder(Sample(mine, "t@example.org"));
|
||||
++started;
|
||||
for (int i = 0; i < 80; ++i) {
|
||||
Server::AppendOrderStatus(mine, "paid",
|
||||
std::format("T{}-{}", t, i), "sepa");
|
||||
}
|
||||
});
|
||||
}
|
||||
writers.emplace_back([&] {
|
||||
while (started.load() < 2) std::this_thread::yield();
|
||||
std::this_thread::sleep_for(std::chrono::microseconds((race * 37) % 1500));
|
||||
AppendRaw(p, std::format(
|
||||
R"({{"type":"status","at":"X-{}","id":"{}","status":"shipped"}})",
|
||||
race, solo));
|
||||
});
|
||||
}
|
||||
const std::vector<Server::OrderRecord> kept = Server::ListOrders();
|
||||
Server::SetOrdersPath(p);
|
||||
if (!SameOrders(kept, Server::ListOrders())) ++stale;
|
||||
}
|
||||
Check(stale == 0,
|
||||
"threads: a line from outside amid this process's own appends is never missed",
|
||||
std::format("missed in {} of {} races", stale, kRaces));
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::error_code ec;
|
||||
// The suites run in parallel, so the scratch directory has to be unique
|
||||
|
|
@ -581,6 +1003,9 @@ int main() {
|
|||
ADonationIsIncomeNeverASale();
|
||||
OnlyAConfirmationMarksTheEmailSent();
|
||||
ATransitionKeepsWhatItDoesNotName();
|
||||
AReferenceNamesOneOrder();
|
||||
TheHeldFoldFollowsTheFile();
|
||||
AppendsFromManyWritersAllLand();
|
||||
|
||||
fs::remove_all(gWork, ec);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue