Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing liveliness manager tests that would fail with GTEST_INDIVIDUAL=OFF [5764] #577

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 80 additions & 82 deletions test/unittest/rtps/writer/LivelinessManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#include <condition_variable>
#include <gtest/gtest.h>

class TimedEventEnvironment : public ::testing::Environment
class LivelinessManagerTests : public ::testing::Test
{
public:

TimedEventEnvironment() : work_(service_) {}
LivelinessManagerTests() : work_(service_) {}

void SetUp()
{
thread_ = new std::thread(&TimedEventEnvironment::run, this);
thread_ = new std::thread(&LivelinessManagerTests::run, this);

writer_losing_liveliness = eprosima::fastrtps::rtps::GUID_t();
writer_recovering_liveliness = eprosima::fastrtps::rtps::GUID_t();
Expand All @@ -52,7 +52,7 @@ class TimedEventEnvironment : public ::testing::Environment

asio::io_service service_;
asio::io_service::work work_;
std::thread *thread_;
std::thread* thread_;

// Callbacks to test the liveliness manager

Expand Down Expand Up @@ -95,22 +95,20 @@ class TimedEventEnvironment : public ::testing::Environment
std::condition_variable liveliness_recovered_cv_;
};

TimedEventEnvironment* const env = dynamic_cast<TimedEventEnvironment*>(testing::AddGlobalTestEnvironment(new TimedEventEnvironment));

namespace eprosima {
namespace fastrtps {

using eprosima::fastrtps::rtps::LivelinessManager;
using eprosima::fastrtps::rtps::GuidPrefix_t;
using eprosima::fastrtps::rtps::GUID_t;

TEST(LivelinessManagerTests, WriterCanAlwaysBeAdded)
TEST_F(LivelinessManagerTests, WriterCanAlwaysBeAdded)
{
LivelinessManager liveliness_manager(
nullptr,
nullptr,
env->service_,
*env->thread_);
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -133,13 +131,13 @@ TEST(LivelinessManagerTests, WriterCanAlwaysBeAdded)
EXPECT_EQ(liveliness_manager.add_writer(guid, AUTOMATIC_LIVELINESS_QOS, Duration_t(1)), true);
}

TEST(LivelinessManagerTests, WriterCannotBeRemovedTwice)
TEST_F(LivelinessManagerTests, WriterCannotBeRemovedTwice)
{
LivelinessManager liveliness_manager(
nullptr,
nullptr,
env->service_,
*env->thread_);
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -160,13 +158,13 @@ TEST(LivelinessManagerTests, WriterCannotBeRemovedTwice)

//! Tests that the assert_liveliness() method that takes liveliness kind as argument sets the alive state and time
//! correctly
TEST(LivelinessManagerTests, AssertLivelinessByKind)
TEST_F(LivelinessManagerTests, AssertLivelinessByKind)
{
LivelinessManager liveliness_manager(
nullptr,
nullptr,
env->service_,
*env->thread_);
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand Down Expand Up @@ -218,13 +216,13 @@ TEST(LivelinessManagerTests, AssertLivelinessByKind)
}

//! Tests that the assert_liveliness() method that takes a writer as an argument sets the alive state and time correctly
TEST(LivelinessManagerTests, AssertLivelinessByWriter)
TEST_F(LivelinessManagerTests, AssertLivelinessByWriter)
{
LivelinessManager liveliness_manager(
nullptr,
nullptr,
env->service_,
*env->thread_);
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand Down Expand Up @@ -297,13 +295,13 @@ TEST(LivelinessManagerTests, AssertLivelinessByWriter)

//! Tests the case when the timer expires and liveliness manager is managing two automatic writers with different
//! lease durations
TEST(LivelinessManagerTests, TimerExpired_Automatic)
TEST_F(LivelinessManagerTests, TimerExpired_Automatic)
{
LivelinessManager liveliness_manager(
std::bind(&TimedEventEnvironment::liveliness_lost, env, std::placeholders::_1),
std::bind(&TimedEventEnvironment::liveliness_recovered, env, std::placeholders::_1),
env->service_,
*env->thread_);
std::bind(&LivelinessManagerTests::liveliness_lost, this, std::placeholders::_1),
std::bind(&LivelinessManagerTests::liveliness_recovered, this, std::placeholders::_1),
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -313,31 +311,31 @@ TEST(LivelinessManagerTests, TimerExpired_Automatic)

// Assert liveliness
liveliness_manager.assert_liveliness(GUID_t(guidP, 2), AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5));
env->num_writers_recovered = 0u;
num_writers_recovered = 0u;

// Wait so that first writer loses liveliness
env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 1));
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 1));

// Wait a bit longer so that second writer loses liveliness
env->wait_liveliness_lost(2u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 2));
wait_liveliness_lost(2u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));

