xref: /haiku/src/tests/kits/support/bautolock/AutolockLooperTest.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2 	$Id: AutolockLooperTest.cpp 332 2002-07-19 06:45:28Z tylerdauwalder $
3 
4 	This file tests all use cases of the BAutolock when used with a BLooper.
5 	BLocker based tests are done seperately.
6 
7 	*/
8 
9 
10 #include "ThreadedTestCaller.h"
11 #include "AutolockLooperTest.h"
12 #include <Autolock.h>
13 #include <Looper.h>
14 #include <OS.h>
15 
16 
17 const bigtime_t SNOOZE_TIME = 250000;
18 
19 
20 /*
21  *  Method: AutolockLooperTest::AutolockLooperTest()
22  *   Descr: This method is the only constructor for the AutolockLooperTest
23  *          class.
24  */
25 
26 
27 	AutolockLooperTest::AutolockLooperTest(std::string name) :
28 		BThreadedTestCase(name), theLooper(new BLooper)
29 {
30 	theLooper->Run();
31 	}
32 
33 
34 /*
35  *  Method: AutolockLooperTest::~AutolockLooperTest()
36  *   Descr: This method is the destructor for the AutolockLooperTest class.
37  *          It only deallocates the autoLooper and Looper.
38  */
39 
40 
41 	AutolockLooperTest::~AutolockLooperTest()
42 {
43 	if (theLooper != NULL)
44 		theLooper->Lock();
45 		theLooper->Quit();
46 	}
47 
48 
49 /*
50  *  Method:  AutolockLooperTest::TestThread1()
51  *   Descr:  This method performs the tests on the Autolock.  It constructs a new
52  *           Autolock and checks that both the Autolock and the Looper are
53  *           both locked.  Then, the Autolock is released by deleting it.  The Looper
54  *           is checked to see that it is now released.
55  */
56 
57 
58 	void AutolockLooperTest::TestThread1(void)
59 {
60 	BAutolock *theAutolock = new BAutolock(theLooper);
61 
62 	NextSubTest();
63 	CPPUNIT_ASSERT(theLooper->IsLocked());
64 	CPPUNIT_ASSERT(theLooper->LockingThread() == find_thread(NULL));
65 	CPPUNIT_ASSERT(theAutolock->IsLocked());
66 
67 	NextSubTest();
68 	delete theAutolock;
69 	theAutolock = NULL;
70 	CPPUNIT_ASSERT(theLooper->LockingThread() != find_thread(NULL));
71 }
72 
73 
74 /*
75  *  Method:  AutolockLooperTest::suite()
76  *   Descr:  This static member function returns a test caller for performing
77  *           the "AutolockLooperTest" test.  The test caller
78  *           is created as a ThreadedTestCaller (typedef'd as
79  *           BenaphoreLockCountTest1Caller) with three independent threads.
80  */
81 
82 
83 CppUnit::Test *AutolockLooperTest::suite(void)
84 {
85 	typedef BThreadedTestCaller <AutolockLooperTest >
86 		AutolockLooperTestCaller;
87 
88 	AutolockLooperTest *theTest = new AutolockLooperTest("");
89 	AutolockLooperTestCaller *threadedTest = new AutolockLooperTestCaller("BAutolock::Looper Test", theTest);
90 	threadedTest->addThread("A", &AutolockLooperTest::TestThread1);
91 	return(threadedTest);
92 }
93 
94 
95 
96 
97