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 | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame^] | 14 | #include "aos/common/util/death_test_log_implementation.h" |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 15 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 16 | namespace aos { |
| 17 | namespace testing { |
| 18 | |
| 19 | class MutexTest : public ::testing::Test { |
| 20 | public: |
| 21 | Mutex test_mutex; |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 22 | |
| 23 | protected: |
| 24 | void SetUp() override { |
| 25 | SetDieTestMode(true); |
| 26 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | typedef MutexTest MutexDeathTest; |
| 30 | |
| 31 | TEST_F(MutexTest, TryLock) { |
| 32 | EXPECT_TRUE(test_mutex.TryLock()); |
| 33 | EXPECT_FALSE(test_mutex.TryLock()); |
| 34 | } |
| 35 | |
| 36 | TEST_F(MutexTest, Lock) { |
| 37 | test_mutex.Lock(); |
| 38 | EXPECT_FALSE(test_mutex.TryLock()); |
| 39 | } |
| 40 | |
| 41 | TEST_F(MutexTest, Unlock) { |
| 42 | test_mutex.Lock(); |
| 43 | EXPECT_FALSE(test_mutex.TryLock()); |
| 44 | test_mutex.Unlock(); |
| 45 | EXPECT_TRUE(test_mutex.TryLock()); |
| 46 | } |
| 47 | |
| 48 | #ifndef __VXWORKS__ |
| 49 | // Sees what happens with multiple unlocks. |
| 50 | TEST_F(MutexDeathTest, RepeatUnlock) { |
| 51 | test_mutex.Lock(); |
| 52 | test_mutex.Unlock(); |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame^] | 53 | EXPECT_DEATH( |
| 54 | { |
| 55 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 56 | test_mutex.Unlock(); |
| 57 | }, |
| 58 | ".*multiple unlock.*"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Sees what happens if you unlock without ever locking (or unlocking) it. |
| 62 | TEST_F(MutexDeathTest, NeverLock) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame^] | 63 | EXPECT_DEATH( |
| 64 | { |
| 65 | logging::AddImplementation(new util::DeathTestLogImplementation()); |
| 66 | test_mutex.Unlock(); |
| 67 | }, |
| 68 | ".*multiple unlock.*"); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 69 | } |
| 70 | #endif |
| 71 | |
| 72 | TEST_F(MutexTest, MutexLocker) { |
| 73 | { |
| 74 | aos::MutexLocker locker(&test_mutex); |
| 75 | EXPECT_FALSE(test_mutex.TryLock()); |
| 76 | } |
| 77 | EXPECT_TRUE(test_mutex.TryLock()); |
| 78 | } |
Brian Silverman | 797e71e | 2013-09-06 17:29:39 -0700 | [diff] [blame] | 79 | |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 80 | TEST_F(MutexTest, MutexUnlocker) { |
| 81 | test_mutex.Lock(); |
| 82 | { |
| 83 | aos::MutexUnlocker unlocker(&test_mutex); |
| 84 | // 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] | 85 | // hang, so fail immediately. |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 86 | ASSERT_TRUE(test_mutex.TryLock()); |
| 87 | test_mutex.Unlock(); |
| 88 | } |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 89 | EXPECT_FALSE(test_mutex.TryLock()); |
Brian Silverman | d41b442 | 2013-09-01 14:02:33 -0700 | [diff] [blame] | 90 | } |
| 91 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 92 | } // namespace testing |
| 93 | } // namespace aos |