// Assert first writer
liveliness_manager.assert_liveliness(GUID_t(guidP, 1), AUTOMATIC_LIVELINESS_QOS, Duration_t(0.1));
env->wait_liveliness_recovered(2u);
EXPECT_EQ(env->num_writers_recovered, 2u);
wait_liveliness_recovered(2u);
EXPECT_EQ(num_writers_recovered, 2u);
}

//! Tests the case when the timer expires and liveliness manager is managing two manual by participant writers
//! with different lease durations
TEST(LivelinessManagerTests, TimerExpired_ManualByParticipant)
TEST_F(LivelinessManagerTests, TimerExpired_ManualByParticipant)
{
LivelinessManager liveliness_manager(
std::bind(&TimedEventEnvironment::liveliness_lost, env, std::placeholders::_1),
std::bind(&TimedEventEnvironment::liveliness_recovered, env, std::placeholders::_1),
env->service_,
*env->thread_);
std::bind(&LivelinessManagerTests::liveliness_lost, this, std::placeholders::_1),
std::bind(&LivelinessManagerTests::liveliness_recovered, this, std::placeholders::_1),
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -347,33 +345,33 @@ TEST(LivelinessManagerTests, TimerExpired_ManualByParticipant)

// Assert liveliness
liveliness_manager.assert_liveliness(GUID_t(guidP, 2), MANUAL_BY_PARTICIPANT_LIVELINESS_QOS, Duration_t(0.5));
env->num_writers_recovered = 0u;
num_writers_recovered = 0u;

// Wait so that first writer loses liveliness
env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(env->num_writers_lost, 1u);
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(num_writers_lost, 1u);

// Wait a bit longer so that second writer loses liveliness
env->wait_liveliness_lost(2u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(env->num_writers_lost, 2u);
wait_liveliness_lost(2u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(num_writers_lost, 2u);

// Assert first writer
liveliness_manager.assert_liveliness(GUID_t(guidP, 1), MANUAL_BY_PARTICIPANT_LIVELINESS_QOS, Duration_t(0.1));
env->wait_liveliness_recovered(2u);
EXPECT_EQ(env->num_writers_recovered, 2u);
wait_liveliness_recovered(2u);
EXPECT_EQ(num_writers_recovered, 2u);
}

//! Tests the case when the timer expires and liveliness manager is managing two manual by topic writers
//! with different lease durations
TEST(LivelinessManagerTests, TimerExpired_ManualByTopic)
TEST_F(LivelinessManagerTests, TimerExpired_ManualByTopic)
{
LivelinessManager liveliness_manager(
std::bind(&TimedEventEnvironment::liveliness_lost, env, std::placeholders::_1),
std::bind(&TimedEventEnvironment::liveliness_recovered, env, std::placeholders::_1),
env->service_,
*env->thread_);
std::bind(&LivelinessManagerTests::liveliness_lost, this, std::placeholders::_1),
std::bind(&LivelinessManagerTests::liveliness_recovered, this, std::placeholders::_1),
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -383,38 +381,38 @@ TEST(LivelinessManagerTests, TimerExpired_ManualByTopic)

// Assert first writer
liveliness_manager.assert_liveliness(GUID_t(guidP, 1), MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(0.1));
env->wait_liveliness_recovered(1u);
wait_liveliness_recovered(1u);

// Wait so that first writer loses liveliness
env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(env->num_writers_lost, 1u);
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(num_writers_lost, 1u);

// Wait a bit longer and check that the second writer does not recover its liveliness
std::this_thread::sleep_for(std::chrono::milliseconds(200));
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(env->num_writers_lost, 1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(num_writers_lost, 1u);

// Assert second writer
liveliness_manager.assert_liveliness(GUID_t(guidP, 2), MANUAL_BY_TOPIC_LIVELINESS_QOS, Duration_t(0.2));
env->wait_liveliness_recovered(2u);
env->num_writers_lost = 0u;
wait_liveliness_recovered(2u);
num_writers_lost = 0u;

// Wait so that it loses liveliness
env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(env->num_writers_lost, 1u);
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(num_writers_lost, 1u);
}

//! Tests that the timer owner is calculated correctly
//! This is tested indirectly by checking which writer lost liveliness last
TEST(LivelinessManagerTests, TimerOwnerCalculation)
TEST_F(LivelinessManagerTests, TimerOwnerCalculation)
{
LivelinessManager liveliness_manager(
std::bind(&TimedEventEnvironment::liveliness_lost, env, std::placeholders::_1),
std::bind(&TimedEventEnvironment::liveliness_recovered, env, std::placeholders::_1),
env->service_,
*env->thread_);
std::bind(&LivelinessManagerTests::liveliness_lost, this, std::placeholders::_1),
std::bind(&LivelinessManagerTests::liveliness_recovered, this, std::placeholders::_1),
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -425,28 +423,28 @@ TEST(LivelinessManagerTests, TimerOwnerCalculation)

liveliness_manager.assert_liveliness(AUTOMATIC_LIVELINESS_QOS);

env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(env->num_writers_lost, 1u);
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 1));
EXPECT_EQ(num_writers_lost, 1u);

env->wait_liveliness_lost(2u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 3));
EXPECT_EQ(env->num_writers_lost, 2u);
wait_liveliness_lost(2u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 3));
EXPECT_EQ(num_writers_lost, 2u);

