File: FifoSchedulerTest.cpp

package info (click to toggle)
boost 1.34.1-14
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 116,412 kB
  • ctags: 259,566
  • sloc: cpp: 642,395; xml: 56,450; python: 17,612; ansic: 14,520; sh: 2,265; yacc: 858; perl: 481; makefile: 478; lex: 94; sql: 74; csh: 6
file content (261 lines) | stat: -rw-r--r-- 7,447 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//////////////////////////////////////////////////////////////////////////////
// Copyright 2005-2006 Andreas Huber Doenni
// Distributed under the Boost Software License, Version 1.0. (See accompany-
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//////////////////////////////////////////////////////////////////////////////



#include <boost/statechart/asynchronous_state_machine.hpp>
#include <boost/statechart/fifo_scheduler.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/termination.hpp>
#include <boost/statechart/custom_reaction.hpp>

#include <boost/mpl/list.hpp>

#include <boost/bind.hpp>
#include <boost/ref.hpp>

#include <boost/test/test_tools.hpp>

#include <stdexcept>



namespace sc = boost::statechart;
namespace mpl = boost::mpl;



struct EvCheckCtorArgs : sc::event< EvCheckCtorArgs >
{
  EvCheckCtorArgs( int expectedArgs ) : expectedArgs_( expectedArgs ) {}
  const int expectedArgs_;
};

struct EvTerminate : sc::event< EvTerminate > {};
struct EvFail : sc::event< EvFail > {};


struct Initial;
struct FifoSchedulerTest :
  sc::asynchronous_state_machine< FifoSchedulerTest, Initial >
{
  public:
    //////////////////////////////////////////////////////////////////////////
    FifoSchedulerTest( my_context ctx ) :
      my_base( ctx ),
      ctorArgs_( 0 )
    {
    }

    FifoSchedulerTest( my_context ctx, int arg1 ) :
      my_base( ctx ),
      ctorArgs_( arg1 )
    {
    }

    FifoSchedulerTest( my_context ctx, int arg1, int arg2 ) :
      my_base( ctx ),
      ctorArgs_( arg1 * 10 + arg2 )
    {
    }

    FifoSchedulerTest( my_context ctx, int arg1, int arg2, int arg3 ) :
      my_base( ctx ),
      ctorArgs_( ( arg1 * 10 + arg2 ) * 10 + arg3 )
    {
    }

    FifoSchedulerTest(
      my_context ctx,
      int arg1, int arg2, int arg3, int arg4
    ) :
      my_base( ctx ),
      ctorArgs_( ( ( arg1 * 10 + arg2 ) * 10 + arg3 ) * 10 + arg4 )
    {
    }

    FifoSchedulerTest(
      my_context ctx,
      int arg1, int arg2, int arg3, int arg4, int arg5
    ) :
      my_base( ctx ),
      ctorArgs_( ( ( ( arg1 * 10 + arg2 ) * 10 +
        arg3 ) * 10 + arg4 ) * 10 + arg5 )
    {
    }

    FifoSchedulerTest(
      my_context ctx,
      int arg1, int arg2, int arg3, int arg4, int arg5, int arg6
    ) :
      my_base( ctx ),
      ctorArgs_( ( ( ( ( arg1 * 10 + arg2 ) * 10 +
        arg3 ) * 10 + arg4 ) * 10 + arg5 ) * 10 + arg6 )
    {
    }

    int CtorArgs()
    {
      return ctorArgs_;
    }

  private:
    //////////////////////////////////////////////////////////////////////////
    const int ctorArgs_;
};

boost::intrusive_ptr< const sc::event_base > MakeEvent(
  const sc::event_base * pEvent )
{
  return boost::intrusive_ptr< const sc::event_base >( pEvent );
}

struct Initial : sc::simple_state< Initial, FifoSchedulerTest >
{
  typedef mpl::list<
    sc::custom_reaction< EvCheckCtorArgs >,
    sc::termination< EvTerminate >,
    sc::custom_reaction< EvFail >
  > reactions;

  sc::result react( const EvCheckCtorArgs & ev )
  {
    BOOST_REQUIRE( ev.expectedArgs_ == outermost_context().CtorArgs() );
    outermost_context_type & machine = outermost_context();
    machine.my_scheduler().queue_event(
      machine.my_handle(), MakeEvent( new EvTerminate() ) );
    return discard_event();
  }

