Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 40s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 53s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m1s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 3m53s
156 lines
8.0 KiB
C++
156 lines
8.0 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);
|
|
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");
|
|
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));
|
|
}
|