env->wait_liveliness_lost(3u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(env->num_writers_lost, 3u);
wait_liveliness_lost(3u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(num_writers_lost, 3u);
}

//! Tests that the writer that is the current timer owner can be removed, and that the timer is restarted
//! for the next writer
TEST(LivelinessManagerTests, TimerOwnerRemoved)
TEST_F(LivelinessManagerTests, TimerOwnerRemoved)
{
LivelinessManager liveliness_manager(
std::bind(&TimedEventEnvironment::liveliness_lost, env, std::placeholders::_1),
std::bind(&TimedEventEnvironment::liveliness_recovered, env, std::placeholders::_1),
env->service_,
*env->thread_);
std::bind(&LivelinessManagerTests::liveliness_lost, this, std::placeholders::_1),
std::bind(&LivelinessManagerTests::liveliness_recovered, this, std::placeholders::_1),
service_,
*thread_);

GuidPrefix_t guidP;
guidP.value[0] = 1;
Expand All @@ -457,9 +455,9 @@ TEST(LivelinessManagerTests, TimerOwnerRemoved)
liveliness_manager.assert_liveliness(GUID_t(guidP, 1), AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5));
liveliness_manager.remove_writer(GUID_t(guidP, 1), AUTOMATIC_LIVELINESS_QOS, Duration_t(0.5));

env->wait_liveliness_lost(1u);
EXPECT_EQ(env->writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(env->num_writers_lost, 1u);
wait_liveliness_lost(1u);
EXPECT_EQ(writer_losing_liveliness, GUID_t(guidP, 2));
EXPECT_EQ(num_writers_lost, 1u);
}

}
Expand Down