1 |
#include "ConditionTest.h" |
2 |
|
3 |
#include <iostream> |
4 |
|
5 |
CPPUNIT_TEST_SUITE_REGISTRATION(ConditionTest); |
6 |
|
7 |
using namespace std; |
8 |
|
9 |
|
10 |
// ConditionChecker |
11 |
|
12 |
ConditionTest::ConditionChecker::ConditionChecker(bool waitFor) : Thread(false, 0, -4) { |
13 |
resource = 0; |
14 |
this->waitFor = waitFor; |
15 |
} |
16 |
|
17 |
int ConditionTest::ConditionChecker::Main() { |
18 |
condition.WaitAndUnlockIf(!waitFor); |
19 |
resource++; |
20 |
} |
21 |
|
22 |
|
23 |
// ConditionSetter |
24 |
|
25 |
ConditionTest::ConditionSetter::ConditionSetter(Condition* condition, bool toSet) : Thread(false, 0, -4) { |
26 |
resource = 0; |
27 |
this->toSet = toSet; |
28 |
this->condition = condition; |
29 |
} |
30 |
|
31 |
int ConditionTest::ConditionSetter::Main() { |
32 |
condition->Set(toSet); |
33 |
resource++; |
34 |
} |
35 |
|
36 |
|
37 |
// ConditionCheckerLocking |
38 |
|
39 |
Condition ConditionTest::ConditionCheckerLocking::staticcondition; // we sheare the same Condition object between all instances of ConditionCheckerLocking |
40 |
|
41 |
ConditionTest::ConditionCheckerLocking::ConditionCheckerLocking(bool waitFor) : ConditionChecker(waitFor) { |
42 |
doUnlock = false; |
43 |
} |
44 |
|
45 |
int ConditionTest::ConditionCheckerLocking::Main() { |
46 |
staticcondition.WaitIf(!waitFor); |
47 |
resource++; |
48 |
while (!doUnlock) usleep(1000); // sleep until ordered to unlock the condition again |
49 |
staticcondition.Unlock(); |
50 |
} |
51 |
|
52 |
|
53 |
// ConditionTest |
54 |
|
55 |
void ConditionTest::printTestSuiteName() { |
56 |
cout << "\b \nRunning Condition Tests: " << flush; |
57 |
} |
58 |
|
59 |
void ConditionTest::setUp() { |
60 |
} |
61 |
|
62 |
void ConditionTest::tearDown() { |
63 |
} |
64 |
|
65 |
|
66 |
// Check if Condition class doesn't block if desired condition is already reached |
67 |
void ConditionTest::testDoesntBlockOnDesiredCondtion() { |
68 |
ConditionChecker t(false); |
69 |
t.SignalStartThread(); |
70 |
usleep(200000); // wait 200ms |
71 |
CPPUNIT_ASSERT(t.resource == 1); |
72 |
} |
73 |
|
74 |
// Check if Condition class blocks if desired condition is not alredy reached |
75 |
void ConditionTest::testBlocksIfNotDesiredCondition() { |
76 |
ConditionChecker t(true); |
77 |
t.SignalStartThread(); |
78 |
usleep(400000); // wait 400ms |
79 |
CPPUNIT_ASSERT(t.resource == 0); |
80 |
} |
81 |
|
82 |
// Check if Condition class blocks until desired condition is reached |
83 |
void ConditionTest::testBlocksUntilDesiredCondition() { |
84 |
ConditionChecker checker(true); // let that thread wait until condition equals 'true' |
85 |
ConditionSetter setter(&checker.condition, true); // let this thread set the condition to 'true' |
86 |
checker.SignalStartThread(); |
87 |
usleep(50000); // wait 50ms |
88 |
CPPUNIT_ASSERT(checker.resource == 0); // check if 'checker' thread has not passed the WaitAndUnlockIf() point yet |
89 |
setter.SignalStartThread(); |
90 |
usleep(100000); // wait 100ms |
91 |
CPPUNIT_ASSERT(checker.resource == 1); // check if 'checker' thread passed the WaitAndUnlockIf() point |
92 |
} |
93 |
|
94 |
// Check if the WaitIf() call blocks concurrent threads until the first WaitIf() caller calls Unlock() |
95 |
void ConditionTest::testWaitIFBlocksConcurrentThread() { |
96 |
// if the following three tests fail, then it doesn't make sense to continue with the actual one here |
97 |
testDoesntBlockOnDesiredCondtion(); |
98 |
testBlocksIfNotDesiredCondition(); |
99 |
testBlocksUntilDesiredCondition(); |
100 |
|
101 |
// actual new test |
102 |
ConditionCheckerLocking checker1(false); |
103 |
ConditionCheckerLocking checker2(false); |
104 |
checker1.SignalStartThread(); |
105 |
usleep(200000); // wait 200ms |
106 |
CPPUNIT_ASSERT(checker1.resource == 1); |
107 |
checker2.SignalStartThread(); |
108 |
usleep(200000); // wait 200ms |
109 |
CPPUNIT_ASSERT(checker2.resource == 0); // check if condition locked by 'checker1' |
110 |
|
111 |
// now order 'checker1' thread to Unlock() the condition again |
112 |
checker1.doUnlock = true; |
113 |
usleep(200000); // wait 200ms |
114 |
CPPUNIT_ASSERT(checker2.resource == 1); // check if condition was unlocked by 'checker1' |
115 |
} |