brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include "aos/common/mutex.h" |
| 2 | |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 3 | #include <sched.h> |
| 4 | #include <math.h> |
| 5 | #include <pthread.h> |
| 6 | #ifdef __VXWORKS__ |
| 7 | #include <taskLib.h> |
| 8 | #endif |
| 9 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include "gtest/gtest.h" |
| 11 | |
Brian Silverman | 14fd0fb | 2014-01-14 21:42:01 -0800 | [diff] [blame] | 12 | #include "aos/linux_code/ipc_lib/aos_sync.h" |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 13 | #include "aos/common/die.h" |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 14 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 15 | namespace aos { |
| 16 | namespace testing { |
| 17 | |
| 18 | class MutexTest : public ::testing::Test { |
| 19 | public: |
| 20 | Mutex test_mutex; |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 21 | |
| 22 | protected: |
| 23 | void SetUp() override { |
| 24 | SetDieTestMode(true); |
| 25 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | typedef MutexTest MutexDeathTest; |
| 29 | |
| 30 | TEST_F(MutexTest, TryLock) { |
| 31 | EXPECT_TRUE(test_mutex.TryLock()); |
| 32 | EXPECT_FALSE(test_mutex.TryLock()); |
| 33 | } |
| 34 | |
| 35 | TEST_F(MutexTest, Lock) { |
| 36 | test_mutex.Lock(); |
| 37 | EXPECT_FALSE(test_mutex.TryLock()); |
| 38 | } |
| 39 | |
| 40 | TEST_F(MutexTest, Unlock) { |
| 41 | test_mutex.Lock(); |
| 42 | EXPECT_FALSE(test_mutex.TryLock()); |
| 43 | test_mutex.Unlock(); |
| 44 | EXPECT_TRUE(test_mutex.TryLock()); |
| 45 | } |
| 46 | |
| 47 | #ifndef __VXWORKS__ |
| 48 | // Sees what happens with multiple unlocks. |
| 49 | TEST_F(MutexDeathTest, RepeatUnlock) { |
| 50 | test_mutex.Lock(); |
| 51 | test_mutex.Unlock(); |
| 52 | EXPECT_DEATH(test_mutex.Unlock(), ".*multiple unlock.*"); |
| 53 | } |
| 54 | |
| 55 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 56 | TEST_F(MutexDeathTest, NeverLock) { |
| 57 | EXPECT_DEATH(test_mutex.Unlock(), ".*multiple unlock.*"); |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | TEST_F(MutexTest, MutexLocker) { |
| 62 | { |
| 63 | aos::MutexLocker locker(&test_mutex); |
| 64 | EXPECT_FALSE(test_mutex.TryLock()); |
| 65 | } |
| 66 | EXPECT_TRUE(test_mutex.TryLock()); |
| 67 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 68 | |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 69 | TEST_F(MutexTest, MutexUnlocker) { |
| 70 | test_mutex.Lock(); |
| 71 | { |
| 72 | aos::MutexUnlocker unlocker(&test_mutex); |
| 73 | // If this fails, then something weird is going on and the next line might |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 74 | // hang, so fail immediately. |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 75 | ASSERT_TRUE(test_mutex.TryLock()); |
| 76 | test_mutex.Unlock(); |
| 77 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 78 | EXPECT_FALSE(test_mutex.TryLock()); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 79 | } |
| 80 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 81 | } // namespace testing |
| 82 | } // namespace aos |