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 | |
Stephan Pleines | 45ea55a | 2024-05-30 20:26:24 -0700 | [diff] [blame^] | 3 | #include <memory> |
| 4 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 6 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 7 | #include "aos/die.h" |
| 8 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 9 | namespace aos::testing { |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 10 | |
| 11 | class StlMutexDeathTest : public ::testing::Test { |
| 12 | protected: |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 13 | void SetUp() override { SetDieTestMode(true); } |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 14 | }; |
| 15 | |
| 16 | typedef StlMutexDeathTest StlRecursiveMutexDeathTest; |
| 17 | |
| 18 | // Tests that locking/unlocking without any blocking works. |
| 19 | TEST(StlMutexTest, Basic) { |
| 20 | stl_mutex mutex; |
| 21 | |
| 22 | mutex.lock(); |
| 23 | mutex.unlock(); |
| 24 | |
| 25 | ASSERT_TRUE(mutex.try_lock()); |
| 26 | ASSERT_FALSE(mutex.try_lock()); |
| 27 | mutex.unlock(); |
| 28 | |
| 29 | mutex.lock(); |
| 30 | ASSERT_FALSE(mutex.try_lock()); |
| 31 | mutex.unlock(); |
| 32 | } |
| 33 | |
| 34 | // Tests that unlocking an unlocked mutex fails. |
| 35 | TEST_F(StlMutexDeathTest, MultipleUnlock) { |
| 36 | stl_mutex mutex; |
| 37 | mutex.lock(); |
| 38 | mutex.unlock(); |
| 39 | EXPECT_DEATH(mutex.unlock(), ".*multiple unlock.*"); |
| 40 | } |
| 41 | |
| 42 | // Tests that locking/unlocking (including recursively) without any blocking |
| 43 | // works. |
| 44 | TEST(StlRecursiveMutexTest, Basic) { |
| 45 | stl_recursive_mutex mutex; |
| 46 | |
| 47 | mutex.lock(); |
| 48 | mutex.unlock(); |
| 49 | |
| 50 | ASSERT_TRUE(mutex.try_lock()); |
| 51 | ASSERT_TRUE(mutex.try_lock()); |
| 52 | mutex.unlock(); |
| 53 | mutex.unlock(); |
| 54 | |
| 55 | mutex.lock(); |
| 56 | ASSERT_TRUE(mutex.try_lock()); |
| 57 | mutex.unlock(); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 58 | mutex.unlock(); |
Brian Silverman | b073f24 | 2014-09-08 16:29:57 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Tests that unlocking an unlocked recursive mutex fails. |
| 62 | TEST_F(StlRecursiveMutexDeathTest, MultipleUnlock) { |
| 63 | stl_recursive_mutex mutex; |
| 64 | mutex.lock(); |
| 65 | mutex.unlock(); |
| 66 | EXPECT_DEATH(mutex.unlock(), ".*multiple unlock.*"); |
| 67 | } |
| 68 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 69 | } // namespace aos::testing |