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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 211 - (hide 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 schoenebeck 57 #include "MutexTest.h"
2    
3     #include <iostream>
4    
5     CPPUNIT_TEST_SUITE_REGISTRATION(MutexTest);
6    
7 schoenebeck 211 using namespace std;
8 schoenebeck 57
9 schoenebeck 211
10 schoenebeck 57 // 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 schoenebeck 63 // 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 schoenebeck 57 // MutexTest
38    
39 schoenebeck 211 void MutexTest::printTestSuiteName() {
40     cout << "\b \nRunning Mutex Tests: " << flush;
41     }
42    
43 schoenebeck 57 void MutexTest::setUp() {
44     }
45    
46     void MutexTest::tearDown() {
47     }
48    
49 schoenebeck 63 // Check with only one thread (thus no concurrency) if locking and unlocking the Mutex works without getting the thread to be locked falsely.
50 schoenebeck 57 void MutexTest::testLockAndUnlockBySingleThread() {
51     ConcurrentThread t;
52     t.StartThread();
53     usleep(200000); // wait 200ms
54     CPPUNIT_ASSERT(t.resource == 1);
55     }
56    
57 schoenebeck 63 // Now check with two concurrent threads if one thread can block the other one by using the Mutex.
58 schoenebeck 57 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 schoenebeck 63 // Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again.
68 schoenebeck 57 void MutexTest::testUnlock() {
69 schoenebeck 63 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 schoenebeck 57 }
77    
78 schoenebeck 63 // Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself.
79 schoenebeck 57 void MutexTest::testDoubleLock() {
80 schoenebeck 63 DummyThread t;
81     t.SignalStartThread();
82     usleep(200000); // wait 200ms
83     CPPUNIT_ASSERT(t.resource == 1);
84     doubleLockSucceeded = (t.resource == 1);
85 schoenebeck 57 }
86 schoenebeck 63
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