From e821d617802ebbfff756844e4d9720342fc78a4e Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 13 Nov 2026 14:12:49 +0100 Subject: Swarm POC --- modules/swarm/tests/SConscript | 32 +++ modules/swarm/tests/capacity.cpp | 234 +++++++++++++++++++++ modules/swarm/tests/interpret.cpp | 133 ++++++++++++ modules/swarm/tests/probe_search.cpp | 202 +++++++++++++++++++ modules/swarm/tests/registry.cpp | 298 +++++++++++++++++++++++++++ modules/swarm/tests/run.cpp | 158 +++++++++++++++ modules/swarm/tests/signal.cpp | 82 ++++++++ modules/swarm/tests/wire.cpp | 381 +++++++++++++++++++++++++++++++++++ 8 files changed, 1520 insertions(+) create mode 100644 modules/swarm/tests/SConscript create mode 100644 modules/swarm/tests/capacity.cpp create mode 100644 modules/swarm/tests/interpret.cpp create mode 100644 modules/swarm/tests/probe_search.cpp create mode 100644 modules/swarm/tests/registry.cpp create mode 100644 modules/swarm/tests/run.cpp create mode 100644 modules/swarm/tests/signal.cpp create mode 100644 modules/swarm/tests/wire.cpp (limited to 'modules/swarm/tests') diff --git a/modules/swarm/tests/SConscript b/modules/swarm/tests/SConscript new file mode 100644 index 0000000..d1b381e --- /dev/null +++ b/modules/swarm/tests/SConscript @@ -0,0 +1,32 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +test_cases_env = env.Clone(); + +test_cases_env.Append(LIBS=['forstio-test']); + +test_cases_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +test_cases_env.headers = sorted(glob.glob(dir_path + "/*.hpp")) + +env.sources += test_cases_env.sources; +env.headers += test_cases_env.headers; + +objects_static = [] +test_cases_env.add_source_files(objects_static, test_cases_env.sources, shared=False); +test_cases_env.program = test_cases_env.Program('#bin/tests', [objects_static]); +# , env.library_static]); + +# Set Alias +env.Alias('test', test_cases_env.program); +env.Alias('check', test_cases_env.program); + +env.targets += ['test','check']; diff --git a/modules/swarm/tests/capacity.cpp b/modules/swarm/tests/capacity.cpp new file mode 100644 index 0000000..c269aa1 --- /dev/null +++ b/modules/swarm/tests/capacity.cpp @@ -0,0 +1,234 @@ +#include + +#include "../c++/capacity.hpp" + +namespace { +using namespace kel::lbm::swarm; + +/** + * The packing arithmetic is small enough that the interesting thing to test is + * not that it computes, but that the ceilings it implies are the ones we think + * they are. These are compile time assertions on purpose: they are statements + * about which descriptors are usable, and the answer should not depend on + * running the test on a particular device. + */ + +SAW_TEST("Packing / planes and idle slots per descriptor"){ + // D1Q3, D2Q5, D2Q9, D3Q19, D3Q27. + static_assert(packing_math::planes(3u) == 1u, "D1Q3 needs one texture"); + static_assert(packing_math::planes(5u) == 2u, "D2Q5 needs two textures"); + static_assert(packing_math::planes(9u) == 3u, "D2Q9 needs three textures"); + static_assert(packing_math::planes(19u) == 5u, "D3Q19 needs five textures"); + static_assert(packing_math::planes(27u) == 7u, "D3Q27 needs seven textures"); + + static_assert(packing_math::idle_slots(3u) == 1u, "D1Q3 wastes one slot"); + static_assert(packing_math::idle_slots(5u) == 3u, "D2Q5 wastes three slots"); + static_assert(packing_math::idle_slots(9u) == 3u, "D2Q9 wastes three slots"); + static_assert(packing_math::idle_slots(19u) == 1u, "D3Q19 wastes one slot"); + static_assert(packing_math::idle_slots(27u) == 1u, "D3Q27 wastes one slot"); + + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Packing / slots are a partition of the texels"){ + // planes*4 is exactly Q plus the waste, for every Q. Cheap, but it is the + // invariant every slot table depends on. + for(uint64_t q = 1u; q <= 64u; ++q){ + const uint64_t total = packing_math::planes(q) * packing_math::slots_per_texel; + SAW_EXPECT(total == q + packing_math::idle_slots(q), + "planes*4 must be Q plus the idle slots"); + SAW_EXPECT(packing_math::idle_slots(q) < packing_math::slots_per_texel, + "Waste below a full texel, or a plane was over allocated"); + } +} + +/** + * The limit that decides whether a descriptor is usable at all. WebGL2 + * guarantees only four colour attachments, so the 3D descriptors cannot write + * their whole distribution in one pass on a conformant minimum spec device. + * Asserting it here means we learn it from a test rather than from a browser. + */ +SAW_TEST("Packing / single pass only reaches Q <= 16 at guaranteed limits"){ + constexpr uint64_t mrt = webgl2_guaranteed::max_draw_buffers; + + static_assert(packing_math::fits_single_pass(9u, mrt), + "D2Q9 must fit one pass on any conformant WebGL2"); + static_assert(packing_math::fits_single_pass(5u, mrt), "D2Q5 must fit"); + static_assert(packing_math::fits_single_pass(3u, mrt), "D1Q3 must fit"); + + static_assert(not packing_math::fits_single_pass(19u, mrt), + "D3Q19 must not claim a single pass at guaranteed limits"); + static_assert(not packing_math::fits_single_pass(27u, mrt), + "D3Q27 must not claim a single pass at guaranteed limits"); + + static_assert(packing_math::passes(19u, mrt) == 2u, "D3Q19 needs two passes"); + static_assert(packing_math::passes(27u, mrt) == 2u, "D3Q27 needs two passes"); + + // Sixteen is the boundary: four planes, four attachments. + static_assert(packing_math::fits_single_pass(16u, mrt), "Q=16 is the last that fits"); + static_assert(not packing_math::fits_single_pass(17u, mrt), "Q=17 must not fit"); + + SAW_EXPECT(packing_math::passes(9u, 0u) == 0u, + "A device reporting no draw buffers must not divide by zero"); +} + +SAW_TEST("Packing / stuffing aux fields into idle slots"){ + // D2Q9 has three idle slots, so a lone info flag rides along free... + static_assert(packing_math::aux_planes(9u, 1u, true) == 0u, + "One aux scalar must fit D2Q9's idle slots"); + // ...but the four fields a PSM run wants do not. + static_assert(packing_math::aux_planes(9u, 4u, true) == 1u, + "Four aux scalars must not fit three idle slots"); + // Unstuffed always pays for its own plane. + static_assert(packing_math::aux_planes(9u, 1u, false) == 1u, + "Unstuffed aux must take a plane of its own"); + + // D3Q19 and D3Q27 have exactly one idle slot, so info is free there too. + static_assert(packing_math::aux_planes(19u, 1u, true) == 0u, "D3Q19 stuffs info"); + static_assert(packing_math::aux_planes(27u, 1u, true) == 0u, "D3Q27 stuffs info"); + + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Packing / bytes per cell"){ + // D2Q9, double buffered, info stuffed: 2*3 textures * 16 bytes. + static_assert(packing_math::bytes_per_cell(9u, 2u, 1u, true) == 96u, + "D2Q9 stuffed must be 96 bytes per cell"); + // Unstuffed adds a plane: 2*3 + 1 textures. + static_assert(packing_math::bytes_per_cell(9u, 2u, 1u, false) == 112u, + "D2Q9 unstuffed must be 112 bytes per cell"); + static_assert(packing_math::bytes_per_cell(19u, 2u, 1u, true) == 160u, + "D3Q19 stuffed must be 160 bytes per cell"); + static_assert(packing_math::bytes_per_cell(27u, 2u, 1u, true) == 224u, + "D3Q27 stuffed must be 224 bytes per cell"); + + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Capacity / integer square root is exact"){ + static_assert(isqrt(0u) == 0u, "isqrt(0)"); + static_assert(isqrt(1u) == 1u, "isqrt(1)"); + static_assert(isqrt(2u) == 1u, "isqrt(2)"); + static_assert(isqrt(3u) == 1u, "isqrt(3)"); + static_assert(isqrt(4u) == 2u, "isqrt(4)"); + + // The property, not just the samples: isqrt(n)^2 <= n < (isqrt(n)+1)^2. + for(uint64_t n = 0u; n < 4096u; ++n){ + const uint64_t r = isqrt(n); + SAW_EXPECT(r * r <= n, "isqrt must not overshoot"); + SAW_EXPECT((r + 1u) * (r + 1u) > n, "isqrt must not undershoot"); + } + + // Exact squares near the top of the range, where a float sqrt would drift. + const uint64_t big = 3037000499u; + SAW_EXPECT(isqrt(big * big) == big, "isqrt must stay exact for large squares"); + SAW_EXPECT(isqrt(big * big - 1u) == big - 1u, "isqrt must round down"); +} + +SAW_TEST("Capacity / the texture bound is the squared usable edge"){ + // Ghost eats a ring off each side, so the edge is S - 2G. + static_assert(capacity_math::texture_bound_cells_2d(2048u, 0u) == 2048u * 2048u, + "No ghost means the full edge"); + static_assert(capacity_math::texture_bound_cells_2d(2048u, 1u) == 2046u * 2046u, + "A one cell ring costs two off the edge"); + + // A device whose texture dimension cannot even hold the ring has no room. + static_assert(capacity_math::texture_bound_cells_2d(2u, 1u) == 0u, + "An edge consumed entirely by ghost leaves nothing"); + + SAW_EXPECT(true, "static asserts carry this one"); +} + +/** + * The headline number: how much lattice a worker can actually hold. The point + * of the test is the relationship between the two bounds, since on real + * hardware the texture bound is roughly a hundred times looser than memory and + * a capacity model that used it would be badly wrong. + */ +SAW_TEST("Capacity / memory binds before texture size on real devices"){ + constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true); + static_assert(bpc == 96u, "D2Q9 stuffed"); + + // A typical desktop: 16384 max texture, 512MB of budget. + constexpr uint64_t desktop_tex = 16384u; + constexpr uint64_t budget = 512ull * 1024ull * 1024ull; + + constexpr uint64_t by_texture = capacity_math::texture_bound_cells_2d(desktop_tex, 0u); + constexpr uint64_t by_memory = capacity_math::cells_from_bytes(budget, bpc); + + static_assert(by_memory < by_texture, + "Memory has to be the binding constraint on a typical desktop"); + static_assert(by_texture / by_memory > 40u, + "The texture bound should be looser by a wide margin, not a hair"); + + constexpr uint64_t cells = capacity_math::max_cells_2d(budget, bpc, desktop_tex, 0u); + static_assert(cells == by_memory, "max_cells must take the tighter bound"); + + SAW_EXPECT(cells * bpc <= budget, "The answer has to fit the budget it came from"); +} + +SAW_TEST("Capacity / minimum spec can be texture bound instead"){ + constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true); + + // Guaranteed limits with a generous budget: now the texture dimension is + // what runs out first, which is a different thing to report upstream. + constexpr uint64_t tex = webgl2_guaranteed::max_texture_size; + constexpr uint64_t budget = 4ull * 1024ull * 1024ull * 1024ull; + + constexpr uint64_t by_texture = capacity_math::texture_bound_cells_2d(tex, 1u); + constexpr uint64_t by_memory = capacity_math::cells_from_bytes(budget, bpc); + + static_assert(by_texture < by_memory, "Texture size must bind at minimum spec"); + static_assert(capacity_math::max_cells_2d(budget, bpc, tex, 1u) == by_texture, + "max_cells must take the tighter bound here too"); + + SAW_EXPECT(true, "static asserts carry this one"); +} + +SAW_TEST("Capacity / a fitted domain always fits"){ + constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true); + + // Sweep budgets and aspect ratios; the invariant is that the answer never + // exceeds the cell count it was given, and never exceeds the texture edge. + const uint64_t aspects[][2] = {{1u,1u}, {4u,1u}, {1u,4u}, {16u,9u}, {2u,1u}}; + const uint64_t tex = 16384u; + const uint64_t ghost = 1u; + const uint64_t edge = tex - 2u * ghost; + + for(uint64_t mb = 1u; mb <= 2048u; mb *= 2u){ + const uint64_t budget = mb * 1024ull * 1024ull; + const uint64_t cells = capacity_math::max_cells_2d(budget, bpc, tex, ghost); + + for(const auto& a : aspects){ + const auto dom = capacity_math::fit_domain_2d(cells, a[0], a[1], tex, ghost); + + SAW_EXPECT(dom.cells() <= cells, + "A fitted domain must not exceed the measured capacity"); + SAW_EXPECT(dom.nx <= edge and dom.ny <= edge, + "A fitted domain must not exceed the texture edge"); + SAW_EXPECT(dom.nx > 0u and dom.ny > 0u, + "A non zero budget must yield a non empty domain"); + SAW_EXPECT(dom.cells() * bpc <= budget, + "The domain must fit the byte budget it was derived from"); + } + } +} + +SAW_TEST("Capacity / fitting respects the requested aspect"){ + // 4:1 on a square-ish budget should come out roughly four times as wide as + // it is tall. Exactness is not the claim -- flooring twice costs a little -- + // but the shape has to be recognisably what was asked for. + const auto dom = capacity_math::fit_domain_2d(1024u * 256u, 4u, 1u, 16384u, 0u); + + SAW_EXPECT(dom.nx > dom.ny, "A 4:1 request must be wider than tall"); + const uint64_t ratio_x100 = (dom.nx * 100u) / dom.ny; + SAW_EXPECT(ratio_x100 >= 380u and ratio_x100 <= 420u, + "A 4:1 request must land near 4:1"); + + // Degenerate inputs are answered, not asserted on. + const auto zero = capacity_math::fit_domain_2d(0u, 4u, 1u, 16384u, 0u); + SAW_EXPECT(zero.cells() == 0u, "No capacity means no domain"); + const auto bad = capacity_math::fit_domain_2d(1024u, 0u, 1u, 16384u, 0u); + SAW_EXPECT(bad.cells() == 0u, "A degenerate aspect must not divide by zero"); +} +} diff --git a/modules/swarm/tests/interpret.cpp b/modules/swarm/tests/interpret.cpp new file mode 100644 index 0000000..53374f1 --- /dev/null +++ b/modules/swarm/tests/interpret.cpp @@ -0,0 +1,133 @@ +#include + +#include "../c++/interpret.hpp" + +#include + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +/** + * The orchestrator's webgl2 branch cannot be reached by the native worker, and + * reaching it with a real browser means a human in the loop. These cover the + * part of it that is pure: turning a reported capability into a capacity and a + * summary line. + */ + +data make_gl(uint64_t safe_bytes, uint64_t max_tex, + bool float_render = true){ + data gl; + gl.get<"limits">().get<"max_texture_size">().set( + static_cast(max_tex)); + gl.get<"limits">().get<"max_draw_buffers">().set(8u); + gl.get<"budget">().get<"probed_bytes_safe">().set(safe_bytes); + gl.get<"features">().get<"color_buffer_float">().set(float_render); + return gl; +} + +data make_native(uint64_t memory, uint32_t threads, + bool sycl = false){ + data nat; + nat.get<"threads">().set(threads); + nat.get<"memory_bytes">().set(memory); + nat.get<"cpu">().set(std::string{"x86_64"}); + nat.get<"has_sycl">().set(sycl); + return nat; +} + +SAW_TEST("Interpret / the reference packing is D2Q9 double buffered"){ + static_assert(reference_bytes_per_cell() == 96u, + "Capacity figures are quoted in 96 byte D2Q9 cells"); + SAW_EXPECT(true, "static assert carries this one"); +} + +SAW_TEST("Interpret / a browser's capacity comes from its measured budget"){ + // 512MiB safe budget on a roomy device: memory binds, not texture size. + const auto gl = make_gl(512ull * 1024ull * 1024ull, 16384u); + const uint64_t cells = webgl2_capacity_cells(gl); + + SAW_EXPECT(cells == (512ull * 1024ull * 1024ull) / 96u, + "Capacity is the budget divided by the reference cell size"); + SAW_EXPECT(cells * 96u <= 512ull * 1024ull * 1024ull, + "and must fit the budget it came from"); +} + +SAW_TEST("Interpret / a small texture dimension binds before memory"){ + // Generous budget, minimum spec texture size: the texture bound wins. + const auto gl = make_gl(4ull * 1024ull * 1024ull * 1024ull, 2048u); + const uint64_t cells = webgl2_capacity_cells(gl); + + SAW_EXPECT(cells == 2048ull * 2048ull, + "The texture dimension is the binding constraint here"); +} + +SAW_TEST("Interpret / a device that measured nothing has no capacity"){ + // What a worker reports when the probe failed outright -- the case the + // node self test produces, and the one a browser produces on a device + // with no float render target. + const auto gl = make_gl(0u, 16384u, false); + SAW_EXPECT(webgl2_capacity_cells(gl) == 0u, + "No measured budget means no capacity, not a guess from the limits"); +} + +SAW_TEST("Interpret / the summary says when a device cannot render float"){ + const auto usable = make_gl(256ull * 1024ull * 1024ull, 16384u, true); + const std::string ok_line = webgl2_summary(usable); + SAW_EXPECT(ok_line.find("tex 16384") != std::string::npos, + "The summary names the texture limit"); + SAW_EXPECT(ok_line.find("256.0 MiB") != std::string::npos, + "and the budget, in units a human reads"); + SAW_EXPECT(ok_line.find("NO FLOAT RENDER") == std::string::npos, + "A usable device is not flagged"); + + const auto broken = make_gl(0u, 16384u, false); + SAW_EXPECT(webgl2_summary(broken).find("NO FLOAT RENDER") != std::string::npos, + "A device with no float render target is flagged loudly, since that " + "is the difference between slow and impossible"); +} + +SAW_TEST("Interpret / a native worker's capacity leaves room for the machine"){ + const auto nat = make_native(32ull * 1024ull * 1024ull * 1024ull, 16u); + const uint64_t cells = native_capacity_cells(nat); + + // A quarter of memory, because the rest of the machine exists. + SAW_EXPECT(cells == (32ull * 1024ull * 1024ull * 1024ull / 4u) / 96u, + "Native capacity is a quarter of memory in reference cells"); + SAW_EXPECT(cells * 96u * 4u <= 32ull * 1024ull * 1024ull * 1024ull, + "and stays well inside what the machine has"); +} + +SAW_TEST("Interpret / native and browser capacities are comparable"){ + // The point of quoting both in the same unit: a registry holding a mix of + // kinds can rank them against each other. + const auto gl = make_gl(2ull * 1024ull * 1024ull * 1024ull, 16384u); + const auto nat = make_native(8ull * 1024ull * 1024ull * 1024ull, 8u); + + const uint64_t browser_cells = webgl2_capacity_cells(gl); + const uint64_t native_cells = native_capacity_cells(nat); + + // 2GiB measured vs 8GiB/4 = 2GiB planned: the same, which is the check + // that one unit really is being used for both. + SAW_EXPECT(browser_cells == native_cells, + "Equivalent budgets must produce equal capacities across kinds"); +} + +SAW_TEST("Interpret / the native summary reports what a process can know"){ + const std::string plain = native_summary( + make_native(8ull * 1024ull * 1024ull * 1024ull, 4u)); + SAW_EXPECT(plain.find("4 threads") != std::string::npos, "thread count"); + SAW_EXPECT(plain.find("8.0 GiB") != std::string::npos, "memory"); + SAW_EXPECT(plain.find("x86_64") != std::string::npos, "architecture"); + SAW_EXPECT(plain.find("sycl") == std::string::npos, + "and does not claim sycl when there is none"); + + const std::string with_sycl = native_summary( + make_native(8ull * 1024ull * 1024ull * 1024ull, 4u, true)); + SAW_EXPECT(with_sycl.find("sycl") != std::string::npos, + "but says so when there is"); +} +} diff --git a/modules/swarm/tests/probe_search.cpp b/modules/swarm/tests/probe_search.cpp new file mode 100644 index 0000000..42f852b --- /dev/null +++ b/modules/swarm/tests/probe_search.cpp @@ -0,0 +1,202 @@ +#include + +#include "../c++/capacity.hpp" +#include "../c++/probe_search.hpp" + +#include + +namespace { +using namespace kel::lbm::swarm; + +/** + * Runs a search against a simulated device whose real capacity is `truth`, + * returning the outcome. This is the whole point of keeping the strategy apart + * from the GL calls: a device that would take a browser and several seconds to + * interrogate is a predicate here. + */ +struct probe_outcome { + uint64_t best; + uint64_t trials; + bool hit_ceiling; +}; + +probe_outcome run_against(uint64_t truth, allocation_search::config cfg = {}){ + allocation_search search{cfg}; + + while(not search.done()){ + search.report(search.candidate() <= truth); + } + + return probe_outcome{search.best_cells(), search.trials(), + search.hit_ceiling()}; +} + +SAW_TEST("Probe / never reports more than the device can do"){ + // The property that matters most. Over-reporting means the orchestrator + // hands out a domain that does not fit, and the worker fails at the point + // where it is least convenient. + const uint64_t truths[] = { + 65536u, 100000u, 1000000u, 2097152u, 5600000u, 12345678u, + 99999999u, 1u << 28, + }; + + for(uint64_t truth : truths){ + const auto out = run_against(truth); + SAW_EXPECT(out.best <= truth, + "A probe must never claim more than the device allows"); + } +} + +SAW_TEST("Probe / gets close to the real capacity"){ + // Within a quarter is plenty: the figure is a planning estimate against a + // budget that shifts as other tabs come and go. + const uint64_t truths[] = { + 200000u, 1000000u, 3000000u, 5600000u, 22000000u, + }; + + for(uint64_t truth : truths){ + const auto out = run_against(truth); + SAW_EXPECT(out.best * 4u >= truth * 3u, + std::string{"Probe landed too far below the truth for "} + + std::to_string(truth)); + } +} + +SAW_TEST("Probe / converges in a handful of trials"){ + // Doubling then bisecting should cost a dozen or so attempts even across + // three orders of magnitude of device. + allocation_search::config cfg; + + for(uint64_t truth = 65536u; truth < (1ull << 30); truth *= 3u){ + const auto out = run_against(truth, cfg); + SAW_EXPECT(out.trials <= cfg.max_trials, + "The backstop must hold"); + SAW_EXPECT(out.trials <= 20u, + std::string{"Too many trials for truth "} + std::to_string(truth)); + } +} + +SAW_TEST("Probe / a device that cannot do the opening attempt reports zero"){ + allocation_search::config cfg; + cfg.start_cells = 65536u; + + const auto out = run_against(1000u, cfg); + SAW_EXPECT(out.best == 0u, + "Failing the smallest attempt must report no capacity, not a guess"); + SAW_EXPECT(out.trials == 1u, "and must not keep trying"); +} + +SAW_TEST("Probe / the texture ceiling is reported, not confused with memory"){ + allocation_search::config cfg; + cfg.ceiling_cells = 1000000u; + + // Memory is effectively unlimited here, so the ceiling is what stops it. + const auto out = run_against(1ull << 40, cfg); + SAW_EXPECT(out.best == cfg.ceiling_cells, "It reaches the ceiling exactly"); + SAW_EXPECT(out.hit_ceiling, + "and says the texture dimension stopped it, not memory"); + + // Whereas a device that runs out of memory first must not claim the + // ceiling stopped it -- those are different things to report upstream. + const auto mem = run_against(300000u, cfg); + SAW_EXPECT(not mem.hit_ceiling, + "Running out of memory below the ceiling is not a ceiling hit"); + SAW_EXPECT(mem.best <= 300000u, "and still must not over-report"); +} + +SAW_TEST("Probe / a ceiling below the opening attempt is clamped"){ + allocation_search::config cfg; + cfg.start_cells = 65536u; + cfg.ceiling_cells = 1024u; + + allocation_search search{cfg}; + SAW_EXPECT(search.candidate() == 1024u, + "The first attempt must not exceed the ceiling"); + + const auto out = run_against(1ull << 30, cfg); + SAW_EXPECT(out.best == 1024u, "and the answer is the ceiling"); + SAW_EXPECT(out.hit_ceiling, "reported as such"); +} + +SAW_TEST("Probe / a zero ceiling means no capacity at all"){ + allocation_search::config cfg; + cfg.ceiling_cells = 0u; + cfg.start_cells = 0u; + + allocation_search search{cfg}; + SAW_EXPECT(search.done(), "Nothing to try means the search is already over"); + SAW_EXPECT(search.best_cells() == 0u, "and the answer is zero"); +} + +SAW_TEST("Probe / reporting after completion is ignored"){ + allocation_search search{allocation_search::config{}}; + while(not search.done()){ + search.report(false); + } + + const uint64_t trials = search.trials(); + search.report(true); + SAW_EXPECT(search.trials() == trials, + "A late answer must not restart a finished search"); + SAW_EXPECT(search.best_cells() == 0u, "nor change the result"); +} + +/** + * A device that answers inconsistently -- which a real one can, since memory + * pressure moves while you are measuring -- must still terminate. + */ +SAW_TEST("Probe / an unreliable device still terminates"){ + allocation_search::config cfg; + + // Alternating yes/no regardless of size. + { + allocation_search search{cfg}; + bool flip = true; + uint64_t guard = 0u; + while(not search.done()){ + search.report(flip); + flip = not flip; + SAW_EXPECT(++guard < 1000u, "The search must terminate"); + } + SAW_EXPECT(search.trials() <= cfg.max_trials, "within the backstop"); + } + + // Always yes, unbounded device: the doubling has to stop somewhere. + { + allocation_search search{cfg}; + uint64_t guard = 0u; + while(not search.done()){ + search.report(true); + SAW_EXPECT(++guard < 1000u, "Unbounded success must still terminate"); + } + SAW_EXPECT(search.trials() <= cfg.max_trials, "within the backstop"); + } +} + +/** + * The probe answers in cells; the orchestrator wants a domain. Checking the + * two compose means the number that comes off a device is usable as it stands. + */ +SAW_TEST("Probe / the measured capacity feeds the domain fitting"){ + constexpr uint64_t bpc = packing_math::bytes_per_cell(9u, 2u, 1u, true); + const uint64_t max_tex = 16384u; + + // A device good for about 512MB of lattice. + const uint64_t truth = capacity_math::cells_from_bytes( + 512ull * 1024ull * 1024ull, bpc); + + allocation_search::config cfg; + cfg.ceiling_cells = capacity_math::texture_bound_cells_2d(max_tex, 0u); + + const auto out = run_against(truth, cfg); + SAW_EXPECT(out.best <= truth, "Measured capacity must not exceed the truth"); + SAW_EXPECT(not out.hit_ceiling, + "Memory binds well before the texture dimension on a device like this"); + + const auto dom = capacity_math::fit_domain_2d(out.best, 4u, 1u, max_tex, 0u); + SAW_EXPECT(dom.cells() <= out.best, + "The fitted domain must fit what was actually measured"); + SAW_EXPECT(dom.cells() * bpc <= 512ull * 1024ull * 1024ull, + "and therefore fit in the memory the device really had"); +} +} diff --git a/modules/swarm/tests/registry.cpp b/modules/swarm/tests/registry.cpp new file mode 100644 index 0000000..45ebab3 --- /dev/null +++ b/modules/swarm/tests/registry.cpp @@ -0,0 +1,298 @@ +#include + +#include "../c++/message.hpp" +#include "../c++/registry.hpp" + +#include + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +/** + * The registry takes `now_ms` at every entry point rather than reading a clock, + * so these tests drive a worker from healthy to dropped in microseconds. Doing + * this against a real connection would mean sleeping through several heartbeat + * intervals and hoping the timing held. + */ + +data some_live(){ + data live; + live.get<"state">().set(std::string{wstate::ready}); + live.get<"uptime_ms">().set(0u); + live.get<"steps_done">().set(0u); + live.get<"last_error">().set(std::string{""}); + return live; +} + +/** + * Registers one worker into an existing registry. Fills in place rather than + * returning one: registry holds a mutex, so it is neither copyable nor movable. + */ +void register_one(registry ®){ + reg.add_session("w1", 1000u); + reg.mark_registered("w1", "alpha", std::string{wkind::native}, 1000u, + "test worker", 1000u); +} + +worker_record only(const registry ®){ + auto snap = reg.snapshot(); + return snap.empty() ? worker_record{} : snap.front(); +} + +SAW_TEST("Registry / a session starts in signalling and is not pinged"){ + registry reg; + reg.add_session("w1", 100u); + + SAW_EXPECT(reg.size() == 1u, "The session is tracked"); + SAW_EXPECT(only(reg).state == worker_state::signalling, + "A fresh session has not said hello yet"); + + // Crucially the thresholds must not apply before a hello. A worker still + // negotiating is not unhealthy, and counting misses against it would drop + // it before it ever had a channel to answer on. + for(uint64_t i = 0u; i < 20u; ++i){ + auto result = reg.tick(200u + i); + SAW_EXPECT(result.to_ping.empty(), "A signalling session is not pinged"); + SAW_EXPECT(result.dropped.empty(), "A signalling session is not dropped"); + } + SAW_EXPECT(reg.size() == 1u, "It is still there after many ticks"); +} + +SAW_TEST("Registry / registration records the capability"){ + registry reg; + register_one(reg); + const auto rec = only(reg); + + SAW_EXPECT(rec.state == worker_state::registered, "hello moves it on"); + SAW_EXPECT(rec.worker_id == "alpha", "The self reported name is kept"); + SAW_EXPECT(rec.kind == wkind::native, "The kind is kept"); + SAW_EXPECT(rec.capacity_cells == 1000u, "The capacity is kept"); + SAW_EXPECT(rec.session_id == "w1", "Keyed on the assigned id"); +} + +SAW_TEST("Registry / registering an unknown session fails rather than creating one"){ + registry reg; + SAW_EXPECT(not reg.mark_registered("ghost", "a", "native", 1u, "", 1u), + "A hello from a session that has gone must not resurrect it"); + SAW_EXPECT(reg.size() == 0u, "and must not invent a record"); +} + +SAW_TEST("Registry / a ping is outstanding until its pong"){ + registry reg; + register_one(reg); + + auto t1 = reg.tick(2000u); + SAW_EXPECT(t1.to_ping.size() == 1u, "One worker, one ping"); + SAW_EXPECT(t1.to_ping.front().seq == 1u, "Sequences start at one"); + SAW_EXPECT(only(reg).outstanding_pings == 1u, "The ping is outstanding"); + + reg.record_pong("w1", 1u, 2000u, some_live(), 2020u); + SAW_EXPECT(only(reg).outstanding_pings == 0u, "The pong clears it"); + SAW_EXPECT(only(reg).pongs == 1u, "and is counted"); +} + +SAW_TEST("Registry / round trip is measured against our own clock"){ + registry reg; + register_one(reg); + + reg.tick(2000u); + // The worker echoes the timestamp we sent, so no clock agreement needed. + reg.record_pong("w1", 1u, 2000u, some_live(), 2040u); + SAW_EXPECT(only(reg).rtt_ewma_ms == 40.0, + "The first sample seeds the average exactly"); + + // Second sample at 80ms: 0.25*80 + 0.75*40 = 50. + reg.tick(3000u); + reg.record_pong("w1", 2u, 3000u, some_live(), 3080u); + const double rtt = only(reg).rtt_ewma_ms; + SAW_EXPECT(rtt > 49.9 and rtt < 50.1, "Smoothing weights the history"); +} + +SAW_TEST("Registry / a clock that goes backwards does not produce a negative rtt"){ + registry reg; + register_one(reg); + + reg.tick(2000u); + // Echoed timestamp later than now. Should clamp, not underflow into a + // huge positive via unsigned subtraction. + reg.record_pong("w1", 1u, 5000u, some_live(), 2000u); + SAW_EXPECT(only(reg).rtt_ewma_ms == 0.0, "A backwards clock reads as zero"); +} + +/** + * The transition that actually matters, and the one that is tedious to provoke + * against a live connection. + */ +SAW_TEST("Registry / stale then dropped, at the configured thresholds"){ + worker_thresholds limits; + limits.stale_after = 2u; + limits.drop_after = 4u; + + registry reg{limits}; + register_one(reg); + uint64_t t = 2000u; + + // Beat 1 and 2: outstanding climbs, still considered healthy. + auto b1 = reg.tick(t += 100u); + SAW_EXPECT(b1.newly_stale.empty(), "One miss is not yet doubtful"); + SAW_EXPECT(only(reg).state == worker_state::registered, "still registered"); + + auto b2 = reg.tick(t += 100u); + SAW_EXPECT(b2.newly_stale.empty(), "Two outstanding, threshold not crossed yet"); + + // Beat 3 sees outstanding == 2, which meets stale_after. + auto b3 = reg.tick(t += 100u); + SAW_EXPECT(b3.newly_stale.size() == 1u, "Crossing into stale is reported once"); + SAW_EXPECT(b3.newly_stale.front() == "w1", "and names the session"); + SAW_EXPECT(only(reg).state == worker_state::stale, "and is recorded"); + + // Reported once, not every beat afterwards -- otherwise the log fills up. + auto b4 = reg.tick(t += 100u); + SAW_EXPECT(b4.newly_stale.empty(), "Stale is announced once, not repeatedly"); + SAW_EXPECT(only(reg).state == worker_state::stale, "and it stays stale"); + + // Beat 5 sees outstanding == 4, which meets drop_after. + auto b5 = reg.tick(t += 100u); + SAW_EXPECT(b5.dropped.size() == 1u, "It is dropped at the threshold"); + SAW_EXPECT(b5.dropped.front().worker_id == "alpha", + "The dropped record is handed back so the caller can log it"); + SAW_EXPECT(reg.size() == 0u, "and removed from the registry"); + SAW_EXPECT(b5.to_ping.empty(), "A dropped session is not also pinged"); +} + +SAW_TEST("Registry / answering again clears stale"){ + worker_thresholds limits; + limits.stale_after = 2u; + limits.drop_after = 10u; + + registry reg{limits}; + register_one(reg); + uint64_t t = 2000u; + + reg.tick(t += 100u); + reg.tick(t += 100u); + auto b3 = reg.tick(t += 100u); + SAW_EXPECT(only(reg).state == worker_state::stale, "It went stale"); + + // The worker comes back, answering the ping that is actually outstanding. + reg.record_pong("w1", b3.to_ping.front().seq, t, some_live(), t + 5u); + + SAW_EXPECT(only(reg).state == worker_state::registered, + "Answering clears the doubt"); + SAW_EXPECT(only(reg).outstanding_pings == 0u, "and resets the miss count"); +} + +SAW_TEST("Registry / recovery does not silently restore readiness"){ + // Readiness is something only the worker can assert, so a recovered worker + // goes back to registered and has to say 'ready' again. + worker_thresholds limits; + limits.stale_after = 1u; + limits.drop_after = 10u; + + registry reg{limits}; + register_one(reg); + reg.mark_ready("w1", some_live(), 1500u); + SAW_EXPECT(only(reg).state == worker_state::ready, "It was ready"); + + reg.tick(2000u); + auto b2 = reg.tick(2100u); + SAW_EXPECT(only(reg).state == worker_state::stale, "Then it went quiet"); + + reg.record_pong("w1", b2.to_ping.front().seq, 2100u, some_live(), 2110u); + SAW_EXPECT(only(reg).state == worker_state::registered, + "Recovery must not reassert readiness on the worker's behalf"); +} + +/** + * A worker consistently one beat behind would otherwise look perfectly healthy: + * every tick adds a miss, every late pong clears it, and the miss count never + * reaches the threshold. + */ +SAW_TEST("Registry / a stale sequence pong does not clear the miss count"){ + registry reg; + register_one(reg); + + auto b1 = reg.tick(2000u); + const uint64_t old_seq = b1.to_ping.front().seq; + + reg.tick(2100u); + reg.tick(2200u); + SAW_EXPECT(only(reg).outstanding_pings == 3u, "Three pings outstanding"); + + // Answer the first one, long after it was superseded. + reg.record_pong("w1", old_seq, 2000u, some_live(), 2250u); + + SAW_EXPECT(only(reg).outstanding_pings == 3u, + "A pong for a superseded ping must not clear the misses"); + SAW_EXPECT(only(reg).pongs == 0u, "nor count as an answer"); + SAW_EXPECT(only(reg).last_seen_ms == 2250u, + "but it does prove the channel is alive"); +} + +SAW_TEST("Registry / removal returns the record once"){ + registry reg; + register_one(reg); + + auto gone = reg.remove("w1"); + SAW_EXPECT(gone.has_value(), "Removing returns what was removed"); + SAW_EXPECT(gone->worker_id == "alpha", "with its contents intact"); + SAW_EXPECT(reg.size() == 0u, "and empties the registry"); + + // Idempotent: several teardown paths converge on this and whichever + // arrives first should win without the others erroring. + SAW_EXPECT(not reg.remove("w1").has_value(), "A second removal is a no-op"); +} + +SAW_TEST("Registry / many workers are ticked independently"){ + worker_thresholds limits; + limits.stale_after = 2u; + limits.drop_after = 3u; + + registry reg{limits}; + for(uint64_t i = 0u; i < 5u; ++i){ + const std::string id = "w" + std::to_string(i); + reg.add_session(id, 1000u); + reg.mark_registered(id, "worker" + std::to_string(i), "native", + 1000u * i, "", 1000u); + } + + uint64_t t = 1000u; + for(int beat = 0; beat < 3; ++beat){ + auto result = reg.tick(t += 100u); + // w3 keeps answering; everyone else goes quiet. + for(const auto &target : result.to_ping){ + if(target.session_id == "w3"){ + reg.record_pong("w3", target.seq, t, some_live(), t + 1u); + } + } + } + + auto result = reg.tick(t += 100u); + SAW_EXPECT(result.dropped.size() == 4u, "The four silent workers are dropped"); + SAW_EXPECT(reg.size() == 1u, "The responsive one survives"); + SAW_EXPECT(only(reg).session_id == "w3", "and it is the right one"); +} + +SAW_TEST("Registry / the table renders without a worker and with one"){ + registry reg; + SAW_EXPECT(render_table(reg.snapshot(), 0u).find("(no workers)") + != std::string::npos, "An empty registry says so"); + + registry one; + register_one(one); + one.tick(2000u); + one.record_pong("w1", 1u, 2000u, some_live(), 2010u); + + const std::string table = render_table(one.snapshot(), 3000u); + SAW_EXPECT(table.find("alpha") != std::string::npos, "The worker is listed"); + SAW_EXPECT(table.find("native") != std::string::npos, "with its kind"); + SAW_EXPECT(table.find("10.0ms") != std::string::npos, "and its round trip"); + // 1000 cells renders as a bare count, not scaled. + SAW_EXPECT(table.find("1.0k") != std::string::npos, + "and a human readable capacity"); +} +} diff --git a/modules/swarm/tests/run.cpp b/modules/swarm/tests/run.cpp new file mode 100644 index 0000000..b7cd8f5 --- /dev/null +++ b/modules/swarm/tests/run.cpp @@ -0,0 +1,158 @@ +#include + +#include "../c++/run.hpp" +#include "../c++/wire.hpp" + +#include +#include + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +/// 2026-11-12T09:45:01Z, as milliseconds since the epoch. +constexpr uint64_t reference_ms = 1794476701000ull; + +SAW_TEST("Run / the id has the documented shape"){ + // Pinned exactly rather than pattern matched: make_run_id takes its clock + // and entropy, so the whole string is determined and a drift in either + // half should fail loudly. + const std::string id = make_run_id(reference_ms, 0xa3f19cull); + SAW_EXPECT(id == "20261112T094501Z-a3f19c", + std::string{"unexpected run id: "} + id); + SAW_EXPECT(id.size() == 23u, "and the fixed width the sink relies on"); +} + +SAW_TEST("Run / only the low 24 bits of entropy are used"){ + const std::string a = make_run_id(reference_ms, 0xffffffffffffffffull); + SAW_EXPECT(a == "20261112T094501Z-ffffff", "the top bits are discarded"); + + const std::string b = make_run_id(reference_ms, 0x0ull); + SAW_EXPECT(b == "20261112T094501Z-000000", "and zero pads rather than shortens"); +} + +SAW_TEST("Run / ids sort chronologically as plain strings"){ + // The point of the format: `ls` and a std::map give time order for free, + // with no parsing anywhere. + const std::string earlier = make_run_id(reference_ms, 0xffffffull); + const std::string later = make_run_id(reference_ms + 1000u, 0x000000ull); + + SAW_EXPECT(earlier < later, + "A later run must sort after an earlier one even with lower entropy"); + + // Across a month and a year boundary, where a naive format breaks. + SAW_EXPECT(make_run_id(reference_ms, 0u) + < make_run_id(reference_ms + 30ull * 86400000ull, 0u), + "and across a month"); +} + +SAW_TEST("Run / the civil date conversion handles the awkward cases"){ + using kel::lbm::swarm::impl::civil_from_days; + + // The epoch itself. + { + const auto d = civil_from_days(0); + SAW_EXPECT(d.year == 1970 and d.month == 1u and d.day == 1u, "epoch"); + } + // A leap day, which is where a hand-rolled conversion usually goes wrong. + { + const auto d = civil_from_days(19782); // 2024-02-29 + SAW_EXPECT(d.year == 2024 and d.month == 2u and d.day == 29u, + "2024 is a leap year and the 29th exists"); + } + // 2000 was a leap year despite being a century. + { + const auto d = civil_from_days(11016); // 2000-02-29 + SAW_EXPECT(d.year == 2000 and d.month == 2u and d.day == 29u, + "the 400 year rule"); + } + // The day after the 2024 leap day, so an off-by-one shows. + { + const auto d = civil_from_days(19783); + SAW_EXPECT(d.year == 2024 and d.month == 3u and d.day == 1u, "march 1st"); + } +} + +SAW_TEST("Run / midnight and the last second of a day format correctly"){ + // 2026-11-12T00:00:00Z + const uint64_t midnight = (reference_ms / 86400000ull) * 86400000ull; + SAW_EXPECT(make_run_id(midnight, 0u) == "20261112T000000Z-000000", + "midnight is all zeroes, not blank"); + + SAW_EXPECT(make_run_id(midnight + 86399000ull, 0u) + == "20261112T235959Z-000000", "and the last second is 235959"); + + // One more second rolls the date rather than producing 24:00:00. + SAW_EXPECT(make_run_id(midnight + 86400000ull, 0u) + == "20261113T000000Z-000000", "which rolls into the next day"); +} + +/** + * The sink uses a run id as a directory name, so this is the check that stops + * a hostile id escaping the storage root. + */ +SAW_TEST("Run / validation rejects anything that is not the exact shape"){ + SAW_EXPECT(is_run_id(make_run_id(reference_ms, 0xabcdefull)), + "what make_run_id produces must validate"); + + const char *bad[] = { + "", + "20261112T094501Z", // no entropy + "20261112T094501Z-a3f19", // one short + "20261112T094501Z-a3f19cd", // one long + "20261112t094501Z-a3f19c", // lowercase separator + "20261112T094501Z-A3F19C", // uppercase hex + "20261112T094501Z-a3f19g", // 'g' is not hex + "2026111 T094501Z-a3f19c", // space + "../../etc/passwd000000Z-a3f19c", // the reason this function exists + "20261112T094501Z/a3f19c", // a separator that is a path one + }; + + for(const char *id : bad){ + SAW_EXPECT(not is_run_id(id), + std::string{"must be rejected: "} + id); + } +} + +SAW_TEST("Run / distinct entropy gives distinct ids within one second"){ + std::set seen; + for(uint64_t e = 0u; e < 512u; ++e){ + seen.insert(make_run_id(reference_ms, e)); + } + SAW_EXPECT(seen.size() == 512u, + "Two runs started in the same second must not collide"); +} + +SAW_TEST("Run / a run descriptor round trips on the wire"){ + auto start = make_message(msg::run_start, "orch", 0u); + + auto &run = start.get<"run">(); + run.get<"run_id">().set(make_run_id(reference_ms, 0xa3f19cull)); + run.get<"name">().set(std::string{"poiseuille 1024x256"}); + run.get<"created_at_ms">().set(reference_ms); + run.get<"schema_version">().set(std::string{schema_version}); + run.get<"halo_exchange">().set(false); + run.get<"params_json">().set(std::string{R"({"nx":1024,"ny":256})"}); + + auto eo_text = encode_message(start); + SAW_EXPECT(not eo_text.is_error(), "a run start encodes"); + + auto eo_hdr = peek_header(eo_text.get_value()); + SAW_EXPECT(not eo_hdr.is_error(), "and routes"); + SAW_EXPECT(header_type(eo_hdr.get_value()) == msg::run_start, "as a run start"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "and decodes"); + + const auto &back = eo_back.get_value().get<"run">(); + SAW_EXPECT(back.get<"run_id">().stl_view() == "20261112T094501Z-a3f19c", + "the id survives"); + SAW_EXPECT(not back.get<"halo_exchange">().get(), + "and the record that the seams were clamped survives with it"); + SAW_EXPECT(back.get<"params_json">().stl_view() == R"({"nx":1024,"ny":256})", + "the opaque params survive being nested inside JSON"); +} +} diff --git a/modules/swarm/tests/signal.cpp b/modules/swarm/tests/signal.cpp new file mode 100644 index 0000000..fc12193 --- /dev/null +++ b/modules/swarm/tests/signal.cpp @@ -0,0 +1,82 @@ +#include + +#include "../c++/signal.hpp" +#include "../c++/wire.hpp" + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +SAW_TEST("Signal / an offer round trips"){ + data desc; + desc.get<"type">().set(std::string{sig::offer}); + desc.get<"sdp">().set(std::string{"v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\n"}); + + auto eo_text = encode_message(desc); + SAW_EXPECT(not eo_text.is_error(), "An offer must encode"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "An offer must decode"); + SAW_EXPECT(eo_back.get_value().get<"type">().stl_view() == sig::offer, + "type survives"); + // SDP is multi-line and full of characters JSON has to escape; that it + // comes back byte for byte is the only interesting property here. + SAW_EXPECT(eo_back.get_value().get<"sdp">().stl_view() + == "v=0\r\no=- 1 1 IN IP4 127.0.0.1\r\n", + "SDP must survive escaping unchanged"); +} + +SAW_TEST("Signal / a candidate round trips"){ + data cand; + cand.get<"type">().set(std::string{sig::candidate}); + cand.get<"candidate">().set(std::string{"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"}); + cand.get<"mid">().set(std::string{"0"}); + + auto eo_text = encode_message(cand); + SAW_EXPECT(not eo_text.is_error(), "A candidate must encode"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "A candidate must decode"); + SAW_EXPECT(eo_back.get_value().get<"mid">().stl_view() == "0", "mid survives"); +} + +SAW_TEST("Signal / the header routes without knowing the payload"){ + data desc; + desc.get<"type">().set(std::string{sig::answer}); + desc.get<"sdp">().set(std::string{"v=0"}); + auto eo_text = encode_message(desc); + SAW_EXPECT(not eo_text.is_error(), "encodes"); + + auto eo_hdr = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_hdr.is_error(), "A header read must skip the sdp"); + SAW_EXPECT(eo_hdr.get_value().get<"type">().stl_view() == sig::answer, + "Header reports answer"); +} + +SAW_TEST("Signal / an empty mid is carried rather than dropped"){ + // libdatachannel accepts an unhinted candidate, so the empty string has to + // survive the round trip as an empty string and not become a decode error. + data cand; + cand.get<"type">().set(std::string{sig::candidate}); + cand.get<"candidate">().set(std::string{"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"}); + cand.get<"mid">().set(std::string{""}); + + auto eo_text = encode_message(cand); + SAW_EXPECT(not eo_text.is_error(), "encodes"); + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "An empty mid must still decode"); + SAW_EXPECT(eo_back.get_value().get<"mid">().stl_view().empty(), + "An empty mid stays empty"); +} + +SAW_TEST("Signal / a candidate missing its mid is rejected"){ + // Strictness applies here too: a signalling message that lost a member in + // transit should fail loudly rather than add a candidate with no mid. + auto eo = decode_message( + R"({"type":"candidate","candidate":"candidate:1 1 UDP 100 10.0.0.1 5000 typ host"})"); + SAW_EXPECT(eo.is_error(), "A missing mid must be an error"); +} +} diff --git a/modules/swarm/tests/wire.cpp b/modules/swarm/tests/wire.cpp new file mode 100644 index 0000000..5238619 --- /dev/null +++ b/modules/swarm/tests/wire.cpp @@ -0,0 +1,381 @@ +#include + +#include "../c++/wire.hpp" + +#include + +namespace { +namespace sch { +using namespace kel::lbm::sch; +} +using namespace kel::lbm; +using namespace kel::lbm::swarm; + +/** + * Builds a filled-in live block. Every member is set, because the wire codecs + * run strict and a half built message is exactly what these tests are trying + * to catch elsewhere. + */ +data make_live(){ + data live; + live.get<"state">().set(std::string{wstate::ready}); + live.get<"uptime_ms">().set(1234u); + live.get<"steps_done">().set(42u); + live.get<"last_error">().set(std::string{""}); + return live; +} + +SAW_TEST("Wire / pong round trips unchanged"){ + auto msg = make_message(msg::pong, "worker-7", 19u); + msg.get<"sent_at_ms">().set(1000u); + msg.get<"worker_now_ms">().set(2000u); + msg.get<"live">() = make_live(); + + auto eo_text = encode_message(msg); + SAW_EXPECT(not eo_text.is_error(), "Encoding a complete message must succeed"); + const std::string text = eo_text.get_value(); + + auto eo_back = decode_message(text); + SAW_EXPECT(not eo_back.is_error(), "Decoding our own output must succeed"); + auto& back = eo_back.get_value(); + + SAW_EXPECT(back.get<"type">().stl_view() == msg::pong, "type survives"); + SAW_EXPECT(back.get<"worker_id">().stl_view() == "worker-7", "worker_id survives"); + SAW_EXPECT(back.get<"seq">().get() == 19u, "seq survives"); + SAW_EXPECT(back.get<"sent_at_ms">().get() == 1000u, "echoed timestamp survives"); + SAW_EXPECT(back.get<"worker_now_ms">().get() == 2000u, "worker clock survives"); + SAW_EXPECT(back.get<"live">().get<"steps_done">().get() == 42u, + "Nested struct members survive"); + SAW_EXPECT(back.get<"live">().get<"state">().stl_view() == wstate::ready, + "Nested strings survive"); +} + +SAW_TEST("Wire / welcome and ping round trip"){ + auto wel = make_message(msg::welcome, "worker-1", 0u); + wel.get<"heartbeat_ms">().set(2000u); + wel.get<"schema_version">().set(std::string{schema_version}); + + auto eo_text = encode_message(wel); + SAW_EXPECT(not eo_text.is_error(), "welcome encodes"); + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "welcome decodes"); + SAW_EXPECT(eo_back.get_value().get<"heartbeat_ms">().get() == 2000u, + "heartbeat survives"); + + auto png = make_message(msg::ping, "worker-1", 5u); + png.get<"sent_at_ms">().set(999u); + auto eo_ptext = encode_message(png); + SAW_EXPECT(not eo_ptext.is_error(), "ping encodes"); + auto eo_pback = decode_message(eo_ptext.get_value()); + SAW_EXPECT(not eo_pback.is_error(), "ping decodes"); + SAW_EXPECT(eo_pback.get_value().get<"sent_at_ms">().get() == 999u, + "ping timestamp survives"); +} + +/** + * The two stage decode is the whole reason this protocol works without a + * tagged union: reading the header out of a full message has to succeed for + * every message type, skipping whatever payload it does not know about. + */ +SAW_TEST("Wire / the header can be read out of any message"){ + auto png = make_message(msg::ping, "w-a", 1u); + png.get<"sent_at_ms">().set(7u); + auto eo_png = encode_message(png); + SAW_EXPECT(not eo_png.is_error(), "ping encodes"); + + auto eo_hdr = peek_header(eo_png.get_value()); + SAW_EXPECT(not eo_hdr.is_error(), + "Reading a header out of a ping must skip the payload, not fail"); + SAW_EXPECT(header_type(eo_hdr.get_value()) == msg::ping, "Header reports ping"); + SAW_EXPECT(header_worker_id(eo_hdr.get_value()) == "w-a", "Header reports the id"); + + auto pong = make_message(msg::pong, "w-b", 2u); + pong.get<"sent_at_ms">().set(1u); + pong.get<"worker_now_ms">().set(2u); + pong.get<"live">() = make_live(); + auto eo_pong = encode_message(pong); + SAW_EXPECT(not eo_pong.is_error(), "pong encodes"); + + auto eo_hdr2 = peek_header(eo_pong.get_value()); + SAW_EXPECT(not eo_hdr2.is_error(), + "A nested struct payload must also be skippable"); + SAW_EXPECT(header_type(eo_hdr2.get_value()) == msg::pong, "Header reports pong"); + SAW_EXPECT(header_worker_id(eo_hdr2.get_value()) == "w-b", "Header reports the id"); +} + +/** + * The failure this protocol must not have. codec_config::strict + * defaults to false so that config files can omit their defaults; on a wire + * that would let a worker send "{}" and have it decode into a silently zeroed + * registration. + */ +SAW_TEST("Wire / an empty object is rejected, not defaulted"){ + auto eo = decode_message("{}"); + SAW_EXPECT(eo.is_error(), + "An empty object must not decode into a default constructed message"); +} + +SAW_TEST("Wire / a missing member is rejected at every nesting depth"){ + // Top level: seq absent. + { + auto eo = decode_message( + R"({"type":"ping","worker_id":"w","sent_at_ms":1})"); + SAW_EXPECT(eo.is_error(), "A missing top level member must fail"); + } + // One level down: live is present but incomplete. This is the case that + // matters most, since strictness has to reach into nested structs or a + // half filled capability would register cleanly. + { + auto eo = decode_message( + R"({"type":"pong","worker_id":"w","seq":1,"sent_at_ms":1,)" + R"("worker_now_ms":2,"live":{"state":"ready"}})"); + SAW_EXPECT(eo.is_error(), "A missing nested member must fail"); + } + // And the complete form of the same message must still pass, so the test + // above is not just rejecting everything. + { + auto eo = decode_message( + R"({"type":"pong","worker_id":"w","seq":1,"sent_at_ms":1,)" + R"("worker_now_ms":2,"live":{"state":"ready","uptime_ms":0,)" + R"("steps_done":0,"last_error":""}})"); + SAW_EXPECT(not eo.is_error(), "The complete message must still decode"); + } +} + +/** + * Forward compatibility in the direction that works: a newer worker sending + * members an older orchestrator has never heard of still registers. + */ +SAW_TEST("Wire / unknown members are skipped"){ + auto eo = decode_message( + R"({"type":"ping","worker_id":"w","seq":3,"sent_at_ms":8,)" + R"("future_field":{"nested":[1,2,3]},"another":"x"})"); + SAW_EXPECT(not eo.is_error(), + "A newer sender's extra members must be skipped, not rejected"); + SAW_EXPECT(eo.get_value().get<"seq">().get() == 3u, "Known members still land"); + SAW_EXPECT(eo.get_value().get<"sent_at_ms">().get() == 8u, "and keep their values"); +} + +SAW_TEST("Wire / malformed input errors rather than defaulting"){ + const char* bad[] = { + "", + "{", + R"({"type":"ping","worker_id":"w","seq":1,"sent_at_ms":1)", // truncated + R"({"type":"ping","worker_id":"w","seq":1,"sent_at_ms":1}})", // trailing + R"({"type":"ping","worker_id":"w","seq":"not a number","sent_at_ms":1})", + "[1,2,3]", + "null", + }; + + for(const char* text : bad){ + auto eo = decode_message(text); + SAW_EXPECT(eo.is_error(), std::string{"Malformed input must error: "} + text); + } +} + +/** + * The capability envelope is the largest thing on the wire and the one whose + * shape has to survive being read by a process built from different source. + */ +SAW_TEST("Wire / hello carries a capability round trip"){ + auto hello = make_message(msg::hello, "worker-9", 0u); + + auto& cap = hello.get<"capability">(); + cap.get<"worker_id">().set(std::string{"worker-9"}); + cap.get<"kind">().set(std::string{wkind::webgl2}); + cap.get<"schema_version">().set(std::string{schema_version}); + cap.get<"reported_at_ms">().set(1700000000000u); + cap.get<"user_agent">().set(std::string{"test-agent"}); + + auto& gl = cap.get<"webgl2">(); + + auto& lim = gl.get<"limits">(); + lim.get<"max_texture_size">().set(16384u); + lim.get<"max_3d_texture_size">().set(2048u); + lim.get<"max_array_texture_layers">().set(2048u); + lim.get<"max_draw_buffers">().set(8u); + lim.get<"max_color_attachments">().set(8u); + lim.get<"max_texture_image_units">().set(16u); + lim.get<"max_combined_texture_image_units">().set(32u); + lim.get<"max_renderbuffer_size">().set(16384u); + lim.get<"max_fragment_uniform_vectors">().set(224u); + lim.get<"max_viewport_dims">().at({{0u}}).set(16384u); + lim.get<"max_viewport_dims">().at({{1u}}).set(16384u); + + auto& feat = gl.get<"features">(); + feat.get<"webgl2">().set(true); + feat.get<"offscreen_canvas">().set(true); + feat.get<"color_buffer_float">().set(true); + feat.get<"color_buffer_half_float">().set(true); + feat.get<"texture_float_linear">().set(false); + feat.get<"float_blend">().set(false); + feat.get<"parallel_shader_compile">().set(true); + feat.get<"disjoint_timer_query">().set(false); + + auto& bud = gl.get<"budget">(); + bud.get<"probed_bytes_max">().set(536870912u); + bud.get<"probed_bytes_safe">().set(322122547u); + bud.get<"probe_trials">().set(14u); + bud.get<"context_lost_during_probe">().set(false); + bud.get<"probe_hit_texture_size_bound">().set(false); + bud.get<"safety_factor">().set(0.6f); + + // One feasible plan, enough to prove the array of nested structs survives. + { + data plan; + plan.get<"layout">().set(std::string{"plain2d"}); + plan.get<"aux_mode">().set(std::string{"stuffed"}); + plan.get<"d">().set(2u); + plan.get<"q">().set(9u); + plan.get<"ghost">().set(0u); + plan.get<"storage_bits">().set(32u); + plan.get<"slots_per_texel">().set(4u); + plan.get<"textures_df">().set(3u); + plan.get<"textures_aux">().set(0u); + plan.get<"states">().set(2u); + plan.get<"passes">().set(1u); + plan.get<"idle_slots">().set(3u); + plan.get<"domain">().at({{0u}}).set(1024u); + plan.get<"domain">().at({{1u}}).set(256u); + plan.get<"domain">().at({{2u}}).set(1u); + plan.get<"tiles">().at({{0u}}).set(1u); + plan.get<"tiles">().at({{1u}}).set(1u); + plan.get<"tiles_pow2">().set(true); + plan.get<"texture_size">().at({{0u}}).set(1024u); + plan.get<"texture_size">().at({{1u}}).set(256u); + plan.get<"bytes_total">().set(1024u * 256u * 96u); + plan.get<"bytes_useful">().set(1024u * 256u * 96u); + + for(uint64_t i = 0u; i < 9u; ++i){ + auto eo = plan.get<"slot_tex_of_q">().add(data{static_cast(i / 4u)}); + SAW_EXPECT(not eo.is_error(), "slot_tex_of_q grows"); + auto eo2 = plan.get<"slot_cmp_of_q">().add(data{static_cast(i % 4u)}); + SAW_EXPECT(not eo2.is_error(), "slot_cmp_of_q grows"); + } + auto eo3 = plan.get<"slot_tex_of_aux">().add(data{2u}); + SAW_EXPECT(not eo3.is_error(), "slot_tex_of_aux grows"); + auto eo4 = plan.get<"slot_cmp_of_aux">().add(data{1u}); + SAW_EXPECT(not eo4.is_error(), "slot_cmp_of_aux grows"); + auto eo5 = plan.get<"aux_names">().add(data{std::string{"info"}}); + SAW_EXPECT(not eo5.is_error(), "aux_names grows"); + + auto eo6 = gl.get<"feasible">().add(std::move(plan)); + SAW_EXPECT(not eo6.is_error(), "feasible grows"); + } + + auto eo_text = encode_message(hello); + SAW_EXPECT(not eo_text.is_error(), "A complete capability must encode"); + + // The header has to be readable out of the biggest message we have. + auto eo_hdr = peek_header(eo_text.get_value()); + SAW_EXPECT(not eo_hdr.is_error(), "The header must survive a large payload"); + SAW_EXPECT(header_type(eo_hdr.get_value()) == msg::hello, "Header reports hello"); + + // Routing pass: the common fields alone are enough to learn the kind, and + // the webgl2 section is skipped because it is not named here. + auto eo_route = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_route.is_error(), "The routing decode must succeed"); + SAW_EXPECT(eo_route.get_value().get<"capability">().get<"kind">().stl_view() + == wkind::webgl2, "The routing decode must report the kind"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "A complete capability must decode"); + auto& back = eo_back.get_value().get<"capability">(); + + SAW_EXPECT(back.get<"kind">().stl_view() == wkind::webgl2, "kind survives"); + SAW_EXPECT(back.get<"webgl2">().get<"limits">().get<"max_texture_size">().get() + == 16384u, "Deeply nested integers survive"); + SAW_EXPECT(back.get<"webgl2">().get<"features">().get<"color_buffer_float">().get(), + "Booleans survive"); + SAW_EXPECT(back.get<"webgl2">().get<"budget">().get<"probe_trials">().get() == 14u, + "Budget survives"); + + auto& feasible = back.get<"webgl2">().get<"feasible">(); + SAW_EXPECT(feasible.meta().flat_at(0u).get() == 1u, "One plan came back"); + auto& plan_back = feasible.at({{0u}}); + SAW_EXPECT(plan_back.get<"q">().get() == 9u, "Plan Q survives"); + SAW_EXPECT(plan_back.get<"layout">().stl_view() == "plain2d", "Plan layout survives"); + SAW_EXPECT(plan_back.get<"slot_tex_of_q">().meta().flat_at(0u).get() == 9u, + "The slot table keeps its length"); + SAW_EXPECT(plan_back.get<"slot_tex_of_q">().at({{8u}}).get() == 2u, + "Direction 8 lives in texture 2"); + SAW_EXPECT(plan_back.get<"slot_cmp_of_q">().at({{8u}}).get() == 0u, + "Direction 8 lives in channel 0"); + SAW_EXPECT(plan_back.get<"aux_names">().at({{0u}}).stl_view() == "info", + "Arrays of strings survive"); +} + +/** + * A worker of a kind we do not know about still has to be readable far enough + * to find out that we do not know about it. This is the envelope earning its + * keep. + */ +SAW_TEST("Wire / an unknown worker kind is still routable"){ + const char* text = + R"({"type":"hello","worker_id":"w-x","seq":0,)" + R"("capability":{"worker_id":"w-x","kind":"webgpu","schema_version":"1",)" + R"("reported_at_ms":1,"user_agent":"a","webgpu":{"whatever":true}}})"; + + auto eo_hdr = peek_header(text); + SAW_EXPECT(not eo_hdr.is_error(), + "An unknown backend section must not stop the header decoding"); + SAW_EXPECT(header_type(eo_hdr.get_value()) == msg::hello, "Still a hello"); + SAW_EXPECT(header_worker_id(eo_hdr.get_value()) == "w-x", "Still identifiable"); + + // The routing decode succeeds and names the kind, which is exactly what it + // is for: an unusable worker is still an identifiable worker. + auto eo_route = decode_message(text); + SAW_EXPECT(not eo_route.is_error(), + "The common fields of an unknown kind must still decode"); + SAW_EXPECT(eo_route.get_value().get<"capability">().get<"kind">().stl_view() + == "webgpu", "The orchestrator learns which kind it cannot use"); + + // Decoding it as a webgl2 hello must fail, because "webgl2" is absent -- + // that is the orchestrator learning it cannot use this worker, which is a + // different and better outcome than a zeroed capability. + auto eo_full = decode_message(text); + SAW_EXPECT(eo_full.is_error(), + "A webgl2 decode of a webgpu worker must fail rather than zero fill"); +} +/** + * Native workers are a worker kind, not a test fixture, so the same routing has + * to work for them -- and crucially a native hello must not be obliged to carry + * a texture budget it has no way to measure. + */ +SAW_TEST("Wire / a native worker registers without a webgl section"){ + auto hello = make_message(msg::hello, "n-1", 0u); + + auto& cap = hello.get<"capability">(); + cap.get<"worker_id">().set(std::string{"n-1"}); + cap.get<"kind">().set(std::string{wkind::native}); + cap.get<"schema_version">().set(std::string{schema_version}); + cap.get<"reported_at_ms">().set(1700000000000u); + cap.get<"user_agent">().set(std::string{"kel-lbm-worker-native"}); + + auto& nat = cap.get<"native">(); + nat.get<"threads">().set(16u); + nat.get<"memory_bytes">().set(34359738368u); + nat.get<"cpu">().set(std::string{"x86_64"}); + nat.get<"has_sycl">().set(false); + + auto eo_text = encode_message(hello); + SAW_EXPECT(not eo_text.is_error(), "A native hello must encode"); + + // Routes the same way a browser does. + auto eo_route = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_route.is_error(), "A native hello must route"); + SAW_EXPECT(eo_route.get_value().get<"capability">().get<"kind">().stl_view() + == wkind::native, "Routing reports native"); + + auto eo_back = decode_message(eo_text.get_value()); + SAW_EXPECT(not eo_back.is_error(), "A native hello must decode"); + SAW_EXPECT(eo_back.get_value().get<"capability">().get<"native">() + .get<"threads">().get() == 16u, "Thread count survives"); + + // And the point of the restructure: this text has no webgl2 section at all, + // so a decode that demands one has to fail rather than zero fill. + auto eo_wrong = decode_message(eo_text.get_value()); + SAW_EXPECT(eo_wrong.is_error(), + "A native worker must not decode as a webgl2 one"); +} +} -- cgit v1.3.1