/[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 211 - (show annotations) (download)
Sun Jul 25 23:27:41 2004 UTC (19 years, 9 months ago) by schoenebeck
File size: 2674 byte(s)
* src/linuxsampler.cpp: tidied up a bit, "initialization completed"
  message shown only after the server is actually running
* src/testcases/: print the name of each test suite before running the
  tests of the suite, added first tests against the LSCP server using a
  socket connection to the LSCP server (tests for the following LSCP
  commands: "ADD CHANNEL", "GET CHANNELS", "REMOVE CHANNEL")

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

  ViewVC Help
Powered by ViewVC