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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (show annotations) (download)
Tue May 4 18:52:24 2004 UTC (19 years, 10 months ago) by schoenebeck
File size: 2559 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 #include "MutexTest.h"
2
3 #include <iostream>
4
5 CPPUNIT_TEST_SUITE_REGISTRATION(MutexTest);
6
7
8 // ConcurrentThread
9
10 MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, 0, -4) {
11 resource = 0;
12 }
13
14 int MutexTest::ConcurrentThread::Main() {
15 mutex.Lock();
16 resource++;
17 mutex.Unlock();
18 }
19
20
21 // DummyThread
22
23 MutexTest::DummyThread::DummyThread() : Thread(false, 0, -4) {
24 resource = 0;
25 }
26
27 int MutexTest::DummyThread::Main() {
28 mutex.Lock();
29 mutex.Lock();
30 resource++;
31 mutex.Unlock();
32 }
33
34
35 // MutexTest
36
37 void MutexTest::setUp() {
38 }
39
40 void MutexTest::tearDown() {
41 }
42
43 // Check with only one thread (thus no concurrency) if locking and unlocking the Mutex works without getting the thread to be locked falsely.
44 void MutexTest::testLockAndUnlockBySingleThread() {
45 ConcurrentThread t;
46 t.StartThread();
47 usleep(200000); // wait 200ms
48 CPPUNIT_ASSERT(t.resource == 1);
49 }
50
51 // Now check with two concurrent threads if one thread can block the other one by using the Mutex.
52 void MutexTest::testLock() {
53 ConcurrentThread t;
54 t.mutex.Lock();
55 t.SignalStartThread();
56 usleep(400000); // wait 400ms
57 CPPUNIT_ASSERT(t.resource == 0);
58 t.mutex.Unlock();
59 }
60
61 // Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again.
62 void MutexTest::testUnlock() {
63 ConcurrentThread t;
64 t.mutex.Lock();
65 t.SignalStartThread();
66 usleep(400000); // wait 400ms
67 t.mutex.Unlock();
68 usleep(400000); // wait 400ms
69 CPPUNIT_ASSERT(t.resource == 1);
70 }
71
72 // Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself.
73 void MutexTest::testDoubleLock() {
74 DummyThread t;
75 t.SignalStartThread();
76 usleep(200000); // wait 200ms
77 CPPUNIT_ASSERT(t.resource == 1);
78 doubleLockSucceeded = (t.resource == 1);
79 }
80
81 // Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously
82 void MutexTest::testDoubleLockStillBlocksConcurrentThread() {
83 bool success = false;
84 // check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway
85 doubleLockSucceeded = false;
86 testDoubleLock();
87 if (doubleLockSucceeded) {
88 ConcurrentThread t;
89 t.mutex.Lock();
90 t.mutex.Lock();
91 t.SignalStartThread();
92 usleep(400000); // wait 400ms
93 success = (t.resource == 0);
94 t.mutex.Unlock();
95
96 if (success) {
97 usleep(200000); // wait 200ms
98 success = (t.resource == 1);
99 }
100 }
101 CPPUNIT_ASSERT(success);
102 }

  ViewVC Help
Powered by ViewVC