mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 13:11:36 +00:00
test(properties): add tests for the JSON and suboption channel parsers
- JSONParser: sub-channels inherit and override the channel values, numSockets expansion, defaults, device selection by key or id, errors for no id, no fairMQOptions, no channel name and no file - SuboptParser: all keys with their types, one sub-channel per address, numSockets, unknown keys, no channel name, no device id - Add disabled tests for a known defect: a value of the wrong type silently gives the default Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
183
test/properties/_jsonparser.cxx
Normal file
183
test/properties/_jsonparser.cxx
Normal file
@@ -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 <fairmq/Channel.h>
|
||||
#include <fairmq/JSONParser.h>
|
||||
#include <fairmq/Properties.h>
|
||||
#include <fairmq/tools/Unique.h>
|
||||
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
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<typename T>
|
||||
auto Get(Properties const& props, string const& key) -> T
|
||||
{
|
||||
return boost::any_cast<T>(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<string>(props, "chans.data.0.type"), "push");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.0.method"), "bind");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.0.address"), "tcp://127.0.0.1:5555");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.0.transport"), "zeromq");
|
||||
EXPECT_EQ(Get<int>(props, "chans.data.0.sndBufSize"), 500);
|
||||
EXPECT_EQ(Get<int>(props, "chans.data.0.rateLogging"), Channel::DefaultRateLogging);
|
||||
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.1.type"), "push");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.1.method"), "connect");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.1.address"), "ipc://data");
|
||||
EXPECT_EQ(Get<string>(props, "chans.data.1.transport"), "shmem");
|
||||
EXPECT_EQ(Get<int>(props, "chans.data.1.sndBufSize"), 500);
|
||||
EXPECT_EQ(Get<int>(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<string>(props, prefix + "type"), "push");
|
||||
EXPECT_EQ(Get<string>(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<string>(props, "chans.plain.0.type"), "pair");
|
||||
EXPECT_EQ(Get<string>(props, "chans.plain.0.method"), Channel::DefaultMethod);
|
||||
EXPECT_EQ(Get<string>(props, "chans.plain.0.address"), Channel::DefaultAddress);
|
||||
EXPECT_EQ(Get<string>(props, "chans.plain.0.transport"), Channel::DefaultTransportName);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.sndBufSize"), Channel::DefaultSndBufSize);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.rcvBufSize"), Channel::DefaultRcvBufSize);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.sndKernelSize"), Channel::DefaultSndKernelSize);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.rcvKernelSize"), Channel::DefaultRcvKernelSize);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.sndTimeoutMs"), Channel::DefaultSndTimeoutMs);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.rcvTimeoutMs"), Channel::DefaultRcvTimeoutMs);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.linger"), Channel::DefaultLinger);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.portRangeMin"), Channel::DefaultPortRangeMin);
|
||||
EXPECT_EQ(Get<int>(props, "chans.plain.0.portRangeMax"), Channel::DefaultPortRangeMax);
|
||||
EXPECT_FALSE(Get<bool>(props, "chans.plain.0.autoBind"));
|
||||
}
|
||||
|
||||
TEST(JSONParser, SelectsTheDeviceByKeyOrId)
|
||||
{
|
||||
auto const sink = Parse(kConfig, "sink");
|
||||
EXPECT_EQ(Get<string>(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<string>(props, "chans.data.1.address"), "ipc://data");
|
||||
EXPECT_THROW(JSONParser(path, "sampler"), boost::property_tree::json_parser_error);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -7,7 +7,9 @@
|
||||
********************************************************************************/
|
||||
|
||||
#include <fairmq/Properties.h>
|
||||
#include <fairmq/JSONParser.h>
|
||||
#include <fairmq/SuboptParser.h>
|
||||
#include <boost/property_tree/exceptions.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -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<string>(get("type")), "pub");
|
||||
EXPECT_EQ(boost::any_cast<string>(get("method")), "bind");
|
||||
EXPECT_EQ(boost::any_cast<string>(get("address")), "ipc://a");
|
||||
EXPECT_EQ(boost::any_cast<string>(get("transport")), "shmem");
|
||||
EXPECT_EQ(boost::any_cast<int>(get("sndBufSize")), 1);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("rcvBufSize")), 2);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("sndKernelSize")), 3);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("rcvKernelSize")), 4);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("sndTimeoutMs")), 5);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("rcvTimeoutMs")), 6);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("linger")), 7);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("rateLogging")), 8);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("portRangeMin")), 9);
|
||||
EXPECT_EQ(boost::any_cast<int>(get("portRangeMax")), 10);
|
||||
EXPECT_FALSE(boost::any_cast<bool>(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
|
||||
|
||||
Reference in New Issue
Block a user