  sc::result react( const EvFail & )
  {
    BOOST_FAIL( "State machine is unexpectedly still running." );
    return discard_event();
  }
};


struct UnexpectedEventCount : public std::runtime_error
{
  UnexpectedEventCount() : std::runtime_error( "" ) {}
};

void RunScheduler(
  sc::fifo_scheduler<> & scheduler, unsigned long expectedEventCount )
{
  // Workaround: For some reason MSVC has a problem with BOOST_REQUIRE here
  // (C1055: compiler limit: out of keys)
  if ( scheduler() != expectedEventCount )
  {
    throw UnexpectedEventCount();
  }
}

void Check(
  sc::fifo_scheduler<> & scheduler,
  const sc::fifo_scheduler<>::processor_handle & processor,
  int ctorArgs )
{
  // Make sure the processor has been created
  RunScheduler( scheduler, 1UL );

  scheduler.initiate_processor( processor );
  // This event triggers the queueing of another event, which itself
  // terminates the machine ...
  scheduler.queue_event(
    processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  // ... that's why 3 instead of two events must have been processed
  RunScheduler( scheduler, 3UL );

  // Since the machine has been terminated, this event will be ignored
  scheduler.queue_event( processor, MakeEvent( new EvFail() ) );
  RunScheduler( scheduler, 1UL );

  // Check that we can reinitiate the machine
  scheduler.initiate_processor( processor );
  scheduler.queue_event(
    processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  RunScheduler( scheduler, 3UL );

  // Check that we are terminated again
  scheduler.queue_event( processor, MakeEvent( new EvFail() ) );
  RunScheduler( scheduler, 1UL );

  scheduler.destroy_processor( processor );
  // The following will simply be ignored because the processor has already
  // be destroyed
  scheduler.initiate_processor( processor );
  scheduler.queue_event(
    processor, MakeEvent( new EvCheckCtorArgs( ctorArgs ) ) );
  RunScheduler( scheduler, 3UL );
}

void SetToTrue( bool & value )
{
  value = true;
}

int test_main( int, char* [] )
{
  try
  {
    sc::fifo_scheduler<> scheduler;
    const sc::fifo_scheduler<>::processor_handle processor0 =
      scheduler.create_processor< FifoSchedulerTest >();
    Check( scheduler, processor0, 0 );

    const sc::fifo_scheduler<>::processor_handle processor1 =
      scheduler.create_processor< FifoSchedulerTest >( 1 );
    Check( scheduler, processor1, 1 );

    const sc::fifo_scheduler<>::processor_handle processor2 =
      scheduler.create_processor< FifoSchedulerTest >( 1, 2 );
    Check( scheduler, processor2, 12 );

    const sc::fifo_scheduler<>::processor_handle processor3 =
      scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3 );
    Check( scheduler, processor3, 123 );

    const sc::fifo_scheduler<>::processor_handle processor4 =
      scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4 );
    Check( scheduler, processor4, 1234 );

    const sc::fifo_scheduler<>::processor_handle processor5 =
      scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4, 5 );
    Check( scheduler, processor5, 12345 );

    const sc::fifo_scheduler<>::processor_handle processor6 =
      scheduler.create_processor< FifoSchedulerTest >( 1, 2, 3, 4, 5, 6 );
    Check( scheduler, processor6, 123456 );

    RunScheduler( scheduler, 0UL );
    bool workItem1Processed = false;
    scheduler.queue_work_item(
      boost::bind( &SetToTrue, boost::ref( workItem1Processed ) ) );
    RunScheduler( scheduler, 1UL );
    BOOST_REQUIRE( workItem1Processed );

    scheduler.terminate();
    RunScheduler( scheduler, 1UL );
    BOOST_REQUIRE( scheduler.terminated() );

    RunScheduler( scheduler, 0UL );
    bool workItem2Processed = false;
    scheduler.queue_work_item(
      boost::bind( &SetToTrue, boost::ref( workItem2Processed ) ) );
    // After being terminated, a call to operator() must not process any more
    // events
    RunScheduler( scheduler, 0UL );
    BOOST_REQUIRE( !workItem2Processed );
  }
  catch ( const UnexpectedEventCount & )
  {
    BOOST_FAIL( "Unexpected event count." );
  }

  return 0;
}