John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/stl_mutex/stl_mutex.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 5 | #include "aos/testing/test_logging.h" |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/util/thread.h" |
| 7 | #include "aos/die.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 8 | |
| 9 | namespace aos { |
| 10 | namespace testing { |
| 11 | |
| 12 | class StlMutexDeathTest : public ::testing::Test { |
| 13 | protected: |
| 14 | void SetUp() override { |
Brian Silverman | f5f8d8e | 2015-12-06 18:39:12 -0500 | [diff] [blame] | 15 | ::aos::testing::EnableTestLogging(); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 16 | SetDieTestMode(true); |
| 17 | } |
| 18 | }; |
| 19 | |
| 20 | typedef StlMutexDeathTest StlRecursiveMutexDeathTest; |
| 21 | |
| 22 | // Tests that locking/unlocking without any blocking works. |
| 23 | TEST(StlMutexTest, Basic) { |
| 24 | stl_mutex mutex; |
| 25 | |
| 26 | mutex.lock(); |
| 27 | mutex.unlock(); |
| 28 | |
| 29 | ASSERT_TRUE(mutex.try_lock()); |
| 30 | ASSERT_FALSE(mutex.try_lock()); |
| 31 | mutex.unlock(); |
| 32 | |
| 33 | mutex.lock(); |
| 34 | ASSERT_FALSE(mutex.try_lock()); |
| 35 | mutex.unlock(); |
| 36 | } |
| 37 | |
| 38 | // Tests that unlocking an unlocked mutex fails. |
| 39 | TEST_F(StlMutexDeathTest, MultipleUnlock) { |
| 40 | stl_mutex mutex; |
| 41 | mutex.lock(); |
| 42 | mutex.unlock(); |
| 43 | EXPECT_DEATH(mutex.unlock(), ".*multiple unlock.*"); |
| 44 | } |
| 45 | |
| 46 | // Tests that locking/unlocking (including recursively) without any blocking |
| 47 | // works. |
| 48 | TEST(StlRecursiveMutexTest, Basic) { |
| 49 | stl_recursive_mutex mutex; |
| 50 | |
| 51 | mutex.lock(); |
| 52 | mutex.unlock(); |
| 53 | |
| 54 | ASSERT_TRUE(mutex.try_lock()); |
| 55 | ASSERT_TRUE(mutex.try_lock()); |
| 56 | mutex.unlock(); |
| 57 | mutex.unlock(); |
| 58 | |
| 59 | mutex.lock(); |
| 60 | ASSERT_TRUE(mutex.try_lock()); |
| 61 | mutex.unlock(); |
| 62 | mutex.unlock(); |
| 63 | } |
| 64 | |
| 65 | // Tests that unlocking an unlocked recursive mutex fails. |
| 66 | TEST_F(StlRecursiveMutexDeathTest, MultipleUnlock) { |
| 67 | stl_recursive_mutex mutex; |
| 68 | mutex.lock(); |
| 69 | mutex.unlock(); |
| 70 | EXPECT_DEATH(mutex.unlock(), ".*multiple unlock.*"); |
| 71 | } |
| 72 | |
| 73 | } // namespace testing |
| 74 | } // namespace aos |