Skip to content

Commit

Permalink
Improve test_time_controller test (#1012)
Browse files Browse the repository at this point in the history
* Improve test time controller test

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>

* Fix linter

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>

* Add missing library

Signed-off-by: Jorge Perez <jjperez@ekumenlabs.com>
  • Loading branch information
Blast545 committed May 14, 2022
1 parent 4cec314 commit a0de6f8
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion rosbag2_cpp/test/rosbag2_cpp/test_time_controller_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <atomic>
#include <thread>
#include <rclcpp/utilities.hpp>
#include "rosbag2_cpp/clocks/time_controller_clock.hpp"

using namespace testing; // NOLINT
Expand Down Expand Up @@ -151,7 +152,21 @@ TEST_F(TimeControllerClockTest, unpaused_sleep_returns_true)

clock.pause();
clock.resume();
EXPECT_TRUE(clock.sleep_until(clock.now() + RCUTILS_S_TO_NS(1)));

auto sleep_until_timestamp = clock.now() + RCUTILS_S_TO_NS(1);
auto start = std::chrono::steady_clock::now();
bool sleep_result = clock.sleep_until(sleep_until_timestamp);
// clock.sleep_until can spuriously wake up and return false.
// Check it until true and use a timeout to avoid a hang
while (rclcpp::ok() && !sleep_result &&
(std::chrono::steady_clock::now() - start) < std::chrono::milliseconds(1200))
{
sleep_result = clock.sleep_until(sleep_until_timestamp);
}
auto end = std::chrono::steady_clock::now();
EXPECT_TRUE(end - start < std::chrono::milliseconds(1200));
EXPECT_TRUE(end - start >= std::chrono::seconds(1));
EXPECT_TRUE(sleep_result);
}

TEST_F(TimeControllerClockTest, paused_sleep_returns_false_quickly)
Expand Down

0 comments on commit a0de6f8

Please sign in to comment.