Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/core_misc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> errors;
int creations = 0;
{
registrar<declared_output_ptr> 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());
}
145 changes: 145 additions & 0 deletions test/multilayer_join_test.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -10,7 +12,14 @@
#include <gsl/pointers>
#include <oneapi/tbb/flow_graph.h>

#include <array>
#include <cstddef>
#include <limits>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -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();

Expand 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();
Expand 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<message_tuple<3>> 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<message_tuple<3>> sink{graph};
make_edge(output_port<0>(join), sink);

auto try_put = [&join](std::array<std::size_t, 3> 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}));
Comment thread
greenc-FNAL marked this conversation as resolved.
Resolved
graph.wait_for_all();

// Only two complete sets of messages should be produced.
std::vector<message_tuple<3>> 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<std::size_t> 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<std::size_t>{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<int, 2> join{graph,
name,
"test_stage"_id,
"job"_id,
{"event"_id, "run"_id},
product_specifications{spec<int>(name)},
[](data_cell_index const&) { return std::make_unique<int>(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<std::size_t>::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);
}
}
32 changes: 26 additions & 6 deletions test/resources_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*;
};
Expand Down Expand Up @@ -274,20 +284,30 @@ TEST_CASE("resource catalog", "[resource]")
CHECK_NOTHROW(catalog.limiter_for<value_token_resource>());
}

SECTION("duplicate serialized registrations throw")
SECTION("duplicate serialized registrations throw without constructing another resource")
{
catalog.add_serialized<catch2_resource>();
CHECK_THROWS_WITH(catalog.add_serialized<catch2_resource>(),
int constructions = 0;
catalog.add_serialized<counted_serialized_resource>(constructions);
auto& limiter = catalog.limiter_for<counted_serialized_resource>();
CHECK(constructions == 1);
CHECK_THROWS_WITH(catalog.add_serialized<counted_serialized_resource>(constructions),
Catch::Matchers::ContainsSubstring("Resource of type '") &&
Catch::Matchers::ContainsSubstring("' has already been registered"));
CHECK(constructions == 1);
CHECK(&catalog.limiter_for<counted_serialized_resource>() == &limiter);
}

SECTION("duplicate unlimited registrations throw")
SECTION("duplicate unlimited registrations throw without constructing another resource")
{
catalog.add_unlimited<unlimited_resource_type>(42);
CHECK_THROWS_WITH(catalog.add_unlimited<unlimited_resource_type>(43),
int constructions = 0;
catalog.add_unlimited<counted_unlimited_resource>(constructions);
auto const resource = catalog.access_for<counted_unlimited_resource>();
CHECK(constructions == 1);
CHECK_THROWS_WITH(catalog.add_unlimited<counted_unlimited_resource>(constructions),
Catch::Matchers::ContainsSubstring("Resource of type '") &&
Catch::Matchers::ContainsSubstring("' has already been registered"));
CHECK(constructions == 1);
CHECK(catalog.access_for<counted_unlimited_resource>() == resource);
}

SECTION("missing resources throw")
Expand Down
Loading