Files
FairMQ/test/transport/_shmem.cxx
Claude e366c40139 test(shmem): add tests for the shared memory monitor API
- SegmentIsPresent() before, during and after the life of a factory
- RegionIsPresent() during and after the life of a region
- With shm-no-cleanup the segment stays, and Cleanup() removes it
- Add a disabled debug-mode test for a known defect: GetDebugInfo()
  writes past the end of its result vectors and returns them empty

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 17:00:01 +01:00

128 lines
4.3 KiB
C++

/********************************************************************************
* Copyright (C) 2021 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/ProgOptions.h>
#include <fairmq/shmem/Monitor.h>
#include <fairmq/tools/Unique.h>
#include <fairmq/TransportFactory.h>
#include <helper/Transport.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cstdint>
#include <string>
#include <vector>
#include <unistd.h> // getpid
namespace
{
using namespace std;
using namespace fair::mq;
void GetFreeMemory()
{
ProgOptions config;
string const sessionId(test::Configure(config));
ASSERT_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 0), shmem::Monitor::MonitorError);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
ASSERT_NO_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 0));
ASSERT_THROW(shmem::Monitor::GetFreeMemory(shmem::SessionId{sessionId}, 1), shmem::Monitor::MonitorError);
}
TEST(Monitor, GetFreeMemory)
{
GetFreeMemory();
}
auto ShmConfig(ProgOptions& config, bool noCleanup) -> shmem::SessionId
{
shmem::SessionId const session{test::Configure(config)};
// Without the monitor: it would clean up the segment of a closed factory.
config.SetProperty<bool>("shm-monitor", false);
config.SetProperty<bool>("shm-no-cleanup", noCleanup);
return session;
}
TEST(Monitor, SegmentIsPresent)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
{
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
EXPECT_TRUE(shmem::Monitor::SegmentIsPresent(session, 0));
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 1));
}
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
}
TEST(Monitor, RegionIsPresent)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
uint16_t id = 0;
{
auto region = factory->CreateUnmanagedRegion(10000, [](void*, size_t, void*) {});
id = region->GetId();
EXPECT_TRUE(shmem::Monitor::RegionIsPresent(session, id));
}
EXPECT_FALSE(shmem::Monitor::RegionIsPresent(session, id));
}
TEST(Monitor, CleanupAfterNoCleanup)
{
ProgOptions config;
auto const session = ShmConfig(config, true);
{
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
auto msg = factory->CreateMessage(1000);
}
// shm-no-cleanup: the segment stays after the factory is gone.
ASSERT_TRUE(shmem::Monitor::SegmentIsPresent(session, 0));
auto const removed = shmem::Monitor::Cleanup(session, false);
EXPECT_FALSE(removed.empty());
EXPECT_FALSE(shmem::Monitor::SegmentIsPresent(session, 0));
}
#ifdef FAIRMQ_DEBUG_MODE
// Known defect: Monitor::GetDebugInfo() only reserves each result vector and
// then writes to result[segment][handle], out of the bounds of the vector.
// It returns empty vectors and corrupts the heap. It also dereferences the
// debug map before its null check. Enable this test with the fix.
TEST(Monitor, DISABLED_GetDebugInfo)
{
ProgOptions config;
auto const session = ShmConfig(config, false);
auto factory = TransportFactory::CreateTransportFactory("shmem", tools::Uuid(), &config);
auto msg1 = factory->CreateMessage(100);
auto msg2 = factory->CreateMessage(200);
auto const info = shmem::Monitor::GetDebugInfo(session);
ASSERT_EQ(info.count(0), 1U);
vector<size_t> sizes;
for (auto const& buffer : info.at(0)) {
EXPECT_EQ(buffer.fPid, getpid());
sizes.push_back(buffer.fSize);
}
sort(sizes.begin(), sizes.end());
EXPECT_EQ(sizes, (vector<size_t>{100, 200}));
}
#endif
} // namespace