OpenWalnut  1.3.1
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  */
38 class Callable
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 should never throw an exception, as well as tear down.
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  {
87  Callable t;
88  t.flag = false;
89  t.c = &c;
90 
91  // start a thread
92  boost::thread thread = boost::thread( boost::bind( &Callable::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