Add deterministic transactional lifecycle command scheduler
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#include "evo/simulation/scheduler.hpp"
|
||||
#include "evo/simulation/world.hpp"
|
||||
#include "evo/telemetry/sinks.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
@@ -217,6 +219,100 @@ void telemetry_is_host_owned() {
|
||||
evo::log_error(log, evo::Error::invalid_snapshot);
|
||||
CHECK(log.str() == "{\"level\":\"error\",\"event\":\"invalid_snapshot\"}\n");
|
||||
}
|
||||
|
||||
void scheduled_order_and_recovery() {
|
||||
for (std::uint64_t seed = 0; seed < 32; ++seed) {
|
||||
auto expected = fixture(seed, 2);
|
||||
CHECK(expected.remove_entity(evo::EntityId{1}));
|
||||
CHECK(expected.create_entity({evo::Millimetres{1023}, evo::Millimetres{0}}));
|
||||
CHECK(expected.create_entity());
|
||||
CHECK(expected.tick());
|
||||
std::array commands{
|
||||
evo::Command{10, evo::RemoveEntity{evo::EntityId{1}}},
|
||||
evo::Command{20, evo::CreateEntity{{evo::Millimetres{1023}, evo::Millimetres{0}}}},
|
||||
evo::Command{30, evo::CreateEntity{}}};
|
||||
do {
|
||||
auto world = fixture(seed, 2);
|
||||
const auto receipts = evo::advance(world, evo::Tick{1}, commands);
|
||||
CHECK(receipts && receipts->size() == 3);
|
||||
CHECK((*receipts)[0] == (evo::CommandReceipt{10, evo::EntityId{1}}));
|
||||
CHECK((*receipts)[1] == (evo::CommandReceipt{20, evo::EntityId{3}}));
|
||||
CHECK((*receipts)[2] == (evo::CommandReceipt{30, evo::EntityId{4}}));
|
||||
CHECK(world.serialize() == expected.serialize());
|
||||
auto restored = evo::SimulationWorld::restore(world.serialize());
|
||||
CHECK(restored);
|
||||
for (std::uint64_t tick = 2; tick < 20; ++tick) {
|
||||
const std::array next{evo::Command{1, evo::CreateEntity{}}};
|
||||
CHECK(evo::advance(world, evo::Tick{tick}, next));
|
||||
CHECK(evo::advance(*restored, evo::Tick{tick}, next));
|
||||
}
|
||||
CHECK(restored->serialize() == world.serialize());
|
||||
} while (std::ranges::next_permutation(commands, {}, &evo::Command::sequence).found);
|
||||
}
|
||||
auto scheduled = fixture(123456, 8);
|
||||
for (std::uint64_t tick = 1; tick <= 1000; ++tick) {
|
||||
CHECK(evo::advance(scheduled, evo::Tick{tick}));
|
||||
}
|
||||
CHECK(scheduled.state_hash() == 0xd61ad5bf0c928c76ULL);
|
||||
}
|
||||
|
||||
void scheduled_rejection() {
|
||||
auto world = fixture(17, 2);
|
||||
const auto before = world.serialize();
|
||||
const auto reject = [&](evo::Tick tick, std::span<const evo::Command> commands,
|
||||
evo::ScheduleErrorCode code) {
|
||||
const auto result = evo::advance(world, tick, commands);
|
||||
CHECK(!result && result.error().code == code);
|
||||
CHECK(world.serialize() == before);
|
||||
return result.error();
|
||||
};
|
||||
reject(evo::Tick{0}, {}, evo::ScheduleErrorCode::wrong_tick);
|
||||
reject(evo::Tick{2}, {}, evo::ScheduleErrorCode::wrong_tick);
|
||||
std::array commands{evo::Command{1, evo::CreateEntity{}},
|
||||
evo::Command{1, evo::RemoveEntity{evo::EntityId{1}}}};
|
||||
reject(evo::Tick{1}, commands, evo::ScheduleErrorCode::duplicate_sequence);
|
||||
commands[1].sequence = 0;
|
||||
reject(evo::Tick{1}, commands, evo::ScheduleErrorCode::invalid_sequence);
|
||||
commands[1] = evo::Command{2, evo::RemoveEntity{evo::EntityId{99}}};
|
||||
auto error = reject(evo::Tick{1}, commands, evo::ScheduleErrorCode::simulation_failure);
|
||||
CHECK(error.sequence == 2 && error.cause == evo::Error::entity_not_found);
|
||||
commands[1] = evo::Command{2, evo::CreateEntity{{evo::Millimetres{1024}, {}}}};
|
||||
error = reject(evo::Tick{1}, commands, evo::ScheduleErrorCode::simulation_failure);
|
||||
CHECK(error.cause == evo::Error::invalid_position);
|
||||
commands = {evo::Command{1, evo::RemoveEntity{evo::EntityId{1}}},
|
||||
evo::Command{2, evo::RemoveEntity{evo::EntityId{1}}}};
|
||||
reject(evo::Tick{1}, commands, evo::ScheduleErrorCode::simulation_failure);
|
||||
std::vector<evo::Command> large(evo::max_commands_per_tick + 1,
|
||||
evo::Command{1, evo::CreateEntity{}});
|
||||
reject(evo::Tick{1}, large, evo::ScheduleErrorCode::batch_limit);
|
||||
large.pop_back();
|
||||
for (std::size_t index = 0; index < large.size(); ++index) {
|
||||
large[index].sequence = index + 1;
|
||||
}
|
||||
CHECK(evo::advance(world, evo::Tick{1}, large));
|
||||
const auto committed = world.serialize();
|
||||
const auto retry = evo::advance(world, evo::Tick{1}, large);
|
||||
CHECK(!retry && retry.error().code == evo::ScheduleErrorCode::wrong_tick);
|
||||
CHECK(world.serialize() == committed);
|
||||
|
||||
auto full = fixture(1, evo::max_entities);
|
||||
const auto full_before = full.serialize();
|
||||
const std::array create{evo::Command{1, evo::CreateEntity{}}};
|
||||
const auto capacity = evo::advance(full, evo::Tick{1}, create);
|
||||
CHECK(!capacity && capacity.error().cause == evo::Error::entity_limit);
|
||||
CHECK(full.serialize() == full_before);
|
||||
for (const auto offset : {32U, 40U}) {
|
||||
auto bytes = before;
|
||||
put(bytes, offset, std::numeric_limits<std::uint64_t>::max(), 8);
|
||||
resign(bytes);
|
||||
auto exhausted = evo::SimulationWorld::restore(bytes);
|
||||
CHECK(exhausted);
|
||||
const auto result = evo::advance(*exhausted, evo::Tick{1}, create);
|
||||
CHECK(!result && result.error().cause == (offset == 32 ? evo::Error::tick_exhausted
|
||||
: evo::Error::id_exhausted));
|
||||
CHECK(exhausted->serialize() == bytes);
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char**) try {
|
||||
@@ -242,7 +338,9 @@ int main(int argc, char**) try {
|
||||
Test{"atomic exhaustion", exhaustion_is_atomic},
|
||||
Test{"capacity", capacity_boundary},
|
||||
Test{"canonical empty state", wire_layout_and_empty_state},
|
||||
Test{"host telemetry", telemetry_is_host_owned}};
|
||||
Test{"host telemetry", telemetry_is_host_owned},
|
||||
Test{"scheduled order and recovery", scheduled_order_and_recovery},
|
||||
Test{"atomic scheduler rejection", scheduled_rejection}};
|
||||
int failures = 0;
|
||||
for (const auto& test : tests) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user