1 |
#include "MutexTest.h" |
2 |
|
3 |
#include <iostream> |
4 |
|
5 |
CPPUNIT_TEST_SUITE_REGISTRATION(MutexTest); |
6 |
|
7 |
using namespace std; |
8 |
|
9 |
|
10 |
// ConcurrentThread |
11 |
|
12 |
MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, false, 0, -4), |
13 |
mutex(Mutex::RECURSIVE) |
14 |
{ |
15 |
resource = 0; |
16 |
} |
17 |
|
18 |
int MutexTest::ConcurrentThread::Main() { |
19 |
mutex.Lock(); |
20 |
resource++; |
21 |
mutex.Unlock(); |
22 |
return 0; |
23 |
} |
24 |
|
25 |
|
26 |
// DummyThread |
27 |
|
28 |
MutexTest::DummyThread::DummyThread() : Thread(false, false, 0, -4), |
29 |
mutex(Mutex::RECURSIVE) |
30 |
{ |
31 |
resource = 0; |
32 |
} |
33 |
|
34 |
int MutexTest::DummyThread::Main() { |
35 |
mutex.Lock(); |
36 |
mutex.Lock(); |
37 |
resource++; |
38 |
mutex.Unlock(); |
39 |
return 0; |
40 |
} |
41 |
|
42 |
|
43 |
// MutexTest |
44 |
|
45 |
void MutexTest::printTestSuiteName() { |
46 |
cout << "\b \nRunning Mutex Tests: " << flush; |
47 |
} |
48 |
|
49 |
void MutexTest::setUp() { |
50 |
} |
51 |
|
52 |
void MutexTest::tearDown() { |
53 |
} |
54 |
|
55 |
// Check with only one thread (thus no concurrency) if locking and unlocking the Mutex works without getting the thread to be locked falsely. |
56 |
void MutexTest::testLockAndUnlockBySingleThread() { |
57 |
ConcurrentThread t; |
58 |
t.StartThread(); |
59 |
usleep(200000); // wait 200ms |
60 |
CPPUNIT_ASSERT(t.resource == 1); |
61 |
} |
62 |
|
63 |
// Now check with two concurrent threads if one thread can block the other one by using the Mutex. |
64 |
void MutexTest::testLock() { |
65 |
ConcurrentThread t; |
66 |
t.mutex.Lock(); |
67 |
t.SignalStartThread(); |
68 |
usleep(400000); // wait 400ms |
69 |
CPPUNIT_ASSERT(t.resource == 0); |
70 |
t.mutex.Unlock(); |
71 |
} |
72 |
|
73 |
// Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again. |
74 |
void MutexTest::testUnlock() { |
75 |
ConcurrentThread t; |
76 |
t.mutex.Lock(); |
77 |
t.SignalStartThread(); |
78 |
usleep(400000); // wait 400ms |
79 |
t.mutex.Unlock(); |
80 |
usleep(400000); // wait 400ms |
81 |
CPPUNIT_ASSERT(t.resource == 1); |
82 |
} |
83 |
|
84 |
// Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself. |
85 |
void MutexTest::testDoubleLock() { |
86 |
DummyThread t; |
87 |
t.SignalStartThread(); |
88 |
usleep(200000); // wait 200ms |
89 |
CPPUNIT_ASSERT(t.resource == 1); |
90 |
doubleLockSucceeded = (t.resource == 1); |
91 |
} |
92 |
|
93 |
// Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously |
94 |
void MutexTest::testDoubleLockStillBlocksConcurrentThread() { |
95 |
// check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway |
96 |
doubleLockSucceeded = false; |
97 |
testDoubleLock(); |
98 |
if (doubleLockSucceeded) { |
99 |
ConcurrentThread t; |
100 |
t.mutex.Lock(); |
101 |
t.mutex.Lock(); |
102 |
t.SignalStartThread(); |
103 |
usleep(400000); // wait 400ms |
104 |
bool success = (t.resource == 0); |
105 |
CPPUNIT_ASSERT(success); |
106 |
if (success) { |
107 |
t.mutex.Unlock(); |
108 |
usleep(200000); // wait 200ms |
109 |
success = (t.resource == 0); // thread should should still be blocked, since recursive mutex |
110 |
CPPUNIT_ASSERT(success); |
111 |
} |
112 |
if (success) { |
113 |
t.mutex.Unlock(); |
114 |
usleep(200000); // wait 200ms |
115 |
CPPUNIT_ASSERT(t.resource == 1); // unlock count matches previous lock count, so thread should have run through now |
116 |
} |
117 |
} else { |
118 |
CPPUNIT_ASSERT(false); |
119 |
} |
120 |
} |