/[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 3548 - (hide annotations) (download)
Wed Jul 31 09:35:10 2019 UTC (4 years, 8 months ago) by schoenebeck
File size: 3096 byte(s)
* Fixed compiler error in Pool.h.
* Fixed compiler errors in test cases.
* Updated README for how to compile & run test cases.
* Updated test case
  MutexTest::testDoubleLockStillBlocksConcurrentThread() to latest
  expected behaviour of the Mutex class implementation (recursive
  mutex type).
* Bumped version (2.1.1.svn1).

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 schoenebeck 3548 MutexTest::ConcurrentThread::ConcurrentThread() : Thread(false, false, 0, -4),
13     mutex(Mutex::RECURSIVE)
14     {
15 schoenebeck 57 resource = 0;
16     }
17    
18     int MutexTest::ConcurrentThread::Main() {
19     mutex.Lock();
20     resource++;
21     mutex.Unlock();
22     }
23    
24    
25 schoenebeck 63 // DummyThread
26    
27 schoenebeck 3548 MutexTest::DummyThread::DummyThread() : Thread(false, false, 0, -4),
28     mutex(Mutex::RECURSIVE)
29     {
30 schoenebeck 63 resource = 0;
31     }
32    
33     int MutexTest::DummyThread::Main() {
34     mutex.Lock();
35     mutex.Lock();
36     resource++;
37     mutex.Unlock();
38     }
39    
40    
41 schoenebeck 57 // MutexTest
42    
43 schoenebeck 211 void MutexTest::printTestSuiteName() {
44     cout << "\b \nRunning Mutex Tests: " << flush;
45     }
46    
47 schoenebeck 57 void MutexTest::setUp() {
48     }
49    
50     void MutexTest::tearDown() {
51     }
52    
53 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.
54 schoenebeck 57 void MutexTest::testLockAndUnlockBySingleThread() {
55     ConcurrentThread t;
56     t.StartThread();
57     usleep(200000); // wait 200ms
58     CPPUNIT_ASSERT(t.resource == 1);
59     }
60    
61 schoenebeck 63 // Now check with two concurrent threads if one thread can block the other one by using the Mutex.
62 schoenebeck 57 void MutexTest::testLock() {
63     ConcurrentThread t;
64     t.mutex.Lock();
65     t.SignalStartThread();
66     usleep(400000); // wait 400ms
67     CPPUNIT_ASSERT(t.resource == 0);
68     t.mutex.Unlock();
69     }
70    
71 schoenebeck 63 // Now check if the blocked thread get unblocked when thread that owns the Mutex unlocks the Mutex again.
72 schoenebeck 57 void MutexTest::testUnlock() {
73 schoenebeck 63 ConcurrentThread t;
74     t.mutex.Lock();
75     t.SignalStartThread();
76     usleep(400000); // wait 400ms
77     t.mutex.Unlock();
78     usleep(400000); // wait 400ms
79     CPPUNIT_ASSERT(t.resource == 1);
80 schoenebeck 57 }
81    
82 schoenebeck 63 // Check if the same thread can lock the Mutex twice in a row without unlocking it and without getting blocked itself.
83 schoenebeck 57 void MutexTest::testDoubleLock() {
84 schoenebeck 63 DummyThread t;
85     t.SignalStartThread();
86     usleep(200000); // wait 200ms
87     CPPUNIT_ASSERT(t.resource == 1);
88     doubleLockSucceeded = (t.resource == 1);
89 schoenebeck 57 }
90 schoenebeck 63
91     // Check if the previous tests namely 'testLock()' and 'testUnlock()' still work with double locking previously
92     void MutexTest::testDoubleLockStillBlocksConcurrentThread() {
93     // check if testDoubleLock() succeeds, otherwise this test doesnt make sense anyway
94     doubleLockSucceeded = false;
95     testDoubleLock();
96     if (doubleLockSucceeded) {
97     ConcurrentThread t;
98     t.mutex.Lock();
99     t.mutex.Lock();
100     t.SignalStartThread();
101     usleep(400000); // wait 400ms
102 schoenebeck 3548 bool success = (t.resource == 0);
103     CPPUNIT_ASSERT(success);
104 schoenebeck 63 if (success) {
105 schoenebeck 3548 t.mutex.Unlock();
106 schoenebeck 63 usleep(200000); // wait 200ms
107 schoenebeck 3548 success = (t.resource == 0); // thread should should still be blocked, since recursive mutex
108     CPPUNIT_ASSERT(success);
109 schoenebeck 63 }
110 schoenebeck 3548 if (success) {
111     t.mutex.Unlock();
112     usleep(200000); // wait 200ms
113     CPPUNIT_ASSERT(t.resource == 1); // unlock count matches previous lock count, so thread should have run through now
114     }
115     } else {
116     CPPUNIT_ASSERT(false);
117 schoenebeck 63 }
118     }

  ViewVC Help
Powered by ViewVC