Add typed primitive life reservation adapter and reconciliation tests
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m0s
Foundation / checks (client) (push) Successful in 12m15s
Foundation / checks (network) (push) Successful in 13m46s
Foundation / checks (tsan) (push) Successful in 8m9s
Foundation / checks (checks) (push) Successful in 33m12s
Foundation / checks (postgres) (push) Successful in 46m13s
Foundation / postgres-tools (push) Successful in 48s
Foundation / image (push) Successful in 2m45s
Foundation / server-image (push) Successful in 4m27s

This commit is contained in:
dingo
2026-10-09 22:00:49 -04:00
parent 8568eff39d
commit c6df1f4e30
9 changed files with 291 additions and 1 deletions
+121
View File
@@ -0,0 +1,121 @@
// Real PostgreSQL schema v15 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);
};
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);
CHECK(failed(reserve(2), LifeReservationError::existing_life));
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, 2, 1, 20ms),
LifeReservationError::unknown));
CHECK(admin->query("COMMIT", {}, 5s));
db = connect_as(env("EVO_PG_RUNTIME_USER"));
const auto reconciled = reserve(2);
CHECK(reconciled && reconciled->identity == first->identity + 1);
CHECK(reserve(2) == *reconciled);
const auto counter = admin->query(
"SELECT next_identity FROM evolution.life_allocators WHERE world_id=9752", {}, 5s);
CHECK(counter && (*counter)[0][0] == "9223372036854775810");
CHECK(reject_primitive_life(*db, a, *lease_a, alice, 1, 2, LifeRefusal::no_placement, 5s) ==
LifeRefusal::no_placement);
CHECK(admin->query("UPDATE evolution.life_allocators SET next_identity=18446744073709551615 "
"WHERE world_id=9752",
{}, 5s));
CHECK(failed(reserve(3), LifeReservationError::exhausted));
const auto moved = claim_account_control(*db, b, *lease_b, alice, 2, 2, 20000ms, 5s);
CHECK(moved);
CHECK(reserve_primitive_life(*db, b, *lease_b, alice, moved->generation, request, 1, 5s) ==
*refused);
CHECK(failed(reject_primitive_life(*db, b, *lease_b, alice, moved->generation, request,
LifeRefusal::cancelled, 5s),
LifeReservationError::not_available));
CHECK(failed(reserve(request), LifeReservationError::fenced));
CHECK(release_region_lease(*db, a, *lease_a, 5s));
CHECK(release_region_lease(*db, b, *lease_b, 5s));
}
+11 -1
View File
@@ -3,8 +3,10 @@
#include <cstdlib>
#include <source_location>
#include <stdexcept>
#include <string_view>
void run_region_lease_store_tests();
void run_account_control_store_tests();
void run_life_reservation_store_tests();
void run_probe_handoff_store_tests();
void run_region_action_tests();
void run_region_checkpoint_store_tests();
@@ -118,12 +120,20 @@ void run() {
CHECK(!Postgres::connect(options, 1s));
}
} // namespace
int main() {
int main(int argc, char** argv) {
try {
if (argc == 2 && std::string_view(argv[1]) == "--life-reservations-only") {
run_life_reservation_store_tests();
std::puts("life reservation adapter integration passed");
return 0;
}
if (argc != 1)
return 2;
run();
std::puts("postgres adapter integration passed");
run_region_lease_store_tests();
run_account_control_store_tests();
run_life_reservation_store_tests();
run_probe_handoff_store_tests();
run_region_action_tests();
std::puts("region lease fencing integration passed");