/[svn]/linuxsampler/trunk/src/testcases/ConditionTest.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/testcases/ConditionTest.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3805 - (show annotations) (download)
Fri Aug 7 15:15:19 2020 UTC (3 years, 7 months ago) by schoenebeck
File size: 3926 byte(s)
* Test Cases: Fixed deadlock of Condition test on Linux.

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

  ViewVC Help
Powered by ViewVC