blob: 2ab3e5cfceefd9a4aca6a31195e9b5309d04976f [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include "aos/common/mutex.h"
2
Brian Silvermand41b4422013-09-01 14:02:33 -07003#include <sched.h>
4#include <math.h>
5#include <pthread.h>
6#ifdef __VXWORKS__
7#include <taskLib.h>
8#endif
9
brians343bc112013-02-10 01:53:46 +000010#include "gtest/gtest.h"
11
Brian Silverman14fd0fb2014-01-14 21:42:01 -080012#include "aos/linux_code/ipc_lib/aos_sync.h"
Brian Silverman8d2e56e2013-09-23 17:55:03 -070013#include "aos/common/die.h"
Brian Silverman01be0002014-05-10 15:44:38 -070014#include "aos/common/util/death_test_log_implementation.h"
Brian Silvermand41b4422013-09-01 14:02:33 -070015
brians343bc112013-02-10 01:53:46 +000016namespace aos {
17namespace testing {
18
19class MutexTest : public ::testing::Test {
20 public:
21 Mutex test_mutex;
Brian Silverman8d2e56e2013-09-23 17:55:03 -070022
23 protected:
24 void SetUp() override {
25 SetDieTestMode(true);
26 }
brians343bc112013-02-10 01:53:46 +000027};
28
29typedef MutexTest MutexDeathTest;
30
31TEST_F(MutexTest, TryLock) {
32 EXPECT_TRUE(test_mutex.TryLock());
33 EXPECT_FALSE(test_mutex.TryLock());
34}
35
36TEST_F(MutexTest, Lock) {
37 test_mutex.Lock();
38 EXPECT_FALSE(test_mutex.TryLock());
39}
40
41TEST_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.
50TEST_F(MutexDeathTest, RepeatUnlock) {
51 test_mutex.Lock();
52 test_mutex.Unlock();
Brian Silverman01be0002014-05-10 15:44:38 -070053 EXPECT_DEATH(
54 {
55 logging::AddImplementation(new util::DeathTestLogImplementation());
56 test_mutex.Unlock();
57 },
58 ".*multiple unlock.*");
brians343bc112013-02-10 01:53:46 +000059}
60
61// Sees what happens if you unlock without ever locking (or unlocking) it.
62TEST_F(MutexDeathTest, NeverLock) {
Brian Silverman01be0002014-05-10 15:44:38 -070063 EXPECT_DEATH(
64 {
65 logging::AddImplementation(new util::DeathTestLogImplementation());
66 test_mutex.Unlock();
67 },
68 ".*multiple unlock.*");
brians343bc112013-02-10 01:53:46 +000069}
70#endif
71
72TEST_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 Silverman797e71e2013-09-06 17:29:39 -070079
Brian Silvermand41b4422013-09-01 14:02:33 -070080TEST_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 Silverman797e71e2013-09-06 17:29:39 -070085 // hang, so fail immediately.
Brian Silvermand41b4422013-09-01 14:02:33 -070086 ASSERT_TRUE(test_mutex.TryLock());
87 test_mutex.Unlock();
88 }
Brian Silverman08661c72013-09-01 17:24:38 -070089 EXPECT_FALSE(test_mutex.TryLock());
Brian Silvermand41b4422013-09-01 14:02:33 -070090}
91
brians343bc112013-02-10 01:53:46 +000092} // namespace testing
93} // namespace aos