From e801c324bfa59fb29cdd92b7a76b55e572049e80 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 28 Dec 2008 02:06:30 +0100 Subject: [PATCH] add a test to ensure the timed wait feature is working properly --- tests/lib/sync-timedwait-test.cpp | 140 ++++++++++++++++++++++++++++++ tests/lib/visitingtoolconcept.cpp | 2 +- 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tests/lib/sync-timedwait-test.cpp diff --git a/tests/lib/sync-timedwait-test.cpp b/tests/lib/sync-timedwait-test.cpp new file mode 100644 index 000000000..21dc41b0c --- /dev/null +++ b/tests/lib/sync-timedwait-test.cpp @@ -0,0 +1,140 @@ +/* + SyncTimedwait(Test) - check the monitor object based timed condition wait + + Copyright (C) Lumiera.org + 2008, Hermann Vosseler + + 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 + +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 + { + + 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 diff --git a/tests/lib/visitingtoolconcept.cpp b/tests/lib/visitingtoolconcept.cpp index 94ad6ad98..3d625c1c5 100644 --- a/tests/lib/visitingtoolconcept.cpp +++ b/tests/lib/visitingtoolconcept.cpp @@ -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();