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

timed events unit tests [7379] #970

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/cpp/rtps/resources/TimedEventImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ bool TimedEventImpl::update(
std::chrono::steady_clock::time_point cancel_time)
{
StateCode expected = StateCode::READY;
bool ret_val = state_.compare_exchange_strong(expected, StateCode::WAITING);
bool set_time = state_.compare_exchange_strong(expected, StateCode::WAITING);

if (ret_val)
if (set_time)
{
std::unique_lock<std::mutex> lock(mutex_);
next_trigger_time_ = current_time + interval_microsec_;
}
else
else if (expected == StateCode::INACTIVE)
{
std::unique_lock<std::mutex> lock(mutex_);
next_trigger_time_ = cancel_time;
}

return ret_val;
return expected != StateCode::INACTIVE;
}

void TimedEventImpl::trigger(
Expand Down
24 changes: 13 additions & 11 deletions test/unittest/rtps/resources/timedevent/TimedEventTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class TimedEventEnvironment : public ::testing::Environment

void SetUp()
{
service_.init_thread();
service_ = new eprosima::fastrtps::rtps::ResourceEvent();
service_->init_thread();
}

void TearDown()
{
delete service_;
}

eprosima::fastrtps::rtps::ResourceEvent service_;
eprosima::fastrtps::rtps::ResourceEvent* service_;
};

TimedEventEnvironment* const env =
Expand All @@ -45,7 +47,7 @@ TimedEventEnvironment* const env =
*/
TEST(TimedEvent, Event_SuccessEvents)
{
MockEvent event(env->service_, 100, false);
MockEvent event(*env->service_, 100, false);

for (int i = 0; i < 10; ++i)
{
Expand All @@ -67,7 +69,7 @@ TEST(TimedEvent, Event_SuccessEvents)
*/
TEST(TimedEvent, Event_CancelEvents)
{
MockEvent event(env->service_, 100, false);
MockEvent event(*env->service_, 100, false);

for (int i = 0; i < 10; ++i)
{
Expand All @@ -88,7 +90,7 @@ TEST(TimedEvent, Event_CancelEvents)
*/
TEST(TimedEvent, Event_RestartEvents)
{
MockEvent event(env->service_, 100, false);
MockEvent event(*env->service_, 100, false);

for (int i = 0; i < 10; ++i)
{
Expand Down Expand Up @@ -116,7 +118,7 @@ TEST(TimedEvent, Event_RestartEvents)
*/
TEST(TimedEvent, Event_QuickCancelEvents)
{
MockEvent event(env->service_, 1, false);
MockEvent event(*env->service_, 1, false);

// Cancel ten times.
for (int i = 0; i < 10; ++i)
Expand Down Expand Up @@ -147,7 +149,7 @@ TEST(TimedEvent, Event_QuickCancelEvents)
*/
TEST(TimedEvent, Event_QuickRestartEvents)
{
MockEvent event(env->service_, 1, false);
MockEvent event(*env->service_, 1, false);

for (int i = 0; i < 10; ++i)
{
Expand All @@ -169,7 +171,7 @@ TEST(TimedEvent, Event_QuickRestartEvents)
*/
TEST(TimedEvent, Event_AutoRestart)
{
MockEvent event(env->service_, 10, true);
MockEvent event(*env->service_, 10, true);

for (unsigned int i = 0; i < 100; ++i)
{
Expand All @@ -195,7 +197,7 @@ TEST(TimedEvent, Event_AutoRestartAndDeleteRandomly)
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(10, 100);

MockEvent event(env->service_, 2, true);
MockEvent event(*env->service_, 2, true);

event.event().restart_timer();
std::this_thread::sleep_for(std::chrono::milliseconds(dis(gen)));
Expand Down Expand Up @@ -258,7 +260,7 @@ TEST(TimedEventMultithread, Event_TwoStartTwoCancel)
std::thread* thr1 = nullptr, * thr2 = nullptr,
* thr3 = nullptr, * thr4 = nullptr;

MockEvent event(env->service_, 3, false);
MockEvent event(*env->service_, 3, false);

// 2 Thread restarting and two thread cancel.
// Thread 1 -> Restart 100 times waiting 100ms between each one.
Expand Down Expand Up @@ -304,7 +306,7 @@ TEST(TimedEventMultithread, Event_FourAutoRestart)
std::thread* thr1 = nullptr, * thr2 = nullptr,
* thr3 = nullptr, * thr4 = nullptr;

MockEvent event(env->service_, 2, true);
MockEvent event(*env->service_, 2, true);

// 2 Thread restarting and two thread cancel.
// Thread 1 -> AutoRestart 100 times waiting 2ms between each one.
Expand Down