xref: /haiku/src/tests/kits/support/blocker/BenaphoreLockCountTest1.cpp (revision 8a8c62d587f69e41548158cf7ecc8d724c230694)
152a38012Sejakowatz /*
2571d840aSOliver Tappe 	$Id: BenaphoreLockCountTest1.cpp 301 2002-07-18 05:32:00Z tylerdauwalder $
352a38012Sejakowatz 
452a38012Sejakowatz 	This file implements a test class for testing BLocker functionality.
552a38012Sejakowatz 	It tests use cases "Count Lock Requests" for a benaphore style BLocker.
652a38012Sejakowatz 
752a38012Sejakowatz 	The test works by:
852a38012Sejakowatz 		- checking the lock requests
952a38012Sejakowatz 		- acquiring the lock
1052a38012Sejakowatz 		- checking the lock requests
1152a38012Sejakowatz 		- staring a thread which times out acquiring the lock and then blocks
1252a38012Sejakowatz 		  again waiting for the lock
1352a38012Sejakowatz 		- checking the lock requests
1452a38012Sejakowatz 		- start a second thread which times out acquiring the lock and then blocks
1552a38012Sejakowatz 		  again waiting for the lock
1652a38012Sejakowatz 		- checking the lock requests
1752a38012Sejakowatz 		- release the lock
1852a38012Sejakowatz 		- each blocked thread acquires the lock, checks the lock requests and releases
1952a38012Sejakowatz 		  the lock before terminating
2052a38012Sejakowatz 		- the main thread checks the lock requests one last time
2152a38012Sejakowatz 
2252a38012Sejakowatz 	*/
2352a38012Sejakowatz 
2452a38012Sejakowatz 
2552a38012Sejakowatz #include "BenaphoreLockCountTest1.h"
26*8a8c62d5SAxel Dörfler 
279285de51STyler Dauwalder #include <cppunit/Test.h>
289285de51STyler Dauwalder #include <cppunit/TestSuite.h>
29*8a8c62d5SAxel Dörfler 
309285de51STyler Dauwalder #include <Locker.h>
3152a38012Sejakowatz 
32*8a8c62d5SAxel Dörfler #include <ThreadedTestCaller.h>
33*8a8c62d5SAxel Dörfler 
3452a38012Sejakowatz 
3552a38012Sejakowatz // This constant is used to determine the number of microseconds to
3652a38012Sejakowatz // sleep during major steps of the test.
3752a38012Sejakowatz 
3852a38012Sejakowatz const bigtime_t SNOOZE_TIME = 100000;
3952a38012Sejakowatz 
4052a38012Sejakowatz 
4152a38012Sejakowatz /*
429285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::BenaphoreLockCountTest1()
4352a38012Sejakowatz  *   Descr:  This is the constructor for this test class.
4452a38012Sejakowatz  */
4552a38012Sejakowatz 
469285de51STyler Dauwalder 
BenaphoreLockCountTest1(std::string name)479285de51STyler Dauwalder 	BenaphoreLockCountTest1::BenaphoreLockCountTest1(std::string name) :
489285de51STyler Dauwalder 		LockerTestCase(name, true)
4952a38012Sejakowatz {
5052a38012Sejakowatz 	}
5152a38012Sejakowatz 
5252a38012Sejakowatz 
5352a38012Sejakowatz /*
549285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::~BenaphoreLockTestCountTest1()
5552a38012Sejakowatz  *   Descr:  This is the destructor for this test class.
5652a38012Sejakowatz  */
5752a38012Sejakowatz 
589285de51STyler Dauwalder 
~BenaphoreLockCountTest1()599285de51STyler Dauwalder 	BenaphoreLockCountTest1::~BenaphoreLockCountTest1()
6052a38012Sejakowatz {
6152a38012Sejakowatz 	}
6252a38012Sejakowatz 
6352a38012Sejakowatz 
6452a38012Sejakowatz /*
659285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::CheckLockRequests()
6652a38012Sejakowatz  *   Descr:  This member function checks the actual number of lock requests
6752a38012Sejakowatz  *           that the BLocker thinks are outstanding versus the number
6852a38012Sejakowatz  *           passed in.  If they match, true is returned.
6952a38012Sejakowatz  */
7052a38012Sejakowatz 
CheckLockRequests(int expected)719285de51STyler Dauwalder bool BenaphoreLockCountTest1::CheckLockRequests(int expected)
7252a38012Sejakowatz {
7352a38012Sejakowatz 	int actual = theLocker->CountLockRequests();
7452a38012Sejakowatz 	return(actual == expected);
7552a38012Sejakowatz }
7652a38012Sejakowatz 
7752a38012Sejakowatz 
7852a38012Sejakowatz /*
799285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::TestThread1()
8052a38012Sejakowatz  *   Descr:  This member function performs the main portion of the test.
8152a38012Sejakowatz  *           It first acquires thread2Lock and thread3Lock.  This ensures
8252a38012Sejakowatz  *           that thread2 and thread3 will block until this thread wants
8352a38012Sejakowatz  *           them to start running.  It then checks the lock count, acquires
8452a38012Sejakowatz  *           the lock and checks the lock count again.  It unlocks each
8552a38012Sejakowatz  *           of the other two threads in turn and rechecks the lock count.
8652a38012Sejakowatz  *           Finally, it releases the lock and sleeps for a short while
8752a38012Sejakowatz  *           for the other two threads to finish.  At the end, it checks
8852a38012Sejakowatz  *           the lock count on final time.
8952a38012Sejakowatz  */
9052a38012Sejakowatz 
TestThread1(void)919285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread1(void)
9252a38012Sejakowatz {
939285de51STyler Dauwalder 	SafetyLock theSafetyLock1(theLocker);
949285de51STyler Dauwalder 	SafetyLock theSafetyLock2(&thread2Lock);
959285de51STyler Dauwalder 	SafetyLock theSafetyLock3(&thread3Lock);
9652a38012Sejakowatz 
97*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(thread2Lock.Lock());
989285de51STyler Dauwalder 	NextSubTest();
99*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(thread3Lock.Lock());
1009285de51STyler Dauwalder 	NextSubTest();
10152a38012Sejakowatz 
102*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(CheckLockRequests(0));
1039285de51STyler Dauwalder 	NextSubTest();
104*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(theLocker->Lock());
1059285de51STyler Dauwalder 	NextSubTest();
10652a38012Sejakowatz 
107*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(CheckLockRequests(1));
1089285de51STyler Dauwalder 	NextSubTest();
10952a38012Sejakowatz 
11052a38012Sejakowatz 	thread2Lock.Unlock();
1119285de51STyler Dauwalder 	NextSubTest();
11252a38012Sejakowatz 	snooze(SNOOZE_TIME);
1139285de51STyler Dauwalder 	NextSubTest();
114*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(CheckLockRequests(3));
1159285de51STyler Dauwalder 	NextSubTest();
11652a38012Sejakowatz 
11752a38012Sejakowatz 	thread3Lock.Unlock();
1189285de51STyler Dauwalder 	NextSubTest();
11952a38012Sejakowatz 	snooze(SNOOZE_TIME);
1209285de51STyler Dauwalder 	NextSubTest();
121*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(CheckLockRequests(5));
1229285de51STyler Dauwalder 	NextSubTest();
12352a38012Sejakowatz 
12452a38012Sejakowatz 	theLocker->Unlock();
1259285de51STyler Dauwalder 	NextSubTest();
12652a38012Sejakowatz 	snooze(SNOOZE_TIME);
1279285de51STyler Dauwalder 	NextSubTest();
128*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(CheckLockRequests(2));
1299285de51STyler Dauwalder 	NextSubTest();
13052a38012Sejakowatz 	}
13152a38012Sejakowatz 
13252a38012Sejakowatz 
13352a38012Sejakowatz /*
1349285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::TestThread2()
13552a38012Sejakowatz  *   Descr:  This member function defines the actions of the second thread of
13652a38012Sejakowatz  *           the test.  First it sleeps for a short while and then blocks on
13752a38012Sejakowatz  *           the thread2Lock.  When the first thread releases it, this thread
13852a38012Sejakowatz  *           begins its testing.  It times out attempting to acquire the main
13952a38012Sejakowatz  *           lock and then blocks to acquire the lock.  Once that lock is
14052a38012Sejakowatz  *           acquired, the lock count is checked before finishing this thread.
14152a38012Sejakowatz  */
14252a38012Sejakowatz 
TestThread2(void)1439285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread2(void)
14452a38012Sejakowatz {
1459285de51STyler Dauwalder 	SafetyLock theSafetyLock1(theLocker);
14652a38012Sejakowatz 
14752a38012Sejakowatz 	snooze(SNOOZE_TIME / 10);
1489285de51STyler Dauwalder 	NextSubTest();
149*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(thread2Lock.Lock());
1509285de51STyler Dauwalder 	NextSubTest();
15152a38012Sejakowatz 
152*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT);
1539285de51STyler Dauwalder 	NextSubTest();
154*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(theLocker->Lock());
1559285de51STyler Dauwalder 	NextSubTest();
15652a38012Sejakowatz 	int actual = theLocker->CountLockRequests();
1579285de51STyler Dauwalder 	NextSubTest();
158*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT((actual == 3) || (actual == 4));
1599285de51STyler Dauwalder 	NextSubTest();
16052a38012Sejakowatz 	theLocker->Unlock();
1619285de51STyler Dauwalder 	NextSubTest();
16252a38012Sejakowatz }
16352a38012Sejakowatz 
16452a38012Sejakowatz 
16552a38012Sejakowatz /*
1669285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::TestThread3()
16752a38012Sejakowatz  *   Descr:  This member function defines the actions of the second thread of
16852a38012Sejakowatz  *           the test.  First it sleeps for a short while and then blocks on
16952a38012Sejakowatz  *           the thread3Lock.  When the first thread releases it, this thread
17052a38012Sejakowatz  *           begins its testing.  It times out attempting to acquire the main
17152a38012Sejakowatz  *           lock and then blocks to acquire the lock.  Once that lock is
17252a38012Sejakowatz  *           acquired, the lock count is checked before finishing this thread.
17352a38012Sejakowatz  */
17452a38012Sejakowatz 
TestThread3(void)1759285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread3(void)
17652a38012Sejakowatz {
1779285de51STyler Dauwalder 	SafetyLock theSafetyLock1(theLocker);
17852a38012Sejakowatz 
17952a38012Sejakowatz 	snooze(SNOOZE_TIME / 10);
1809285de51STyler Dauwalder 	NextSubTest();
181*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(thread3Lock.Lock());
1829285de51STyler Dauwalder 	NextSubTest();
18352a38012Sejakowatz 
184*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT);
1859285de51STyler Dauwalder 	NextSubTest();
186*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT(theLocker->Lock());
1879285de51STyler Dauwalder 	NextSubTest();
18852a38012Sejakowatz 	int actual = theLocker->CountLockRequests();
1899285de51STyler Dauwalder 	NextSubTest();
190*8a8c62d5SAxel Dörfler 	CPPUNIT_ASSERT((actual == 3) || (actual == 4));
1919285de51STyler Dauwalder 	NextSubTest();
19252a38012Sejakowatz 	theLocker->Unlock();
1939285de51STyler Dauwalder 	NextSubTest();
19452a38012Sejakowatz }
19552a38012Sejakowatz 
19652a38012Sejakowatz 
19752a38012Sejakowatz /*
1989285de51STyler Dauwalder  *  Method:  BenaphoreLockCountTest1::suite()
19952a38012Sejakowatz  *   Descr:  This static member function returns a test caller for performing
20052a38012Sejakowatz  *           the "BenaphoreLockCountTest1" test.  The test caller
20152a38012Sejakowatz  *           is created as a ThreadedTestCaller (typedef'd as
20252a38012Sejakowatz  *           BenaphoreLockCountTest1Caller) with three independent threads.
20352a38012Sejakowatz  */
20452a38012Sejakowatz 
suite(void)2059285de51STyler Dauwalder CppUnit::Test *BenaphoreLockCountTest1::suite(void)
20652a38012Sejakowatz {
2079285de51STyler Dauwalder 	BenaphoreLockCountTest1 *theTest = new BenaphoreLockCountTest1("");
2089285de51STyler Dauwalder 	BThreadedTestCaller<BenaphoreLockCountTest1> *threadedTest =
2099285de51STyler Dauwalder 		new BThreadedTestCaller<BenaphoreLockCountTest1>("BLocker::Benaphore Lock Count Test #1", theTest);
2109285de51STyler Dauwalder 	threadedTest->addThread("A", &BenaphoreLockCountTest1::TestThread1);
2119285de51STyler Dauwalder 	threadedTest->addThread("B", &BenaphoreLockCountTest1::TestThread2);
2129285de51STyler Dauwalder 	threadedTest->addThread("C", &BenaphoreLockCountTest1::TestThread3);
21352a38012Sejakowatz 	return(threadedTest);
21452a38012Sejakowatz }
21552a38012Sejakowatz 
216