Files
aevolution/tests/life_reservation_store_tests.cpp
T
dingo 94edeba474
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 1m58s
Foundation / checks (tsan) (push) Successful in 8m4s
Foundation / checks (postgres) (push) Failing after 21m34s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 24m49s
Fix optional access in reservation tests
2026-10-10 16:28:27 -04:00

142 lines
7.2 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 <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);
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);
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");
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 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);
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));
}