/[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 3552 - (show annotations) (download)
Tue Aug 6 13:18:59 2019 UTC (4 years, 8 months ago) by schoenebeck
File size: 3744 byte(s)
* Test cases: Fixed thread tests segfaulting 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 #if CONFIG_PTHREAD_TESTCANCEL
53 TestCancel();
54 #endif
55 }
56 staticcondition.Unlock();
57 return 0;
58 }
59
60
61 // ConditionTest
62
63 void ConditionTest::printTestSuiteName() {
64 cout << "\b \nRunning Condition Tests: " << flush;
65 }
66
67 void ConditionTest::setUp() {
68 }
69
70 void ConditionTest::tearDown() {
71 }
72
73
74 // Check if Condition class doesn't block if desired condition is already reached
75 void ConditionTest::testDoesntBlockOnDesiredCondtion() {
76 ConditionChecker t(false);
77 t.SignalStartThread();
78 usleep(200000); // wait 200ms
79 CPPUNIT_ASSERT(t.resource == 1);
80 }
81
82 // Check if Condition class blocks if desired condition is not alredy reached
83 void ConditionTest::testBlocksIfNotDesiredCondition() {
84 ConditionChecker t(true);
85 t.SignalStartThread();
86 usleep(400000); // wait 400ms
87 CPPUNIT_ASSERT(t.resource == 0);
88 }
89
90 // Check if Condition class blocks until desired condition is reached
91 void ConditionTest::testBlocksUntilDesiredCondition() {
92 ConditionChecker checker(true); // let that thread wait until condition equals 'true'
93 ConditionSetter setter(&checker.condition, true); // let this thread set the condition to 'true'
94 checker.SignalStartThread();
95 usleep(50000); // wait 50ms
96 CPPUNIT_ASSERT(checker.resource == 0); // check if 'checker' thread has not passed the WaitAndUnlockIf() point yet
97 setter.SignalStartThread();
98 usleep(100000); // wait 100ms
99 CPPUNIT_ASSERT(checker.resource == 1); // check if 'checker' thread passed the WaitAndUnlockIf() point
100 }
101
102 // Check if the WaitIf() call blocks concurrent threads until the first WaitIf() caller calls Unlock()
103 void ConditionTest::testWaitIFBlocksConcurrentThread() {
104 // if the following three tests fail, then it doesn't make sense to continue with the actual one here
105 testDoesntBlockOnDesiredCondtion();
106 testBlocksIfNotDesiredCondition();
107 testBlocksUntilDesiredCondition();
108
109 // actual new test
110 ConditionCheckerLocking checker1(false);
111 ConditionCheckerLocking checker2(false);
112 checker1.SignalStartThread();
113 usleep(200000); // wait 200ms
114 CPPUNIT_ASSERT(checker1.resource == 1);
115 checker2.SignalStartThread();
116 usleep(200000); // wait 200ms
117 CPPUNIT_ASSERT(checker2.resource == 0); // check if condition locked by 'checker1'
118
119 // now order 'checker1' thread to Unlock() the condition again
120 checker1.doUnlock = true;
121 usleep(200000); // wait 200ms
122 CPPUNIT_ASSERT(checker2.resource == 1); // check if condition was unlocked by 'checker1'
123 }

  ViewVC Help
Powered by ViewVC