Files
aevolution/tests/life_reservation_store_tests.cpp
dingoandClaude Opus 5.5 1446d36d26
Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (deploy) (push) Successful in 4m1s
Foundation / checks (tsan) (push) Successful in 9m49s
Foundation / checks (client) (push) Successful in 19m23s
Foundation / checks (network) (push) Successful in 19m31s
Foundation / checks (checks) (push) Successful in 42m41s
Foundation / checks (postgres) (push) Successful in 45m36s
Foundation / postgres-tools (push) Successful in 28s
Foundation / server-image (push) Successful in 3m15s
Foundation / image (push) Successful in 3m18s
Decline a mutation offer (ADR-0120 amendment, schema v40)
An offer the cell cannot afford stayed open, and only one offer is issued at a
time, so banked feeding credit never became a new offer. A v10 choice naming
operator 0 now declines the exact outstanding offer. It is reserved and published
like any choice: schema v40 accepts operator 0 in the reserved input and trace
selections and stores result 3, declined. The owner consumes the offer and
changes nothing else, and the client reports it as committed. Old traces never
carry operator 0, so their replay is unchanged. The Evolution panel gets a
"Decline this offer" button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:43:36 -04:00

197 lines
10 KiB
C++

// Real PostgreSQL schema v17 adapter checks, isolated world 9752.
#include "evo/persistence/account_control_store.hpp"
#include "evo/persistence/life_reservation_store.hpp"
#include "evo/persistence/region_lease_store.hpp"
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
void run_life_reservation_store_tests();
namespace {
using namespace evo::persistence;
using namespace std::chrono_literals;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("life reservation store test line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
std::string env(const char* name) {
const auto* value = std::getenv(name);
if (!value)
throw std::runtime_error("missing PostgreSQL test environment");
return value;
}
std::unique_ptr<Postgres> connect_as(const std::string& user) {
auto db =
Postgres::connect({env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), user, {}, {}}, 5s);
CHECK(db);
return std::move(*db);
}
bool failed(const auto& result, LifeReservationError kind) {
return !result && result.error().kind == kind;
}
} // namespace
void run_life_reservation_store_tests() {
auto admin = connect_as(env("PGUSER"));
auto db = connect_as(env("EVO_PG_RUNTIME_USER"));
CHECK(admin->query("INSERT INTO evolution.retained_builds(build_identity) VALUES "
"(decode(repeat('d6',32),'hex')) ON CONFLICT DO NOTHING",
{}, 5s));
CHECK(admin->query("INSERT INTO evolution.region_leases(world_id,region_id,frame_id) "
"VALUES (9752,1,1),(9752,2,1)",
{}, 5s));
Sha256Digest build{};
build.fill(0xD6);
const RegionKey a{9752, 1}, b{9752, 2};
const auto lease_a = acquire_region_lease(*db, a, build, 20000ms, 5s);
const auto lease_b = acquire_region_lease(*db, b, build, 20000ms, 5s);
CHECK(lease_a && lease_b);
Uuid alice{};
alice.fill(0x61);
const auto control = claim_account_control(*db, a, *lease_a, alice, 1, 1, 20000ms, 5s);
CHECK(control);
const auto request = std::numeric_limits<std::uint64_t>::max();
auto reserve = [&](std::uint64_t action, std::uint32_t rules = 1) {
return reserve_primitive_life(*db, a, *lease_a, alice, control->generation, action, rules,
5s);
};
const auto absent = query_primitive_life(*db, a, *lease_a, alice, control->generation, 77, 5s);
CHECK(absent && !*absent);
Uuid bob{};
bob.fill(0x62);
const auto no_owner = primitive_life_owner(*db, a, *lease_a, bob, 5s);
CHECK(no_owner && !*no_owner);
CHECK(
failed(primitive_life_owner(*db, a, *lease_a, Uuid{}, 5s), LifeReservationError::invalid));
CHECK(failed(reserve(request), LifeReservationError::not_available));
CHECK(admin->query("INSERT INTO evolution.life_allocators VALUES "
"(9752,9223372036854775808)",
{}, 5s));
CHECK(failed(reserve(0), LifeReservationError::invalid));
CHECK(failed(reserve(request, 0), LifeReservationError::invalid));
CHECK(failed(reserve(request, std::numeric_limits<std::uint32_t>::max()),
LifeReservationError::invalid));
const auto first = reserve(request);
CHECK(first && first->identity == 9223372036854775808ULL && first->region == 1 &&
first->refusal == LifeRefusal::none);
const auto owner_here = primitive_life_owner(*db, a, *lease_a, alice, 5s);
const auto owner_from_other_region = primitive_life_owner(*db, b, *lease_b, alice, 5s);
CHECK(owner_here && *owner_here == 1);
CHECK(owner_from_other_region && *owner_from_other_region == 1);
db = connect_as(env("EVO_PG_RUNTIME_USER"));
CHECK(reserve(request) == *first);
const auto conflict = reserve(2);
CHECK(conflict && conflict->refusal == LifeRefusal::existing_life && conflict->region == 1 &&
conflict->identity == first->identity + 1);
CHECK(reserve(2) == *conflict);
auto conflict_result =
query_primitive_life(*db, a, *lease_a, alice, control->generation, 2, 5s);
const auto conflict_outcome = conflict_result.value_or(std::nullopt);
const auto conflict_value = conflict_outcome.value_or(PrimitiveLifeOutcome{});
CHECK(conflict_result.has_value() && conflict_outcome.has_value() &&
conflict_value.reservation.refusal == LifeRefusal::existing_life &&
!conflict_value.published);
CHECK(failed(reserve_primitive_life(*db, a, *lease_a, alice, 2, request, 1, 5s),
LifeReservationError::fenced));
CHECK(failed(reject_primitive_life(*db, a, *lease_a, alice, 1, request, LifeRefusal::none, 5s),
LifeReservationError::invalid));
CHECK(reject_primitive_life(*db, a, *lease_a, alice, 1, request,
LifeRefusal::insufficient_funding,
5s) == LifeRefusal::insufficient_funding);
CHECK(reject_primitive_life(*db, a, *lease_a, alice, 1, request, LifeRefusal::cancelled, 5s) ==
LifeRefusal::insufficient_funding);
const auto refused = reserve(request);
CHECK(refused && refused->identity == first->identity &&
refused->refusal == LifeRefusal::insufficient_funding);
// Force an uncertain mutation response. Reconcile with the same request on
// a new connection, whether the interrupted statement committed or not.
CHECK(admin->query("BEGIN", {}, 5s));
CHECK(admin->query("SELECT * FROM evolution.life_allocators WHERE world_id=9752 FOR UPDATE", {},
5s));
CHECK(failed(reserve_primitive_life(*db, a, *lease_a, alice, 1, 3, 1, 20ms),
LifeReservationError::unknown));
CHECK(admin->query("COMMIT", {}, 5s));
db = connect_as(env("EVO_PG_RUNTIME_USER"));
const auto reconciled = reserve(3);
CHECK(reconciled && reconciled->identity == first->identity + 2);
CHECK(reserve(3) == *reconciled);
const auto counter = admin->query(
"SELECT next_identity FROM evolution.life_allocators WHERE world_id=9752", {}, 5s);
CHECK(counter && (*counter)[0][0] == "9223372036854775811");
std::array<std::uint8_t, cellular_mutation_reservation_input_bytes> mutation_input{};
auto put_u64 = [&](std::size_t offset, std::uint64_t value) {
for (std::size_t i = 0; i < 8; ++i)
mutation_input[offset + i] = static_cast<std::uint8_t>(value >> (i * 8));
};
put_u64(0, 20); // target tick
put_u64(8, first->identity);
put_u64(16, 1); // exact offer event
put_u64(24, 10); // offer tick
mutation_input[32] = 1; // operator id
const auto mutation =
reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, mutation_input, 5s);
CHECK(mutation && mutation->region == a.region && mutation->target_tick == 20 &&
mutation->successor_identity == 9223372036854775811ULL &&
mutation->input == mutation_input);
db = connect_as(env("EVO_PG_RUNTIME_USER"));
CHECK(reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, mutation_input, 5s) ==
*mutation);
auto changed_mutation_input = mutation_input;
changed_mutation_input[32] = 2;
CHECK(failed(
reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 500, changed_mutation_input, 5s),
LifeReservationError::identity_conflict));
CHECK(failed(reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 501, mutation_input, 5s),
LifeReservationError::pending_mutation));
const auto pending_mutations = load_pending_cellular_mutations(*db, a, *lease_a, 5s);
CHECK(pending_mutations && pending_mutations->size() == 1 &&
pending_mutations->front() == *mutation);
auto invalid_mutation_input = mutation_input;
std::fill_n(invalid_mutation_input.begin() + 24, 8, std::uint8_t{0}); // No offer tick.
CHECK(failed(
reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 502, invalid_mutation_input, 5s),
LifeReservationError::invalid));
// Schema v40 (ADR-0120 amendment): operator 0 is a decline, a valid input; it is
// refused here only because the account already has a pending choice.
auto decline_input = mutation_input;
decline_input[32] = 0;
decline_input[33] = 0;
CHECK(failed(reserve_cellular_mutation(*db, a, *lease_a, alice, 1, 503, decline_input, 5s),
LifeReservationError::pending_mutation));
CHECK(reject_primitive_life(*db, a, *lease_a, alice, 1, 3, LifeRefusal::no_placement, 5s) ==
LifeRefusal::no_placement);
const auto pending = reserve(4);
CHECK(pending && pending->refusal == LifeRefusal::none);
const auto pending_owner = primitive_life_owner(*db, a, *lease_a, alice, 5s);
CHECK(pending_owner && *pending_owner == 1);
const auto moved = claim_account_control(*db, b, *lease_b, alice, 2, 2, 20000ms, 5s);
CHECK(moved);
auto moved_result = query_primitive_life(*db, b, *lease_b, alice, moved->generation, 4, 5s);
const auto moved_outcome = moved_result.value_or(std::nullopt);
const auto moved_value = moved_outcome.value_or(PrimitiveLifeOutcome{});
CHECK(moved_result.has_value() && moved_outcome.has_value() &&
moved_value.reservation.region == 1 &&
moved_value.reservation.refusal == LifeRefusal::cancelled);
const auto cancelled_retry =
reserve_primitive_life(*db, b, *lease_b, alice, moved->generation, 4, 1, 5s);
CHECK(cancelled_retry && cancelled_retry->refusal == LifeRefusal::cancelled);
const auto cancelled_owner = primitive_life_owner(*db, b, *lease_b, alice, 5s);
CHECK(cancelled_owner && !*cancelled_owner);
CHECK(admin->query("UPDATE evolution.life_allocators SET next_identity=18446744073709551615 "
"WHERE world_id=9752",
{}, 5s));
CHECK(failed(reserve_primitive_life(*db, b, *lease_b, alice, moved->generation, 5, 1, 5s),
LifeReservationError::exhausted));
CHECK(reserve_primitive_life(*db, b, *lease_b, alice, moved->generation, request, 1, 5s) ==
*refused);
CHECK(failed(reserve(request), LifeReservationError::fenced));
CHECK(release_region_lease(*db, a, *lease_a, 5s));
CHECK(release_region_lease(*db, b, *lease_b, 5s));
}