From c12892947402689216cbadb3ea3b8181b00453ca Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 18 Jul 2002 22:19:16 +0000 Subject: [PATCH] Added BMessenger::LockTarget() tests. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@316 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/app/bmessenger/BMessengerCases | 39 +++ src/tests/kits/app/bmessenger/Jamfile | 1 + .../kits/app/bmessenger/LockTargetTester.cpp | 266 ++++++++++++++++++ .../kits/app/bmessenger/LockTargetTester.h | 51 ++++ src/tests/kits/app/bmessenger/main.cpp | 2 + 5 files changed, 359 insertions(+) create mode 100644 src/tests/kits/app/bmessenger/LockTargetTester.cpp create mode 100644 src/tests/kits/app/bmessenger/LockTargetTester.h diff --git a/src/tests/kits/app/bmessenger/BMessengerCases b/src/tests/kits/app/bmessenger/BMessengerCases index 37598c91e3..fb82cd14cb 100644 --- a/src/tests/kits/app/bmessenger/BMessengerCases +++ b/src/tests/kits/app/bmessenger/BMessengerCases @@ -71,4 +71,43 @@ case 4: this is initialized to local target with preferred handler => case 5: this is initialized to remote target with specific handler => should return false. +BHandler *Target(BLooper **looper) const +case 1: this is uninitialized, looper is NULL => + should return NULL. +case 2: this is initialized to local target with preferred handler, + looper is NULL => + should return NULL. +case 3: this is initialized to local target with specific handler, + looper is NULL => + should return correct handler. +case 4: this is initialized to remote target with preferred handler, + looper is NULL => + should return NULL. +case 5: this is initialized to remote target with specific handler, + looper is NULL => + should return NULL. +other cases in included in BMessenger(...) +bool LockTarget() const +case 1: this is uninitialized => + should return false. +case 2: this is initialized to local target with preferred handler, + looper is not locked => + should lock the looper and return true. +case 3: this is initialized to local target with specific handler, + looper is not locked => + should lock the looper and return true. +case 4: this is initialized to local target with preferred handler, + looper is locked by another thread => + should block until the looper is unlocked, lock it and return true. +case 5: this is initialized to local target with specific handler, + looper is locked by another thread => + should block until the looper is unlocked, lock it and return true. +case 6: this is initialized to remote target with preferred handler, + looper is not locked => + should not lock the looper and return false. +case 7: this is initialized to remote target with specific handler, + looper is not locked => + should not lock the looper and return false. + +status_t LockTargetWithTimeout(bigtime_t timeout) const diff --git a/src/tests/kits/app/bmessenger/Jamfile b/src/tests/kits/app/bmessenger/Jamfile index 908ceb6966..a739df0e79 100644 --- a/src/tests/kits/app/bmessenger/Jamfile +++ b/src/tests/kits/app/bmessenger/Jamfile @@ -2,6 +2,7 @@ SubDir OBOS_TOP src tests kits app bmessenger ; CommonUnitTest BMessengerTester : main.cpp + LockTargetTester.cpp BMessengerTester.cpp TargetTester.cpp : kits app diff --git a/src/tests/kits/app/bmessenger/LockTargetTester.cpp b/src/tests/kits/app/bmessenger/LockTargetTester.cpp new file mode 100644 index 0000000000..014e9f7b0e --- /dev/null +++ b/src/tests/kits/app/bmessenger/LockTargetTester.cpp @@ -0,0 +1,266 @@ +//------------------------------------------------------------------------------ +// LockTargetTester.cpp +// +//------------------------------------------------------------------------------ + +// Standard Includes ----------------------------------------------------------- +#include + +// System Includes ------------------------------------------------------------- +#include +#include + +#include +#include +#include + +// Project Includes ------------------------------------------------------------ +#include +#include + +// Local Includes -------------------------------------------------------------- +#include "Helpers.h" +#include "LockTargetTester.h" + +// Local Defines --------------------------------------------------------------- +#define CHK CPPUNIT_ASSERT + +// Globals --------------------------------------------------------------------- + +//------------------------------------------------------------------------------ + +// constructor +LockTargetTester::LockTargetTester() + : BThreadedTestCase(), + fHandler(NULL), + fLooper(NULL) +{ +} + +// constructor +LockTargetTester::LockTargetTester(std::string name) + : BThreadedTestCase(name), + fHandler(NULL), + fLooper(NULL) +{ +} + +// destructor +LockTargetTester::~LockTargetTester() +{ + if (fLooper) { + fLooper->Lock(); + if (fHandler) { + fLooper->RemoveHandler(fHandler); + delete fHandler; + } + fLooper->Quit(); + } +} + +/* + bool LockTarget() const + @case 1 this is uninitialized + @results should return false. + */ +void LockTargetTester::LockTargetTest1() +{ + BMessenger messenger; + CHK(messenger.LockTarget() == false); +} + +/* + bool LockTarget() const + @case 2 this is initialized to local target with preferred handler, + looper is not locked + @results should lock the looper and return true. + */ +void LockTargetTester::LockTargetTest2() +{ + status_t result = B_OK; + BLooper *looper = new BLooper; + looper->Run(); + LooperQuitter quitter(looper); + BMessenger messenger(NULL, looper, &result); + CHK(messenger.LockTarget() == true); + CHK(looper->IsLocked() == true); + looper->Unlock(); + CHK(looper->IsLocked() == false); +} + +/* + bool LockTarget() const + @case 3 this is initialized to local target with specific handler, + looper is not locked + @results should lock the looper and return true. + */ +void LockTargetTester::LockTargetTest3() +{ + // create looper and handler + status_t result = B_OK; + BLooper *looper = new BLooper; + looper->Run(); + LooperQuitter quitter(looper); + BHandler *handler = new BHandler; + HandlerDeleter deleter(handler); + CHK(looper->Lock()); + looper->AddHandler(handler); + looper->Unlock(); + // create the messenger and do the checks + BMessenger messenger(handler, NULL, &result); + CHK(messenger.LockTarget() == true); + CHK(looper->IsLocked() == true); + looper->Unlock(); + CHK(looper->IsLocked() == false); +} + +/* + bool LockTarget() const + @case 4 this is initialized to local target with preferred handler, + looper is locked by another thread + @results should block until the looper is unlocked, lock it and + return true. + @thread A - locks the looper + - waits 100ms + - unlocks the looper + */ +void LockTargetTester::LockTargetTest4A() +{ + CHK(fLooper->Lock() == true); + snooze(100000); + fLooper->Unlock(); +} + +/* + bool LockTarget() const + @case 4 this is initialized to local target with preferred handler, + looper is locked by another thread + @results should block until the looper is unlocked, lock it and + return true. + @thread B - waits 50ms (until thread A has acquired the looper lock) + - tries to lock the looper via messenger and blocks + - acquires the lock successfully after 50ms + - unlocks the looper + */ +void LockTargetTester::LockTargetTest4B() +{ + enum { JITTER = 10000 }; // Maybe critical on slow machines. + snooze(50000); + BMessenger messenger(NULL, fLooper); + bigtime_t time = system_time(); + CHK(messenger.LockTarget() == true); + time = system_time() - time - 50000; + CHK(fLooper->IsLocked() == true); + fLooper->Unlock(); + CHK(fLooper->IsLocked() == false); + CHK(time > -JITTER && time < JITTER); +} + +/* + bool LockTarget() const + @case 5 this is initialized to local target with specific handler, + looper is locked by another thread + @results should block until the looper is unlocked, lock it and + return true. + @thread A - locks the looper + - waits 100ms + - unlocks the looper + */ +void LockTargetTester::LockTargetTest5A() +{ + CHK(fLooper->Lock() == true); + snooze(100000); + fLooper->Unlock(); +} + +/* + bool LockTarget() const + @case 5 this is initialized to local target with specific handler, + looper is locked by another thread + @results should block until the looper is unlocked, lock it and + return true. + @thread B - waits 50ms (until thread A has acquired the looper lock) + - tries to lock the looper via messenger and blocks + - acquires the lock successfully after 50ms + - unlocks the looper + */ +void LockTargetTester::LockTargetTest5B() +{ + enum { JITTER = 10000 }; // Maybe critical on slow machines. + snooze(50000); + BMessenger messenger(fHandler, NULL); + bigtime_t time = system_time(); + CHK(messenger.LockTarget() == true); + time = system_time() - time - 50000; + CHK(fLooper->IsLocked() == true); + fLooper->Unlock(); + CHK(fLooper->IsLocked() == false); + CHK(time > -JITTER && time < JITTER); +} + +/* + bool LockTarget() const + @case 6 this is initialized to remote target with preferred + handler, looper is not locked + @results should not lock the looper and return false. + */ +void LockTargetTester::LockTargetTest6() +{ + // TODO: Implement! +} + +/* + bool LockTarget() const + @case 7 this is initialized to remote target with specific handler, + looper is not locked + @results should not lock the looper and return false. + */ +void LockTargetTester::LockTargetTest7() +{ + // TODO: Implement! +} + + +Test* LockTargetTester::Suite() +{ + typedef BThreadedTestCaller TC; + + TestSuite* testSuite = new TestSuite; + + ADD_TEST(testSuite, LockTargetTester, LockTargetTest1); + ADD_TEST(testSuite, LockTargetTester, LockTargetTest2); + ADD_TEST(testSuite, LockTargetTester, LockTargetTest3); + // test4 + LockTargetTester *test4 + = new LockTargetTester("BMessenger::LockTarget Test"); + test4->fLooper = new BLooper; + test4->fLooper->Run(); + // test4 test caller + TC *caller4 = new TC("BMessenger::LockTarget Test 4", test4); + caller4->addThread("A", &LockTargetTester::LockTargetTest4A); + caller4->addThread("B", &LockTargetTester::LockTargetTest4B); + testSuite->addTest(caller4); + // test5 + LockTargetTester *test5 + = new LockTargetTester("BMessenger::LockTarget Test"); + // create looper and handler + test5->fLooper = new BLooper; + test5->fLooper->Run(); + test5->fHandler = new BHandler; + if (test5->fLooper->Lock()) { + test5->fLooper->AddHandler(test5->fHandler); + test5->fLooper->Unlock(); + } else + printf("ERROR: Can't init LockTargetTester test5!\n"); + // test5 test caller + TC *caller5 = new TC("BMessenger::LockTarget Test 5", test5); + caller5->addThread("A", &LockTargetTester::LockTargetTest5A); + caller5->addThread("B", &LockTargetTester::LockTargetTest5B); + testSuite->addTest(caller5); + // tests 6-7 + ADD_TEST(testSuite, LockTargetTester, LockTargetTest6); + ADD_TEST(testSuite, LockTargetTester, LockTargetTest7); + + return testSuite; +} + diff --git a/src/tests/kits/app/bmessenger/LockTargetTester.h b/src/tests/kits/app/bmessenger/LockTargetTester.h new file mode 100644 index 0000000000..88ba0e3270 --- /dev/null +++ b/src/tests/kits/app/bmessenger/LockTargetTester.h @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// LockTargetTester.h +// +//------------------------------------------------------------------------------ + +#ifndef LOCK_TARGET_TESTER_H +#define LOCK_TARGET_TESTER_H + +// Standard Includes ----------------------------------------------------------- + +// System Includes ------------------------------------------------------------- + +// Project Includes ------------------------------------------------------------ +#include + +// Local Includes -------------------------------------------------------------- +#include "../common.h" + +// Local Defines --------------------------------------------------------------- + +// Globals --------------------------------------------------------------------- + +class BHandler; +class BLooper; + +class LockTargetTester : public BThreadedTestCase +{ +public: + LockTargetTester(); + LockTargetTester(std::string name); + virtual ~LockTargetTester(); + + void LockTargetTest1(); + void LockTargetTest2(); + void LockTargetTest3(); + void LockTargetTest4A(); + void LockTargetTest4B(); + void LockTargetTest5A(); + void LockTargetTest5B(); + void LockTargetTest6(); + void LockTargetTest7(); + + static Test* Suite(); + +private: + BHandler *fHandler; + BLooper *fLooper; +}; + +#endif // LOCK_TARGET_TESTER_H + diff --git a/src/tests/kits/app/bmessenger/main.cpp b/src/tests/kits/app/bmessenger/main.cpp index c2f8556566..0b7fc44035 100644 --- a/src/tests/kits/app/bmessenger/main.cpp +++ b/src/tests/kits/app/bmessenger/main.cpp @@ -13,6 +13,7 @@ // Local Includes -------------------------------------------------------------- #include "../common.h" #include "BMessengerTester.h" +#include "LockTargetTester.h" #include "TargetTester.h" // Local Defines --------------------------------------------------------------- @@ -32,6 +33,7 @@ Test* addonTestFunc(void) { TestSuite* tests = new TestSuite("BHandler"); + tests->addTest(LockTargetTester::Suite()); tests->addTest(TBMessengerTester::Suite()); tests->addTest(TargetTester::Suite());