diff --git a/test/core_misc_test.cpp b/test/core_misc_test.cpp index 836804139..0a013fcdf 100644 --- a/test/core_misc_test.cpp +++ b/test/core_misc_test.cpp @@ -266,3 +266,22 @@ TEST_CASE("Moving a registrar transfers node creation", "[core]") CHECK(errors.empty()); } + +TEST_CASE("A throwing registrar creator is not retried by the destructor", "[core]") +{ + using namespace phlex::detail; + + declared_outputs outputs; + std::vector errors; + int creations = 0; + { + registrar reg{outputs, errors}; + reg.set_creator([&](auto const&, auto const&) -> declared_output_ptr { + ++creations; + throw std::runtime_error{"creator failed"}; + }); + CHECK_THROWS_WITH(reg.set_output_product_suffixes({}), "creator failed"); + } + CHECK(creations == 1); + CHECK(errors.empty()); +} diff --git a/test/multilayer_join_test.cpp b/test/multilayer_join_test.cpp index 185007459..ca5a55e97 100644 --- a/test/multilayer_join_test.cpp +++ b/test/multilayer_join_test.cpp @@ -1,5 +1,7 @@ +#include "phlex/core/fold_join_node.hpp" #include "phlex/core/message.hpp" #include "phlex/core/multilayer_join_node.hpp" +#include "phlex/core/product_selector.hpp" #include "phlex/model/algorithm_name.hpp" #include "phlex/model/fwd.hpp" #include "phlex/model/identifier.hpp" @@ -10,7 +12,14 @@ #include #include +#include +#include +#include +#include +#include +#include #include +#include #include #include @@ -73,6 +82,8 @@ TEST_CASE("multilayer_join_node joins multiple input products", "[join]") auto& left_port = receiver_for<0ull, 2>(join, 0u); auto& right_port = receiver_for<0ull, 2>(join, 1u); + CHECK_THROWS_AS(receiver_for<0ull>(join, 2u), std::runtime_error); + REQUIRE(left_port.try_put({.store = left_store, .id = message_id})); graph.wait_for_all(); @@ -85,6 +96,10 @@ TEST_CASE("multilayer_join_node joins multiple input products", "[join]") auto index_ports = join.index_ports(); REQUIRE(index_ports.size() == 2u); + CHECK(index_ports[0].layer == "left_layer"_id); + CHECK(index_ports[1].layer == "right_layer"_id); + CHECK_FALSE(index_ports[0].counting_layer.has_value()); + CHECK_FALSE(index_ports[1].counting_layer.has_value()); REQUIRE(index_ports[0].index_port->try_put( {.index = left_store->index(), .msg_id = message_id, .cache = true})); graph.wait_for_all(); @@ -110,3 +125,133 @@ TEST_CASE("multilayer_join_node joins multiple input products", "[join]") REQUIRE(index_ports[1].token_port->try_put({.index = right_store->index(), .count = 1})); graph.wait_for_all(); } + +TEST_CASE("Same-layer join receiver lookup and input ordering", "[join]") +{ + tbb::flow::graph graph; + multilayer_join_node<3> join{graph, "same_layer_join", {"event"_id, "event"_id, "event"_id}}; + CHECK(join.index_ports().empty()); + + auto const left_creator = algorithm_name{"left"}; + auto const middle_creator = algorithm_name{"middle"}; + auto const right_creator = algorithm_name{"right"}; + + product_selectors const selectors{{.creator = left_creator.to_string()}, + {.creator = middle_creator.to_string()}, + {.creator = right_creator.to_string()}}; + auto const stage = "test"_id; + std::array const stores{product_store::base(&left_creator, &stage), + product_store::base(&middle_creator, &stage), + product_store::base(&right_creator, &stage)}; + tbb::flow::queue_node> sink{graph}; + + using internal::receiver_for; + auto try_put_for_receiver = [&join, &selectors, &stores](std::size_t const index) { + return receiver_for<3>(join, selectors, selectors[index]) + .try_put({.store = stores.at(index), .id = message_id}); + }; + + make_edge(output_port<0>(join), sink); + SECTION("joins wait for all inputs and preserve selector order") + { + REQUIRE(try_put_for_receiver(2)); + REQUIRE(try_put_for_receiver(0)); + graph.wait_for_all(); + message_tuple<3> output; + CHECK_FALSE(sink.try_get(output)); + REQUIRE(try_put_for_receiver(1)); + graph.wait_for_all(); + REQUIRE(sink.try_get(output)); + CHECK(std::get<0>(output).id == message_id); + CHECK(std::get<1>(output).id == message_id); + CHECK(std::get<2>(output).id == message_id); + CHECK(std::get<0>(output).store == stores[0]); + CHECK(std::get<1>(output).store == stores[1]); + CHECK(std::get<2>(output).store == stores[2]); + CHECK_FALSE(sink.try_get(output)); + } + + SECTION("receiver lookup rejects unknown selectors") + { + CHECK_THROWS_AS(receiver_for<3>(join, selectors, {.creator = "missing"_id}), + std::runtime_error); + } +} + +TEST_CASE("Same-layer joins match message IDs rather than arrival order", "[join]") +{ + tbb::flow::graph graph; + multilayer_join_node<3> join{graph, "same_layer_id_join", {"event"_id, "event"_id, "event"_id}}; + tbb::flow::queue_node> sink{graph}; + make_edge(output_port<0>(join), sink); + + auto try_put = [&join](std::array const message_ids) { + unsigned int valid_puts = 0; + // This test verifies ID matching, so real product stores are unnecessary. + valid_puts += receiver_for<0ull, 3>(join, 0).try_put({.id = message_ids[0]}); + valid_puts += receiver_for<1ull, 3>(join, 1).try_put({.id = message_ids[1]}); + valid_puts += receiver_for<2ull, 3>(join, 2).try_put({.id = message_ids[2]}); + return valid_puts == 3u; + }; + + // Every port has a message, but neither ID has a complete input set. + REQUIRE(try_put({42u, 42u, 43u})); + graph.wait_for_all(); + message_tuple<3> output; + CHECK_FALSE(sink.try_get(output)); + + REQUIRE(try_put({43u, 43u, 42u})); + graph.wait_for_all(); + + // Only two complete sets of messages should be produced. + std::vector> outputs; + CHECK(sink.try_get(output)); + outputs.push_back(output); + CHECK(sink.try_get(output)); + outputs.push_back(output); + CHECK_FALSE(sink.try_get(output)); + + // All message IDs should agree per output + std::set consumed_message_ids; + for (auto const& [msg0, msg1, msg2] : outputs) { + CHECK(msg0.id == msg1.id); + CHECK(msg1.id == msg2.id); + consumed_message_ids.insert(msg0.id); + } + + CHECK(consumed_message_ids == std::set{42u, 43u}); +} + +TEST_CASE("Fold join receiver lookup and index metadata", "[join]") +{ + tbb::flow::graph graph; + auto const name = algorithm_name{"fold_join_test"}; + fold_join_node join{graph, + name, + "test_stage"_id, + "job"_id, + {"event"_id, "run"_id}, + product_specifications{spec(name)}, + [](data_cell_index const&) { return std::make_unique(0); }}; + SECTION("data receivers exclude the partition port and reject invalid indices") + { + CHECK_THROWS_AS(receiver_for<1ull>(join, 2u), std::runtime_error); + CHECK_THROWS_AS(receiver_for<1ull>(join, std::numeric_limits::max()), + std::runtime_error); + product_selectors const selectors{{.creator = "left"_id}, {.creator = "right"_id}}; + CHECK(&receiver_for(join, selectors, selectors[0]) == &input_port<1>(join)); + CHECK(&receiver_for(join, selectors, selectors[1]) == &input_port<2>(join)); + } + + SECTION("index ports identify routing and counting layers with the deepest input first") + { + auto const ports = join.index_ports(); + REQUIRE(ports.size() == 3); + CHECK(ports[0].layer == "job"_id); + CHECK(ports[0].counting_layer == "event"_id); + CHECK(ports[1].layer == "event"_id); + CHECK(ports[1].counting_layer == "event"_id); + CHECK(ports[2].layer == "run"_id); + CHECK(ports[2].counting_layer == "run"_id); + } +} diff --git a/test/resources_test.cpp b/test/resources_test.cpp index 8a525ae67..e4b3620ab 100644 --- a/test/resources_test.cpp +++ b/test/resources_test.cpp @@ -44,6 +44,16 @@ namespace { struct default_unlimited_resource {}; + struct counted_unlimited_resource { + explicit counted_unlimited_resource(int& constructions) { ++constructions; } + }; + + struct counted_serialized_resource { + using token_type = counted_serialized_resource const*; + + explicit counted_serialized_resource(int& constructions) { ++constructions; } + }; + struct invalid_serialized_resource { using token_type = int*; }; @@ -274,20 +284,30 @@ TEST_CASE("resource catalog", "[resource]") CHECK_NOTHROW(catalog.limiter_for()); } - SECTION("duplicate serialized registrations throw") + SECTION("duplicate serialized registrations throw without constructing another resource") { - catalog.add_serialized(); - CHECK_THROWS_WITH(catalog.add_serialized(), + int constructions = 0; + catalog.add_serialized(constructions); + auto& limiter = catalog.limiter_for(); + CHECK(constructions == 1); + CHECK_THROWS_WITH(catalog.add_serialized(constructions), Catch::Matchers::ContainsSubstring("Resource of type '") && Catch::Matchers::ContainsSubstring("' has already been registered")); + CHECK(constructions == 1); + CHECK(&catalog.limiter_for() == &limiter); } - SECTION("duplicate unlimited registrations throw") + SECTION("duplicate unlimited registrations throw without constructing another resource") { - catalog.add_unlimited(42); - CHECK_THROWS_WITH(catalog.add_unlimited(43), + int constructions = 0; + catalog.add_unlimited(constructions); + auto const resource = catalog.access_for(); + CHECK(constructions == 1); + CHECK_THROWS_WITH(catalog.add_unlimited(constructions), Catch::Matchers::ContainsSubstring("Resource of type '") && Catch::Matchers::ContainsSubstring("' has already been registered")); + CHECK(constructions == 1); + CHECK(catalog.access_for() == resource); } SECTION("missing resources throw")