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