Skip to content

Commit

Permalink
Refs #20993: Refactor flaky test
Browse files Browse the repository at this point in the history
Signed-off-by: JesusPoderoso <jesuspoderoso@eprosima.com>
  • Loading branch information
JesusPoderoso committed May 23, 2024
1 parent e1770f0 commit 83850ac
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions test/unittest/transport/SharedMemTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <MockReceiverResource.h>
#include <SharedMemGlobalMock.hpp>

#include "../logging/mock/MockConsumer.h"

using namespace eprosima::fastrtps;
using namespace eprosima::fastrtps::rtps;
using namespace eprosima::fastdds;
Expand Down Expand Up @@ -1409,20 +1411,18 @@ TEST_F(SHMTransportTests, port_not_ok_listener_recover)
thread_listener.join();
}

//! This test has been updated to avoid flakiness #20993
TEST_F(SHMTransportTests, buffer_recover)
{
auto shared_mem_manager = SharedMemManager::create(domain_name);

auto segment = shared_mem_manager->create_segment(3, 3);

shared_mem_manager->remove_port(1);
auto pub_sub1_write = shared_mem_manager->open_port(1, 8, 1000, SharedMemGlobal::Port::OpenMode::Write);

shared_mem_manager->remove_port(2);
auto pub_sub2_write = shared_mem_manager->open_port(2, 8, 1000, SharedMemGlobal::Port::OpenMode::Write);

auto sub1_read = shared_mem_manager->open_port(1, 8, 1000, SharedMemGlobal::Port::OpenMode::ReadExclusive);

auto sub2_read = shared_mem_manager->open_port(2, 8, 1000, SharedMemGlobal::Port::OpenMode::ReadExclusive);

bool exit_listeners = false;
Expand Down Expand Up @@ -1461,23 +1461,28 @@ TEST_F(SHMTransportTests, buffer_recover)

if (buffer)
{
{
std::lock_guard<SharedMemSegment::mutex> lock(received_mutex);
listener2_recv_count.fetch_add(1);
}
listener2_recv_count.fetch_add(1);
//std::cout << "Listener2 received " << listener2_recv_count.load() << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(listener2_sleep_ms));
buffer.reset();
received_cv.notify_one();
}
}
});

Log::ClearConsumers();
Log::Flush();
eprosima::fastdds::dds::MockConsumer* mock_consumer = new eprosima::fastdds::dds::MockConsumer("RTPS_TRANSPORT_SHM");
Log::RegisterConsumer(std::unique_ptr<eprosima::fastdds::dds::LogConsumer>(mock_consumer));
Log::SetVerbosity(Log::Warning);
Log::SetErrorStringFilter(std::regex("Buffer is being invalidated.*"));

// Test 1 (without port overflow)
uint32_t send_counter = 0u;

bool is_port_ok = false;

while (listener1_recv_count.load() < 16u)
while (listener2_recv_count.load() < 32u)
{
{
// The segment should never overflow
Expand Down Expand Up @@ -1506,14 +1511,20 @@ TEST_F(SHMTransportTests, buffer_recover)
}
}

// The slow listener is 4 times slower than the fast one
ASSERT_LT(listener1_recv_count.load() * 3, listener2_recv_count.load());
ASSERT_GT(listener1_recv_count.load(), listener2_recv_count.load() / 5);
// The ration between the listeners activity (sleep) should correspond to the amount of entries read plus lost
auto consumed_entries = mock_consumer->ConsumedEntries().size(); // buffer reset warnings: non-read samples
std::cout << "Test1:"
<< " Listener1_recv_count " << listener1_recv_count.load()
<< " Listener2_recv_count " << listener2_recv_count.load()
<< " Warning entries consumed: " << consumed_entries
<< std::endl;

//ASSERT_EQ((listener1_recv_count.load() * (listener1_sleep_ms/listener2_sleep_ms)),
// (listener2_recv_count.load() + consumed_entries));
//ASSERT_EQ((listener1_recv_count.load()+consumed_entries), listener2_recv_count.load());
// The slow listener is 4 times slower than the fast one
EXPECT_LT(listener1_recv_count.load() * 3, listener2_recv_count.load());
EXPECT_GE(listener1_recv_count.load() * 4, listener2_recv_count.load());
ASSERT_EQ(1, 0);
// Test 2 (with port overflow)
listener2_sleep_ms = 0u;
send_counter = 0u;
Expand Down Expand Up @@ -1580,6 +1591,9 @@ TEST_F(SHMTransportTests, buffer_recover)

thread_listener1.join();
thread_listener2.join();

Log::Reset();
Log::KillThread();
}

TEST_F(SHMTransportTests, remote_segments_free)
Expand Down

0 comments on commit 83850ac

Please sign in to comment.