From bf128e0044509ec8cbc6adacb39bd7c402e5b0c6 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 14:50:33 +0000 Subject: [PATCH] test(transport): add tests for the transport factory API edges - An unknown transport name throws; an empty id gets a uuid - shmem refuses pub and sub sockets, zeromq creates them - GetType() per transport - Receive is interrupted after Interrupt() and works after Resume() - NewSimpleMessage() copies string, literal and value data; NewStaticMessage() uses the caller's buffer Co-Authored-By: Claude Opus 5.5 --- test/CMakeLists.txt | 1 + test/transport/_factory.cxx | 149 ++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/transport/_factory.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 497cffc2..9ac4e278 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -286,6 +286,7 @@ add_testsuite(Channel add_testsuite(Transport SOURCES ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx + transport/_factory.cxx transport/_transfer_timeout.cxx transport/_options.cxx transport/_shmem.cxx diff --git a/test/transport/_factory.cxx b/test/transport/_factory.cxx new file mode 100644 index 00000000..1361b953 --- /dev/null +++ b/test/transport/_factory.cxx @@ -0,0 +1,149 @@ +/******************************************************************************** + * 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; + +auto AsString(Message const& msg) -> string +{ + return {static_cast(msg.GetData()), msg.GetSize()}; +} + +TEST(TransportFactory, UnknownTransportThrows) +{ + ProgOptions config; + test::Configure(config); + EXPECT_THROW(TransportFactory::CreateTransportFactory("carrier-pigeon", tools::Uuid(), &config), + TransportFactoryError); +} + +TEST(TransportFactory, EmptyIdGetsAUuid) +{ + ProgOptions config; + test::Configure(config); + auto factory = TransportFactory::CreateTransportFactory("zeromq", "", &config); + EXPECT_FALSE(factory->GetId().empty()); + EXPECT_EQ(TransportFactory::CreateTransportFactory("zeromq", "my-id", &config)->GetId(), "my-id"); +} + +TEST(TransportFactory, ShmemRejectsPubSub) +{ + ProgOptions config; + test::Configure(config); + auto shm = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config); + EXPECT_THROW(shm->CreateSocket("pub", "pub"), SocketError); + EXPECT_THROW(shm->CreateSocket("sub", "sub"), SocketError); + + auto zmq = TransportFactory::CreateTransportFactory("zeromq", tools::Uuid(), &config); + EXPECT_NE(zmq->CreateSocket("pub", "pub"), nullptr); + EXPECT_NE(zmq->CreateSocket("sub", "sub"), nullptr); +} + +using Factory = test::TransportTest; + +TEST_P(Factory, Type) +{ + ProgOptions config; + test::Configure(config, tools::Uuid(), GetParam().expandedShmMetadata); + auto factory = TransportFactory::CreateTransportFactory(GetParam().transport, tools::Uuid(), &config); + EXPECT_EQ(factory->GetType(), GetParam().transport == "zeromq" ? Transport::ZMQ : Transport::SHM); +} + +TEST_P(Factory, ResumeAfterInterrupt) +{ + ProgOptions config; + test::Configure(config, tools::Uuid(), GetParam().expandedShmMetadata); + auto factory = TransportFactory::CreateTransportFactory(GetParam().transport, tools::Uuid(), &config); + + Channel push("push", "push", factory); + Channel pull("pull", "pull", factory); + test::IpcAddress const address("resume_after_interrupt"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + + factory->Interrupt(); + auto in(pull.NewMessage()); + EXPECT_EQ(pull.Receive(in), static_cast(TransferCode::interrupted)); + + factory->Resume(); + auto out(push.NewSimpleMessage("after resume")); + ASSERT_EQ(push.Send(out, 5000), 12); + ASSERT_EQ(pull.Receive(in, 5000), 12); + EXPECT_EQ(AsString(*in), "after resume"); +} + +TEST_P(Factory, SimpleAndStaticMessages) +{ + ProgOptions config; + test::Configure(config, tools::Uuid(), GetParam().expandedShmMetadata); + auto factory = TransportFactory::CreateTransportFactory(GetParam().transport, tools::Uuid(), &config); + + Channel push("push", "push", factory); + Channel pull("pull", "pull", factory); + test::IpcAddress const address("simple_and_static"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + + auto receive = [&]() { + auto msg(pull.NewMessage()); + EXPECT_GE(pull.Receive(msg, 5000), 0); + return msg; + }; + + // NewSimpleMessage() copies the data: the source can change after the call. + string text("simple"); + auto simple(factory->NewSimpleMessage(text)); + text = "changed"; + ASSERT_EQ(push.Send(simple, 5000), 6); + EXPECT_EQ(AsString(*receive()), "simple"); + + auto literal(factory->NewSimpleMessage("literal")); + ASSERT_EQ(push.Send(literal, 5000), 7); + EXPECT_EQ(AsString(*receive()), "literal"); + + uint64_t const value = 0x0123456789abcdef; + auto number(factory->NewSimpleMessage(value)); + ASSERT_EQ(push.Send(number, 5000), static_cast(sizeof(value))); + auto numberIn = receive(); + ASSERT_EQ(numberIn->GetSize(), sizeof(value)); + uint64_t received = 0; + memcpy(&received, numberIn->GetData(), sizeof(received)); + EXPECT_EQ(received, value); + + // NewStaticMessage() uses the buffer of the caller, which must outlive the message. + static string const staticText("static"); + auto staticMsg(factory->NewStaticMessage(staticText)); + EXPECT_EQ(staticMsg->GetSize(), staticText.size()); + if (GetParam().transport == "zeromq") { + EXPECT_EQ(staticMsg->GetData(), staticText.data()); // zeromq does not copy + } + ASSERT_EQ(push.Send(staticMsg, 5000), 6); + EXPECT_EQ(AsString(*receive()), "static"); +} + +INSTANTIATE_TEST_SUITE_P(, Factory, test::AllTransports(), test::TransportParamName()); + +} // namespace