summaryrefslogtreecommitdiff
path: root/ACE/tests/Proactor_Timer_Test.cpp
blob: 8fdc0af8a892be4e67b29c7e3294e6c5e314b569 (plain)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

//=============================================================================
/**
 *  @file    Proactor_Timer_Test.cpp
 *
 *    This is a simple test that illustrates the timer mechanism of
 *    the <ACE_Proactor>.  Scheduling timers, handling expired timers and
 *    cancelling scheduled timers are all exercised in this test.
 *
 *  @author Prashant Jain <pjain@cs.wustl.edu>
 *  @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
 *  @author and Miljenko Norsic <Miljenko.Norsic@etk.ericsson.se>
 */
//=============================================================================


#include "test_config.h"
#include "ace/Trace.h"

#if defined (ACE_HAS_WIN32_OVERLAPPED_IO) || defined (ACE_HAS_AIO_CALLS)
  // This only works on Win32 platforms and on Unix platforms
  // supporting POSIX aio calls.

#include "ace/OS_NS_unistd.h"
#include "ace/Proactor.h"
#include "ace/High_Res_Timer.h"
#include "ace/Asynch_IO.h"
#include "ace/Timer_Heap.h"
#include "ace/Auto_Ptr.h"

static int    done = 0;
static size_t counter = 0;
static int    odd = 0;

class Time_Handler : public ACE_Handler
{
public:
  /// Default constructor
  Time_Handler ();

  /// Handle the timeout.
  void handle_time_out (const ACE_Time_Value &tv, const void *arg) override;

  /// Return our timer id.
  long timer_id () const;

  /// Set our timer id;
  void timer_id (long);

private:
  /// Stores the id of this timer.
  long timer_id_;
};

/*
 * Need a variant of this that will track if a repeating timer is working
 * correctly. This class should be scheduled with a repeating timer that
 * repeats on a specified number of seconds. This class will let two
 * expirations happen then wait in handle_time_out() longer than the repeat
 * time to cause at least one timer expiration to be queued up while we're
 * waiting; then cancel the timer.
 */
class Repeat_Timer_Handler : public ACE_Handler
{
public:
  static const int REPEAT_INTERVAL = 2;

  // Constructor arg tells how many seconds we intend to do the repeat with.
  // The internals will use this to tell how long to wait in order to cause
  // a timer expiration to be missed and queued up.
  Repeat_Timer_Handler (const int repeat_time = REPEAT_INTERVAL)
    : repeat_secs_ (repeat_time), expirations_ (0) {};

  ~Repeat_Timer_Handler () override;

  // Handle the timeout.
  void handle_time_out (const ACE_Time_Value &tv, const void *arg) override;

private:
  int repeat_secs_;
  int expirations_;
};


Time_Handler::Time_Handler ()
  : timer_id_ (-1)
{
  // Nothing
}

void
Time_Handler::handle_time_out (const ACE_Time_Value &, const void *arg)
{
  size_t current_counter = *(reinterpret_cast<const size_t *> (arg));
  if (current_counter != counter)
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("Expected timer %d, not %d\n"),
                counter,
                current_counter));

  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("[%@] Timer id %d with counter #%d|%d expired.\n"),
              this,
              this->timer_id (),
              counter,
              current_counter));

  if (current_counter == (ACE_MAX_TIMERS - 1))
    done = 1;
  else if (counter == ACE_MAX_TIMERS - 1)
    {
      done = 1;
      return;
    }

  counter += (1 + odd);
  return;
}

long
Time_Handler::timer_id () const
{
  return this->timer_id_;
}

void
Time_Handler::timer_id (long t)
{
  this->timer_id_ = t;
}

Repeat_Timer_Handler::~Repeat_Timer_Handler ()
{
  if (this->expirations_ == 2)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Repeater expired twice; correct\n")));
  else
    ACE_ERROR ((LM_ERROR,
                ACE_TEXT ("Repeater expired %d times; should be 2\n"),
                this->expirations_));
}

void
Repeat_Timer_Handler::handle_time_out (const ACE_Time_Value &, const void *)
{
  // Let the first one go.
  if (++this->expirations_ == 1)
    return;

  if (this->expirations_ == 2)
    {
      ACE_OS::sleep (this->repeat_secs_ + 1);
      int canceled = this->proactor ()->cancel_timer (*this);
      if (canceled != 1)
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("Repeater cancel timer: %d; should be 1\n"),
                      canceled));
        }
      delete this;
    }
  else
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("Repeater expiration #%d; should get only 2\n"),
                  this->expirations_));
    }
  return;
}

