xref: /haiku/src/tests/kits/support/bautolock/AutolockLooperTest.cpp (revision a6b33ea3deb0df368b529219081f04f443aab403)
152a38012Sejakowatz /*
2*a6b33ea3STyler Dauwalder 	$Id: AutolockLooperTest.cpp,v 1.2 2002/07/19 06:45:28 tylerdauwalder Exp $
352a38012Sejakowatz 
452a38012Sejakowatz 	This file tests all use cases of the BAutolock when used with a BLooper.
552a38012Sejakowatz 	BLocker based tests are done seperately.
652a38012Sejakowatz 
752a38012Sejakowatz 	*/
852a38012Sejakowatz 
952a38012Sejakowatz 
10*a6b33ea3STyler Dauwalder #include "ThreadedTestCaller.h"
1152a38012Sejakowatz #include "AutolockLooperTest.h"
12*a6b33ea3STyler Dauwalder #include <Autolock.h>
13*a6b33ea3STyler Dauwalder #include <Looper.h>
1452a38012Sejakowatz #include <OS.h>
1552a38012Sejakowatz 
1652a38012Sejakowatz 
1752a38012Sejakowatz const bigtime_t SNOOZE_TIME = 250000;
1852a38012Sejakowatz 
1952a38012Sejakowatz 
2052a38012Sejakowatz /*
21*a6b33ea3STyler Dauwalder  *  Method: AutolockLooperTest::AutolockLooperTest()
2252a38012Sejakowatz  *   Descr: This method is the only constructor for the AutolockLooperTest
2352a38012Sejakowatz  *          class.
2452a38012Sejakowatz  */
2552a38012Sejakowatz 
26*a6b33ea3STyler Dauwalder 
27*a6b33ea3STyler Dauwalder 	AutolockLooperTest::AutolockLooperTest(std::string name) :
28*a6b33ea3STyler Dauwalder 		BThreadedTestCase(name), theLooper(new BLooper)
2952a38012Sejakowatz {
3052a38012Sejakowatz 	theLooper->Run();
3152a38012Sejakowatz 	}
3252a38012Sejakowatz 
3352a38012Sejakowatz 
3452a38012Sejakowatz /*
35*a6b33ea3STyler Dauwalder  *  Method: AutolockLooperTest::~AutolockLooperTest()
3652a38012Sejakowatz  *   Descr: This method is the destructor for the AutolockLooperTest class.
3752a38012Sejakowatz  *          It only deallocates the autoLooper and Looper.
3852a38012Sejakowatz  */
3952a38012Sejakowatz 
40*a6b33ea3STyler Dauwalder 
41*a6b33ea3STyler Dauwalder 	AutolockLooperTest::~AutolockLooperTest()
4252a38012Sejakowatz {
4352a38012Sejakowatz 	if (theLooper != NULL)
4452a38012Sejakowatz 		theLooper->Lock();
4552a38012Sejakowatz 		theLooper->Quit();
4652a38012Sejakowatz 	}
4752a38012Sejakowatz 
4852a38012Sejakowatz 
4952a38012Sejakowatz /*
50*a6b33ea3STyler Dauwalder  *  Method:  AutolockLooperTest::TestThread1()
5152a38012Sejakowatz  *   Descr:  This method performs the tests on the Autolock.  It constructs a new
5252a38012Sejakowatz  *           Autolock and checks that both the Autolock and the Looper are
5352a38012Sejakowatz  *           both locked.  Then, the Autolock is released by deleting it.  The Looper
5452a38012Sejakowatz  *           is checked to see that it is now released.
5552a38012Sejakowatz  */
5652a38012Sejakowatz 
57*a6b33ea3STyler Dauwalder 
58*a6b33ea3STyler Dauwalder 	void AutolockLooperTest::TestThread1(void)
5952a38012Sejakowatz {
60*a6b33ea3STyler Dauwalder 	BAutolock *theAutolock = new BAutolock(theLooper);
6152a38012Sejakowatz 
62*a6b33ea3STyler Dauwalder 	NextSubTest();
63*a6b33ea3STyler Dauwalder 	CPPUNIT_ASSERT(theLooper->IsLocked());
64*a6b33ea3STyler Dauwalder 	CPPUNIT_ASSERT(theLooper->LockingThread() == find_thread(NULL));
65*a6b33ea3STyler Dauwalder 	CPPUNIT_ASSERT(theAutolock->IsLocked());
6652a38012Sejakowatz 
67*a6b33ea3STyler Dauwalder 	NextSubTest();
6852a38012Sejakowatz 	delete theAutolock;
6952a38012Sejakowatz 	theAutolock = NULL;
70*a6b33ea3STyler Dauwalder 	CPPUNIT_ASSERT(theLooper->LockingThread() != find_thread(NULL));
7152a38012Sejakowatz }
7252a38012Sejakowatz 
7352a38012Sejakowatz 
7452a38012Sejakowatz /*
75*a6b33ea3STyler Dauwalder  *  Method:  AutolockLooperTest::suite()
7652a38012Sejakowatz  *   Descr:  This static member function returns a test caller for performing
7752a38012Sejakowatz  *           the "AutolockLooperTest" test.  The test caller
7852a38012Sejakowatz  *           is created as a ThreadedTestCaller (typedef'd as
7952a38012Sejakowatz  *           BenaphoreLockCountTest1Caller) with three independent threads.
8052a38012Sejakowatz  */
8152a38012Sejakowatz 
82*a6b33ea3STyler Dauwalder 
83*a6b33ea3STyler Dauwalder CppUnit::Test *AutolockLooperTest::suite(void)
8452a38012Sejakowatz {
85*a6b33ea3STyler Dauwalder 	typedef BThreadedTestCaller <AutolockLooperTest >
86*a6b33ea3STyler Dauwalder 		AutolockLooperTestCaller;
87*a6b33ea3STyler Dauwalder 
88*a6b33ea3STyler Dauwalder 	AutolockLooperTest *theTest = new AutolockLooperTest("");
89*a6b33ea3STyler Dauwalder 	AutolockLooperTestCaller *threadedTest = new AutolockLooperTestCaller("BAutolock::Looper Test", theTest);
90*a6b33ea3STyler Dauwalder 	threadedTest->addThread("A", &AutolockLooperTest::TestThread1);
9152a38012Sejakowatz 	return(threadedTest);
9252a38012Sejakowatz }
9352a38012Sejakowatz 
9452a38012Sejakowatz 
95*a6b33ea3STyler Dauwalder 
96*a6b33ea3STyler Dauwalder 
97