|
11 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 |
| - |
15 | 14 | #include "autoware/universe_utils/system/time_keeper.hpp"
|
16 | 15 |
|
17 | 16 | #include <rclcpp/node.hpp>
|
|
20 | 19 | #include <gtest/gtest.h>
|
21 | 20 |
|
22 | 21 | #include <chrono>
|
23 |
| -#include <iostream> |
| 22 | +#include <sstream> |
24 | 23 | #include <thread>
|
25 | 24 |
|
26 |
| -TEST(system, TimeKeeper) |
| 25 | +class TimeKeeperTest : public ::testing::Test |
27 | 26 | {
|
28 |
| - using autoware::universe_utils::ScopedTimeTrack; |
29 |
| - using autoware::universe_utils::TimeKeeper; |
| 27 | +protected: |
| 28 | + std::ostringstream oss; |
| 29 | + rclcpp::Node::SharedPtr node; |
| 30 | + rclcpp::Publisher<autoware::universe_utils::ProcessingTimeDetail>::SharedPtr publisher; |
| 31 | + std::unique_ptr<autoware::universe_utils::TimeKeeper> time_keeper; |
30 | 32 |
|
31 |
| - rclcpp::Node node{"sample_node"}; |
32 |
| - |
33 |
| - auto publisher = node.create_publisher<autoware::universe_utils::ProcessingTimeDetail>( |
34 |
| - "~/debug/processing_time_tree", 1); |
| 33 | + void SetUp() override |
| 34 | + { |
| 35 | + node = std::make_shared<rclcpp::Node>("test_node"); |
| 36 | + publisher = node->create_publisher<autoware::universe_utils::ProcessingTimeDetail>( |
| 37 | + "~/debug/processing_time_tree", 1); |
| 38 | + time_keeper = std::make_unique<autoware::universe_utils::TimeKeeper>(&oss, publisher); |
| 39 | + } |
| 40 | +}; |
35 | 41 |
|
36 |
| - TimeKeeper time_keeper(&std::cerr, publisher); |
| 42 | +TEST_F(TimeKeeperTest, BasicFunctionality) |
| 43 | +{ |
| 44 | + using autoware::universe_utils::ScopedTimeTrack; |
37 | 45 |
|
38 |
| - ScopedTimeTrack st{"main_func", time_keeper}; |
| 46 | + { |
| 47 | + ScopedTimeTrack st{"main_func", *time_keeper}; |
39 | 48 |
|
40 |
| - { // funcA |
41 |
| - ScopedTimeTrack st{"funcA", time_keeper}; |
42 |
| - std::this_thread::sleep_for(std::chrono::seconds(1)); |
43 |
| - } |
| 49 | + { // funcA |
| 50 | + ScopedTimeTrack st{"funcA", *time_keeper}; |
| 51 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 52 | + } |
44 | 53 |
|
45 |
| - { // funcB |
46 |
| - ScopedTimeTrack st{"funcB", time_keeper}; |
47 |
| - std::this_thread::sleep_for(std::chrono::seconds(1)); |
48 |
| - { // funcC |
49 |
| - ScopedTimeTrack st{"funcC", time_keeper}; |
50 |
| - std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 54 | + { // funcB |
| 55 | + ScopedTimeTrack st{"funcB", *time_keeper}; |
| 56 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 57 | + { // funcC |
| 58 | + ScopedTimeTrack st{"funcC", *time_keeper}; |
| 59 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 60 | + } |
51 | 61 | }
|
52 | 62 | }
|
| 63 | + |
| 64 | + // Check if the output contains all function names |
| 65 | + std::string output = oss.str(); |
| 66 | + EXPECT_TRUE(output.find("main_func") != std::string::npos); |
| 67 | + EXPECT_TRUE(output.find("funcA") != std::string::npos); |
| 68 | + EXPECT_TRUE(output.find("funcB") != std::string::npos); |
| 69 | + EXPECT_TRUE(output.find("funcC") != std::string::npos); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_F(TimeKeeperTest, MultiThreadWarning) |
| 73 | +{ |
| 74 | + testing::internal::CaptureStderr(); |
| 75 | + |
| 76 | + std::thread t([this]() { |
| 77 | + time_keeper->start_track("ThreadFunction"); |
| 78 | + std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 79 | + time_keeper->end_track("ThreadFunction"); |
| 80 | + }); |
| 81 | + |
| 82 | + time_keeper->start_track("MainFunction"); |
| 83 | + std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 84 | + time_keeper->end_track("MainFunction"); |
| 85 | + |
| 86 | + t.join(); |
| 87 | + |
| 88 | + std::string err = testing::internal::GetCapturedStderr(); |
| 89 | + EXPECT_TRUE( |
| 90 | + err.find("TimeKeeper::start_track() is called from a different thread. Ignoring the call.") != |
| 91 | + std::string::npos); |
53 | 92 | }
|
0 commit comments