Lumiera  0.pre.03
»edit your freedom«
sync-timedwait-test.cpp
Go to the documentation of this file.
1 /*
2  SyncTimedwait(Test) - check the monitor object based timed condition wait
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/error.hpp"
30 #include "lib/sync.hpp"
31 
32 #include <chrono>
33 
34 using test::Test;
35 using std::chrono::system_clock;
36 using std::chrono::milliseconds;
37 
38 
39 namespace lib {
40 namespace test{
41 
42  namespace { // test parameters...
43 
44  const uint WAIT_mSec = 20;
45  const milliseconds TIMEOUT{WAIT_mSec};
46 
47  using CLOCK_SCALE = std::milli; // Results are in ms
48  using Dur = std::chrono::duration<double, CLOCK_SCALE>;
49 
50  }//(End) parameters
51 
52 
53 
54 
55 
56 
57  /****************************************************************************/
66  : public Test,
67  Sync<NonrecursiveLock_Waitable>
68  {
69 
70  friend class Lock; // allows inheriting privately from Sync
71 
72 
73  virtual void
74  run (Arg)
75  {
76  Lock lock{this};
77 
78  auto start = system_clock::now();
79 
80  auto salvation = []{ return false; };
81  bool fulfilled = lock.wait_for (TIMEOUT, salvation);
82 
83  CHECK (not fulfilled); // condition not fulfilled, but timeout
84 
85  Dur duration = system_clock::now () - start;
86  CHECK (WAIT_mSec <= duration.count());
87  CHECK (duration.count() < 2*WAIT_mSec);
88  }
89  };
90 
91 
92 
94  LAUNCHER (SyncTimedwait_test, "unit common");
95 
96 
97 
98 }} // namespace lib::test
Facility for monitor object based locking.
Definition: sync.hpp:217
Definition: run.hpp:49
scoped guard to control the actual locking.
Definition: sync.hpp:234
Implementation namespace for support and library code.
Object Monitor based synchronisation.
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple test class runner.
const uint WAIT_mSec
milliseconds to wait before timeout
Lumiera error handling (C++ interface).