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 <noreply@anthropic.com>
This commit is contained in:
Claude
2026-11-03 14:50:33 +00:00
committed by Dennis Klein
parent 03465e1993
commit bf128e0044
2 changed files with 150 additions and 0 deletions

View File

@@ -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

149
test/transport/_factory.cxx Normal file
View File

@@ -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 <fairmq/Channel.h>
#include <fairmq/ProgOptions.h>
#include <fairmq/Socket.h>
#include <fairmq/TransportFactory.h>
#include <fairmq/tools/Unique.h>
#include <helper/Transport.h>
#include <helper/TransportParam.h>
#include <gtest/gtest.h>
#include <cstdint>
#include <cstring>
#include <string>
namespace
{
using namespace std;
using namespace fair::mq;
auto AsString(Message const& msg) -> string
{
return {static_cast<char const*>(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<int64_t>(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<int64_t>(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