xref: /haiku/src/tests/kits/support/bautolock/AutolockLockerTest.cpp (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2 	$Id: AutolockLockerTest.cpp 332 2002-07-19 06:45:28Z tylerdauwalder $
3 
4 	This file tests all use cases of the BAutolock when used with a BLocker.
5 	BLooper based tests are done seperately.
6 
7 	*/
8 
9 
10 #include "ThreadedTestCaller.h"
11 #include "AutolockLockerTest.h"
12 #include <Autolock.h>
13 #include <OS.h>
14 
15 
16 const bigtime_t SNOOZE_TIME = 250000;
17 
18 
19 /*
20  *  Method: AutolockLockerTest::AutolockLockerTest()
21  *   Descr: This method is the only constructor for the AutolockLockerTest
22  *          class.
23  */
24 
25 
26 	AutolockLockerTest::AutolockLockerTest(std::string name) :
27 		BThreadedTestCase(name), theLocker(new BLocker)
28 {
29 	}
30 
31 
32 /*
33  *  Method: AutolockLockerTest::~AutolockLockerTest()
34  *   Descr: This method is the destructor for the AutolockLockerTest class.
35  *          It only deallocates the autolocker and locker.
36  */
37 
38 
39 	AutolockLockerTest::~AutolockLockerTest()
40 {
41 	delete theLocker;
42 	theLocker = NULL;
43 	}
44 
45 
46 /*
47  *  Method:  AutolockLockerTest::TestThread1()
48  *   Descr:  This method performs the tests on the Autolock.  It first acquires the
49  *           lock and sleeps for a short time.  It deletes the lock rather than Unlock()
50  *           it in order to test the other two threads.  Then, it constructs a new
51  *           Locker and Autolock and checks that both the Autolock and the Locker are
52  *           both locked.  Then, the Autolock is released by deleting it.  The Locker
53  *           is checked to see that it is now released.  This is then repeated for an Autolock
54  *           constructed by passing a reference to the Locker.
55  */
56 
57 
58 	void AutolockLockerTest::TestThread1(void)
59 {
60 	BAutolock *theAutolock;
61 
62 	NextSubTest();
63 	CPPUNIT_ASSERT(theLocker->Lock());
64 	CPPUNIT_ASSERT(theLocker->LockingThread() == find_thread(NULL));
65 	snooze(SNOOZE_TIME);
66 	delete theLocker;
67 
68 	NextSubTest();
69 	theLocker =  new BLocker;
70 	theAutolock = new BAutolock(theLocker);
71 
72 	CPPUNIT_ASSERT(theLocker->IsLocked());
73 	CPPUNIT_ASSERT(theLocker->LockingThread() == find_thread(NULL));
74 	CPPUNIT_ASSERT(theAutolock->IsLocked());
75 
76 	NextSubTest();
77 	delete theAutolock;
78 	theAutolock = NULL;
79 	CPPUNIT_ASSERT(theLocker->LockingThread() != find_thread(NULL));
80 
81 	NextSubTest();
82 	theAutolock = new BAutolock(*theLocker);
83 	CPPUNIT_ASSERT(theLocker->IsLocked());
84 	CPPUNIT_ASSERT(theLocker->LockingThread() == find_thread(NULL));
85 	CPPUNIT_ASSERT(theAutolock->IsLocked());
86 
87 	NextSubTest();
88 	delete theAutolock;
89 	theAutolock = NULL;
90 	CPPUNIT_ASSERT(theLocker->LockingThread() != find_thread(NULL));
91 }
92 
93 
94 /*
95  *  Method:  AutolockLockerTest::TestThread2()
96  *   Descr:  This method performs the tests on the Autolock.  It first sleeps for a short
97  *           time and then tries to acquire the lock with an Autolock.  It passes a pointer
98  *           to the lock to the Autolock.  It expects the acquisition to fail and IsLocked()
99  *           is tested to be sure.
100  */
101 
102 
103 	void AutolockLockerTest::TestThread2(void)
104 {
105 	NextSubTest();
106 	snooze(SNOOZE_TIME / 10);
107 	BAutolock theAutolock(theLocker);
108 	CPPUNIT_ASSERT(!theAutolock.IsLocked());
109 }
110 
111 
112 /*
113  *  Method:  AutolockLockerTest::TestThread3()
114  *   Descr:  This method performs the tests on the Autolock.  It first sleeps for a short
115  *           time and then tries to acquire the lock with an Autolock.  It passes a reference
116  *           to the lock to the Autolock.  It expects the acquisition to fail and IsLocked()
117  *           is tested to be sure.
118  */
119 
120 
121 	void AutolockLockerTest::TestThread3(void)
122 {
123 	NextSubTest();
124 	snooze(SNOOZE_TIME / 10);
125 	BAutolock theAutolock(*theLocker);
126 	CPPUNIT_ASSERT(!theAutolock.IsLocked());
127 }
128 
129 
130 /*
131  *  Method:  AutolockLockerTest::suite()
132  *   Descr:  This static member function returns a test caller for performing
133  *           the "AutolockLockerTest" test.  The test caller
134  *           is created as a ThreadedTestCaller (typedef'd as
135  *           BenaphoreLockCountTest1Caller) with three independent threads.
136  */
137 
138 
139 CppUnit::Test *AutolockLockerTest::suite(void)
140 {
141 	typedef BThreadedTestCaller <AutolockLockerTest >
142 		AutolockLockerTestCaller;
143 
144 	AutolockLockerTest *theTest = new AutolockLockerTest("");
145 	AutolockLockerTestCaller *threadedTest = new AutolockLockerTestCaller("BAutolock::Locker Test", theTest);
146 	threadedTest->addThread("A", &AutolockLockerTest::TestThread1);
147 	threadedTest->addThread("B", &AutolockLockerTest::TestThread2);
148 	threadedTest->addThread("C", &AutolockLockerTest::TestThread3);
149 	return(threadedTest);
150 }
151 
152 
153 
154