Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 47s
Foundation / checks (tsan) (push) Canceled after 24s
Foundation / checks (ecs) (push) Canceled after 59s
139 lines
6.1 KiB
C++
139 lines
6.1 KiB
C++
#pragma once
|
|
// M10.8c test fixture (ADR-0096): a fake world database with the same probe transfer state
|
|
// machine as schema v12 (prepared -> committed or aborted; stage and commit happen together
|
|
// at the ticket login), serving one region's ProbeLedger, HandoffLedger and ControlLedger.
|
|
#include "evo/network/handoff_ticket.hpp"
|
|
#include "evo/network/session_control.hpp"
|
|
#include "evo/server/probe_ledger.hpp"
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
namespace evo::test {
|
|
using network::AccountId;
|
|
using network::ControlLease;
|
|
using network::ControlLedger;
|
|
using network::ControlLedgerError;
|
|
using network::HandoffAdmission;
|
|
using network::HandoffLedger;
|
|
using network::HandoffLedgerError;
|
|
using network::TicketClaims;
|
|
using network::TransferId;
|
|
struct WorldDb {
|
|
struct Probe {
|
|
std::uint64_t owner{}, generation{};
|
|
std::int64_t x{}, y{};
|
|
std::optional<TransferId> transfer;
|
|
};
|
|
enum class State : std::uint8_t { prepared, committed, aborted };
|
|
struct Transfer {
|
|
AccountId account;
|
|
std::uint64_t source{}, destination{}, generation{};
|
|
std::int64_t x{}, y{};
|
|
State state{};
|
|
};
|
|
std::map<AccountId, Probe> probes;
|
|
std::map<AccountId, std::uint64_t> life_owners;
|
|
std::map<TransferId, Transfer> transfers;
|
|
bool all_overdue{}; // overdue() reports every open transfer (a restarted source's view).
|
|
std::uint64_t overdue_reads{};
|
|
};
|
|
struct RegionDb final : server::ProbeLedger, HandoffLedger, ControlLedger {
|
|
RegionDb(WorldDb& w, std::uint64_t r) : world{&w}, region{r} {}
|
|
std::expected<server::ProbeHome, server::ProbeLedgerError>
|
|
open(const AccountId& account, std::int64_t x, std::int64_t y, std::int64_t) override {
|
|
auto [it, created] =
|
|
world->probes.try_emplace(account, WorldDb::Probe{region, 1, x, y, std::nullopt});
|
|
const auto& p = it->second;
|
|
return server::ProbeHome{p.owner, p.generation, p.x, p.y, p.transfer.has_value()};
|
|
}
|
|
void save(const AccountId& account, std::uint64_t generation, std::int64_t x, std::int64_t y,
|
|
std::int64_t) override {
|
|
auto& p = world->probes.at(account);
|
|
if (p.owner == region && p.generation == generation && !p.transfer) {
|
|
p.x = x;
|
|
p.y = y;
|
|
++saves;
|
|
}
|
|
}
|
|
std::expected<void, server::ProbeLedgerError>
|
|
prepare(const AccountId& account, std::uint64_t generation, const TransferId& transfer,
|
|
std::uint64_t destination, std::int64_t x, std::int64_t y, std::int64_t) override {
|
|
auto& p = world->probes.at(account);
|
|
if (p.owner != region || p.generation != generation || p.transfer)
|
|
return std::unexpected(server::ProbeLedgerError::unavailable);
|
|
p.transfer = transfer;
|
|
p.x = x;
|
|
p.y = y;
|
|
world->transfers[transfer] = {
|
|
account, region, destination, generation, x, y, WorldDb::State::prepared};
|
|
return {};
|
|
}
|
|
std::expected<bool, server::ProbeLedgerError> abort(const TransferId& transfer) override {
|
|
auto& t = world->transfers.at(transfer);
|
|
if (t.state == WorldDb::State::prepared) {
|
|
t.state = WorldDb::State::aborted;
|
|
world->probes.at(t.account).transfer.reset();
|
|
}
|
|
return t.state == WorldDb::State::aborted;
|
|
}
|
|
std::expected<std::vector<server::IncomingProbe>, server::ProbeLedgerError>
|
|
incoming() override {
|
|
std::vector<server::IncomingProbe> out;
|
|
for (const auto& [id, t] : world->transfers)
|
|
if (t.destination == region && t.state == WorldDb::State::prepared)
|
|
out.push_back({id, t.x, t.y});
|
|
return out;
|
|
}
|
|
std::expected<std::vector<TransferId>, server::ProbeLedgerError>
|
|
overdue(std::chrono::seconds) override {
|
|
++world->overdue_reads;
|
|
std::vector<TransferId> out;
|
|
for (const auto& [id, t] : world->transfers)
|
|
if (world->all_overdue && t.source == region && t.state == WorldDb::State::prepared)
|
|
out.push_back(id);
|
|
return out;
|
|
}
|
|
std::expected<HandoffAdmission, HandoffLedgerError> admit(const TicketClaims& ticket,
|
|
std::uint64_t) override {
|
|
const auto found = world->transfers.find(ticket.transfer);
|
|
if (found == world->transfers.end() || found->second.destination != region ||
|
|
found->second.account != ticket.account ||
|
|
found->second.state == WorldDb::State::aborted)
|
|
return std::unexpected(HandoffLedgerError::refused);
|
|
auto& t = found->second;
|
|
auto& p = world->probes.at(t.account);
|
|
if (t.state == WorldDb::State::prepared) { // Stage and commit: the switch.
|
|
t.state = WorldDb::State::committed;
|
|
p = {region, t.generation + 1, t.x, t.y, std::nullopt};
|
|
}
|
|
return HandoffAdmission{ControlLease{++control, {}, std::chrono::milliseconds{20000}},
|
|
p.generation, p.x, p.y};
|
|
}
|
|
std::expected<ControlLease, ControlLedgerError> claim(const AccountId&, std::uint64_t,
|
|
std::uint64_t) override {
|
|
++claims;
|
|
return ControlLease{++control, {}, std::chrono::milliseconds{20000}};
|
|
}
|
|
std::expected<std::optional<std::uint64_t>, ControlLedgerError>
|
|
primitive_life_owner(const AccountId& account) override {
|
|
const auto found = world->life_owners.find(account);
|
|
return found == world->life_owners.end() ? std::optional<std::uint64_t>{}
|
|
: std::optional{found->second};
|
|
}
|
|
std::expected<std::chrono::milliseconds, ControlLedgerError> renew(const AccountId&,
|
|
std::uint64_t) override {
|
|
return std::chrono::milliseconds{20000};
|
|
}
|
|
void release(const AccountId&, std::uint64_t) override {
|
|
++releases;
|
|
}
|
|
WorldDb* world;
|
|
std::uint64_t region;
|
|
std::uint64_t control{}, claims{}, saves{}, releases{};
|
|
};
|
|
} // namespace evo::test
|