static void
test_registering_all_handlers ()
{
  ACE_Trace t (ACE_TEXT ("test_registering_all_handler"),
               __LINE__,
               ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
  Time_Handler rt[ACE_MAX_TIMERS];
  long t_id[ACE_MAX_TIMERS];
  size_t which[ACE_MAX_TIMERS];
  long secs = 0;
  size_t i = 0;
  for ( ; i < ACE_MAX_TIMERS; i++, secs++)
    {
      which[i] = i;
      t_id[i] =
        ACE_Proactor::instance ()->schedule_timer
            (rt[i], &which[i], ACE_Time_Value (2 * secs + 1));
      if (t_id[i] == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
      rt[i].timer_id (t_id[i]);
    }

  while (!done)
    ACE_Proactor::instance ()->handle_events ();
}

static void
test_registering_one_handler ()
{
  ACE_Trace t (ACE_TEXT ("test_registering_one_handler"),
               __LINE__,
               ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
  Time_Handler rt[ACE_MAX_TIMERS];
  long t_id[ACE_MAX_TIMERS];
  size_t which[ACE_MAX_TIMERS];

  done = 0;
  counter = 0;
  long secs = 0;
  size_t i = 0;
  for ( ; i < ACE_MAX_TIMERS; i++, secs++)
    {
      which[i] = i;
      t_id[i] =
        ACE_Proactor::instance ()->schedule_timer
          (rt[0], &which[i], ACE_Time_Value (2 * secs + 1));
      if (t_id[i] == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
    }

  while (!done)
    ACE_Proactor::instance ()->handle_events ();
}

static void
test_canceling_odd_timers ()
{
  ACE_Trace t (ACE_TEXT ("test_canceling_odd_timers"),
               __LINE__,
               ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
  Time_Handler rt[ACE_MAX_TIMERS];
  long t_id[ACE_MAX_TIMERS];
  size_t which[ACE_MAX_TIMERS];

  done = 0;
  counter = 1;
  odd = 1;
  size_t i = 0;
  long secs = 0;
  for ( ; i < ACE_MAX_TIMERS; i++, secs++)
    {
      which[i] = i;
      t_id[i] = ACE_Proactor::instance ()->schedule_timer
        (rt[i], &which[i], ACE_Time_Value (2 * secs + 1));
      if (t_id[i] == -1)
        ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("schedule_timer")));
      rt[i].timer_id (t_id[i]);
    }

  for (i = 0; i < ACE_MAX_TIMERS; i++)
    // Cancel handlers with odd numbered timer ids.
    if (ACE_ODD (rt[i].timer_id ()))
      {
        if (ACE_Proactor::instance ()->cancel_timer (rt[i].timer_id ()) == -1)
          ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("cancel_timer")));
      }

  while (!done)
    ACE_Proactor::instance ()->handle_events ();
}

static void
test_cancel_repeat_timer ()
{
  Repeat_Timer_Handler *handler = new Repeat_Timer_Handler;
  ACE_Time_Value timeout (Repeat_Timer_Handler::REPEAT_INTERVAL);
  long t_id = ACE_Proactor::instance ()->schedule_repeating_timer
    (*handler, 0, timeout);
  if (t_id == -1)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT ("%p\n"),
                  ACE_TEXT ("schedule_repeating_timer")));
      delete handler;
      return;
    }

  ACE_Time_Value test_timer (4 * Repeat_Timer_Handler::REPEAT_INTERVAL);
  if (-1 == ACE_Proactor::instance ()->proactor_run_event_loop (test_timer))
    ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("proactor loop fail")));

  // handler should be deleted by its own handle_time_out().
  return;
}


// If any command line arg is given, run the test with high res timer
// queue. Else run it normally.
int
run_main (int argc, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Proactor_Timer_Test"));

  if (argc > 1)
    {
      ACE_DEBUG ((LM_DEBUG,
                  ACE_TEXT ("Running with high-res timer queue\n")));
      ACE_Proactor *r = ACE_Proactor::instance ();

      (void) ACE_High_Res_Timer::global_scale_factor ();

      // Change the source of time in the Proactor to the
      // high-resolution  timer.  Why does this test require such
      // precision for a 1 second timer is beyond me ...  I think it
      // is a cut&paste error.
      //
      // The use of auto_ptr<> is optional, ACE uses dangerous memory
      // management idioms everywhere, I thought I could demonstrate how
      // to do it right in at least one test.  Notice the lack of
      // ACE_NEW_RETURN, that monstrosity has no business in proper C++
      // code ...
      using Timer_Queue = ACE_Timer_Heap_T<ACE_Handler *, ACE_Proactor_Handle_Timeout_Upcall, ACE_MT_SYNCH::RECURSIVE_MUTEX, ACE_FPointer_Time_Policy>;

      std::unique_ptr<Timer_Queue> tq(new Timer_Queue);
      // ... notice how the policy is in the derived timer queue type.
      // The abstract timer queue does not have a time policy ...
      tq->set_time_policy(&ACE_High_Res_Timer::gettimeofday_hr);
      // ... and then the timer queue is replaced.  Strangely, the
      // Proactor does *not* copy the timers, it just deletes the
      // existing timer queue ....
      r->timer_queue(tq.get());
      // ... the Proactor has assumed ownership, release the
      // auto_ptr<> ...
      tq.release();
    }

  // Register all different handlers, i.e., one per timer.
  test_registering_all_handlers ();

  // Now try multiple timers for ONE event handler (should produce the
  // same result).
  test_registering_one_handler ();

  // Try canceling handlers with odd numbered timer ids.
  test_canceling_odd_timers ();

  test_cancel_repeat_timer ();

  ACE_END_TEST;
  return 0;
}

#else

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Proactor_Timer_Test"));

  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("Asynchronous IO is unsupported.\n")
              ACE_TEXT ("Proactor_Timer_Test will not be run.\n")));

  ACE_END_TEST;

  return 0;
}

#endif  /* ACE_HAS_WIN32_OVERLAPPED_IO || ACE_HAS_AIO_CALLS */