152a38012Sejakowatz /* 2*9285de51STyler Dauwalder $Id: BenaphoreLockCountTest1.cpp,v 1.3 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 "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 25*9285de51STyler Dauwalder #include <ThreadedTestCaller.h> 2652a38012Sejakowatz #include "BenaphoreLockCountTest1.h" 27*9285de51STyler Dauwalder #include <cppunit/Test.h> 28*9285de51STyler Dauwalder #include <cppunit/TestSuite.h> 29*9285de51STyler Dauwalder #include <Locker.h> 3052a38012Sejakowatz 3152a38012Sejakowatz 3252a38012Sejakowatz // This constant is used to determine the number of microseconds to 3352a38012Sejakowatz // sleep during major steps of the test. 3452a38012Sejakowatz 3552a38012Sejakowatz const bigtime_t SNOOZE_TIME = 100000; 3652a38012Sejakowatz 3752a38012Sejakowatz 3852a38012Sejakowatz /* 39*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::BenaphoreLockCountTest1() 4052a38012Sejakowatz * Descr: This is the constructor for this test class. 4152a38012Sejakowatz */ 4252a38012Sejakowatz 43*9285de51STyler Dauwalder 44*9285de51STyler Dauwalder BenaphoreLockCountTest1::BenaphoreLockCountTest1(std::string name) : 45*9285de51STyler Dauwalder LockerTestCase(name, true) 4652a38012Sejakowatz { 4752a38012Sejakowatz } 4852a38012Sejakowatz 4952a38012Sejakowatz 5052a38012Sejakowatz /* 51*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::~BenaphoreLockTestCountTest1() 5252a38012Sejakowatz * Descr: This is the destructor for this test class. 5352a38012Sejakowatz */ 5452a38012Sejakowatz 55*9285de51STyler Dauwalder 56*9285de51STyler Dauwalder BenaphoreLockCountTest1::~BenaphoreLockCountTest1() 5752a38012Sejakowatz { 5852a38012Sejakowatz } 5952a38012Sejakowatz 6052a38012Sejakowatz 6152a38012Sejakowatz /* 62*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::CheckLockRequests() 6352a38012Sejakowatz * Descr: This member function checks the actual number of lock requests 6452a38012Sejakowatz * that the BLocker thinks are outstanding versus the number 6552a38012Sejakowatz * passed in. If they match, true is returned. 6652a38012Sejakowatz */ 6752a38012Sejakowatz 68*9285de51STyler Dauwalder bool BenaphoreLockCountTest1::CheckLockRequests(int expected) 6952a38012Sejakowatz { 7052a38012Sejakowatz int actual = theLocker->CountLockRequests(); 7152a38012Sejakowatz return(actual == expected); 7252a38012Sejakowatz } 7352a38012Sejakowatz 7452a38012Sejakowatz 7552a38012Sejakowatz /* 76*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::TestThread1() 7752a38012Sejakowatz * Descr: This member function performs the main portion of the test. 7852a38012Sejakowatz * It first acquires thread2Lock and thread3Lock. This ensures 7952a38012Sejakowatz * that thread2 and thread3 will block until this thread wants 8052a38012Sejakowatz * them to start running. It then checks the lock count, acquires 8152a38012Sejakowatz * the lock and checks the lock count again. It unlocks each 8252a38012Sejakowatz * of the other two threads in turn and rechecks the lock count. 8352a38012Sejakowatz * Finally, it releases the lock and sleeps for a short while 8452a38012Sejakowatz * for the other two threads to finish. At the end, it checks 8552a38012Sejakowatz * the lock count on final time. 8652a38012Sejakowatz */ 8752a38012Sejakowatz 88*9285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread1(void) 8952a38012Sejakowatz { 90*9285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 91*9285de51STyler Dauwalder SafetyLock theSafetyLock2(&thread2Lock); 92*9285de51STyler Dauwalder SafetyLock theSafetyLock3(&thread3Lock); 9352a38012Sejakowatz 9452a38012Sejakowatz assert(thread2Lock.Lock()); 95*9285de51STyler Dauwalder NextSubTest(); 9652a38012Sejakowatz assert(thread3Lock.Lock()); 97*9285de51STyler Dauwalder NextSubTest(); 9852a38012Sejakowatz 9952a38012Sejakowatz assert(CheckLockRequests(0)); 100*9285de51STyler Dauwalder NextSubTest(); 10152a38012Sejakowatz assert(theLocker->Lock()); 102*9285de51STyler Dauwalder NextSubTest(); 10352a38012Sejakowatz 10452a38012Sejakowatz assert(CheckLockRequests(1)); 105*9285de51STyler Dauwalder NextSubTest(); 10652a38012Sejakowatz 10752a38012Sejakowatz thread2Lock.Unlock(); 108*9285de51STyler Dauwalder NextSubTest(); 10952a38012Sejakowatz snooze(SNOOZE_TIME); 110*9285de51STyler Dauwalder NextSubTest(); 11152a38012Sejakowatz assert(CheckLockRequests(3)); 112*9285de51STyler Dauwalder NextSubTest(); 11352a38012Sejakowatz 11452a38012Sejakowatz thread3Lock.Unlock(); 115*9285de51STyler Dauwalder NextSubTest(); 11652a38012Sejakowatz snooze(SNOOZE_TIME); 117*9285de51STyler Dauwalder NextSubTest(); 11852a38012Sejakowatz assert(CheckLockRequests(5)); 119*9285de51STyler Dauwalder NextSubTest(); 12052a38012Sejakowatz 12152a38012Sejakowatz theLocker->Unlock(); 122*9285de51STyler Dauwalder NextSubTest(); 12352a38012Sejakowatz snooze(SNOOZE_TIME); 124*9285de51STyler Dauwalder NextSubTest(); 12552a38012Sejakowatz assert(CheckLockRequests(2)); 126*9285de51STyler Dauwalder NextSubTest(); 12752a38012Sejakowatz } 12852a38012Sejakowatz 12952a38012Sejakowatz 13052a38012Sejakowatz /* 131*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::TestThread2() 13252a38012Sejakowatz * Descr: This member function defines the actions of the second thread of 13352a38012Sejakowatz * the test. First it sleeps for a short while and then blocks on 13452a38012Sejakowatz * the thread2Lock. When the first thread releases it, this thread 13552a38012Sejakowatz * begins its testing. It times out attempting to acquire the main 13652a38012Sejakowatz * lock and then blocks to acquire the lock. Once that lock is 13752a38012Sejakowatz * acquired, the lock count is checked before finishing this thread. 13852a38012Sejakowatz */ 13952a38012Sejakowatz 140*9285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread2(void) 14152a38012Sejakowatz { 142*9285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 14352a38012Sejakowatz 14452a38012Sejakowatz snooze(SNOOZE_TIME / 10); 145*9285de51STyler Dauwalder NextSubTest(); 14652a38012Sejakowatz assert(thread2Lock.Lock()); 147*9285de51STyler Dauwalder NextSubTest(); 14852a38012Sejakowatz 14952a38012Sejakowatz assert(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT); 150*9285de51STyler Dauwalder NextSubTest(); 15152a38012Sejakowatz assert(theLocker->Lock()); 152*9285de51STyler Dauwalder NextSubTest(); 15352a38012Sejakowatz int actual = theLocker->CountLockRequests(); 154*9285de51STyler Dauwalder NextSubTest(); 15552a38012Sejakowatz assert((actual == 3) || (actual == 4)); 156*9285de51STyler Dauwalder NextSubTest(); 15752a38012Sejakowatz theLocker->Unlock(); 158*9285de51STyler Dauwalder NextSubTest(); 15952a38012Sejakowatz } 16052a38012Sejakowatz 16152a38012Sejakowatz 16252a38012Sejakowatz /* 163*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::TestThread3() 16452a38012Sejakowatz * Descr: This member function defines the actions of the second thread of 16552a38012Sejakowatz * the test. First it sleeps for a short while and then blocks on 16652a38012Sejakowatz * the thread3Lock. When the first thread releases it, this thread 16752a38012Sejakowatz * begins its testing. It times out attempting to acquire the main 16852a38012Sejakowatz * lock and then blocks to acquire the lock. Once that lock is 16952a38012Sejakowatz * acquired, the lock count is checked before finishing this thread. 17052a38012Sejakowatz */ 17152a38012Sejakowatz 172*9285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread3(void) 17352a38012Sejakowatz { 174*9285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 17552a38012Sejakowatz 17652a38012Sejakowatz snooze(SNOOZE_TIME / 10); 177*9285de51STyler Dauwalder NextSubTest(); 17852a38012Sejakowatz assert(thread3Lock.Lock()); 179*9285de51STyler Dauwalder NextSubTest(); 18052a38012Sejakowatz 18152a38012Sejakowatz assert(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT); 182*9285de51STyler Dauwalder NextSubTest(); 18352a38012Sejakowatz assert(theLocker->Lock()); 184*9285de51STyler Dauwalder NextSubTest(); 18552a38012Sejakowatz int actual = theLocker->CountLockRequests(); 186*9285de51STyler Dauwalder NextSubTest(); 18752a38012Sejakowatz assert((actual == 3) || (actual == 4)); 188*9285de51STyler Dauwalder NextSubTest(); 18952a38012Sejakowatz theLocker->Unlock(); 190*9285de51STyler Dauwalder NextSubTest(); 19152a38012Sejakowatz } 19252a38012Sejakowatz 19352a38012Sejakowatz 19452a38012Sejakowatz /* 195*9285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::suite() 19652a38012Sejakowatz * Descr: This static member function returns a test caller for performing 19752a38012Sejakowatz * the "BenaphoreLockCountTest1" test. The test caller 19852a38012Sejakowatz * is created as a ThreadedTestCaller (typedef'd as 19952a38012Sejakowatz * BenaphoreLockCountTest1Caller) with three independent threads. 20052a38012Sejakowatz */ 20152a38012Sejakowatz 202*9285de51STyler Dauwalder CppUnit::Test *BenaphoreLockCountTest1::suite(void) 20352a38012Sejakowatz { 204*9285de51STyler Dauwalder BenaphoreLockCountTest1 *theTest = new BenaphoreLockCountTest1(""); 205*9285de51STyler Dauwalder BThreadedTestCaller<BenaphoreLockCountTest1> *threadedTest = 206*9285de51STyler Dauwalder new BThreadedTestCaller<BenaphoreLockCountTest1>("BLocker::Benaphore Lock Count Test #1", theTest); 207*9285de51STyler Dauwalder threadedTest->addThread("A", &BenaphoreLockCountTest1::TestThread1); 208*9285de51STyler Dauwalder threadedTest->addThread("B", &BenaphoreLockCountTest1::TestThread2); 209*9285de51STyler Dauwalder threadedTest->addThread("C", &BenaphoreLockCountTest1::TestThread3); 21052a38012Sejakowatz return(threadedTest); 21152a38012Sejakowatz } 21252a38012Sejakowatz 213