Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 1 | #include "aos/ipc_lib/event.h" |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 2 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 3 | #include <chrono> |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 4 | #include <thread> |
| 5 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 6 | #include "gtest/gtest.h" |
| 7 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 8 | #include "aos/testing/test_logging.h" |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 9 | #include "aos/time/time.h" |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | namespace testing { |
| 13 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 14 | namespace chrono = ::std::chrono; |
| 15 | namespace this_thread = ::std::this_thread; |
| 16 | |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 17 | class EventTest : public ::testing::Test { |
| 18 | public: |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 19 | Event test_event_; |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 20 | |
| 21 | protected: |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 22 | void SetUp() override { ::aos::testing::EnableTestLogging(); } |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | // Makes sure that basic operations with no blocking or anything work. |
| 26 | TEST_F(EventTest, Basic) { |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 27 | EXPECT_FALSE(test_event_.Clear()); |
| 28 | EXPECT_FALSE(test_event_.Clear()); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 29 | |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 30 | test_event_.Set(); |
| 31 | test_event_.Wait(); |
| 32 | EXPECT_TRUE(test_event_.Clear()); |
| 33 | EXPECT_FALSE(test_event_.Clear()); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Tests that tsan understands that events establish a happens-before |
| 37 | // relationship. |
| 38 | TEST_F(EventTest, ThreadSanitizer) { |
Brian Silverman | d6ee9a1 | 2015-03-31 01:37:49 -0400 | [diff] [blame] | 39 | for (int i = 0; i < 3000; ++i) { |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 40 | int variable = 0; |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 41 | test_event_.Clear(); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 42 | ::std::thread thread([this, &variable]() { |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 43 | test_event_.Wait(); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 44 | --variable; |
| 45 | }); |
| 46 | ++variable; |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 47 | test_event_.Set(); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 48 | thread.join(); |
| 49 | EXPECT_EQ(0, variable); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Tests that an event blocks correctly. |
| 54 | TEST_F(EventTest, Blocks) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 55 | monotonic_clock::time_point start_time, finish_time; |
Brian Silverman | d6ee9a1 | 2015-03-31 01:37:49 -0400 | [diff] [blame] | 56 | // Without this, it sometimes manages to fail under tsan. |
| 57 | Event started; |
| 58 | ::std::thread thread([this, &start_time, &finish_time, &started]() { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 59 | start_time = monotonic_clock::now(); |
Brian Silverman | d6ee9a1 | 2015-03-31 01:37:49 -0400 | [diff] [blame] | 60 | started.Set(); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 61 | test_event_.Wait(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 62 | finish_time = monotonic_clock::now(); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 63 | }); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 64 | static constexpr auto kWaitTime = chrono::milliseconds(50); |
Brian Silverman | d6ee9a1 | 2015-03-31 01:37:49 -0400 | [diff] [blame] | 65 | started.Wait(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 66 | this_thread::sleep_for(kWaitTime); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 67 | test_event_.Set(); |
| 68 | thread.join(); |
| 69 | EXPECT_GE(finish_time - start_time, kWaitTime); |
| 70 | } |
| 71 | |
| 72 | TEST_F(EventTest, WaitTimeout) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 73 | EXPECT_FALSE(test_event_.WaitTimeout(chrono::milliseconds(50))); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 74 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 75 | monotonic_clock::time_point start_time, finish_time; |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 76 | // Without this, it sometimes manages to fail under tsan. |
| 77 | Event started; |
| 78 | ::std::thread thread([this, &start_time, &finish_time, &started]() { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 79 | start_time = monotonic_clock::now(); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 80 | started.Set(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 81 | EXPECT_TRUE(test_event_.WaitTimeout(chrono::milliseconds(500))); |
| 82 | finish_time = monotonic_clock::now(); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 83 | }); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 84 | constexpr auto kWaitTime = chrono::milliseconds(50); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 85 | started.Wait(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 86 | this_thread::sleep_for(kWaitTime); |
Brian Silverman | 3060894 | 2015-04-08 19:16:46 -0400 | [diff] [blame] | 87 | test_event_.Set(); |
Brian Silverman | f5f3490 | 2015-03-29 17:57:59 -0400 | [diff] [blame] | 88 | thread.join(); |
| 89 | EXPECT_GE(finish_time - start_time, kWaitTime); |
| 90 | } |
| 91 | |
| 92 | } // namespace testing |
| 93 | } // namespace aos |