OpenWalnut  1.5.0dev
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 #include <boost/thread.hpp>
31 #include <cxxtest/TestSuite.h>
32 
33 #include "../WConditionOneShot.h"
34 
35 /**
36  * Helper class.
37  */
39 {
40 public:
41  /**
42  * Flag set true when the thread starts.
43  */
44  bool flag;
45 
46  /**
47  * The condition to use.
48  */
50 
51  /**
52  * The thread.
53  */
54  void threadMain()
55  {
56  flag = true;
57 
58  // Let the test's thread reach its "wait" call first
59  boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
60  c->notify();
61  };
62 };
63 
64 /**
65  * Test WConditionOneShot
66  */
67 class WConditionOneShotTest : public CxxTest::TestSuite
68 {
69 public:
70  /**
71  * An instantiation as well as tear down should never throw an exception.
72  */
73  void testInstantiation( void )
74  {
75  WConditionOneShot* c = 0;
76 
77  TS_ASSERT_THROWS_NOTHING( c = new WConditionOneShot() );
78  TS_ASSERT_THROWS_NOTHING( delete c );
79  }
80 
81  /**
82  * Test whether notification is working.
83  */
85  {
88  t.flag = false;
89  t.c = &c;
90 
91  // Start a thread
92  boost::thread thread = boost::thread( boost::bind( &CallableHelper::threadMain, &t ) );
93 
94  // Wait for condition
95  c.wait();
96 
97  // Since it is a one shot condition -> another wait will not cause a freeze
98  c.wait();
99 
100  TS_ASSERT( t.flag );
101 
102  // Notifying twice should not cause exceptions (boost::lock_error)
103  TS_ASSERT_THROWS_NOTHING( c.notify() );
104  }
105 };
106 
107 #endif // WCONDITIONONESHOT_TEST_H
108 
WConditionOneShot * c
The condition to use.
bool flag
Flag set true when the thread starts.
void threadMain()
The thread.
Test WConditionOneShot.
void testInstantiation(void)
An instantiation as well as tear down should never throw an exception.
void testWaitNotify()
Test whether notification is working.
Implements a WCondition, but can be fired only ONCE.
virtual void notify()
Notifies all waiting threads.
virtual void wait() const
Wait for the condition.