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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (hide annotations) (download)
Tue May 4 18:52:24 2004 UTC (20 years, 1 month ago) by schoenebeck
File size: 3505 byte(s)
* src/common/Thread.cpp: threads are now stoppable even if they are
  waiting for a condition
* src/common/Condition.cpp: fixed little misbehavior of Set() method,
  which locked the Condition object on return
* src/testcases: added a couple of new unit tests (against classes
  'Mutex', 'Condition' and 'Thread')

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

  ViewVC Help
Powered by ViewVC