1 |
#ifndef __LS_MUTEXTEST_H__ |
2 |
#define __LS_MUTEXTEST_H__ |
3 |
|
4 |
#include <cppunit/TestFixture.h> |
5 |
#include <cppunit/extensions/HelperMacros.h> |
6 |
|
7 |
// needed for sleep() and usleep() calls |
8 |
#include <unistd.h> |
9 |
|
10 |
// the Mutex class we want to test |
11 |
#include "../common/Mutex.h" |
12 |
|
13 |
// we need an additional thread to test the Mutex class |
14 |
#include "../common/Thread.h" |
15 |
|
16 |
using namespace LinuxSampler; |
17 |
|
18 |
class MutexTest : public CppUnit::TestFixture { |
19 |
|
20 |
CPPUNIT_TEST_SUITE(MutexTest); |
21 |
CPPUNIT_TEST(printTestSuiteName); |
22 |
CPPUNIT_TEST(testLockAndUnlockBySingleThread); |
23 |
CPPUNIT_TEST(testLock); |
24 |
CPPUNIT_TEST(testUnlock); |
25 |
CPPUNIT_TEST(testDoubleLock); |
26 |
CPPUNIT_TEST(testDoubleLockStillBlocksConcurrentThread); |
27 |
CPPUNIT_TEST_SUITE_END(); |
28 |
|
29 |
private: |
30 |
// dummy thread to simulate usage of a shared resource |
31 |
class ConcurrentThread : public Thread { |
32 |
public: |
33 |
int resource; |
34 |
Mutex mutex; |
35 |
|
36 |
ConcurrentThread(); |
37 |
int Main(); |
38 |
}; |
39 |
|
40 |
// dummy thread we use to check if the Mutex falsely blcoks if we do a double lock and to avoid that our unit test main thread gets blocked |
41 |
class DummyThread : public Thread { |
42 |
public: |
43 |
int resource; |
44 |
|
45 |
DummyThread(); |
46 |
int Main(); |
47 |
private: |
48 |
Mutex mutex; |
49 |
}; |
50 |
|
51 |
bool doubleLockSucceeded; // true if testDoubleLock() was successful |
52 |
public: |
53 |
void setUp(); |
54 |
void tearDown(); |
55 |
|
56 |
void printTestSuiteName(); |
57 |
|
58 |
void testLockAndUnlockBySingleThread(); |
59 |
void testLock(); |
60 |
void testUnlock(); |
61 |
void testDoubleLock(); |
62 |
void testDoubleLockStillBlocksConcurrentThread(); |
63 |
}; |
64 |
|
65 |
#endif // __LS_MUTEXTEST_H__ |