mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2026-11-25 21:21:37 +00:00
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>
This commit is contained in:
@@ -14,7 +14,12 @@
|
||||
#include <helper/Transport.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <unistd.h> // getpid
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -40,4 +45,83 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user