OpenWalnut  1.4.0
WConditionOneShot_test.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WCONDITIONONESHOT_TEST_H
26 #define WCONDITIONONESHOT_TEST_H
27 
28 #include <iostream>
29 
30 #ifndef Q_MOC_RUN
31 #include <boost/thread.hpp>
32 #endif
33 #include <cxxtest/TestSuite.h>
34 
35 #include "../WConditionOneShot.h"
36 
37 /**
38  * Helper class.
39  */
40 class Callable
41 {
42 public:
43  /**
44  * Flag set true when the thread starts.
45  */
46  bool flag;
47 
48  /**
49  * The condition to use.
50  */
52 
53  /**
54  * The thread.
55  */
56  void threadMain()
57  {
58  flag = true;
59 
60  // let the test's thread reach its "wait" call first
61  boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
62  c->notify();
63  };
64 };
65 
66 /**
67  * Test WConditionOneShot
68  */
69 class WConditionOneShotTest : public CxxTest::TestSuite
70 {
71 public:
72  /**
73  * An instantiation should never throw an exception, as well as tear down.
74  */
75  void testInstantiation( void )
76  {
77  WConditionOneShot* c = 0;
78 
79  TS_ASSERT_THROWS_NOTHING( c = new WConditionOneShot() );
80  TS_ASSERT_THROWS_NOTHING( delete c );
81  }
82 
83  /**
84  * Test whether notification is working.
85  */
87  {
89  Callable t;
90  t.flag = false;
91  t.c = &c;
92 
93  // start a thread
94  boost::thread thread = boost::thread( boost::bind( &Callable::threadMain, &t ) );
95 
96  // wait for condition
97  c.wait();
98 
99  // since it is a one shot condition -> another wait will not cause a freeze
100  c.wait();
101 
102  TS_ASSERT( t.flag );
103 
104  // notifying twice should not cause exceptions (boost::lock_error)
105  TS_ASSERT_THROWS_NOTHING( c.notify() );
106  }
107 };
108 
109 #endif // WCONDITIONONESHOT_TEST_H
110 
bool flag
Flag set to true when thread starts.
Implements a WCondition, but can be fired only ONCE.
void threadMain()
The thread.
Helper class.
Test WConditionOneShot.
virtual void notify()
Notifies all waiting threads.
WCondition * c
The condition to be used for signalling.
void testInstantiation(void)
An instantiation should never throw an exception, as well as tear down.
void testWaitNotify()
Test whether notification is working.
virtual void wait() const
Wait for the condition.
WConditionOneShot * c
The condition to use.