Added BMessenger::LockTarget() tests.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@316 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2002-07-18 22:19:16 +00:00
parent 63c5cc0aa1
commit c128929474
5 changed files with 359 additions and 0 deletions

View File

@ -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

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@ -2,6 +2,7 @@ SubDir OBOS_TOP src tests kits app bmessenger ;
CommonUnitTest BMessengerTester
: main.cpp
LockTargetTester.cpp
BMessengerTester.cpp
TargetTester.cpp
: kits app

View File

@ -0,0 +1,266 @@
//------------------------------------------------------------------------------
// LockTargetTester.cpp
//
//------------------------------------------------------------------------------
// Standard Includes -----------------------------------------------------------
#include <stdio.h>
// System Includes -------------------------------------------------------------
#include <be/app/Message.h>
#include <be/kernel/OS.h>
#include <Handler.h>
#include <Looper.h>
#include <Messenger.h>
// Project Includes ------------------------------------------------------------
#include <ThreadedTestCaller.h>
#include <cppunit/TestSuite.h>
// 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<LockTargetTester> 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;
}

View File

@ -0,0 +1,51 @@
//------------------------------------------------------------------------------
// LockTargetTester.h
//
//------------------------------------------------------------------------------
#ifndef LOCK_TARGET_TESTER_H
#define LOCK_TARGET_TESTER_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
// Project Includes ------------------------------------------------------------
#include <ThreadedTestCase.h>
// 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

View File

@ -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());