xref: /haiku/src/tests/kits/support/blocker/DestructionTest2.cpp (revision 9285de5124a336ecd9ab26aea1acb7c79aa2f02c)
152a38012Sejakowatz /*
2*9285de51STyler Dauwalder 	$Id: DestructionTest2.cpp,v 1.2 2002/07/18 05:32:00 tylerdauwalder Exp $
352a38012Sejakowatz 
452a38012Sejakowatz 	This file implements a test class for testing BLocker functionality.
552a38012Sejakowatz 	It tests use cases "Destruction" and "Locking 4".
652a38012Sejakowatz 
752a38012Sejakowatz 	The test works like the following:
852a38012Sejakowatz 		- the main thread acquires the lock
952a38012Sejakowatz 		- it creates a new thread and sleeps
1052a38012Sejakowatz 		- the new thread attempts to acquire the lock but times out
1152a38012Sejakowatz 		- the new thread then attempts to acquire the lock again
1252a38012Sejakowatz 		- before the new thread times out a second time, the first thread releases
1352a38012Sejakowatz 		  the lock
1452a38012Sejakowatz 		- at this time, the new thread acquires the lock and goes to sleep
1552a38012Sejakowatz 		- the first thread attempts to acquire the lock
1652a38012Sejakowatz 		- the second thread deletes the lock
1752a38012Sejakowatz 		- the first thread is woken up indicating that the lock wasn't acquired.
1852a38012Sejakowatz 
1952a38012Sejakowatz 	*/
2052a38012Sejakowatz 
2152a38012Sejakowatz 
22*9285de51STyler Dauwalder #include <ThreadedTestCaller.h>
2352a38012Sejakowatz #include "DestructionTest2.h"
24*9285de51STyler Dauwalder #include <cppunit/TestSuite.h>
25*9285de51STyler Dauwalder #include <Locker.h>
2652a38012Sejakowatz 
2752a38012Sejakowatz // This constant is used to determine the number of microseconds to
2852a38012Sejakowatz // sleep during major steps of the test.
2952a38012Sejakowatz 
3052a38012Sejakowatz const bigtime_t SNOOZE_TIME = 200000;
3152a38012Sejakowatz 
3252a38012Sejakowatz 
3352a38012Sejakowatz /*
34*9285de51STyler Dauwalder  *  Method:  DestructionTest2::DestructionTest2()
3552a38012Sejakowatz  *   Descr:  This is the only constructor for this test class.
3652a38012Sejakowatz  */
3752a38012Sejakowatz 
38*9285de51STyler Dauwalder 
39*9285de51STyler Dauwalder 	DestructionTest2::DestructionTest2(std::string name,
4052a38012Sejakowatz 											   bool isBenaphore) :
41*9285de51STyler Dauwalder 		LockerTestCase(name, isBenaphore)
4252a38012Sejakowatz {
4352a38012Sejakowatz 	}
4452a38012Sejakowatz 
4552a38012Sejakowatz 
4652a38012Sejakowatz /*
47*9285de51STyler Dauwalder  *  Method:  DestructionTest2::~DestructionTest2()
4852a38012Sejakowatz  *   Descr:  This is the only destructor for this test class.
4952a38012Sejakowatz  */
5052a38012Sejakowatz 
51*9285de51STyler Dauwalder 
52*9285de51STyler Dauwalder 	DestructionTest2::~DestructionTest2()
5352a38012Sejakowatz {
5452a38012Sejakowatz 	}
5552a38012Sejakowatz 
5652a38012Sejakowatz 
5752a38012Sejakowatz /*
58*9285de51STyler Dauwalder  *  Method:  DestructionTest2::TestThread1()
5952a38012Sejakowatz  *   Descr:  This method immediately acquires the lock, sleeps
6052a38012Sejakowatz  *           for SNOOZE_TIME and then releases the lock.  It sleeps
6152a38012Sejakowatz  *           again for SNOOZE_TIME and then tries to re-acquire the
6252a38012Sejakowatz  *           lock.  By this time, the other thread should have
6352a38012Sejakowatz  *           deleted the lock.  This acquisition should fail.
6452a38012Sejakowatz  */
6552a38012Sejakowatz 
66*9285de51STyler Dauwalder void DestructionTest2::TestThread1(void)
6752a38012Sejakowatz {
6852a38012Sejakowatz 	assert(theLocker->LockWithTimeout(SNOOZE_TIME) == B_OK);
69*9285de51STyler Dauwalder 	NextSubTest();
7052a38012Sejakowatz 	snooze(SNOOZE_TIME);
71*9285de51STyler Dauwalder 	NextSubTest();
7252a38012Sejakowatz 	theLocker->Unlock();
73*9285de51STyler Dauwalder 	NextSubTest();
7452a38012Sejakowatz 	snooze(SNOOZE_TIME);
75*9285de51STyler Dauwalder 	NextSubTest();
7652a38012Sejakowatz 	assert(theLocker->LockWithTimeout(SNOOZE_TIME * 10) == B_BAD_SEM_ID);
77*9285de51STyler Dauwalder 	NextSubTest();
7852a38012Sejakowatz }
7952a38012Sejakowatz 
8052a38012Sejakowatz 
8152a38012Sejakowatz /*
82*9285de51STyler Dauwalder  *  Method:  DestructionTest2::TestThread2()
8352a38012Sejakowatz  *   Descr:  This method sleeps for SNOOZE_TIME/10 and then attempts to acquire
8452a38012Sejakowatz  *           the lock for SNOOZE_TIME/10 seconds.  This acquisition will timeout
8552a38012Sejakowatz  *           because the other thread is holding the lock.  Then it acquires the
8652a38012Sejakowatz  *           lock by using a larger timeout. It sleeps again for 2*SNOOZE_TIME and
8752a38012Sejakowatz  *           then deletes the lock.  This should wake up the other thread.
8852a38012Sejakowatz  */
8952a38012Sejakowatz 
90*9285de51STyler Dauwalder void DestructionTest2::TestThread2(void)
9152a38012Sejakowatz {
92*9285de51STyler Dauwalder 	BLocker *tmpLock;
9352a38012Sejakowatz 
9452a38012Sejakowatz 	snooze(SNOOZE_TIME/10);
95*9285de51STyler Dauwalder 	NextSubTest();
9652a38012Sejakowatz 	assert(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT);
97*9285de51STyler Dauwalder 	NextSubTest();
9852a38012Sejakowatz 	assert(theLocker->LockWithTimeout(SNOOZE_TIME * 10) == B_OK);
99*9285de51STyler Dauwalder 	NextSubTest();
10052a38012Sejakowatz 	snooze(SNOOZE_TIME);
101*9285de51STyler Dauwalder 	NextSubTest();
10252a38012Sejakowatz 	snooze(SNOOZE_TIME);
103*9285de51STyler Dauwalder 	NextSubTest();
10452a38012Sejakowatz 	tmpLock = theLocker;
10552a38012Sejakowatz 	theLocker = NULL;
10652a38012Sejakowatz 	delete tmpLock;
10752a38012Sejakowatz }
10852a38012Sejakowatz 
10952a38012Sejakowatz 
11052a38012Sejakowatz /*
111*9285de51STyler Dauwalder  *  Method:  DestructionTest2::suite()
11252a38012Sejakowatz  *   Descr:  This static member function returns a test suite for performing
11352a38012Sejakowatz  *           all combinations of "DestructionTest2".  The test suite contains
11452a38012Sejakowatz  *           two instances of the test.  One is performed on a benaphore,
11552a38012Sejakowatz  *           the other on a semaphore based BLocker.  Each individual test
11652a38012Sejakowatz  *           is created as a ThreadedTestCase (typedef'd as
11752a38012Sejakowatz  *           DestructionTest2Caller) with two independent threads.
11852a38012Sejakowatz  */
11952a38012Sejakowatz 
120*9285de51STyler Dauwalder CppUnit::Test *DestructionTest2::suite(void)
12152a38012Sejakowatz {
122*9285de51STyler Dauwalder 	typedef BThreadedTestCaller<DestructionTest2> DestructionTest2Caller;
123*9285de51STyler Dauwalder 	CppUnit::TestSuite *testSuite = new CppUnit::TestSuite("DestructionTest2");
12452a38012Sejakowatz 
12552a38012Sejakowatz 	// Make a benaphore based test object, create a ThreadedTestCase for it and add
12652a38012Sejakowatz 	// two threads to it.
127*9285de51STyler Dauwalder 	DestructionTest2 *theTest = new DestructionTest2("Benaphore", true);
128*9285de51STyler Dauwalder 	DestructionTest2Caller *threadedTest1 = new DestructionTest2Caller("BLocker::Destruction Test #2 (benaphore)", theTest);
129*9285de51STyler Dauwalder 	threadedTest1->addThread("A", &DestructionTest2::TestThread1);
130*9285de51STyler Dauwalder 	threadedTest1->addThread("B", &DestructionTest2::TestThread2);
13152a38012Sejakowatz 
13252a38012Sejakowatz 	// Make a semaphore based test object, create a ThreadedTestCase for it and add
13352a38012Sejakowatz 	// three threads to it.
134*9285de51STyler Dauwalder 	theTest = new DestructionTest2("Semaphore", false);
135*9285de51STyler Dauwalder 	DestructionTest2Caller *threadedTest2 = new DestructionTest2Caller("BLocker::Destruction Test #2 (semaphore)", theTest);
136*9285de51STyler Dauwalder 	threadedTest2->addThread("A", &DestructionTest2::TestThread1);
137*9285de51STyler Dauwalder 	threadedTest2->addThread("B", &DestructionTest2::TestThread2);
13852a38012Sejakowatz 
13952a38012Sejakowatz 	testSuite->addTest(threadedTest1);
14052a38012Sejakowatz 	testSuite->addTest(threadedTest2);
14152a38012Sejakowatz 	return(testSuite);
14252a38012Sejakowatz }
14352a38012Sejakowatz 
144*9285de51STyler Dauwalder 
145*9285de51STyler Dauwalder 
146*9285de51STyler Dauwalder 
147