1 |
schoenebeck |
57 |
#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 |
|
|
class MutexTest : public CppUnit::TestFixture { |
17 |
|
|
|
18 |
|
|
CPPUNIT_TEST_SUITE(MutexTest); |
19 |
schoenebeck |
211 |
CPPUNIT_TEST(printTestSuiteName); |
20 |
schoenebeck |
57 |
CPPUNIT_TEST(testLockAndUnlockBySingleThread); |
21 |
|
|
CPPUNIT_TEST(testLock); |
22 |
|
|
CPPUNIT_TEST(testUnlock); |
23 |
|
|
CPPUNIT_TEST(testDoubleLock); |
24 |
schoenebeck |
63 |
CPPUNIT_TEST(testDoubleLockStillBlocksConcurrentThread); |
25 |
schoenebeck |
57 |
CPPUNIT_TEST_SUITE_END(); |
26 |
|
|
|
27 |
|
|
private: |
28 |
schoenebeck |
63 |
// dummy thread to simulate usage of a shared resource |
29 |
schoenebeck |
57 |
class ConcurrentThread : public Thread { |
30 |
|
|
public: |
31 |
|
|
int resource; |
32 |
|
|
Mutex mutex; |
33 |
|
|
|
34 |
|
|
ConcurrentThread(); |
35 |
|
|
int Main(); |
36 |
|
|
}; |
37 |
schoenebeck |
63 |
|
38 |
|
|
// 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 |
39 |
|
|
class DummyThread : public Thread { |
40 |
|
|
public: |
41 |
|
|
int resource; |
42 |
|
|
|
43 |
|
|
DummyThread(); |
44 |
|
|
int Main(); |
45 |
|
|
private: |
46 |
|
|
Mutex mutex; |
47 |
|
|
}; |
48 |
|
|
|
49 |
|
|
bool doubleLockSucceeded; // true if testDoubleLock() was successful |
50 |
schoenebeck |
57 |
public: |
51 |
|
|
void setUp(); |
52 |
|
|
void tearDown(); |
53 |
|
|
|
54 |
schoenebeck |
211 |
void printTestSuiteName(); |
55 |
|
|
|
56 |
schoenebeck |
57 |
void testLockAndUnlockBySingleThread(); |
57 |
|
|
void testLock(); |
58 |
|
|
void testUnlock(); |
59 |
|
|
void testDoubleLock(); |
60 |
schoenebeck |
63 |
void testDoubleLockStillBlocksConcurrentThread(); |
61 |
schoenebeck |
57 |
}; |
62 |
|
|
|
63 |
|
|
#endif // __LS_MUTEXTEST_H__ |