Import simulation and LAN tests
This commit is contained in:
1 parent
ade3b607cc
commit
6bf57afc73
2 files changed
+900
No files matched your search
@@ -0,0 +1,144 @@
|
||||
#include "lan_session.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <thread>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using namespace settlement;
|
||||
using namespace settlement::viewer;
|
||||
|
||||
void require(bool condition, const char* message) {
|
||||
if (!condition) throw std::runtime_error(message);
|
||||
}
|
||||
|
||||
void test_shared_colony_over_loopback() {
|
||||
auto authoritative = make_first_colony(4);
|
||||
auto client_view = make_first_colony(4);
|
||||
for (int i = 0; i < 25; ++i) authoritative->step();
|
||||
LanSession host;
|
||||
LanSession guest;
|
||||
host.host(0, true);
|
||||
const auto port = host.port();
|
||||
const auto code = host.join_code();
|
||||
auto wrong_code = code;
|
||||
wrong_code.front() = wrong_code.front() == 'f' ? 'e' : 'f';
|
||||
const auto pump_until = [&](auto success, const char* message) {
|
||||
const auto deadline = std::chrono::steady_clock::now() + 3s;
|
||||
while (std::chrono::steady_clock::now() < deadline) {
|
||||
static_cast<void>(host.poll(*authoritative));
|
||||
if (auto snapshot = guest.poll(*client_view)) client_view = std::move(snapshot);
|
||||
if (success()) return;
|
||||
std::this_thread::sleep_for(1ms);
|
||||
}
|
||||
throw std::runtime_error(message);
|
||||
};
|
||||
guest.join("127.0.0.1", wrong_code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestDisconnected; },
|
||||
"a guest with the wrong code was not disconnected");
|
||||
require(host.state() == LanState::Hosting, "an invalid guest occupied the player slot");
|
||||
|
||||
guest.join("127.0.0.1", code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not join");
|
||||
require(client_view->tick() == 25 && client_view->agents().size() == authoritative->agents().size(),
|
||||
"joining did not deliver the host's current snapshot");
|
||||
require(!guest.submit(CommandKind::SetPaused, {}, 1), "a guest could control pause");
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, static_cast<std::uint8_t>(BedBase::Stone)),
|
||||
"the guest could not send a build command");
|
||||
pump_until([&] {
|
||||
return client_view->production_orders().size() == 1 &&
|
||||
client_view->production_orders().front().position == GridPosition{16, 18} &&
|
||||
client_view->production_orders().front().bed_base == BedBase::Stone;
|
||||
}, "the guest's bed was not applied and synchronized");
|
||||
require(authoritative->production_orders().front().bed_base == BedBase::Stone,
|
||||
"the guest's choice of bed material was lost");
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, 0), "the guest could not send a conflicting command");
|
||||
pump_until([&] { return guest.take_rejection() == CommandStatus::InvalidTarget; },
|
||||
"a conflicting guest command did not return a rejection");
|
||||
require(authoritative->production_orders().size() == 1, "the same cell was claimed twice");
|
||||
|
||||
guest.leave();
|
||||
pump_until([&] { return host.state() == LanState::Hosting; }, "the guest slot was not released");
|
||||
for (int i = 0; i < 10; ++i) authoritative->step();
|
||||
guest.join("127.0.0.1", code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not rejoin");
|
||||
require(client_view->tick() == authoritative->tick() && client_view->next_command_sequence(2) == 2 &&
|
||||
client_view->production_orders().size() == 1,
|
||||
"rejoining did not restore the current colony and guest sequence");
|
||||
require(guest.submit(CommandKind::SetForaging, {14, 12}, 1), "the reconnected guest could not send a command");
|
||||
pump_until([&] {
|
||||
if (client_view->next_command_sequence(2) != 3) return false;
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
if (node.position == GridPosition{14, 12}) return node.foraging_enabled;
|
||||
}
|
||||
return false;
|
||||
}, "the reconnected guest's command did not synchronize");
|
||||
const auto owner_sleeping = [](const World& simulation) {
|
||||
for (const auto& agent : simulation.agents()) {
|
||||
if (agent.state == AgentState::Sleeping && simulation.assigned_bed(agent.id) != nullptr) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
for (int i = 0; i < 600 && !owner_sleeping(*authoritative); ++i) authoritative->step();
|
||||
require(owner_sleeping(*authoritative), "the owner went without sleep");
|
||||
pump_until([&] {
|
||||
return client_view->tick() == authoritative->tick() && owner_sleeping(*client_view);
|
||||
}, "the host's sleep state did not reach the guest");
|
||||
const GridPosition field{14, 15};
|
||||
const GridPosition new_field{14, 17};
|
||||
require(guest.submit(CommandKind::PlaceBerryField, field, 0), "the guest could not plant a field");
|
||||
const auto has_field = [&](GridPosition position) {
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
if (node.site == ResourceSite::BerryField && node.position == position) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
pump_until([&] { return has_field(field); }, "the planted field was not synchronized");
|
||||
require(guest.submit(CommandKind::MoveObject, field, 0, new_field), "the guest could not move the field");
|
||||
pump_until([&] { return has_field(new_field) && !has_field(field); }, "the move destination was not synchronized");
|
||||
require(guest.submit(CommandKind::DemolishObject, new_field, 0), "the guest could not remove the field");
|
||||
pump_until([&] { return !has_field(new_field); },
|
||||
"demolishing a field was not synchronized");
|
||||
const auto resident = authoritative->agents().back().id;
|
||||
require(guest.submit(CommandKind::AssignProfession, {}, static_cast<std::uint8_t>(Profession::Miner), {}, resident),
|
||||
"the guest could not assign a profession");
|
||||
pump_until([&] {
|
||||
return client_view->agents().back().id == resident &&
|
||||
client_view->agents().back().profession == Profession::Miner;
|
||||
}, "the assigned profession was not synchronized");
|
||||
require(guest.submit(CommandKind::PlaceFlaxField, field, 0), "the guest could not plant flax");
|
||||
pump_until([&] {
|
||||
return client_view->resource_nodes().back().site == ResourceSite::FlaxField &&
|
||||
client_view->resource_nodes().back().position == field;
|
||||
}, "the flax field did not arrive in the guest snapshot");
|
||||
require(guest.submit(CommandKind::PlaceLoom, new_field, 0), "the guest could not place a loom");
|
||||
pump_until([&] {
|
||||
return client_view->production_orders().back().recipe == RecipeId::WeaveCloth &&
|
||||
client_view->production_orders().back().position == new_field;
|
||||
}, "the loom did not arrive in the guest snapshot");
|
||||
for (int i = 0; i < 160 && authoritative->resource_nodes().back().reserved_workers == 0; ++i) {
|
||||
authoritative->step();
|
||||
}
|
||||
require(authoritative->resource_nodes().back().needs_tending() &&
|
||||
authoritative->resource_nodes().back().reserved_workers == 1, "no resident reserved the field for sowing");
|
||||
pump_until([&] {
|
||||
return client_view->tick() == authoritative->tick() &&
|
||||
client_view->resource_nodes().back().needs_tending() &&
|
||||
client_view->resource_nodes().back().reserved_workers == 1;
|
||||
}, "a snapshot lost the resident's sowing assignment");
|
||||
std::cout << "Loopback LAN checks passed.\n";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
try {
|
||||
test_shared_colony_over_loopback();
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user