152a38012Sejakowatz /* 2*571d840aSOliver 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 259285de51STyler Dauwalder #include <ThreadedTestCaller.h> 2652a38012Sejakowatz #include "BenaphoreLockCountTest1.h" 279285de51STyler Dauwalder #include <cppunit/Test.h> 289285de51STyler Dauwalder #include <cppunit/TestSuite.h> 299285de51STyler 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 /* 399285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::BenaphoreLockCountTest1() 4052a38012Sejakowatz * Descr: This is the constructor for this test class. 4152a38012Sejakowatz */ 4252a38012Sejakowatz 439285de51STyler Dauwalder 449285de51STyler Dauwalder BenaphoreLockCountTest1::BenaphoreLockCountTest1(std::string name) : 459285de51STyler Dauwalder LockerTestCase(name, true) 4652a38012Sejakowatz { 4752a38012Sejakowatz } 4852a38012Sejakowatz 4952a38012Sejakowatz 5052a38012Sejakowatz /* 519285de51STyler Dauwalder * Method: BenaphoreLockCountTest1::~BenaphoreLockTestCountTest1() 5252a38012Sejakowatz * Descr: This is the destructor for this test class. 5352a38012Sejakowatz */ 5452a38012Sejakowatz 559285de51STyler Dauwalder 569285de51STyler Dauwalder BenaphoreLockCountTest1::~BenaphoreLockCountTest1() 5752a38012Sejakowatz { 5852a38012Sejakowatz } 5952a38012Sejakowatz 6052a38012Sejakowatz 6152a38012Sejakowatz /* 629285de51STyler 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 689285de51STyler Dauwalder bool BenaphoreLockCountTest1::CheckLockRequests(int expected) 6952a38012Sejakowatz { 7052a38012Sejakowatz int actual = theLocker->CountLockRequests(); 7152a38012Sejakowatz return(actual == expected); 7252a38012Sejakowatz } 7352a38012Sejakowatz 7452a38012Sejakowatz 7552a38012Sejakowatz /* 769285de51STyler 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 889285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread1(void) 8952a38012Sejakowatz { 909285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 919285de51STyler Dauwalder SafetyLock theSafetyLock2(&thread2Lock); 929285de51STyler Dauwalder SafetyLock theSafetyLock3(&thread3Lock); 9352a38012Sejakowatz 9452a38012Sejakowatz assert(thread2Lock.Lock()); 959285de51STyler Dauwalder NextSubTest(); 9652a38012Sejakowatz assert(thread3Lock.Lock()); 979285de51STyler Dauwalder NextSubTest(); 9852a38012Sejakowatz 9952a38012Sejakowatz assert(CheckLockRequests(0)); 1009285de51STyler Dauwalder NextSubTest(); 10152a38012Sejakowatz assert(theLocker->Lock()); 1029285de51STyler Dauwalder NextSubTest(); 10352a38012Sejakowatz 10452a38012Sejakowatz assert(CheckLockRequests(1)); 1059285de51STyler Dauwalder NextSubTest(); 10652a38012Sejakowatz 10752a38012Sejakowatz thread2Lock.Unlock(); 1089285de51STyler Dauwalder NextSubTest(); 10952a38012Sejakowatz snooze(SNOOZE_TIME); 1109285de51STyler Dauwalder NextSubTest(); 11152a38012Sejakowatz assert(CheckLockRequests(3)); 1129285de51STyler Dauwalder NextSubTest(); 11352a38012Sejakowatz 11452a38012Sejakowatz thread3Lock.Unlock(); 1159285de51STyler Dauwalder NextSubTest(); 11652a38012Sejakowatz snooze(SNOOZE_TIME); 1179285de51STyler Dauwalder NextSubTest(); 11852a38012Sejakowatz assert(CheckLockRequests(5)); 1199285de51STyler Dauwalder NextSubTest(); 12052a38012Sejakowatz 12152a38012Sejakowatz theLocker->Unlock(); 1229285de51STyler Dauwalder NextSubTest(); 12352a38012Sejakowatz snooze(SNOOZE_TIME); 1249285de51STyler Dauwalder NextSubTest(); 12552a38012Sejakowatz assert(CheckLockRequests(2)); 1269285de51STyler Dauwalder NextSubTest(); 12752a38012Sejakowatz } 12852a38012Sejakowatz 12952a38012Sejakowatz 13052a38012Sejakowatz /* 1319285de51STyler 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 1409285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread2(void) 14152a38012Sejakowatz { 1429285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 14352a38012Sejakowatz 14452a38012Sejakowatz snooze(SNOOZE_TIME / 10); 1459285de51STyler Dauwalder NextSubTest(); 14652a38012Sejakowatz assert(thread2Lock.Lock()); 1479285de51STyler Dauwalder NextSubTest(); 14852a38012Sejakowatz 14952a38012Sejakowatz assert(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT); 1509285de51STyler Dauwalder NextSubTest(); 15152a38012Sejakowatz assert(theLocker->Lock()); 1529285de51STyler Dauwalder NextSubTest(); 15352a38012Sejakowatz int actual = theLocker->CountLockRequests(); 1549285de51STyler Dauwalder NextSubTest(); 15552a38012Sejakowatz assert((actual == 3) || (actual == 4)); 1569285de51STyler Dauwalder NextSubTest(); 15752a38012Sejakowatz theLocker->Unlock(); 1589285de51STyler Dauwalder NextSubTest(); 15952a38012Sejakowatz } 16052a38012Sejakowatz 16152a38012Sejakowatz 16252a38012Sejakowatz /* 1639285de51STyler 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 1729285de51STyler Dauwalder void BenaphoreLockCountTest1::TestThread3(void) 17352a38012Sejakowatz { 1749285de51STyler Dauwalder SafetyLock theSafetyLock1(theLocker); 17552a38012Sejakowatz 17652a38012Sejakowatz snooze(SNOOZE_TIME / 10); 1779285de51STyler Dauwalder NextSubTest(); 17852a38012Sejakowatz assert(thread3Lock.Lock()); 1799285de51STyler Dauwalder NextSubTest(); 18052a38012Sejakowatz 18152a38012Sejakowatz assert(theLocker->LockWithTimeout(SNOOZE_TIME / 10) == B_TIMED_OUT); 1829285de51STyler Dauwalder NextSubTest(); 18352a38012Sejakowatz assert(theLocker->Lock()); 1849285de51STyler Dauwalder NextSubTest(); 18552a38012Sejakowatz int actual = theLocker->CountLockRequests(); 1869285de51STyler Dauwalder NextSubTest(); 18752a38012Sejakowatz assert((actual == 3) || (actual == 4)); 1889285de51STyler Dauwalder NextSubTest(); 18952a38012Sejakowatz theLocker->Unlock(); 1909285de51STyler Dauwalder NextSubTest(); 19152a38012Sejakowatz } 19252a38012Sejakowatz 19352a38012Sejakowatz 19452a38012Sejakowatz /* 1959285de51STyler 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 2029285de51STyler Dauwalder CppUnit::Test *BenaphoreLockCountTest1::suite(void) 20352a38012Sejakowatz { 2049285de51STyler Dauwalder BenaphoreLockCountTest1 *theTest = new BenaphoreLockCountTest1(""); 2059285de51STyler Dauwalder BThreadedTestCaller<BenaphoreLockCountTest1> *threadedTest = 2069285de51STyler Dauwalder new BThreadedTestCaller<BenaphoreLockCountTest1>("BLocker::Benaphore Lock Count Test #1", theTest); 2079285de51STyler Dauwalder threadedTest->addThread("A", &BenaphoreLockCountTest1::TestThread1); 2089285de51STyler Dauwalder threadedTest->addThread("B", &BenaphoreLockCountTest1::TestThread2); 2099285de51STyler Dauwalder threadedTest->addThread("C", &BenaphoreLockCountTest1::TestThread3); 21052a38012Sejakowatz return(threadedTest); 21152a38012Sejakowatz } 21252a38012Sejakowatz 213