#include "settlement/core/world.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace settlement; using Void::Testing::require; using Void::Testing::Test; static_assert(std::is_same_v().capture()), std::shared_ptr>); static_assert(std::is_move_constructible_v); static_assert(std::is_move_assignable_v); static_assert(!std::is_copy_constructible_v); std::string bytes(const World& world) { std::ostringstream stream(std::ios::binary); world.write_save(stream); return stream.str(); } void send(World& world, CommandKind kind, GridPosition cell = {}, std::uint8_t value = 0) { const auto receipt = world.apply_command( {colony_host, world.next_command_sequence(colony_host), kind, cell, value}); require(receipt.status == CommandStatus::Accepted); } void compare_read_views(const World& source, const World& snapshot) { require(bytes(snapshot) == bytes(source)); require(snapshot.seed() == source.seed()); require(snapshot.width() == source.width() && snapshot.height() == source.height()); require(snapshot.tick() == source.tick() && snapshot.worker_count() == source.worker_count()); require(snapshot.path_searches() == source.path_searches()); require(snapshot.path_nodes_visited() == source.path_nodes_visited()); require(snapshot.assignment_path_cache_hits() == source.assignment_path_cache_hits()); require(snapshot.jobs_completed() == source.jobs_completed()); require(snapshot.meals_eaten() == source.meals_eaten()); require(snapshot.scenario_active() == source.scenario_active()); require(snapshot.staged_beds() == source.staged_beds()); require(snapshot.paused() == source.paused()); require(snapshot.work_priority() == source.work_priority()); require(snapshot.carpenter_workshop() == source.carpenter_workshop()); for (std::uint8_t player = 1; player <= colony_player_slots; ++player) { require(snapshot.next_command_sequence(player) == source.next_command_sequence(player)); } for (int y = 0; y < source.height(); ++y) { for (int x = 0; x < source.width(); ++x) { require(snapshot.is_walkable({x, y}) == source.is_walkable({x, y})); } } } struct [[= Void::Testing::name("SnapshotTests")]] SnapshotTests : Void::Testing::Suite { [[= Void::Testing::tag("snapshot")]] Test test_independent_state_in_all_execution_modes(); [[= Void::Testing::tag("snapshot")]] Test test_mid_delivery_and_paused_command_state_survives_owner(); [[= Void::Testing::tag("snapshot")]] Test test_reader_runs_while_owner_advances(); [[= Void::Testing::tag("snapshot")]] Test test_capture_excludes_execution_callbacks_and_workers(); }; Test SnapshotTests::test_independent_state_in_all_execution_modes() { constexpr std::array workers{1U, 2U, 6U}; for (const auto count : workers) { const auto mode = count == 1 ? ExecutionMode::SerialReference : ExecutionMode::Parallel; World source(53, 12, 12, count, mode); source.set_blocked({4, 4}); static_cast(source.spawn_agent({1, 1})); static_cast(source.add_job({8, 8}, 3)); source.step(); const auto snapshot = source.capture(); compare_read_views(source, *snapshot); require(source.agents().data() != snapshot->agents().data()); require(source.agents().front().route.data() != snapshot->agents().front().route.data()); require(source.jobs().data() != snapshot->jobs().data()); const auto expected = bytes(*snapshot); const auto position = snapshot->agents().front().position; const auto searches = snapshot->path_searches(); static_cast(source.spawn_agent({2, 2})); static_cast(source.add_job({2, 7}, 1)); source.set_blocked({4, 4}, false); for (int i = 0; i < 30; ++i) source.step(); require(bytes(*snapshot) == expected); require(snapshot->agents().front().position == position); require(snapshot->path_searches() == searches && !snapshot->is_walkable({4, 4})); auto nested_snapshot = snapshot->capture(); compare_read_views(*snapshot, *nested_snapshot); World moved(std::move(source)); require(moved.agents().size() == 2); require(bytes(*snapshot) == expected); } } Test SnapshotTests::test_mid_delivery_and_paused_command_state_survives_owner() { auto source = make_first_colony(6); send(*source, CommandKind::PlaceBed, {16, 18}); send(*source, CommandKind::PlaceBed, {17, 18}, 1); send(*source, CommandKind::PlaceBed, {18, 18}); bool carrying = false; for (int step = 0; step < 300 && !carrying; ++step) { source->step(); carrying = source->assignment_path_cache_hits() > 0 && std::any_of(source->agents().begin(), source->agents().end(), [](const Agent& agent) { return !agent.inventory.empty() && agent.production_index != no_entity_index; }); } require(carrying); send(*source, CommandKind::SetPriority, {}, 1); send(*source, CommandKind::SetPaused, {}, 1); auto snapshot = source->capture(); compare_read_views(*source, *snapshot); require(snapshot->paused() && snapshot->work_priority() == WorkPriority::FoodFirst); require(source->storages().data() != snapshot->storages().data()); require(source->production_orders().data() != snapshot->production_orders().data()); bool nested_materials = false; bool nested_routes = false; bool nested_stacks = false; for (std::size_t i = 0; i < source->production_orders().size(); ++i) { const auto& live = source->production_orders()[i]; const auto& held = snapshot->production_orders()[i]; if (!live.materials.empty()) { require(live.materials.data() != held.materials.data()); nested_materials = true; } for (std::size_t stop = 0; stop < live.delivery_stops.size(); ++stop) { if (live.delivery_stops[stop].route_to_next.empty()) continue; require(live.delivery_stops[stop].route_to_next.data() != held.delivery_stops[stop].route_to_next.data()); nested_routes = true; } } for (std::size_t i = 0; i < source->storages().size(); ++i) { if (source->storages()[i].items.empty()) continue; require(source->storages()[i].items.data() != snapshot->storages()[i].items.data()); nested_stacks = true; } require(nested_materials && nested_routes && nested_stacks); const auto expected = bytes(*snapshot); const auto hits = snapshot->assignment_path_cache_hits(); const auto sequence = snapshot->next_command_sequence(colony_host); send(*source, CommandKind::SetPaused, {}, 0); for (int step = 0; step < 100; ++step) source->step(); source.reset(); require(bytes(*snapshot) == expected); require(snapshot->paused() && snapshot->assignment_path_cache_hits() == hits); require(snapshot->next_command_sequence(colony_host) == sequence); std::istringstream first(expected, std::ios::binary); std::istringstream second(expected, std::ios::binary); auto serial = World::read_save(first, 1, ExecutionMode::SerialReference); auto parallel = World::read_save(second, 2); send(*serial, CommandKind::SetPaused, {}, 0); send(*parallel, CommandKind::SetPaused, {}, 0); for (int step = 0; step < 100; ++step) { serial->step(); parallel->step(); require(bytes(*serial) == bytes(*parallel)); } require(bytes(*snapshot) == expected); } Test SnapshotTests::test_reader_runs_while_owner_advances() { auto source = make_first_colony(2); send(*source, CommandKind::PlaceBed, {16, 18}); source->step(); const auto snapshot = source->capture(); const auto expected = bytes(*snapshot); std::barrier rendezvous(2); auto reader = std::async(std::launch::async, [snapshot, expected, &rendezvous] { bool consistent = true; for (int step = 0; step < 32; ++step) { rendezvous.arrive_and_wait(); try { consistent = consistent && bytes(*snapshot) == expected; consistent = consistent && snapshot->tick() == 1 && snapshot->agents().size() == 3; } catch (...) { consistent = false; } rendezvous.arrive_and_wait(); } return consistent; }); // Drop this participant before joining the reader if an owner step fails. auto release_reader = Void::Scope::on_exit([&] { rendezvous.arrive_and_drop(); }); for (int step = 0; step < 32; ++step) { rendezvous.arrive_and_wait(); source->step(); rendezvous.arrive_and_wait(); } release_reader.release(); require(reader.get()); require(source->tick() == 33 && bytes(*snapshot) == expected); } Test SnapshotTests::test_capture_excludes_execution_callbacks_and_workers() { std::shared_ptr snapshot; auto callback_state = std::make_shared>(0); std::weak_ptr> weak_state = callback_state; auto source = [&] { WorkerPoolOptions options; options.before_worker_start = [callback_state](std::size_t) { ++*callback_state; }; return std::make_unique(61, 8, 8, 2, ExecutionMode::Parallel, std::move(options)); }(); static_cast(source->spawn_agent({1, 1})); static_cast(source->add_job({6, 6}, 4)); auto unstarted = source->capture(); require(callback_state->load() == 0); source->step(); require(callback_state->load() == 2); snapshot = source->capture(); require(callback_state->load() == 2); const auto expected = bytes(*snapshot); callback_state.reset(); source.reset(); require(weak_state.expired()); require(snapshot->worker_count() == 2 && bytes(*snapshot) == expected); require(unstarted->tick() == 0 && unstarted->agents().size() == 1); } } // namespace