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