FifoSchedulerTest.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2005-2008 Andreas Huber Doenni
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #include <boost/test/test_tools.hpp>
  7. #include <boost/statechart/asynchronous_state_machine.hpp>
  8. #include <boost/statechart/fifo_scheduler.hpp>
  9. #include <boost/statechart/event.hpp>
  10. #include <boost/statechart/simple_state.hpp>
  11. #include <boost/statechart/termination.hpp>
  12. #include <boost/statechart/custom_reaction.hpp>
  13. #include <boost/mpl/list.hpp>
  14. #include <boost/bind.hpp>
  15. #include <boost/ref.hpp>
  16. #include <stdexcept>
  17. namespace sc = boost::statechart;
  18. namespace mpl = boost::mpl;
  19. struct EvCheckCtorArgs : sc::event< EvCheckCtorArgs >
  20. {
  21. public:
  22. EvCheckCtorArgs( int expectedArgs ) : expectedArgs_( expectedArgs ) {}
  23. const int expectedArgs_;
  24. private:
  25. // avoids C4512 (assignment operator could not be generated)
  26. EvCheckCtorArgs & operator=( const EvCheckCtorArgs & );
  27. };
  28. struct EvTerminate : sc::event< EvTerminate > {};
  29. struct EvFail : sc::event< EvFail > {};
  30. struct Initial;
  31. struct FifoSchedulerTest :
  32. sc::asynchronous_state_machine< FifoSchedulerTest, Initial >
  33. {
  34. public:
  35. //////////////////////////////////////////////////////////////////////////
  36. FifoSchedulerTest( my_context ctx ) :
  37. my_base( ctx ),
  38. ctorArgs_( 0 )
  39. {
  40. }
  41. FifoSchedulerTest( my_context ctx, int arg1 ) :
  42. my_base( ctx ),
  43. ctorArgs_( arg1 )
  44. {
  45. }
  46. FifoSchedulerTest( my_context ctx, int arg1, int arg2 ) :
  47. my_base( ctx ),
  48. ctorArgs_( arg1 * 10 + arg2 )
  49. {
  50. }
  51. FifoSchedulerTest( my_context ctx, int arg1, int arg2, int arg3 ) :
  52. my_base( ctx ),
  53. ctorArgs_( ( arg1 * 10 + arg2 ) * 10 + arg3 )
  54. {
  55. }
  56. FifoSchedulerTest(
  57. my_context ctx,
  58. int arg1, int arg2, int arg3, int arg4
  59. ) :
  60. my_base( ctx ),
  61. ctorArgs_( ( ( arg1 * 10 + arg2 ) * 10 + arg3 ) * 10 + arg4 )
  62. {
  63. }
  64. FifoSchedulerTest(
  65. my_context ctx,
  66. int arg1, int arg2, int arg3, int arg4, int arg5
  67. ) :
  68. my_base( ctx ),
  69. ctorArgs_( ( ( ( arg1 * 10 + arg2 ) * 10 +
  70. arg3 ) * 10 + arg4 ) * 10 + arg5 )
  71. {
  72. }
  73. FifoSchedulerTest(
  74. my_context ctx,
  75. int arg1, int arg2, int arg3, int arg4, int arg5, int arg6
  76. ) :
  77. my_base( ctx ),
  78. ctorArgs_( ( ( ( ( arg1 * 10 + arg2 ) * 10 +
  79. arg3 ) * 10 + arg4 ) * 10 + arg5 ) * 10 + arg6 )
  80. {
  81. }
  82. int CtorArgs()
  83. {
  84. return ctorArgs_;
  85. }
  86. private:
  87. //////////////////////////////////////////////////////////////////////////
  88. const int ctorArgs_;
  89. };
  90. boost::intrusive_ptr< const sc::event_base > MakeEvent(
  91. const sc::event_base * pEvent )
  92. {
  93. return boost::intrusive_ptr< const sc::event_base >( pEvent );
  94. }
  95. struct Initial : sc::simple_state< Initial, FifoSchedulerTest >
  96. {
  97. typedef mpl::list<
  98. sc::custom_reaction< EvCheckCtorArgs >,
  99. sc::termination< EvTerminate >,
  100. sc::custom_reaction< EvFail >
  101. > reactions;
  102. sc::result react( const EvCheckCtorArgs & ev )
  103. {
  104. BOOST_REQUIRE( ev.expectedArgs_ == outermost_context().CtorArgs() );
  105. outermost_context_type & machine = outermost_context();
  106. machine.my_scheduler().queue_event(
  107. machine.my_handle(), MakeEvent( new EvTerminate() ) );
  108. return discard_event();
  109. }
  110. sc::result react( const EvFail & )
  111. {
  112. BOOST_FAIL( "State machine is unexpectedly still running." );
  113. return discard_event();
  114. }
  115. };
  116. struct UnexpectedEventCount : public std::runtime_error
  117. {
  118. UnexpectedEventCount() : std::runtime_error( "" ) {}
  119. };
  120. void RunScheduler(
  121. sc::fifo_scheduler<> & scheduler, unsigned long expectedEventCount )
  122. {
  123. // Workaround: For some reason MSVC has a problem with BOOST_REQUIRE here
  124. // (C1055: compiler limit: out of keys)
  125. if ( scheduler() != expectedEventCount )
  126. {
  127. throw UnexpectedEventCount();
  128. }
  129. }
  130. static int refArg1;
  131. static int refArg2;
  132. static int refArg3;
  133. static int refArg4;
  134. static int refArg5;
  135. static int refArg6;
  136. void Check(
  137. sc::fifo_scheduler<> & scheduler,
  138. const sc::fifo_scheduler<>::processor_handle & processor,
  139. int ctorArgs )
  140. {
  141. refArg1 = 6;
  142. refArg2 = 5;
  143. refArg3 = 4;
  144. refArg4 = 3;
  145. refArg5 = 2;
  146. refArg6 = 1;
  147. // Make sure the processor has been created
  148. RunScheduler( scheduler, 1UL );
  149. refArg1 = refArg2 = refArg3 = refArg4 = refArg5 = refArg6 = 0;
  150. scheduler.initiate_processor( processor );
  151. // This event triggers the queueing of another event, which itself
  152. // terminates the machine ...
  153. scheduler.queue_event(
  154. processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  155. // ... that's why 3 instead of two events must have been processed
  156. RunScheduler( scheduler, 3UL );
  157. // Since the machine has been terminated, this event will be ignored
  158. scheduler.queue_event( processor, MakeEvent( new EvFail() ) );
  159. RunScheduler( scheduler, 1UL );
  160. // Check that we can reinitiate the machine
  161. scheduler.initiate_processor( processor );
  162. scheduler.queue_event(
  163. processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  164. RunScheduler( scheduler, 3UL );
  165. // Check that we are terminated again
  166. scheduler.queue_event( processor, MakeEvent( new EvFail() ) );
  167. RunScheduler( scheduler, 1UL );
  168. scheduler.destroy_processor( processor );
  169. // The following will simply be ignored because the processor has already
  170. // be destroyed
  171. scheduler.initiate_processor( processor );
  172. scheduler.queue_event(
  173. processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  174. RunScheduler( scheduler, 3UL );
  175. }
  176. void SetToTrue( bool & value )
  177. {
  178. value = true;
  179. }
  180. int test_main( int, char* [] )
  181. {
  182. try
  183. {
  184. sc::fifo_scheduler<> scheduler;
  185. Check( scheduler, scheduler.create_processor< FifoSchedulerTest >(), 0 );
  186. Check(
  187. scheduler, scheduler.create_processor< FifoSchedulerTest >( 1 ), 1 );
  188. Check(
  189. scheduler,
  190. scheduler.create_processor< FifoSchedulerTest >(
  191. boost::cref( refArg1 ) ),
  192. 6 );
  193. Check(
  194. scheduler,
  195. scheduler.create_processor< FifoSchedulerTest >( 1, 2 ),
  196. 12 );
  197. Check(
  198. scheduler,
  199. scheduler.create_processor< FifoSchedulerTest >(
  200. boost::cref( refArg1 ), boost::cref( refArg2 ) ),
  201. 65 );
  202. Check(
  203. scheduler,
  204. scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3 ),
  205. 123 );
  206. Check(
  207. scheduler,
  208. scheduler.create_processor< FifoSchedulerTest >(
  209. boost::cref( refArg1 ), boost::cref( refArg2 ),
  210. boost::cref( refArg3 ) ),
  211. 654 );
  212. Check(
  213. scheduler,
  214. scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4 ),
  215. 1234 );
  216. Check(
  217. scheduler,
  218. scheduler.create_processor< FifoSchedulerTest >(
  219. boost::cref( refArg1 ), boost::cref( refArg2 ),
  220. boost::cref( refArg3 ), boost::cref( refArg4 ) ),
  221. 6543 );
  222. Check(
  223. scheduler,
  224. scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4, 5 ),
  225. 12345 );
  226. Check(
  227. scheduler,
  228. scheduler.create_processor< FifoSchedulerTest >(
  229. boost::cref( refArg1 ), boost::cref( refArg2 ),
  230. boost::cref( refArg3 ), boost::cref( refArg4 ),
  231. boost::cref( refArg5 ) ),
  232. 65432 );
  233. Check(
  234. scheduler,
  235. scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4, 5, 6 ),
  236. 123456 );
  237. Check(
  238. scheduler,
  239. scheduler.create_processor< FifoSchedulerTest >(
  240. boost::cref( refArg1 ), boost::cref( refArg2 ),
  241. boost::cref( refArg3 ), boost::cref( refArg4 ),
  242. boost::cref( refArg5 ), boost::cref( refArg6 ) ),
  243. 654321 );
  244. RunScheduler( scheduler, 0UL );
  245. bool workItem1Processed = false;
  246. scheduler.queue_work_item(
  247. boost::bind( &SetToTrue, boost::ref( workItem1Processed ) ) );
  248. RunScheduler( scheduler, 1UL );
  249. BOOST_REQUIRE( workItem1Processed );
  250. scheduler.terminate();
  251. RunScheduler( scheduler, 1UL );
  252. BOOST_REQUIRE( scheduler.terminated() );
  253. RunScheduler( scheduler, 0UL );
  254. bool workItem2Processed = false;
  255. scheduler.queue_work_item(
  256. boost::bind( &SetToTrue, boost::ref( workItem2Processed ) ) );
  257. // After being terminated, a call to operator() must not process any more
  258. // events
  259. RunScheduler( scheduler, 0UL );
  260. BOOST_REQUIRE( !workItem2Processed );
  261. }
  262. catch ( const UnexpectedEventCount & )
  263. {
  264. BOOST_FAIL( "Unexpected event count." );
  265. }
  266. return 0;
  267. }