small refactor and void library
Signed-off-by: Aleksei (Faumaray) Selikhov <contact@faumaray.com>
This commit is contained in:
1 parent
74b89ae3ca
commit
083b84e11a
12 files changed
+646
-343
No files matched your search
+156
-41
@@ -1,28 +1,156 @@
|
||||
#include "testing.hpp"
|
||||
|
||||
#include "lan_session.hpp"
|
||||
#include "lan_wire.hpp"
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using namespace std::string_view_literals;
|
||||
using namespace settlement;
|
||||
using namespace settlement::viewer;
|
||||
|
||||
void require(bool condition, const char* message) {
|
||||
if (!condition) throw std::runtime_error(message);
|
||||
}
|
||||
|
||||
using Void::Testing::expect_throw;
|
||||
using Void::Testing::require;
|
||||
using Void::Testing::Test;
|
||||
|
||||
constexpr auto hello_bytes = "\x0d\x00\x00\x00\x01\x0b\x00\x00\x00\x10\x32\x54\x76\x98\xba\xdc\xfe"sv;
|
||||
constexpr auto command_bytes = "\x23\x00\x00\x00\x03"
|
||||
"\x11\x22\x33\x44\x55\x66\x77\x88\x07"
|
||||
"\xfe\xff\xff\xff\x78\x56\x34\x12\xa5"
|
||||
"\x04\x03\x02\x01\xfc\xfc\xfd\xfe\x87\x96\xa5\xb4\xc3\xd2\xe1\xf0"sv;
|
||||
constexpr auto receipt_bytes = "\x0a\x00\x00\x00\x04\x04\x10\x32\x54\x76\x98\xba\xdc\xfe"sv;
|
||||
constexpr auto ping_bytes = "\x01\x00\x00\x00\x06"sv;
|
||||
|
||||
struct LanTests : Void::Testing::Suite<LanTests> {
|
||||
[[= Void::Testing::tag("wire")]] Test test_protocol_11_fixed_messages();
|
||||
[[= Void::Testing::tag("wire")]] Test test_fragmented_and_coalesced_frames();
|
||||
[[= Void::Testing::tag("wire")]] Test test_frame_size_limits();
|
||||
[[= Void::Testing::tag("lan")]] Test test_shared_colony_over_loopback();
|
||||
};
|
||||
|
||||
Test LanTests::test_protocol_11_fixed_messages() {
|
||||
using namespace lan_wire;
|
||||
constexpr Hello hello{protocol_version, 0xfedcba9876543210ULL};
|
||||
constexpr PlayerCommand command{
|
||||
1, 0x8877665544332211ULL, CommandKind::MoveObject, {-2, 0x12345678}, 0xa5,
|
||||
{0x01020304, -0x01020304}, 0xf0e1d2c3b4a59687ULL
|
||||
};
|
||||
constexpr Receipt receipt{CommandStatus::InvalidTarget, 0xfedcba9876543210ULL};
|
||||
|
||||
std::string encoded;
|
||||
const auto hello_payload = encode_hello(hello);
|
||||
append_frame(encoded, Message::Hello, std::string_view(hello_payload));
|
||||
require(encoded == hello_bytes);
|
||||
encoded.clear();
|
||||
const auto command_payload = encode_command(command);
|
||||
append_frame(encoded, Message::Command, std::string_view(command_payload));
|
||||
require(encoded == command_bytes);
|
||||
encoded.clear();
|
||||
const auto receipt_payload = encode_receipt(receipt);
|
||||
append_frame(encoded, Message::Receipt, std::string_view(receipt_payload));
|
||||
require(encoded == receipt_bytes);
|
||||
|
||||
const auto hello_frame = next_frame(hello_bytes);
|
||||
require(hello_frame && hello_frame->message == Message::Hello && hello_frame->size == hello_bytes.size());
|
||||
const auto decoded_hello = decode_hello(hello_frame->payload);
|
||||
require(decoded_hello && decoded_hello->version == 11 && decoded_hello->key == hello.key);
|
||||
require(!decode_hello(hello_frame->payload.substr(1)));
|
||||
require(!decode_hello(std::string(hello_frame->payload) + '\0'));
|
||||
|
||||
const auto command_frame = next_frame(command_bytes);
|
||||
require(command_frame && command_frame->message == Message::Command && command_frame->size == command_bytes.size());
|
||||
const auto decoded_command = decode_command(command_frame->payload);
|
||||
require(decoded_command.has_value());
|
||||
require(decoded_command->player == 2);
|
||||
require(decoded_command->sequence == command.sequence);
|
||||
require(decoded_command->kind == command.kind);
|
||||
require(decoded_command->cell == command.cell);
|
||||
require(decoded_command->value == command.value);
|
||||
require(decoded_command->destination == command.destination);
|
||||
require(decoded_command->resident == command.resident);
|
||||
require(!decode_command(command_frame->payload.substr(1)));
|
||||
require(!decode_command(std::string(command_frame->payload) + '\0'));
|
||||
|
||||
const auto receipt_frame = next_frame(receipt_bytes);
|
||||
require(receipt_frame && receipt_frame->message == Message::Receipt && receipt_frame->size == receipt_bytes.size());
|
||||
const auto decoded_receipt = decode_receipt(receipt_frame->payload);
|
||||
require(decoded_receipt && decoded_receipt->status == receipt.status &&
|
||||
decoded_receipt->next_sequence == receipt.next_sequence);
|
||||
require(!decode_receipt(receipt_frame->payload.substr(1)));
|
||||
require(!decode_receipt(std::string(receipt_frame->payload) + '\0'));
|
||||
}
|
||||
|
||||
Test LanTests::test_fragmented_and_coalesced_frames() {
|
||||
using namespace lan_wire;
|
||||
std::string received(command_bytes.substr(0, 3));
|
||||
require(!next_frame(received));
|
||||
received.append(command_bytes.substr(3, 8));
|
||||
require(!next_frame(received));
|
||||
received.append(command_bytes.substr(11));
|
||||
received.append(ping_bytes);
|
||||
received.append(hello_bytes.substr(0, 5));
|
||||
|
||||
std::size_t consumed = 0;
|
||||
{
|
||||
const auto command = next_frame(received);
|
||||
require(command && command->message == Message::Command && command->size == command_bytes.size());
|
||||
require(command->payload.data() == received.data() + 5);
|
||||
require(command->payload == command_bytes.substr(5));
|
||||
|
||||
const auto ping = next_frame(std::string_view(received).substr(command->size));
|
||||
require(ping && ping->message == Message::Ping && ping->payload.empty() && ping->size == ping_bytes.size());
|
||||
consumed = command->size + ping->size;
|
||||
require(!next_frame(std::string_view(received).substr(consumed)));
|
||||
}
|
||||
|
||||
received.erase(0, consumed);
|
||||
received.append(hello_bytes.substr(5));
|
||||
const auto hello = next_frame(received);
|
||||
require(hello && hello->message == Message::Hello && hello->size == received.size());
|
||||
require(hello->payload == hello_bytes.substr(5));
|
||||
}
|
||||
|
||||
Test LanTests::test_frame_size_limits() {
|
||||
using namespace lan_wire;
|
||||
constexpr auto empty_prefix = "\x00\x00\x00\x00"sv;
|
||||
constexpr auto max_prefix = "\x00\x00\x40\x00"sv;
|
||||
constexpr auto oversized_prefix = "\x01\x00\x40\x00"sv;
|
||||
|
||||
require(!next_frame(empty_prefix.substr(0, 3)));
|
||||
require(!next_frame(oversized_prefix.substr(0, 3)));
|
||||
expect_throw<std::runtime_error>([] { static_cast<void>(next_frame(empty_prefix)); });
|
||||
expect_throw<std::runtime_error>([] { static_cast<void>(next_frame(oversized_prefix)); });
|
||||
require(!next_frame(max_prefix));
|
||||
|
||||
std::string output;
|
||||
append_frame(output, Message::Ping);
|
||||
require(output == ping_bytes);
|
||||
|
||||
std::string payload(4 * 1024 * 1024 - 1, '\xa5');
|
||||
output.clear();
|
||||
append_frame(output, Message::Snapshot, payload);
|
||||
require(output.starts_with(max_prefix));
|
||||
{
|
||||
const auto snapshot = next_frame(output);
|
||||
require(snapshot && snapshot->message == Message::Snapshot && snapshot->size == output.size());
|
||||
require(snapshot->payload == payload);
|
||||
payload.front() = '\0';
|
||||
require(snapshot->payload.front() == '\xa5');
|
||||
}
|
||||
|
||||
payload.push_back('\0');
|
||||
expect_throw<std::runtime_error>([&] { append_frame(output, Message::Snapshot, payload); });
|
||||
require(output.size() == ping_bytes.size() + payload.size() - 1);
|
||||
}
|
||||
|
||||
Test LanTests::test_shared_colony_over_loopback() {
|
||||
auto authoritative = make_first_colony(4);
|
||||
auto client_view = make_first_colony(4);
|
||||
@@ -47,23 +175,20 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
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");
|
||||
require(host.state() == LanState::Hosting);
|
||||
|
||||
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");
|
||||
require(client_view->tick() == 25 && client_view->agents().size() == authoritative->agents().size());
|
||||
require(!guest.submit(CommandKind::SetPaused, {}, 1));
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, static_cast<std::uint8_t>(BedBase::Stone)));
|
||||
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");
|
||||
require(authoritative->production_orders().front().bed_base == BedBase::Stone);
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, 0));
|
||||
pump_until([&] {
|
||||
const auto rejected = guest.take_rejection();
|
||||
return rejected && rejected->status == CommandStatus::InvalidTarget &&
|
||||
@@ -71,7 +196,7 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
rejected->value == 0;
|
||||
},
|
||||
"a conflicting guest command did not return a rejection");
|
||||
require(authoritative->production_orders().size() == 1, "the same cell was claimed twice");
|
||||
require(authoritative->production_orders().size() == 1);
|
||||
|
||||
guest.leave();
|
||||
pump_until([&] { return host.state() == LanState::Hosting; }, "the guest slot was not released");
|
||||
@@ -79,9 +204,8 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
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");
|
||||
client_view->production_orders().size() == 1);
|
||||
require(guest.submit(CommandKind::SetForaging, {14, 12}, 1));
|
||||
pump_until([&] {
|
||||
if (client_view->next_command_sequence(2) != 3) return false;
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
@@ -96,13 +220,13 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
return false;
|
||||
};
|
||||
for (int i = 0; i < 600 && !owner_sleeping(*authoritative); ++i) authoritative->step();
|
||||
require(owner_sleeping(*authoritative), "the owner went without sleep");
|
||||
require(owner_sleeping(*authoritative));
|
||||
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");
|
||||
require(guest.submit(CommandKind::PlaceBerryField, field, 0));
|
||||
const auto has_field = [&](GridPosition position) {
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
if (node.site == ResourceSite::BerryField && node.position == position) return true;
|
||||
@@ -110,24 +234,23 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
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");
|
||||
require(guest.submit(CommandKind::MoveObject, field, 0, new_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");
|
||||
require(guest.submit(CommandKind::DemolishObject, new_field, 0));
|
||||
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");
|
||||
require(guest.submit(CommandKind::AssignProfession, {}, static_cast<std::uint8_t>(Profession::Miner), {}, resident));
|
||||
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");
|
||||
require(guest.submit(CommandKind::PlaceFlaxField, field, 0));
|
||||
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");
|
||||
require(guest.submit(CommandKind::PlaceLoom, new_field, 0));
|
||||
pump_until([&] {
|
||||
const auto& orders = client_view->production_orders();
|
||||
return orders.size() >= 2 && orders.back().recipe == RecipeId::CraftLoom &&
|
||||
@@ -138,7 +261,7 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
authoritative->step();
|
||||
}
|
||||
require(authoritative->resource_nodes().back().needs_tending() &&
|
||||
authoritative->resource_nodes().back().reserved_workers == 1, "no resident reserved the field for sowing");
|
||||
authoritative->resource_nodes().back().reserved_workers == 1);
|
||||
pump_until([&] {
|
||||
return client_view->tick() == authoritative->tick() &&
|
||||
client_view->resource_nodes().back().needs_tending() &&
|
||||
@@ -154,30 +277,22 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
found_room = true;
|
||||
}
|
||||
}
|
||||
require(found_room, "could not find a test plot for a room");
|
||||
require(found_room);
|
||||
const auto entrance = static_cast<std::uint8_t>(room.entrance);
|
||||
const auto previous_size = authoritative->production_orders().size();
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last),
|
||||
"guest could not send the room");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last));
|
||||
pump_until([&] {
|
||||
return client_view->production_orders().size() == previous_size + 10 &&
|
||||
client_view->production_orders().back().recipe == RecipeId::CraftWoodenDoor;
|
||||
}, "snapshot did not carry the room and carpenter's order");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last),
|
||||
"guest could not send the conflicting room");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last));
|
||||
pump_until([&] {
|
||||
const auto rejected = guest.take_rejection();
|
||||
return rejected && rejected->kind == CommandKind::PlaceRoom && rejected->cell == room.first &&
|
||||
rejected->destination == room.last && rejected->value == entrance &&
|
||||
rejected->status == CommandStatus::InvalidTarget;
|
||||
}, "room rejection lost the geometry needed to restore the draft");
|
||||
require(authoritative->production_orders().size() == previous_size + 10, "room conflict scheduled extra work");
|
||||
std::cout << "Loopback LAN checks passed.\n";
|
||||
return {};
|
||||
require(authoritative->production_orders().size() == previous_size + 10);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
return LanTests::run(argc, argv);
|
||||
}
|
||||
Reference in new issue
Block a user