From cc3297b2fd590f055d80a11a4764251dd439e9e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 3 Nov 2026 14:52:14 +0000 Subject: [PATCH] test(parts): add tests for Parts access and pmr messages on all transports - Parts: variadic constructor, At() and operator[] (const and not), At() out of range throws, Empty(), Clear(), move - Transfer of Parts with an empty part on all transports - A pmr container from the transport memory resource becomes a message without a copy and transfers its data, on all transports - Add disabled tests for two known defects: zeromq refuses an empty Parts and shmem sends it; shmem Receive(Parts) of a single-message Send() reads the message size as the number of parts and crashes Co-Authored-By: Claude Opus 5.5 --- test/CMakeLists.txt | 1 + test/memory_resources/_memory_resources.cxx | 43 +++++ test/parts/_access.cxx | 169 ++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 test/parts/_access.cxx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9ac4e278..21226a09 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -84,6 +84,7 @@ add_testsuite(Protocols add_testsuite(Parts SOURCES ${CMAKE_CURRENT_BINARY_DIR}/runner.cxx + parts/_access.cxx parts/_add_part.cxx parts/_iterator_interface.cxx diff --git a/test/memory_resources/_memory_resources.cxx b/test/memory_resources/_memory_resources.cxx index 7c9a87c1..4116fb23 100644 --- a/test/memory_resources/_memory_resources.cxx +++ b/test/memory_resources/_memory_resources.cxx @@ -13,7 +13,10 @@ #include +#include + #include +#include #include #include @@ -176,4 +179,44 @@ TEST(MemoryResources, getMessage) EXPECT_EQ(messageArray[2], 6); } +using PmrMessage = test::TransportTest; + +// A container that uses the memory resource of a transport becomes a message +// without a copy, and the message transfers the container data. +TEST_P(PmrMessage, FromContainerWithoutCopy) +{ + ProgOptions config; + test::Configure(config, tools::Uuid(), GetParam().expandedShmMetadata); + auto factory = TransportFactory::CreateTransportFactory(GetParam().transport, tools::Uuid(), &config); + + std::vector> v(polymorphic_allocator{factory->GetMemoryResource()}); + v.reserve(4); + v.push_back(7); + v.push_back(8); + v.push_back(9); + void const* data = v.data(); + + auto message = getMessage(std::move(v)); + ASSERT_NE(message, nullptr); + EXPECT_EQ(message->GetData(), data); + EXPECT_EQ(message->GetSize(), 3 * sizeof(int)); + EXPECT_EQ(factory->GetMemoryResource()->getNumberOfMessages(), 0U); + + Channel push("push", "push", factory); + Channel pull("pull", "pull", factory); + test::IpcAddress const address("pmr_message"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + ASSERT_EQ(push.Send(message, 5000), static_cast(3 * sizeof(int))); + + auto in(pull.NewMessage()); + ASSERT_EQ(pull.Receive(in, 5000), static_cast(3 * sizeof(int))); + auto const* received = static_cast(in->GetData()); + EXPECT_EQ(received[0], 7); + EXPECT_EQ(received[1], 8); + EXPECT_EQ(received[2], 9); +} + +INSTANTIATE_TEST_SUITE_P(, PmrMessage, test::AllTransports(), test::TransportParamName()); + } // namespace diff --git a/test/parts/_access.cxx b/test/parts/_access.cxx new file mode 100644 index 00000000..e3bed4bb --- /dev/null +++ b/test/parts/_access.cxx @@ -0,0 +1,169 @@ +/******************************************************************************** + * 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 + +namespace +{ + +using namespace std; +using namespace fair::mq; + +auto AsString(Message const& msg) -> string +{ + return {static_cast(msg.GetData()), msg.GetSize()}; +} + +class PartsAccess : public testing::Test +{ + protected: + PartsAccess() + { + test::Configure(fConfig); + fFactory = TransportFactory::CreateTransportFactory("zeromq", tools::Uuid(), &fConfig); + } + + ProgOptions fConfig; + shared_ptr fFactory; +}; + +TEST_F(PartsAccess, VariadicConstructor) +{ + Parts parts(fFactory->NewSimpleMessage("a"), fFactory->NewSimpleMessage("b"), fFactory->NewSimpleMessage("c")); + ASSERT_EQ(parts.Size(), 3U); + EXPECT_EQ(AsString(parts[0]), "a"); + EXPECT_EQ(AsString(parts[1]), "b"); + EXPECT_EQ(AsString(parts[2]), "c"); +} + +TEST_F(PartsAccess, AtAndIndex) +{ + Parts parts(fFactory->NewSimpleMessage("a"), fFactory->NewSimpleMessage("b")); + Parts const& constParts = parts; + + EXPECT_EQ(AsString(*parts.At(1)), "b"); + EXPECT_EQ(AsString(*constParts.At(0)), "a"); + EXPECT_EQ(&parts[1], parts.At(1).get()); + EXPECT_EQ(&constParts[0], constParts.At(0).get()); + EXPECT_THROW(parts.At(2), out_of_range); + EXPECT_THROW(constParts.At(2), out_of_range); + + // At() returns a reference to the owning pointer: the part can be replaced. + parts.At(0) = fFactory->NewSimpleMessage("z"); + EXPECT_EQ(AsString(parts[0]), "z"); +} + +TEST_F(PartsAccess, EmptyAndClear) +{ + Parts parts; + EXPECT_TRUE(parts.Empty()); + EXPECT_EQ(parts.Size(), 0U); + EXPECT_EQ(parts.begin(), parts.end()); + + parts.AddPart(fFactory->NewSimpleMessage("a"), fFactory->NewSimpleMessage("b")); + EXPECT_FALSE(parts.Empty()); + + parts.Clear(); + EXPECT_TRUE(parts.Empty()); + EXPECT_EQ(parts.Size(), 0U); +} + +TEST_F(PartsAccess, MoveLeavesTheSourceEmpty) +{ + Parts source(fFactory->NewSimpleMessage("a")); + Parts target(std::move(source)); + EXPECT_EQ(target.Size(), 1U); + EXPECT_TRUE(source.Empty()); // NOLINT(bugprone-use-after-move) +} + +using PartsTransfer = test::TransportTest; + +TEST_P(PartsTransfer, EmptyAndNonEmpty) +{ + 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("parts_transfer"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + + Parts out(push.NewSimpleMessage("ab"), push.NewMessage(), push.NewSimpleMessage("cde")); + ASSERT_EQ(push.Send(out, 5000), 5); + + Parts in; + ASSERT_EQ(pull.Receive(in, 5000), 5); + ASSERT_EQ(in.Size(), 3U); + EXPECT_EQ(AsString(in[0]), "ab"); + EXPECT_EQ(in[1].GetSize(), 0U); + EXPECT_EQ(AsString(in[2]), "cde"); +} + +// Known defect: the transports do not agree on an empty Parts. zeromq refuses +// it (TransferCode::error). shmem sends a message with zero parts, returns 0, +// and the receiver gets a Receive() with zero parts. Enable this test when +// both transports have the same behavior. +TEST_P(PartsTransfer, DISABLED_EmptyPartsIsRefused) +{ + 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("parts_transfer_empty"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + + Parts empty; + EXPECT_EQ(push.Send(empty, 1000), static_cast(TransferCode::error)); + Parts in; + EXPECT_EQ(pull.Receive(in, 100), static_cast(TransferCode::timeout)); +} + +// Known defect: shmem uses different metadata formats for a single message +// and for Parts. Receive(Parts) of a message sent with Send(MessagePtr) reads +// the size of the message as the number of parts, and reads metadata past the +// received data. zeromq receives it as one part. +TEST_P(PartsTransfer, DISABLED_SingleSendMultipartReceive) +{ + 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("parts_transfer_single"); + ASSERT_TRUE(pull.Bind(address)); + ASSERT_TRUE(push.Connect(address)); + + auto out(push.NewSimpleMessage("single")); + ASSERT_EQ(push.Send(out, 5000), 6); + Parts in; + ASSERT_EQ(pull.Receive(in, 5000), 6); + ASSERT_EQ(in.Size(), 1U); + EXPECT_EQ(AsString(in[0]), "single"); +} + +INSTANTIATE_TEST_SUITE_P(, PartsTransfer, test::AllTransports(), test::TransportParamName()); + +} // namespace