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
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Reactor_Timer_Test.cpp
//
// = DESCRIPTION
// This is a simple test that illustrates the timer mechanism of
// the reactor. Scheduling timers, resetting timer intervals,
// handling expired timers and cancelling scheduled timers are
// all exercised in this test.
//
// = AUTHOR
// Prashant Jain <pjain@cs.wustl.edu> and Douglas C. Schmidt
// <schmidt@cs.wustl.edu>
//
// ============================================================================
#include "test_config.h"
#include "ace/Timer_Queue.h"
#include "ace/Reactor.h"
#include "ace/High_Res_Timer.h"
#include "ace/Trace.h"
#include "ace/Recursive_Thread_Mutex.h"
ACE_RCSID(tests, Reactor_Timer_Test, "$Id$")
static int done = 0;
static int count = 0;
static int odd = 0;
class Time_Handler : public ACE_Event_Handler
{
public:
Time_Handler ();
// Default constructor
virtual int handle_timeout (const ACE_Time_Value &tv,
const void *arg);
// Handle the timeout.
virtual int handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask);
// Called when <Time_Handler> is removed.
long timer_id (void) const;
// Return our timer id.
void timer_id (long);
// Set our timer id;
private:
long timer_id_;
// Stores the id of this timer.
};
Time_Handler::Time_Handler ()
: timer_id_ (-1)
{
// Nothing
}
int
Time_Handler::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%x] handle = %d, close_mask = %d, timer id = %d\n"),
this,
handle,
close_mask,
this->timer_id ()));
return 0;
}
int
Time_Handler::handle_timeout (const ACE_Time_Value &tv,
const void *arg)
{
long current_count = ACE_static_cast (long,
ACE_reinterpret_cast (size_t, arg));
if (current_count >= 0)
ACE_ASSERT (current_count == count);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("[%x] Timer id %d with count #%d|%d timed out at %d!\n"),
this,
this->timer_id (),
count,
current_count,
tv.sec ()));
if (current_count == long (ACE_MAX_TIMERS - 1))
done = 1;
else if (count == ACE_MAX_TIMERS - 1)
{
done = 1;
return -1;
}
else if (current_count == -1)
{
int result = ACE_Reactor::instance ()->reset_timer_interval (this->timer_id (),
ACE_Time_Value (count + 1));
ACE_ASSERT (result != -1);
}
count += (1 + odd);
return 0;
}
long
Time_Handler::timer_id (void) const
{
return this->timer_id_;
}
void
Time_Handler::timer_id (long t)
{
this->timer_id_ = t;
}
static void
test_registering_all_handlers (void)
{
ACE_Trace t (ACE_TEXT ("test_registering_all_handler"),
__LINE__,
ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
Time_Handler rt[ACE_MAX_TIMERS];
int t_id[ACE_MAX_TIMERS];
for (size_t i = 0; i < ACE_MAX_TIMERS; i++)
{
t_id[i] =
ACE_Reactor::instance ()->schedule_timer (&rt[i],
(const void *) i,
ACE_Time_Value (2 * i + 1));
ACE_ASSERT (t_id[i] != -1);
rt[i].timer_id (t_id[i]);
}
while (!done)
ACE_Reactor::instance ()->handle_events ();
}
static void
test_registering_one_handler (void)
{
ACE_Trace t (ACE_TEXT ("test_registering_one_handler"),
__LINE__,
ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
Time_Handler rt[ACE_MAX_TIMERS];
int t_id[ACE_MAX_TIMERS];
done = 0;
count = 0;
for (size_t i = 0; i < ACE_MAX_TIMERS; i++)
{
t_id[i] =
ACE_Reactor::instance ()->schedule_timer (&rt[0],
(const void *) i,
ACE_Time_Value (2 * i + 1));
ACE_ASSERT (t_id[i] != -1);
}
while (!done)
ACE_Reactor::instance ()->handle_events ();
}
static void
test_canceling_odd_timers (void)
{
ACE_Trace t (ACE_TEXT ("test_canceling_odd_timers"),
__LINE__,
ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
Time_Handler rt[ACE_MAX_TIMERS];
int t_id[ACE_MAX_TIMERS];
done = 0;
count = 1;
odd = 1;
for (size_t i = 0; i < ACE_MAX_TIMERS; i++)
{
t_id[i] = ACE_Reactor::instance ()->schedule_timer (&rt[i],
(const void *) i,
ACE_Time_Value (2 * i + 1));
ACE_ASSERT (t_id[i] != -1);
rt[i].timer_id (t_id[i]);
}
for (size_t j = 0; (u_long) j < ACE_MAX_TIMERS; j++)
// Cancel handlers with odd numbered timer ids.
if (ACE_ODD (rt[j].timer_id ()))
{
int result =
ACE_Reactor::instance ()->cancel_timer (rt[j].timer_id ());
ACE_ASSERT (result != -1);
}
while (!done)
ACE_Reactor::instance ()->handle_events ();
}
static void
test_resetting_timer_intervals (void)
{
ACE_Trace t (ACE_TEXT ("test_resetting_timer_intervals"),
__LINE__,
ACE_TEXT_CHAR_TO_TCHAR (__FILE__));
Time_Handler rt;
int t_id;
done = 0;
count = 0;
odd = 0;
t_id =
ACE_Reactor::instance ()->schedule_timer
(&rt,
(const void *) -1,
ACE_Time_Value (1),
// Start off by making this an interval timer.
ACE_Time_Value (1));
ACE_ASSERT (t_id != -1);
rt.timer_id (t_id);
while (!done)
ACE_Reactor::instance ()->handle_events ();
}
// 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 ("Reactor_Timer_Test"));
if (argc > 1)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Running with high-res timer queue\n")));
ACE_Reactor *r = ACE_Reactor::instance ();
(void) ACE_High_Res_Timer::global_scale_factor ();
r->timer_queue ()->gettimeofday (&ACE_High_Res_Timer::gettimeofday_hr);
}
// 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 ();
// Make sure <reset_timer_inverval> works.
test_resetting_timer_intervals ();
ACE_END_TEST;
return 0;
}
|