diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3cd4e0e2..7b2f56b4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -247,6 +247,7 @@ add_testsuite(EventManager add_testsuite(Properties SOURCES ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx + properties/_jsonparser.cxx properties/_properties.cxx properties/_suboptparser.cxx diff --git a/test/properties/_jsonparser.cxx b/test/properties/_jsonparser.cxx new file mode 100644 index 00000000..3a7eca3d --- /dev/null +++ b/test/properties/_jsonparser.cxx @@ -0,0 +1,183 @@ +/******************************************************************************** + * Copyright (C) 2026 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * + * * + * This software is distributed under the terms of the * + * GNU Lesser General Public Licence (LGPL) version 3, * + * copied verbatim in the file "LICENSE" * + ********************************************************************************/ + +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +namespace +{ + +using namespace std; +using namespace fair::mq; +using boost::property_tree::ptree; + +auto Parse(string const& json, string const& id) -> Properties +{ + ptree pt; + stringstream ss(json); + boost::property_tree::read_json(ss, pt); + return PtreeParser(pt, id); +} + +template +auto Get(Properties const& props, string const& key) -> T +{ + return boost::any_cast(props.at(key)); +} + +auto const kConfig = R"({ + "fairMQOptions": { + "devices": [ + { + "id": "sampler", + "channels": [ + { + "name": "data", + "type": "push", + "method": "bind", + "transport": "zeromq", + "sndBufSize": 500, + "sockets": [ + { "address": "tcp://127.0.0.1:5555" }, + { "address": "ipc://data", "transport": "shmem", "method": "connect", "rateLogging": 0 } + ] + }, + { + "name": "fanout", + "type": "push", + "address": "tcp://127.0.0.1:5556", + "numSockets": 3 + }, + { + "name": "plain", + "type": "pair", + "autoBind": false + } + ] + }, + { + "id": "other", + "key": "sink", + "channels": [ { "name": "data", "type": "pull" } ] + } + ] + } +})"; + +TEST(JSONParser, SubChannelsInheritAndOverride) +{ + auto const props = Parse(kConfig, "sampler"); + + EXPECT_EQ(Get(props, "chans.data.0.type"), "push"); + EXPECT_EQ(Get(props, "chans.data.0.method"), "bind"); + EXPECT_EQ(Get(props, "chans.data.0.address"), "tcp://127.0.0.1:5555"); + EXPECT_EQ(Get(props, "chans.data.0.transport"), "zeromq"); + EXPECT_EQ(Get(props, "chans.data.0.sndBufSize"), 500); + EXPECT_EQ(Get(props, "chans.data.0.rateLogging"), Channel::DefaultRateLogging); + + EXPECT_EQ(Get(props, "chans.data.1.type"), "push"); + EXPECT_EQ(Get(props, "chans.data.1.method"), "connect"); + EXPECT_EQ(Get(props, "chans.data.1.address"), "ipc://data"); + EXPECT_EQ(Get(props, "chans.data.1.transport"), "shmem"); + EXPECT_EQ(Get(props, "chans.data.1.sndBufSize"), 500); + EXPECT_EQ(Get(props, "chans.data.1.rateLogging"), 0); + + EXPECT_EQ(props.count("chans.data.2.address"), 0); +} + +TEST(JSONParser, NumSocketsExpandsTheChannel) +{ + auto const props = Parse(kConfig, "sampler"); + + for (int i = 0; i < 3; ++i) { + auto const prefix = "chans.fanout." + to_string(i) + "."; + EXPECT_EQ(Get(props, prefix + "type"), "push"); + EXPECT_EQ(Get(props, prefix + "address"), "tcp://127.0.0.1:5556"); + } + EXPECT_EQ(props.count("chans.fanout.3.address"), 0); +} + +TEST(JSONParser, ChannelWithoutSocketsGetsTheDefaults) +{ + auto const props = Parse(kConfig, "sampler"); + + EXPECT_EQ(Get(props, "chans.plain.0.type"), "pair"); + EXPECT_EQ(Get(props, "chans.plain.0.method"), Channel::DefaultMethod); + EXPECT_EQ(Get(props, "chans.plain.0.address"), Channel::DefaultAddress); + EXPECT_EQ(Get(props, "chans.plain.0.transport"), Channel::DefaultTransportName); + EXPECT_EQ(Get(props, "chans.plain.0.sndBufSize"), Channel::DefaultSndBufSize); + EXPECT_EQ(Get(props, "chans.plain.0.rcvBufSize"), Channel::DefaultRcvBufSize); + EXPECT_EQ(Get(props, "chans.plain.0.sndKernelSize"), Channel::DefaultSndKernelSize); + EXPECT_EQ(Get(props, "chans.plain.0.rcvKernelSize"), Channel::DefaultRcvKernelSize); + EXPECT_EQ(Get(props, "chans.plain.0.sndTimeoutMs"), Channel::DefaultSndTimeoutMs); + EXPECT_EQ(Get(props, "chans.plain.0.rcvTimeoutMs"), Channel::DefaultRcvTimeoutMs); + EXPECT_EQ(Get(props, "chans.plain.0.linger"), Channel::DefaultLinger); + EXPECT_EQ(Get(props, "chans.plain.0.portRangeMin"), Channel::DefaultPortRangeMin); + EXPECT_EQ(Get(props, "chans.plain.0.portRangeMax"), Channel::DefaultPortRangeMax); + EXPECT_FALSE(Get(props, "chans.plain.0.autoBind")); +} + +TEST(JSONParser, SelectsTheDeviceByKeyOrId) +{ + auto const sink = Parse(kConfig, "sink"); + EXPECT_EQ(Get(sink, "chans.data.0.type"), "pull"); + EXPECT_EQ(sink.count("chans.fanout.0.type"), 0); + + // With a key, the id does not select the device. + EXPECT_TRUE(Parse(kConfig, "other").empty()); + EXPECT_TRUE(Parse(kConfig, "unknown").empty()); +} + +TEST(JSONParser, Errors) +{ + EXPECT_THROW(Parse(kConfig, ""), ParserError); + EXPECT_THROW(Parse(R"({"options": {}})", "sampler"), boost::property_tree::ptree_bad_path); + // Channel without a name. + EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"type": "push"}]}]}})", "a"), + boost::property_tree::ptree_bad_path); +} + +// Known defect: ChannelParser() reads the values with ptree::get(key, default). +// That overload returns the default for a value of the wrong type, so a typo +// (e.g. "sndBufSize": "big") silently gives the default. Enable this test +// with the fix. +TEST(JSONParser, DISABLED_RejectsValuesOfTheWrongType) +{ + EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"name": "c", "sndBufSize": "big"}]}]}})", "a"), + boost::property_tree::ptree_bad_data); + EXPECT_THROW(Parse(R"({"fairMQOptions": {"devices": [{"id": "a", "channels": [{"name": "c", "autoBind": "maybe"}]}]}})", "a"), + boost::property_tree::ptree_bad_data); +} + +TEST(JSONParser, File) +{ + auto const path = "/tmp/fmq_test_jsonparser_" + to_string(tools::UuidHash()) + ".json"; + { + ofstream file(path); + file << kConfig; + } + auto const props = JSONParser(path, "sampler"); + remove(path.c_str()); + + EXPECT_EQ(Get(props, "chans.data.1.address"), "ipc://data"); + EXPECT_THROW(JSONParser(path, "sampler"), boost::property_tree::json_parser_error); +} + +} // namespace diff --git a/test/properties/_suboptparser.cxx b/test/properties/_suboptparser.cxx index 423bf077..2e0c7939 100644 --- a/test/properties/_suboptparser.cxx +++ b/test/properties/_suboptparser.cxx @@ -7,7 +7,9 @@ ********************************************************************************/ #include +#include #include +#include #include #include #include @@ -33,4 +35,66 @@ TEST(SuboptParser, ChannelNameSelector) ASSERT_TRUE(contains(parsed, "chans.bar-data.0.type", "pull")); } +TEST(SuboptParser, AllKeys) +{ + Properties const parsed(SuboptParser({"name=data,type=pub,method=bind,address=ipc://a,transport=shmem," + "sndBufSize=1,rcvBufSize=2,sndKernelSize=3,rcvKernelSize=4," + "sndTimeoutMs=5,rcvTimeoutMs=6,linger=7,rateLogging=8," + "portRangeMin=9,portRangeMax=10,autoBind=0"}, + "dev")); + auto get = [&](string const& key) { return parsed.at("chans.data.0." + key); }; + EXPECT_EQ(boost::any_cast(get("type")), "pub"); + EXPECT_EQ(boost::any_cast(get("method")), "bind"); + EXPECT_EQ(boost::any_cast(get("address")), "ipc://a"); + EXPECT_EQ(boost::any_cast(get("transport")), "shmem"); + EXPECT_EQ(boost::any_cast(get("sndBufSize")), 1); + EXPECT_EQ(boost::any_cast(get("rcvBufSize")), 2); + EXPECT_EQ(boost::any_cast(get("sndKernelSize")), 3); + EXPECT_EQ(boost::any_cast(get("rcvKernelSize")), 4); + EXPECT_EQ(boost::any_cast(get("sndTimeoutMs")), 5); + EXPECT_EQ(boost::any_cast(get("rcvTimeoutMs")), 6); + EXPECT_EQ(boost::any_cast(get("linger")), 7); + EXPECT_EQ(boost::any_cast(get("rateLogging")), 8); + EXPECT_EQ(boost::any_cast(get("portRangeMin")), 9); + EXPECT_EQ(boost::any_cast(get("portRangeMax")), 10); + EXPECT_FALSE(boost::any_cast(get("autoBind"))); +} + +TEST(SuboptParser, OneSubChannelPerAddress) +{ + Properties const parsed(SuboptParser({"name=data,type=push,address=ipc://a,address=ipc://b"}, "dev")); + EXPECT_TRUE(contains(parsed, "chans.data.0.address", "ipc://a")); + EXPECT_TRUE(contains(parsed, "chans.data.1.address", "ipc://b")); + EXPECT_TRUE(contains(parsed, "chans.data.1.type", "push")); + EXPECT_EQ(parsed.count("chans.data.2.address"), 0); +} + +TEST(SuboptParser, NumSockets) +{ + Properties const parsed(SuboptParser({"name=data,type=push,numSockets=2"}, "dev")); + EXPECT_TRUE(contains(parsed, "chans.data.0.type", "push")); + EXPECT_TRUE(contains(parsed, "chans.data.1.type", "push")); + EXPECT_EQ(parsed.count("chans.data.2.type"), 0); +} + +TEST(SuboptParser, IgnoresUnknownKeys) +{ + Properties const parsed(SuboptParser({"name=data,colour=blue,type=pull"}, "dev")); + EXPECT_TRUE(contains(parsed, "chans.data.0.type", "pull")); +} + +TEST(SuboptParser, Errors) +{ + // No channel name. + EXPECT_THROW(SuboptParser({"type=push,address=ipc://a"}, "dev"), boost::property_tree::ptree_bad_path); + // No device id. + EXPECT_THROW(SuboptParser({"name=data"}, ""), ParserError); +} + +// Known defect: see JSONParser.DISABLED_RejectsValuesOfTheWrongType. +TEST(SuboptParser, DISABLED_RejectsValuesOfTheWrongType) +{ + EXPECT_THROW(SuboptParser({"name=data,sndBufSize=big"}, "dev"), boost::property_tree::ptree_bad_data); +} + } // namespace