From 4ae5a6d2f5b253e1fbaa8dcbb558635578b7f7b4 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 14:56:14 +0000 Subject: [PATCH] test(device): add tests for the data callbacks and ConditionalRun() - OnData() with single messages and with Parts on all transports; a callback that returns false ends Run() and the device goes to Ready - Two input channels (poller path) and one input channel per transport (one poll thread per transport, per-channel transport override) - ConditionalRun() ends Run() when it returns false; the rate option limits the calls Co-Authored-By: Claude Opus 5.5 --- test/CMakeLists.txt | 1 + test/device/_data_callbacks.cxx | 271 ++++++++++++++++++++++++++++++++ 2 files changed, 272 insertions(+) create mode 100644 test/device/_data_callbacks.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a482a5a8..7ccd16d2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -133,6 +133,7 @@ add_testsuite(Device device/_multiple_devices.cxx device/_version.cxx device/_config.cxx + device/_data_callbacks.cxx device/_waitfor.cxx device/_exceptions.cxx device/_error_state.cxx diff --git a/test/device/_data_callbacks.cxx b/test/device/_data_callbacks.cxx new file mode 100644 index 00000000..e0974697 --- /dev/null +++ b/test/device/_data_callbacks.cxx @@ -0,0 +1,271 @@ +/******************************************************************************** + * 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 + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +using namespace std; +using namespace fair::mq; + +auto AsString(Message const& msg) -> string +{ + return {static_cast(msg.GetData()), msg.GetSize()}; +} + +/// A device with in-process control, and peer channels for the test. +class DataCallbacks : public test::TransportTest +{ + protected: + DataCallbacks() + : fSession(test::Configure(fConfig, tools::Uuid(), GetParam().expandedShmMetadata)) + { + fConfig.SetProperty("id", "receiver"); + fConfig.SetProperty("transport", GetParam().transport); + test::Configure(fPeerConfig, fSession, GetParam().expandedShmMetadata); + } + + /// Add a bound input channel to the device. + void AddInput(Device& device, string const& name, test::IpcAddress const& address, string const& transport = "") + { + Channel channel("pull", "bind", address); + channel.UpdateRateLogging(0); + if (!transport.empty()) { + channel.UpdateTransport(transport); + } + device.AddChannel(name, std::move(channel)); + } + + /// A push channel that connects to address, on the given transport. + auto Peer(string const& transport, test::IpcAddress const& address) -> unique_ptr + { + auto& factory = fPeerFactories[transport]; + if (!factory) { + factory = TransportFactory::CreateTransportFactory(transport, tools::Uuid(), &fPeerConfig); + } + // Unique names: zeromq monitors each socket on an inproc address made of the name. + auto channel = make_unique("peer" + to_string(fPeerCount++), "push", factory); + EXPECT_TRUE(channel->Connect(address)); + return channel; + } + + static void Send(Channel& channel, string const& text) + { + auto msg(channel.NewSimpleMessage(text)); + ASSERT_EQ(channel.Send(msg, 5000), static_cast(text.size())); + } + + /// Wait until Run() ended by itself, and the device is Ready again. + static auto RunEnds(Device& device) -> bool + { + return test::WaitUntil([&] { return device.GetCurrentState() == State::Ready; }); + } + + ProgOptions fConfig; + string fSession; + ProgOptions fPeerConfig; + map> fPeerFactories; + int fPeerCount = 0; +}; + +TEST_P(DataCallbacks, SingleMessage) +{ + Device device; + device.SetConfig(fConfig); + test::IpcAddress const address("ondata_single"); + AddInput(device, "data", address); + + vector received; + device.OnData("data", [&](MessagePtr& msg, int index) { + EXPECT_EQ(index, 0); + received.push_back(AsString(*msg)); + return received.size() < 3; // false ends Run() + }); + + test::StateMachineThread fsm(device); + test::Control(device, test::Cycle::ToRun); + auto peer = Peer(GetParam().transport, address); + for (auto const& text : {"1", "2", "3"}) { + Send(*peer, text); + } + + ASSERT_TRUE(RunEnds(device)); + EXPECT_EQ(received, (vector{"1", "2", "3"})); +} + +TEST_P(DataCallbacks, Multipart) +{ + Device device; + device.SetConfig(fConfig); + test::IpcAddress const address("ondata_multipart"); + AddInput(device, "data", address); + + vector received; + device.OnData("data", [&](Parts& parts, int) { + string joined; + for (auto const& part : parts) { + joined += AsString(*part) + "|"; + } + received.push_back(joined); + return false; + }); + + test::StateMachineThread fsm(device); + test::Control(device, test::Cycle::ToRun); + auto peer = Peer(GetParam().transport, address); + Parts parts(peer->NewSimpleMessage("a"), peer->NewSimpleMessage("bc")); + ASSERT_EQ(peer->Send(parts, 5000), 3); + + ASSERT_TRUE(RunEnds(device)); + EXPECT_EQ(received, (vector{"a|bc|"})); +} + +TEST_P(DataCallbacks, TwoChannels) +{ + Device device; + device.SetConfig(fConfig); + test::IpcAddress const address1("ondata_two_1"); + test::IpcAddress const address2("ondata_two_2"); + AddInput(device, "one", address1); + AddInput(device, "two", address2); + + mutex mtx; + set received; + auto record = [&](string const& text) { + lock_guard lock(mtx); + received.insert(text); + return received.size() < 2; + }; + device.OnData("one", [&](MessagePtr& msg, int) { return record("one:" + AsString(*msg)); }); + device.OnData("two", [&](Parts& parts, int) { return record("two:" + AsString(*parts.At(0))); }); + + test::StateMachineThread fsm(device); + test::Control(device, test::Cycle::ToRun); + auto peer1 = Peer(GetParam().transport, address1); + auto peer2 = Peer(GetParam().transport, address2); + Send(*peer1, "x"); + Parts parts(peer2->NewSimpleMessage("y")); + ASSERT_EQ(peer2->Send(parts, 5000), 1); + + ASSERT_TRUE(RunEnds(device)); + lock_guard lock(mtx); + EXPECT_EQ(received, (set{"one:x", "two:y"})); +} + +INSTANTIATE_TEST_SUITE_P(, DataCallbacks, test::AllTransports(), test::TransportParamName()); + +// One input per transport: the device polls each transport in its own thread. +class MultipleTransports : public DataCallbacks +{}; + +TEST_P(MultipleTransports, OneInputEach) +{ + Device device; + device.SetConfig(fConfig); + test::IpcAddress const zmqAddress("ondata_zmq"); + test::IpcAddress const shmAddress("ondata_shm"); + AddInput(device, "zmq", zmqAddress, "zeromq"); + AddInput(device, "shm", shmAddress, "shmem"); + + mutex mtx; + set received; + auto record = [&](string const& text) { + lock_guard lock(mtx); + received.insert(text); + return received.size() < 2; + }; + device.OnData("zmq", [&](MessagePtr& msg, int) { return record("zmq:" + AsString(*msg)); }); + device.OnData("shm", [&](MessagePtr& msg, int) { return record("shm:" + AsString(*msg)); }); + + test::StateMachineThread fsm(device); + test::Control(device, test::Cycle::ToRun); + EXPECT_EQ(device.GetChannel("zmq").GetTransportType(), Transport::ZMQ); + EXPECT_EQ(device.GetChannel("shm").GetTransportType(), Transport::SHM); + auto zmqPeer = Peer("zeromq", zmqAddress); + auto shmPeer = Peer("shmem", shmAddress); + Send(*zmqPeer, "1"); + Send(*shmPeer, "2"); + + ASSERT_TRUE(RunEnds(device)); + lock_guard lock(mtx); + EXPECT_EQ(received, (set{"zmq:1", "shm:2"})); +} + +// The default transport of the device does not matter for this test. +INSTANTIATE_TEST_SUITE_P(, MultipleTransports, test::BasicTransports(), test::TransportParamName()); + +class Conditional : public Device +{ + public: + explicit Conditional(int iterations) + : fIterations(iterations) + {} + + std::atomic fCalls{0}; + + protected: + bool ConditionalRun() override { return ++fCalls < fIterations; } + + private: + int fIterations; +}; + +TEST(ConditionalRun, EndsRunWhenItReturnsFalse) +{ + ProgOptions config; + test::Configure(config); + Conditional device(5); + device.SetConfig(config); + + test::StateMachineThread fsm(device); + test::Control(device, test::Cycle::ToRun); + + ASSERT_TRUE(test::WaitUntil([&] { return device.GetCurrentState() == State::Ready; })); + EXPECT_EQ(device.fCalls, 5); +} + +TEST(ConditionalRun, Rate) +{ + ProgOptions config; + test::Configure(config); + config.SetProperty("rate", 100.F); + Conditional device(21); + device.SetConfig(config); + + test::StateMachineThread fsm(device); + auto const start = chrono::steady_clock::now(); + test::Control(device, test::Cycle::ToRun); + ASSERT_TRUE(test::WaitUntil([&] { return device.GetCurrentState() == State::Ready; })); + + // 20 sleeps at 100 Hz take approx. 200 ms; check only a loose lower bound. + EXPECT_GE(chrono::steady_clock::now() - start, chrono::milliseconds(140)); + EXPECT_EQ(device.fCalls, 21); +} + +} // namespace