Files
aevolution/tests/network_session.cpp
T
dingoandClaude Opus 5.5 897055883a Add M9.8c launcher, legend, labelled inspector and click selection
Toolkit-independent launcher, selection, label and outcome-log models with
bounded inputs and fixed error text; Dear ImGui overlay in the native adapter
with toolkit-first event routing and capture; engineering panels for fixture vs
dev region, legend, inspector, session outcomes, notices and dialog. Additive
SessionClient::unreachable_attempts() bounds unreachable connects. Docs, CI
targets and validation evidence updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 10:09:11 -04:00

732 lines
31 KiB
C++

#include "evo/network/grant_ledger.hpp"
#include "evo/network/session_client.hpp"
#include "evo/network/session_server.hpp"
#include "evo/network/tcp.hpp"
#include "network_pki.hpp"
#include "network_test.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <functional>
#include <limits>
#include <string>
#include <thread>
#include <vector>
namespace evo::network_test {
namespace {
using namespace evo::network;
using Clock = std::chrono::steady_clock;
constexpr world::WorldIdentity identity{11, 12};
constexpr std::uint64_t region = 21;
constexpr std::uint64_t now_seconds = 1'000'000;
DevGrantKey grant_key(std::uint8_t fill = 0x33) {
std::array<std::uint8_t, dev_key_bytes> secret{};
secret.fill(fill);
auto k = DevGrantKey::import(DeploymentEnvironment::dev, secret);
CHECK(k);
return std::move(*k);
}
std::vector<DevGrant> grants(std::uint64_t subject, std::size_t count, std::uint64_t first_id,
std::uint8_t fill = 0x33, std::uint64_t grant_region = region) {
const auto k = grant_key(fill);
std::vector<DevGrant> out;
for (std::size_t i = 0; i < count; ++i) {
const auto g = k.issue(
{first_id + i, subject, identity, grant_region, now_seconds - 60, now_seconds + 1800});
CHECK(g);
out.push_back(*g);
}
return out;
}
client::EnvironmentPresentation environment(std::uint64_t tick) {
client::EnvironmentCell c;
c.voxel = {0, 0, -16000};
c.chunk_revision = 1;
c.habitat = {{0}, {0}, {0}, {10}, {20}, {3}};
c.volume = {600};
c.terrain_material = {1};
c.presence[0] = client::FieldPresence::present;
c.quantity[0] = tick; // Visible change per tick.
c.moisture_status = world::MoistureStatus::zero_capacity;
auto v = client::EnvironmentPresentation::create({identity, tick, tick}, 1, -16000, {&c, 1});
CHECK(v);
return std::move(*v);
}
client::OrganismPresentation no_organisms(std::uint64_t tick) {
auto v = client::OrganismPresentation::create({identity, tick, 0}, -16000, {});
CHECK(v);
return std::move(*v);
}
std::filesystem::path fresh_ledger() {
static std::atomic<unsigned> counter{0};
const auto stamp = Clock::now().time_since_epoch().count();
return std::filesystem::temp_directory_path() /
("evo-grant-ledger-" + std::to_string(stamp) + "-" + std::to_string(++counter));
}
struct Harness {
explicit Harness(std::size_t max_sessions = 4,
std::chrono::milliseconds hello = std::chrono::milliseconds{3000},
std::function<void(SessionServerConfig&)> server_tweak = {},
std::function<void(SessionClientConfig&)> client_tweak = {})
: tweak_server{std::move(server_tweak)}, tweak_client{std::move(client_tweak)} {
server = make_server(0, max_sessions, hello);
port = server->port();
}
~Harness() {
server.reset();
std::error_code ignored;
std::filesystem::remove(ledger, ignored);
std::filesystem::remove(std::filesystem::path{ledger}.concat(".lock"), ignored);
}
Harness(const Harness&) = delete;
Harness& operator=(const Harness&) = delete;
std::unique_ptr<SessionServer> make_server(std::uint16_t at, std::size_t max_sessions,
std::chrono::milliseconds hello) {
SessionServerConfig c;
c.listen = "127.0.0.1:" + std::to_string(at);
c.credentials = pki.server;
c.admission.world = identity;
c.admission.region = region;
c.admission.max_sessions = max_sessions;
c.admission.intent_window = 8;
c.admission.window_ticks = 8;
c.admission.max_intents_per_window = 8;
c.hello_deadline = hello;
c.grant_ledger = ledger; // Shared by restarts of this harness's server.
if (tweak_server)
tweak_server(c);
auto s = SessionServer::create(std::move(c), grant_key(), now_seconds);
CHECK(s);
return std::move(*s);
}
std::unique_ptr<SessionClient> client(std::vector<DevGrant> g) {
SessionClientConfig c;
c.host = "127.0.0.1";
c.port = port;
c.trust = {pki.authority, server_name};
c.grants = std::move(g);
c.first_retry = std::chrono::milliseconds{50};
c.max_retry = std::chrono::milliseconds{200};
c.attempt_deadline = std::chrono::milliseconds{3000};
if (tweak_client)
tweak_client(c);
auto s = SessionClient::create(std::move(c));
CHECK(s);
clients.push_back(s->get());
return std::move(*s);
}
void tick() {
++facts.simulated_tick;
if (server)
server->publish(environment(facts.simulated_tick), no_organisms(facts.simulated_tick),
facts);
}
// Pumps everything until `done` or the wall-clock limit.
bool until(const std::function<bool()>& done,
std::chrono::milliseconds limit = std::chrono::milliseconds{5000}) {
const auto end = Clock::now() + limit;
while (Clock::now() < end) {
const auto now = Clock::now();
if (server)
server->poll(facts, now_seconds, now);
for (auto* c : clients)
c->poll(now);
if (done())
return true;
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
return false;
}
void collect(SessionClient& c, std::vector<ClientEvent>& into) {
auto e = c.take_events();
into.insert(into.end(), e.begin(), e.end());
}
std::function<void(SessionServerConfig&)> tweak_server;
std::function<void(SessionClientConfig&)> tweak_client;
std::filesystem::path ledger = fresh_ledger();
Pki pki;
RegionFacts facts{true, 1, 100, 90, 5};
std::unique_ptr<SessionServer> server;
std::uint16_t port{};
std::vector<SessionClient*> clients;
};
bool has(const std::vector<ClientEvent>& events, ClientEvent::Kind kind) {
return std::ranges::any_of(events, [&](const ClientEvent& e) { return e.kind == kind; });
}
const ClientEvent* last(const std::vector<ClientEvent>& events, ClientEvent::Kind kind) {
const auto it = std::ranges::find_if(events.rbegin(), events.rend(),
[&](const ClientEvent& e) { return e.kind == kind; });
return it == events.rend() ? nullptr : &*it;
}
void connect_view_intents_and_outcomes() {
Harness h;
h.tick();
auto c = h.client(grants(7, 4, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] {
h.collect(*c, events);
return c->latest_view().has_value();
}));
const auto w = c->welcome();
CHECK(w && w->session == 1 && w->epoch == 1 && w->world == 11 && w->frame == 12 &&
w->region == region && w->commit_watermark == 90 && w->server_tick == 101);
CHECK(c->grants_left() == 3 && h.server->sessions() == 1);
CHECK(c->environment().freshness() == client::Freshness::current);
CHECK(c->environment().current()->cells()[0].quantity[0] == 101);
CHECK(must(c->latest_view()).commit_watermark == 90 &&
must(c->latest_view()).server_tick == 101);
CHECK(h.until([&] { return c->organisms().current() != nullptr; }));
CHECK(c->organisms().current()->cells().empty()); // Explicitly no organisms.
// Views follow the region: a new tick replaces the presentation.
h.tick();
CHECK(h.until([&] { return must(c->latest_view()).server_tick == 102; }));
CHECK(c->environment().current()->cells()[0].quantity[0] == 102);
// Admitted intention: carried by a host input, then durably committed.
CHECK(c->send({1, 0, 1000, 0}));
CHECK(h.until([&] {
h.collect(*c, events);
return last(events, ClientEvent::Kind::result) != nullptr;
}));
CHECK(last(events, ClientEvent::Kind::result)->result.status == IntentStatus::admitted);
CHECK(!h.server->carry(105)); // Intended tick 102 + 4 = 106.
const auto request = h.server->carry(106);
// 'I','N' + 48-bit epoch (1) + 64-bit server ordinal (first carrier: 1).
CHECK(must(request)[0] == 'I' && must(request)[1] == 'N' && must(request)[7] == 1 &&
must(request)[15] == 1);
CHECK(!h.server->carry(200)); // Carried once.
h.server->committed(must(request), 106, 6);
CHECK(h.until([&] {
h.collect(*c, events);
return last(events, ClientEvent::Kind::outcome) != nullptr;
}));
const auto* outcome = last(events, ClientEvent::Kind::outcome);
CHECK(outcome->outcome.sequence == 1 &&
outcome->outcome.finality == IntentFinality::committed_no_effect &&
outcome->outcome.tick == 106 && outcome->outcome.commit_sequence == 6);
// Rejected: replay and an out-of-bound vector never reach the host.
events.clear();
CHECK(c->send({1, 0, 0, 1000}) && c->send({2, 0, 1000, 1000}));
CHECK(h.until([&] {
h.collect(*c, events);
return std::ranges::count_if(events, [](const ClientEvent& e) {
return e.kind == ClientEvent::Kind::result;
}) == 2;
}));
CHECK(events[0].result.status == IntentStatus::replayed &&
events[1].result.status == IntentStatus::invalid_vector);
CHECK(!h.server->carry(1000));
// A dropped host input is reported as not committed.
events.clear();
CHECK(c->send({3, 0, -707, 707}));
CHECK(h.until([&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::result);
}));
CHECK(last(events, ClientEvent::Kind::result)->result.status == IntentStatus::admitted);
const auto dropped = h.server->carry(1000);
CHECK(dropped);
h.server->not_committed(must(dropped));
CHECK(h.until([&] {
h.collect(*c, events);
return last(events, ClientEvent::Kind::outcome) != nullptr;
}));
CHECK(last(events, ClientEvent::Kind::outcome)->outcome.finality ==
IntentFinality::not_committed);
}
void duplicate_capacity_and_authentication() {
Harness h{2};
h.tick();
auto first = h.client(grants(7, 2, 100));
CHECK(h.until([&] { return first->phase() == SessionPhase::established; }));
// Same subject, fresh grant: refused; control is never duplicated.
auto twin = h.client(grants(7, 1, 200));
std::vector<ClientEvent> twin_events;
CHECK(h.until([&] {
h.collect(*twin, twin_events);
return has(twin_events, ClientEvent::Kind::closed);
}));
CHECK(last(twin_events, ClientEvent::Kind::closed)->reason == CloseReason::duplicate_session);
CHECK(first->phase() == SessionPhase::established && h.server->sessions() == 1);
// One grant only: after the refusal, reconnection stops explicitly.
CHECK(h.until([&] {
h.collect(*twin, twin_events);
return twin->phase() == SessionPhase::exhausted;
}));
CHECK(has(twin_events, ClientEvent::Kind::exhausted));
// A reused grant is refused even for a different connection.
auto reuse = h.client(grants(7, 1, 100)); // Grant id 100 was used by `first`.
std::vector<ClientEvent> reuse_events;
CHECK(h.until([&] {
h.collect(*reuse, reuse_events);
return has(reuse_events, ClientEvent::Kind::closed);
}));
CHECK(last(reuse_events, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
// Foreign key and foreign region are unauthenticated.
std::vector<std::unique_ptr<SessionClient>> others; // Kept alive: the harness polls them.
for (auto g : {grants(8, 1, 300, 0x44), grants(9, 1, 301, 0x33, region + 1)}) {
others.push_back(h.client(std::move(g)));
auto* other = others.back().get();
std::vector<ClientEvent> e;
CHECK(h.until([&] {
h.collect(*other, e);
return has(e, ClientEvent::Kind::closed);
}));
CHECK(last(e, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
}
// Capacity: a second subject fits, a third is refused with `capacity`.
auto second = h.client(grants(10, 1, 400));
CHECK(h.until([&] { return second->phase() == SessionPhase::established; }));
auto third = h.client(grants(11, 1, 500));
std::vector<ClientEvent> third_events;
CHECK(h.until([&] {
h.collect(*third, third_events);
return has(third_events, ClientEvent::Kind::closed);
}));
CHECK(last(third_events, ClientEvent::Kind::closed)->reason == CloseReason::capacity);
}
void stale_epoch_restart_and_reconnect() {
Harness h;
h.tick();
auto c = h.client(grants(7, 6, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] { return c->latest_view().has_value(); }));
CHECK(c->send({1, 0, 1000, 0}));
CHECK(h.until([&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::result);
}));
// New ownership epoch: the session is closed as stale, its pending intention's
// outcome is unknown, and the presentation is kept but marked stale.
h.facts.epoch = 2;
CHECK(h.until([&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::closed);
}));
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::stale_epoch);
CHECK(has(events, ClientEvent::Kind::unknown));
CHECK(c->environment().freshness() == client::Freshness::stale);
CHECK(!h.server->carry(1000)); // Old-epoch intentions never ride new inputs.
// Reconnect with a fresh grant: new session, new epoch, fresh baseline.
h.tick();
CHECK(h.until([&] {
h.collect(*c, events);
return c->phase() == SessionPhase::established && c->latest_view().has_value();
}));
CHECK(must(c->welcome()).epoch == 2 && must(c->welcome()).session == 2 &&
c->grants_left() == 4);
CHECK(c->environment().freshness() == client::Freshness::current);
CHECK(must(c->latest_view()).epoch == 2);
// Server process restart: the listener disappears, then a new one takes the port
// with a new epoch. The client reconnects without the old session's state.
events.clear();
h.server.reset();
CHECK(h.until([&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::lost);
}));
CHECK(c->phase() != SessionPhase::established &&
c->environment().freshness() == client::Freshness::stale);
h.facts = {true, 3, 40, 40, 2}; // Recovered at the committed checkpoint: tick went back.
h.server = h.make_server(h.port, 4, std::chrono::milliseconds{3000});
h.tick();
CHECK(h.until([&] {
h.collect(*c, events);
return c->phase() == SessionPhase::established && c->latest_view().has_value();
}));
CHECK(must(c->welcome()).epoch == 3 && must(c->welcome()).session == 1);
// The layer was reset for the new session, so the earlier tick does not regress it.
CHECK(must(c->latest_view()).server_tick == 41 &&
c->environment().current()->revision().tick == 41);
}
void abuse() {
// Not ready yet: Hello is refused with server_shutdown and retried later.
Harness h;
h.facts.ready = false;
h.facts.epoch = 0;
auto c = h.client(grants(7, 3, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::closed);
}));
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::server_shutdown);
// Refused before admission: the grant was never recorded, so it is not spent.
CHECK(c->grants_left() == 3);
h.facts = {true, 1, 100, 90, 5};
h.tick();
CHECK(h.until([&] { return c->phase() == SessionPhase::established; }));
CHECK(c->grants_left() == 2);
// The client never buffers without bound: with the server not answering, at most
// max_outstanding intentions may be unresolved; further sends are refused.
CHECK(h.until([&] { return c->latest_view().has_value(); }));
std::size_t accepted = 0;
for (std::uint64_t s = 10; s < 2000; ++s)
accepted += c->send({s, 0, 0, 0}) ? 1 : 0;
CHECK(accepted == SessionClient::max_outstanding);
// Flooding: a raw TLS peer (no client-side bounds) exceeds the byte-rate limit after
// a valid Hello; the server closes it. Check the server's decision, since the flooding
// peer may see a reset before it could read the Close frame.
(void)h.server->take_events();
auto raw = TcpStream::connect("127.0.0.1", h.port);
CHECK(raw);
auto tls = TlsChannel::client({h.pki.authority, server_name}, **raw);
CHECK(tls);
const auto flood_grant = grants(9, 1, 900).front();
const auto hello = encode(Hello{0, {flood_grant.begin(), flood_grant.end()}});
CHECK(hello);
bool flooded = false;
std::vector<SessionEvent> server_events;
CHECK(h.until([&] {
auto e = h.server->take_events();
server_events.insert(server_events.end(), e.begin(), e.end());
if (const auto p = (*tls)->handshake(); p && *p == TlsProgress::done && !flooded) {
flooded = hello && (*tls)->queue(*hello).has_value();
const auto beat = encode_heartbeat();
for (int i = 0; i < 7000 && flooded; ++i)
flooded = (*tls)->queue(beat).has_value();
}
if (flooded)
static_cast<void>((*tls)->flush().has_value()); // Best effort: may be reset.
return std::ranges::any_of(server_events, [](const SessionEvent& x) {
return x.kind == SessionEvent::Kind::closed && x.reason == CloseReason::rate_limited;
});
}));
// Plaintext and silent connections are dropped without a session: the server
// closes the socket (plaintext at the TLS handshake, silence at the deadline).
Harness quiet{4, std::chrono::milliseconds{200}};
const auto dropped_by_server = [&](TcpStream& s, std::span<const std::uint8_t> bytes) {
std::size_t sent = 0;
return quiet.until([&] {
if (sent < bytes.size())
if (const auto n = s.write(bytes.subspan(sent)))
sent += *n;
std::array<std::uint8_t, 64> sink{};
const auto r = s.read(sink);
return !r.has_value();
});
};
auto plain = TcpStream::connect("127.0.0.1", quiet.port);
CHECK(plain);
const auto bytes = encode(IntentMessage{1, 1, 1, 1, 0, 0});
CHECK(dropped_by_server(**plain, bytes));
auto silent = TcpStream::connect("127.0.0.1", quiet.port);
CHECK(silent);
const auto start = Clock::now();
CHECK(dropped_by_server(**silent, {}));
CHECK(Clock::now() - start >= std::chrono::milliseconds{150}); // Held until the deadline.
CHECK(quiet.until([&] { return quiet.server->connections() == 0; }));
CHECK(quiet.server->sessions() == 0);
}
// Review M9.4-M9.7 #2: client sequences 2^32 apart must get distinct carriers and
// each final outcome must reach its own sequence.
void carriers_never_alias() {
Harness h;
h.tick();
auto c = h.client(grants(7, 2, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] { return c->latest_view().has_value(); }));
constexpr std::uint64_t far = (std::uint64_t{1} << 32) + 1;
CHECK(c->send({1, 0, 1000, 0}) && c->send({far, 0, 0, 1000}));
CHECK(h.until([&] {
h.collect(*c, events);
return std::ranges::count_if(events, [](const ClientEvent& e) {
return e.kind == ClientEvent::Kind::result &&
e.result.status == IntentStatus::admitted;
}) == 2;
}));
const auto first = h.server->carry(1000);
const auto second = h.server->carry(1000);
CHECK(must(first) != must(second));
CHECK(must(first)[0] == 'I' && must(first)[1] == 'N' && must(first)[7] == 1); // Epoch 1.
h.server->not_committed(must(second));
h.server->committed(must(first), 106, 6);
CHECK(h.until([&] {
h.collect(*c, events);
return std::ranges::count_if(events, [](const ClientEvent& e) {
return e.kind == ClientEvent::Kind::outcome;
}) == 2;
}));
for (const auto& e : events)
if (e.kind == ClientEvent::Kind::outcome)
CHECK((e.sequence == far && e.outcome.finality == IntentFinality::not_committed) ||
(e.sequence == 1 && e.outcome.finality == IntentFinality::committed_no_effect &&
e.outcome.commit_sequence == 6));
}
// Review M9.4-M9.7 #3: a grant consumed before a restart stays consumed after it.
void grants_stay_consumed_across_restarts() {
Harness h;
h.tick();
auto first = h.client(grants(7, 1, 100));
CHECK(h.until([&] { return first->phase() == SessionPhase::established; }));
h.clients.clear();
first.reset();
h.server.reset(); // Process restart: only the ledger file survives.
h.facts = {true, 2, 40, 40, 2};
h.server = h.make_server(h.port, 4, std::chrono::milliseconds{3000});
h.tick();
auto replay = h.client(grants(7, 1, 100)); // The exact consumed grant.
std::vector<ClientEvent> events;
CHECK(h.until([&] {
h.collect(*replay, events);
return has(events, ClientEvent::Kind::closed);
}));
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
auto fresh = h.client(grants(7, 1, 101));
CHECK(h.until([&] { return fresh->phase() == SessionPhase::established; }));
CHECK(must(fresh->welcome()).epoch == 2);
// The ledger fails closed on corruption and drops expired entries when opened.
const auto path = fresh_ledger();
{
std::ofstream out{path, std::ios::binary};
out << "not a ledger line\n";
}
SessionServerConfig bad;
bad.listen = "127.0.0.1:0";
bad.credentials = h.pki.server;
bad.admission.world = identity;
bad.admission.region = region;
bad.grant_ledger = path;
CHECK(
refused(SessionServer::create(bad, grant_key(), now_seconds), SessionServerError::ledger));
{
std::ofstream out{path, std::ios::binary | std::ios::trunc}; // LF only.
out << "0000000000000005 " << now_seconds - 1 << "\n"
<< "0000000000000006 " << now_seconds + 10 << "\n";
}
{
const auto ledger = GrantLedger::open(path, now_seconds);
CHECK(ledger && (*ledger)->loaded().size() == 1 && (*ledger)->loaded()[0].grant_id == 6);
CHECK(ledger && (*ledger)->record({7, now_seconds + 20}));
}
const auto reopened = GrantLedger::open(path, now_seconds);
CHECK(reopened && (*reopened)->loaded().size() == 2);
std::error_code ignored;
std::filesystem::remove(path, ignored);
}
// Follow-up review: a duplicate startup must not replace an active owner's ledger.
void ledger_exclusive_owner() {
Harness h;
h.tick();
SessionServerConfig duplicate;
duplicate.credentials = h.pki.server;
duplicate.admission.world = identity;
duplicate.admission.region = region;
duplicate.grant_ledger = h.ledger;
for (const auto port : {h.port, std::uint16_t{0}}) {
duplicate.listen = "127.0.0.1:" + std::to_string(port);
CHECK(refused(SessionServer::create(duplicate, grant_key(), now_seconds),
SessionServerError::ledger));
}
CHECK(refused(GrantLedger::open(h.ledger, now_seconds), LedgerError::busy));
// Written AFTER the failed duplicate starts, then recovered through real TLS.
auto first = h.client(grants(7, 1, 100));
CHECK(h.until([&] { return first->phase() == SessionPhase::established; }));
h.clients.clear();
first.reset();
h.server.reset();
h.facts.epoch = 2;
h.server = h.make_server(h.port, 4, std::chrono::milliseconds{3000});
h.tick();
auto replay = h.client(grants(7, 1, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] {
h.collect(*replay, events);
return has(events, ClientEvent::Kind::closed);
}));
CHECK(last(events, ClientEvent::Kind::closed)->reason == CloseReason::unauthenticated);
}
void ledger_incomplete_records() {
const auto path = fresh_ledger();
const std::string prefix = "0000000000000002 1000\n";
const std::string record = "0000000000000001 1000\n";
const auto reject_unchanged = [&](const std::string& bytes) {
{
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << bytes;
CHECK(out.good());
}
CHECK(refused(GrantLedger::open(path, 100), LedgerError::corrupt));
std::ifstream in{path, std::ios::binary};
const std::string after{std::istreambuf_iterator<char>{in}, {}};
CHECK(after == bytes);
CHECK(!std::filesystem::exists(std::filesystem::path{path}.concat(".tmp")));
};
// All nonempty partial tails, including a plausible but expired decimal prefix.
for (std::size_t length = 1; length < record.size(); ++length)
reject_unchanged(prefix + record.substr(0, length));
reject_unchanged(prefix + std::string(4096, '1') + "\n");
// A complete file (including empty) remains valid; failed opens released the lock.
for (const auto& bytes : {std::string{}, prefix, prefix + record}) {
{
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << bytes;
}
const auto ledger = GrantLedger::open(path, 100);
CHECK(ledger);
CHECK((*ledger)->loaded().size() == (bytes.empty() ? 0 : bytes == prefix ? 1 : 2));
}
std::error_code ignored;
std::filesystem::remove(path, ignored);
std::filesystem::remove(std::filesystem::path{path}.concat(".lock"), ignored);
}
// Review M9.4-M9.7 #4: liveness after Welcome, both directions, without spurious drops.
void liveness() {
const auto fast_server = [](SessionServerConfig& c) {
c.heartbeat_interval = std::chrono::milliseconds{100};
c.liveness_timeout = std::chrono::milliseconds{500};
};
const auto fast_client = [](SessionClientConfig& c) {
c.heartbeat_interval = std::chrono::milliseconds{100};
c.liveness_timeout = std::chrono::milliseconds{500};
};
{
// Healthy but idle: no ticks are published; heartbeats keep both sides alive.
Harness h{4, std::chrono::milliseconds{3000}, fast_server, fast_client};
h.tick();
auto c = h.client(grants(7, 2, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] { return c->latest_view().has_value(); }));
const auto start = Clock::now();
CHECK(!h.until(
[&] {
h.collect(*c, events);
return has(events, ClientEvent::Kind::lost) ||
has(events, ClientEvent::Kind::closed);
},
std::chrono::milliseconds{1500}));
CHECK(Clock::now() - start >= std::chrono::milliseconds{1500});
CHECK(c->phase() == SessionPhase::established &&
c->environment().freshness() == client::Freshness::current);
}
{
// Silent server (socket open, never polled): the client times out, keeps the view
// as stale, reports the sent-but-unresolved intention as unknown, and reconnects.
Harness h{4, std::chrono::milliseconds{3000}, fast_server, fast_client};
h.tick();
auto c = h.client(grants(7, 3, 100));
std::vector<ClientEvent> events;
CHECK(h.until([&] { return c->latest_view().has_value(); }));
CHECK(c->send({5, 0, 1000, 0})); // Never answered: the server is not polled.
const auto end = Clock::now() + std::chrono::milliseconds{2000};
while (Clock::now() < end && !has(events, ClientEvent::Kind::lost)) {
c->poll(Clock::now());
h.collect(*c, events);
std::this_thread::sleep_for(std::chrono::milliseconds{5});
}
CHECK(has(events, ClientEvent::Kind::lost));
const auto* unknown = last(events, ClientEvent::Kind::unknown);
CHECK(unknown != nullptr && unknown->sequence == 5);
CHECK(c->environment().freshness() == client::Freshness::stale);
h.tick();
CHECK(h.until([&] {
return c->phase() == SessionPhase::established && c->latest_view().has_value();
}));
CHECK(c->environment().freshness() == client::Freshness::current);
}
{
// Silent client: the server closes it with `timeout`, freeing the subject.
Harness h{4, std::chrono::milliseconds{3000}, fast_server, fast_client};
h.tick();
auto c = h.client(grants(7, 1, 100));
CHECK(h.until([&] { return c->phase() == SessionPhase::established; }));
h.clients.clear(); // Stop polling the client; its socket stays open.
std::vector<SessionEvent> server_events;
CHECK(h.until([&] {
auto e = h.server->take_events();
server_events.insert(server_events.end(), e.begin(), e.end());
return h.server->sessions() == 0;
}));
CHECK(std::ranges::any_of(server_events, [](const SessionEvent& e) {
return e.kind == SessionEvent::Kind::closed && e.reason == CloseReason::timeout;
}));
auto again = h.client(grants(7, 1, 200)); // Same subject, fresh grant: admitted.
CHECK(h.until([&] { return again->phase() == SessionPhase::established; }));
}
}
void event_backpressure() {
Harness h;
h.tick();
auto c = h.client(grants(7, 3, 100));
CHECK(h.until([&] { return c->latest_view().has_value(); }));
std::uint64_t sent = 0;
CHECK(h.until([&] {
if (c->phase() != SessionPhase::established)
return true;
// Invalid vector: final rejection frees outstanding capacity, but the host
// intentionally never drains its event queue throughout this burst.
for (int batch = 0; batch < 16; ++batch)
if (c->send({sent + 1, 0, 1000, 1000}))
++sent;
return false;
}));
CHECK(c->phase() == SessionPhase::backoff);
const auto remaining = c->grants_left();
for (int i = 0; i < 100; ++i)
c->poll(Clock::now() + std::chrono::seconds{60});
CHECK(c->grants_left() == remaining && c->phase() == SessionPhase::backoff);
CHECK(c->environment().freshness() == client::Freshness::stale);
const auto events = c->take_events();
CHECK(events.size() <= SessionClient::max_events);
CHECK(has(events, ClientEvent::Kind::backpressure) && has(events, ClientEvent::Kind::lost));
const auto accounted = std::ranges::count_if(events, [](const ClientEvent& e) {
return e.kind == ClientEvent::Kind::result || e.kind == ClientEvent::Kind::unknown;
});
CHECK(static_cast<std::uint64_t>(accounted) == sent);
CHECK(h.until([&] { return c->latest_view().has_value(); }));
CHECK(c->phase() == SessionPhase::established);
}
} // namespace
void unreachable_server() {
// Refused connects produce no events (one per retry would flood callers); the
// counter lets a launcher bound "unreachable". No grant is spent without a server.
Harness h;
h.server.reset();
auto c = h.client(grants(7, 2, 100));
std::vector<ClientEvent> events;
// Windows reports a refused loopback connect only after about two seconds.
const auto end = Clock::now() + std::chrono::milliseconds{10000};
while (Clock::now() < end && c->unreachable_attempts() < 2) {
c->poll(Clock::now());
h.collect(*c, events);
std::this_thread::sleep_for(std::chrono::milliseconds{5});
}
CHECK(c->unreachable_attempts() >= 2);
CHECK(events.empty());
CHECK(c->grants_left() == 2 && c->phase() == SessionPhase::backoff);
}
void session_tests() {
event_backpressure();
ledger_exclusive_owner();
ledger_incomplete_records();
connect_view_intents_and_outcomes();
duplicate_capacity_and_authentication();
stale_epoch_restart_and_reconnect();
abuse();
carriers_never_alias();
grants_stay_consumed_across_restarts();
liveness();
unreachable_server();
}
} // namespace evo::network_test