add a test to ensure the timed wait feature is working properly
This commit is contained in:
parent
00a5e7028d
commit
e801c324bf
2 changed files with 141 additions and 1 deletions
140
tests/lib/sync-timedwait-test.cpp
Normal file
140
tests/lib/sync-timedwait-test.cpp
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
SyncTimedwait(Test) - check the monitor object based timed condition wait
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/error.hpp"
|
||||
|
||||
#include "lib/sync.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
using test::Test;
|
||||
|
||||
|
||||
namespace lib {
|
||||
namespace test {
|
||||
|
||||
namespace { // private test classes and data...
|
||||
|
||||
const uint WAIT_mSec = 200; ///< milliseconds to wait before timeout
|
||||
|
||||
} // (End) test classes and data....
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/********************************************************************************
|
||||
* @test timeout feature on condition wait as provided by pthread and accessible
|
||||
* via the object monitor based locking/waiting mechanism. Without creating
|
||||
* multiple threads, we engage into a blocking wait, which aborts due to
|
||||
* setting a timeout. (Note it is discouraged to use the timed wait feature;
|
||||
* when possible, you should prefer relying on the Lumiera scheduler)
|
||||
*
|
||||
* @see SyncWaiting_test
|
||||
* @see sync::Timeout
|
||||
* @see sync.hpp
|
||||
*/
|
||||
class SyncTimedwait_test
|
||||
: public Test,
|
||||
Sync<RecursiveLock_Waitable>
|
||||
{
|
||||
|
||||
friend class Lock; // allows inheriting privately from Sync
|
||||
|
||||
|
||||
virtual void
|
||||
run (Arg)
|
||||
{
|
||||
checkTimeoutStruct();
|
||||
|
||||
Lock block(this, &SyncTimedwait_test::neverHappens);
|
||||
|
||||
cout << "back from LaLaLand, alive and thriving!\n";
|
||||
ASSERT (block.isTimedWait());
|
||||
}
|
||||
|
||||
|
||||
volatile bool
|
||||
neverHappens() ///< the "condition test" used for waiting....
|
||||
{
|
||||
Lock currentLock(this); // get the Lock recursively
|
||||
if (!currentLock.isTimedWait()) // right from within the condition test:
|
||||
currentLock.setTimeout(WAIT_mSec); // switch waiting mode to timed wait and set timeout
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
checkTimeoutStruct()
|
||||
{
|
||||
sync::Timeout tout;
|
||||
|
||||
ASSERT (!tout);
|
||||
ASSERT (0 == tout.tv_sec);
|
||||
ASSERT (0 == tout.tv_nsec);
|
||||
|
||||
tout.setOffset (0);
|
||||
ASSERT (!tout);
|
||||
ASSERT (0 == tout.tv_sec);
|
||||
ASSERT (0 == tout.tv_nsec);
|
||||
|
||||
timespec ref;
|
||||
clock_gettime(CLOCK_REALTIME, &ref);
|
||||
tout.setOffset (1);
|
||||
ASSERT (tout);
|
||||
ASSERT (0 < tout.tv_sec);
|
||||
ASSERT (ref.tv_sec <= tout.tv_sec);
|
||||
ASSERT (ref.tv_nsec <= 1000000 + tout.tv_nsec || ref.tv_nsec > 1000000000-100000);
|
||||
|
||||
clock_gettime(CLOCK_REALTIME, &ref);
|
||||
tout.setOffset (1000);
|
||||
ASSERT (tout);
|
||||
if (ref.tv_nsec!=0) // should have gotten an overflow to the seconds part
|
||||
{
|
||||
ASSERT (ref.tv_sec <= 2 + tout.tv_sec );
|
||||
ASSERT ((ref.tv_nsec + 1000000 * 999) % 1000000000
|
||||
<= tout.tv_nsec);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/** Register this test class... */
|
||||
LAUNCHER (SyncTimedwait_test, "unit common");
|
||||
|
||||
|
||||
|
||||
} // namespace test
|
||||
|
||||
} // namespace lumiera
|
||||
|
|
@ -410,7 +410,7 @@ namespace lumiera
|
|||
*/
|
||||
class VisitingTool_concept : public Test
|
||||
{
|
||||
virtual void run(Arg arg)
|
||||
virtual void run(Arg)
|
||||
{
|
||||
known_visitor_known_class();
|
||||
visitor_not_visiting_some_class();
|
||||
|
|
|
|||
Loading…
Reference in a new issue