276 lines
16 KiB
C++
276 lines
16 KiB
C++
#include "settlement/core/simulation_runtime.hpp"
|
|
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <chrono>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
using namespace settlement;
|
|
using Void::Testing::Test;
|
|
using namespace std::chrono_literals;
|
|
constexpr TrustedClient host{colony_host};
|
|
|
|
void require(bool condition, const char* message = "production command invariant failed") {
|
|
if (!condition) throw std::runtime_error(message);
|
|
}
|
|
|
|
SessionIdentity identity() {
|
|
SessionIdentity result;
|
|
result.world.bytes.back() = 61;
|
|
result.session.bytes.back() = 62;
|
|
return result;
|
|
}
|
|
|
|
CommandEnvelope envelope(std::uint64_t request, SessionPayload payload, SessionIdentity context = identity()) {
|
|
return {command_envelope_version, context.world, context.session, context.city, request, std::move(payload)};
|
|
}
|
|
|
|
DispatchReceipt send(World& world, CommandDispatcher& dispatcher, SessionPayload payload) {
|
|
return dispatcher.dispatch(world, host, envelope(dispatcher.next_request(host), std::move(payload)));
|
|
}
|
|
|
|
void accepted(const DispatchReceipt& receipt) {
|
|
require(receipt.status == DispatchStatus::Completed && receipt.command_status == CommandStatus::Accepted,
|
|
"production command was not accepted");
|
|
}
|
|
|
|
template<class Predicate>
|
|
void step_until(World& world, Predicate done) {
|
|
for (int tick = 0; tick < 600 && !done(); ++tick) world.step();
|
|
require(done(), "actual work did not reach the required phase");
|
|
world.validate_custody();
|
|
}
|
|
|
|
std::unique_ptr<World> cabin() {
|
|
auto world = std::make_unique<World>(43, 32, 32, 1, ExecutionMode::SerialReference);
|
|
const auto store = world->add_storage({3, 3}, 20);
|
|
world->stock_storage(store, ItemType::Plank, 3);
|
|
world->stock_storage(store, ItemType::StoneBlock, 2);
|
|
static_cast<void>(world->spawn_agent({1, 3}));
|
|
world->queue_production({14, 3}, RecipeId::BuildCabin);
|
|
return world;
|
|
}
|
|
|
|
RuntimeResult finish(RuntimeSubmission submission) {
|
|
require(submission.admission == RuntimeAdmission::Accepted);
|
|
require(submission.completion.wait_for(5s) == std::future_status::ready);
|
|
auto result = submission.completion.get();
|
|
require(result.outcome == RuntimeOutcome::Completed && result.receipt.has_value());
|
|
return result;
|
|
}
|
|
|
|
struct [[= Void::Testing::name("ProductionCommandsTests")]] ProductionCommandsTests
|
|
: Void::Testing::Suite<ProductionCommandsTests> {
|
|
[[= Void::Testing::tag("production-command")]] Test exact_results_and_conflicts_use_durable_ids();
|
|
[[= Void::Testing::tag("production-command")]] Test context_and_guest_denials_leave_orders_untouched();
|
|
[[= Void::Testing::tag("production-command")]] Test cancel_at_each_phase_returns_actual_cargo_once();
|
|
[[= Void::Testing::tag("production-command")]] Test full_storage_preserves_return_until_space_exists();
|
|
[[= Void::Testing::tag("production-command")]] Test resume_keeps_partial_bed_and_installed_loom_identity();
|
|
[[= Void::Testing::tag("production-command")]] Test runtime_publishes_and_retains_modern_receipts();
|
|
};
|
|
|
|
Test ProductionCommandsTests::exact_results_and_conflicts_use_durable_ids() {
|
|
World world(43, 32, 32, 1, ExecutionMode::SerialReference);
|
|
world.queue_production({8, 8}, RecipeId::SawPlank);
|
|
world.queue_production({8, 8}, RecipeId::SawPlank);
|
|
const auto first = world.production_orders().front().id;
|
|
const auto second = world.production_orders().back().id;
|
|
CommandDispatcher dispatcher(identity());
|
|
const auto cancel = envelope(1, CancelProductionPayload{first});
|
|
const auto result = dispatcher.dispatch(world, host, cancel);
|
|
accepted(result);
|
|
require(result.revision == 1 && world.find_production(first) == nullptr && world.find_production(second),
|
|
"cancel selected by vector position or cell instead of exact durable id");
|
|
require(dispatcher.dispatch(world, host, cancel) == result && dispatcher.lookup(host, cancel) == result);
|
|
for (const SessionPayload changed : {SessionPayload{CancelProductionPayload{second}},
|
|
SessionPayload{ResumeProductionPayload{first}}, SessionPayload{CommandPayload{}},
|
|
SessionPayload{LegacyCabinPayload{{8, 8}, {2, 2}, {3, 2}}}}) {
|
|
require(dispatcher.dispatch(world, host, envelope(1, changed)).status == DispatchStatus::Conflict,
|
|
"same request accepted a different id or payload variant");
|
|
}
|
|
const auto refused = envelope(2, ResumeProductionPayload{first});
|
|
const auto rejection = dispatcher.dispatch(world, host, refused);
|
|
require(rejection.status == DispatchStatus::Completed && rejection.command_status == CommandStatus::InvalidTarget &&
|
|
rejection.revision == 1 && dispatcher.dispatch(world, host, refused) == rejection);
|
|
const auto zero = envelope(3, CancelProductionPayload{0});
|
|
const auto zero_result = dispatcher.dispatch(world, host, zero);
|
|
require(zero_result.command_status == CommandStatus::InvalidTarget && dispatcher.dispatch(world, host, zero) == zero_result);
|
|
accepted(send(world, dispatcher, CancelProductionPayload{second}));
|
|
require(world.production_orders().empty() && dispatcher.revision() == 2 &&
|
|
world.next_command_sequence(1) == 1 && world.next_command_sequence(2) == 1,
|
|
"modern command changed legacy accepted-command numbering");
|
|
require(dispatcher.dispatch(world, host, cancel) == result, "removed target lost the original retained receipt");
|
|
world.validate_custody();
|
|
}
|
|
|
|
Test ProductionCommandsTests::context_and_guest_denials_leave_orders_untouched() {
|
|
World world(43, 32, 32, 1, ExecutionMode::SerialReference);
|
|
world.queue_production({8, 8}, RecipeId::BuildCabin);
|
|
const auto id = world.production_orders().front().id;
|
|
CommandDispatcher dispatcher(identity());
|
|
auto command = envelope(1, CancelProductionPayload{id});
|
|
command.world.bytes.back() ^= 1;
|
|
require(dispatcher.dispatch(world, host, command).status == DispatchStatus::WrongWorld);
|
|
command = envelope(1, CancelProductionPayload{id});
|
|
command.session.bytes.back() ^= 1;
|
|
require(dispatcher.dispatch(world, host, command).status == DispatchStatus::StaleSession);
|
|
command = envelope(1, CancelProductionPayload{id});
|
|
require(dispatcher.dispatch(world, {0}, command).status == DispatchStatus::UnauthorizedClient);
|
|
require(dispatcher.dispatch(world, {3}, command).status == DispatchStatus::UnauthorizedClient);
|
|
require(dispatcher.receipt_count(host) == 0 && dispatcher.revision() == 0 && world.find_production(id));
|
|
const auto guest_result = dispatcher.dispatch(world, {2}, command);
|
|
require(guest_result.command_status == CommandStatus::Unauthorized &&
|
|
dispatcher.dispatch(world, {2}, command) == guest_result);
|
|
const auto guest_resume = envelope(2, ResumeProductionPayload{id});
|
|
require(dispatcher.dispatch(world, {2}, guest_resume).command_status == CommandStatus::Unauthorized);
|
|
command.city = 2;
|
|
const auto denied = dispatcher.dispatch(world, host, command);
|
|
require(denied.command_status == CommandStatus::Unauthorized && denied.revision == 0 &&
|
|
dispatcher.dispatch(world, host, command) == denied && world.find_production(id));
|
|
command.city = 1;
|
|
require(dispatcher.dispatch(world, host, command).status == DispatchStatus::Conflict);
|
|
command.request = 2;
|
|
accepted(dispatcher.dispatch(world, host, command));
|
|
command.world.bytes.back() ^= 1;
|
|
require(dispatcher.dispatch(world, host, command).status == DispatchStatus::WrongWorld,
|
|
"cached receipt bypassed world identity check");
|
|
require(world.next_command_sequence(1) == 1 && world.next_command_sequence(2) == 1);
|
|
}
|
|
|
|
Test ProductionCommandsTests::cancel_at_each_phase_returns_actual_cargo_once() {
|
|
for (int phase = 0; phase < 3; ++phase) {
|
|
auto world = cabin();
|
|
const auto id = world->production_orders().front().id;
|
|
const auto storage = world->storages().front().id;
|
|
step_until(*world, [&] {
|
|
const auto& agent = world->agents().front();
|
|
return phase == 0 ? agent.state == AgentState::FetchingMaterials && agent.inventory.empty() :
|
|
phase == 1 ? !agent.inventory.empty() : agent.state == AgentState::Working;
|
|
});
|
|
CommandDispatcher dispatcher(identity());
|
|
const auto sequence = world->next_command_sequence(1);
|
|
require(send(*world, dispatcher, CommandPayload{CommandKind::DemolishObject, {3, 3}}).command_status == CommandStatus::Busy);
|
|
require(send(*world, dispatcher, CommandPayload{CommandKind::MoveObject, {3, 3}, 0, {4, 3}}).command_status == CommandStatus::Busy);
|
|
const auto cancel = envelope(dispatcher.next_request(host), CancelProductionPayload{id});
|
|
const auto result = dispatcher.dispatch(*world, host, cancel);
|
|
accepted(result);
|
|
if (phase != 0) require(world->agents().front().state == AgentState::Returning &&
|
|
!world->agents().front().inventory.empty(), "cancellation erased carried or worksite input");
|
|
require(dispatcher.dispatch(*world, host, cancel) == result);
|
|
step_until(*world, [&] { return world->agents().front().state != AgentState::Returning; });
|
|
require(world->find_storage(storage)->item_count(ItemType::Plank) == 3 &&
|
|
world->find_storage(storage)->item_count(ItemType::StoneBlock) == 2 &&
|
|
world->agents().front().inventory.empty() && world->find_production(id) == nullptr,
|
|
"cancelled material was lost, duplicated or left assigned");
|
|
for (int tick = 0; tick < 4; ++tick) world->step();
|
|
require(dispatcher.dispatch(*world, host, cancel) == result && world->next_command_sequence(1) == sequence &&
|
|
world->find_storage(storage)->item_count(ItemType::Plank) == 3 &&
|
|
world->find_storage(storage)->item_count(ItemType::StoneBlock) == 2);
|
|
world->validate_custody();
|
|
}
|
|
}
|
|
|
|
Test ProductionCommandsTests::full_storage_preserves_return_until_space_exists() {
|
|
auto world = cabin();
|
|
const auto id = world->production_orders().front().id;
|
|
const auto original = world->storages().front().id;
|
|
step_until(*world, [&] { return !world->agents().front().inventory.empty(); });
|
|
world->stock_storage(original, ItemType::Wood, 20);
|
|
CommandDispatcher dispatcher(identity());
|
|
accepted(send(*world, dispatcher, CancelProductionPayload{id}));
|
|
for (int tick = 0; tick < 10; ++tick) world->step();
|
|
const auto& waiting = world->agents().front();
|
|
require(waiting.state == AgentState::Returning && waiting.execution &&
|
|
(waiting.execution->blocked == CustodyStatus::NoDestination || waiting.execution->blocked == CustodyStatus::Capacity) &&
|
|
waiting.inventory.item_count(ItemType::Plank) == 3 && waiting.inventory.item_count(ItemType::StoneBlock) == 2,
|
|
"full storage did not preserve the cargo and meaningful blocked reason");
|
|
const auto destination = world->add_storage({4, 3}, 5);
|
|
step_until(*world, [&] { return world->agents().front().state != AgentState::Returning; });
|
|
require(world->find_storage(destination)->item_count(ItemType::Plank) == 3 &&
|
|
world->find_storage(destination)->item_count(ItemType::StoneBlock) == 2 &&
|
|
world->find_storage(original)->item_count(ItemType::Wood) == 20);
|
|
}
|
|
|
|
Test ProductionCommandsTests::resume_keeps_partial_bed_and_installed_loom_identity() {
|
|
for (const bool bed : {true, false}) {
|
|
World world(43, 32, 32, 1, ExecutionMode::SerialReference);
|
|
world.start_scenario({2, 2});
|
|
const auto storage = world.add_storage({3, 3}, 20);
|
|
world.stock_storage(storage, ItemType::Wood, 2);
|
|
static_cast<void>(world.spawn_agent({1, 3}));
|
|
CommandDispatcher dispatcher(identity());
|
|
if (bed) {
|
|
accepted(send(world, dispatcher, CommandPayload{CommandKind::PlaceBed, {9, 3}}));
|
|
} else {
|
|
world.queue_production({9, 3}, RecipeId::BuildLoom);
|
|
}
|
|
const auto id = world.production_orders().back().id;
|
|
step_until(world, [&] {
|
|
const auto* order = world.find_production(id);
|
|
return bed ? order->bed_stage == BedStage::Bedding : order->recipe == RecipeId::WeaveCloth;
|
|
});
|
|
const auto before_cancel = world.capture();
|
|
const auto stage = world.find_production(id)->bed_stage;
|
|
const auto sequence = world.next_command_sequence(1);
|
|
accepted(send(world, dispatcher, CancelProductionPayload{id}));
|
|
require(world.find_production(id) && world.find_production(id)->suspended &&
|
|
world.find_production(id)->bed_stage == stage && !world.can_place_storage({9, 3}),
|
|
"cancel removed installed progress or its occupied site");
|
|
const auto suspended_cancel = envelope(dispatcher.next_request(host), CancelProductionPayload{id});
|
|
const auto busy = dispatcher.dispatch(world, host, suspended_cancel);
|
|
require(busy.status == DispatchStatus::Completed && busy.command_status == CommandStatus::Busy &&
|
|
dispatcher.dispatch(world, host, suspended_cancel) == busy);
|
|
for (int tick = 0; tick < 5; ++tick) world.step();
|
|
const auto resume = envelope(dispatcher.next_request(host), ResumeProductionPayload{id});
|
|
const auto resumed = dispatcher.dispatch(world, host, resume);
|
|
accepted(resumed);
|
|
require(world.find_production(id) && !world.find_production(id)->suspended &&
|
|
world.find_production(id)->bed_stage == stage && dispatcher.dispatch(world, host, resume) == resumed &&
|
|
world.next_command_sequence(1) == sequence && !before_cancel->find_production(id)->suspended);
|
|
require(dispatcher.dispatch(world, host, suspended_cancel) == busy && !world.find_production(id)->suspended,
|
|
"old refused cancellation changed after the same durable order was resumed");
|
|
world.stock_storage(storage, bed ? ItemType::Cloth : ItemType::Flax, 2);
|
|
step_until(world, [&] { return bed ? world.find_production(id)->state == JobState::Completed :
|
|
world.find_storage(storage)->item_count(ItemType::Cloth) > 0; });
|
|
require(world.find_storage(storage)->item_count(ItemType::Wood) == (bed ? 1U : 0U),
|
|
"resume charged for an already installed stage twice");
|
|
if (bed) {
|
|
const auto refused = send(world, dispatcher, CancelProductionPayload{id});
|
|
require(refused.command_status == CommandStatus::Busy,
|
|
"completed final bed accepted cancel");
|
|
}
|
|
}
|
|
}
|
|
|
|
Test ProductionCommandsTests::runtime_publishes_and_retains_modern_receipts() {
|
|
auto world = cabin();
|
|
step_until(*world, [&] { return !world->agents().front().inventory.empty(); });
|
|
const auto id = world->production_orders().front().id;
|
|
const auto rules = default_rules_manifest();
|
|
require(rules.has_value());
|
|
SimulationRuntime runtime(WorldCheckpoint{std::move(world), identity().world, 1, *rules}, {.ticking = false});
|
|
const auto opening = runtime.latest();
|
|
const auto cancel = envelope(1, CancelProductionPayload{id}, opening->identity);
|
|
const auto first = finish(runtime.try_submit(host, cancel));
|
|
accepted(*first.receipt);
|
|
require(first.snapshot->world->find_production(id) == nullptr &&
|
|
first.snapshot->world->agents().front().state == AgentState::Returning &&
|
|
opening->world->find_production(id) && opening->world->agents().front().state != AgentState::Returning,
|
|
"modern runtime effect was not published with independent old snapshot");
|
|
const auto repeat = finish(runtime.try_submit(host, cancel));
|
|
require(repeat.receipt == first.receipt && runtime.lookup(host, cancel) == first.receipt &&
|
|
repeat.snapshot->revision == first.snapshot->revision && repeat.snapshot->next_requests[0] == 2);
|
|
const auto changed = finish(runtime.try_submit(host, envelope(1, ResumeProductionPayload{id}, opening->identity)));
|
|
require(changed.receipt->status == DispatchStatus::Conflict);
|
|
const auto refusal = envelope(2, ResumeProductionPayload{id}, opening->identity);
|
|
const auto denied = finish(runtime.try_submit(host, refusal));
|
|
require(denied.receipt->command_status == CommandStatus::InvalidTarget &&
|
|
finish(runtime.try_submit(host, refusal)).receipt == denied.receipt &&
|
|
runtime.latest()->world->next_command_sequence(1) == 1 && runtime.view().state == RuntimeState::Running);
|
|
runtime.latest()->world->validate_custody();
|
|
}
|
|
} // namespace
|