2 //=============================================================================
4 * @file Auto_Event_Test.cpp
6 * This test verifies the functionality of the <ACE_Auto_Event>
9 * @author Martin Corino <mcorino@remedy.nl>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Auto_Event.h"
16 #include "ace/Thread.h"
17 #include "ace/Thread_Manager.h"
18 #include "ace/Get_Opt.h"
19 #include "ace/OS_NS_sys_time.h"
20 #include "ace/OS_NS_time.h"
21 #include "ace/OS_NS_unistd.h"
22 #include "ace/Atomic_Op.h"
24 // msec that times are allowed to differ before test fails.
25 #if defined (ACE_HAS_HI_RES_TIMER) || \
26 defined (ACE_HAS_PENTIUM) || \
27 defined (ACE_HAS_POWERPC_TIMER)
28 # define ACE_ALLOWED_SLACK 100
29 #else /* don't have a high-res timer */
30 # define ACE_ALLOWED_SLACK 1100
31 #endif /* don't have a high-res timer */
33 // Test results, 'success' is 0
34 static int test_result
= 0;
36 #if defined (ACE_HAS_THREADS)
38 // Event used in the tests. Start it "unsignalled" (i.e., its initial
40 static ACE_Auto_Event
evt ((unsigned int) 0);
42 // Default number of iterations.
43 static int n_iterations
= 10;
45 // Number of worker threads.
46 static size_t n_workers
= 10;
48 // Number of timeouts.
49 static ACE_Atomic_Op
<ACE_SYNCH_MUTEX
, size_t> timeouts
= 0;
51 // Number of times to call test_timeout ().
52 static size_t test_timeout_count
= 3;
54 // Tests the amount of time spent in a timed wait.
65 // Wait a little longer each time
66 static long wait_secs
= 3;
68 ACE_Time_Value wait
= ACE_OS::gettimeofday ();
70 ACE_Time_Value begin
= wait
;
72 wait
.sec (wait
.sec () + wait_secs
);
74 if (evt
.wait (&wait
) == -1)
80 ACE_TEXT ("test_timeout should be ETIME but is")));
84 ACE_Time_Value wait_diff
= ACE_OS::gettimeofday () - begin
;
86 msecs_waited
= wait_diff
.msec ();
87 msecs_expected
= wait_secs
* 1000;
88 msecs_diff
= labs (msecs_expected
- msecs_waited
);
90 if (msecs_diff
> ACE_ALLOWED_SLACK
)
93 ACE_TEXT ("Timed wait fails length test\n")));
95 ACE_TEXT ("Expected %d ms, actual %d ms; %d allowed\n"),
98 (int)ACE_ALLOWED_SLACK
));
106 // Explain usage and exit.
108 print_usage_and_die ()
110 ACE_DEBUG ((LM_DEBUG
,
111 ACE_TEXT ("usage: %n [-w n_workers] [-n iteration_count]\n")));
116 parse_args (int argc
, ACE_TCHAR
*argv
[])
118 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT ("w:n:"));
122 while ((c
= get_opt ()) != -1)
126 n_workers
= ACE_OS::atoi (get_opt
.opt_arg ());
129 n_iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
132 print_usage_and_die ();
137 // Worker tries to acquire the semaphore, hold it for a while, and
143 for (int iterations
= 1;
144 iterations
<= n_iterations
;
147 //FUZZ: disable check_for_lack_ACE_OS
148 ACE_Time_Value
wait (0,
149 iterations
* 1000 * 100); // Wait 'iter' msec
150 //FUZZ: enable check_for_lack_ACE_OS
152 ACE_Time_Value tv
= ACE_OS::gettimeofday () + wait
;
153 if (evt
.wait (&tv
) == -1)
155 // verify that we have ETIME
156 if (ACE_OS::last_error() != ETIME
)
158 ACE_ERROR ((LM_ERROR
,
160 ACE_TEXT ("Worker should be ETIME but is")));
164 ACE_Time_Value diff
= ACE_OS::gettimeofday ();
165 diff
= diff
- tv
; // tv should have been reset to time acquired
166 long diff_msec
= diff
.msec ();
168 if (diff_msec
> ACE_ALLOWED_SLACK
)
170 ACE_ERROR ((LM_ERROR
,
171 ACE_TEXT ("Acquire fails time reset test\n")));
172 ACE_ERROR ((LM_ERROR
,
173 ACE_TEXT ("Diff btw now and returned time: %d ms; ")
174 ACE_TEXT ("%d allowed\n"),
176 (int)ACE_ALLOWED_SLACK
));
179 // Hold the lock for a while.
180 ACE_OS::sleep (ACE_Time_Value (0,
181 (ACE_OS::rand () % 1000) * 1000));
185 ACE_Thread::yield ();
191 #endif /* ACE_HAS_THREADS */
193 // Test event functionality.
195 int run_main (int argc
, ACE_TCHAR
*argv
[])
197 ACE_START_TEST (ACE_TEXT ("Auto_Event_Test"));
199 #if defined (ACE_HAS_THREADS)
200 parse_args (argc
, argv
);
201 ACE_OS::srand ((u_int
) ACE_OS::time (0L));
204 for (size_t i
= 0; i
< test_timeout_count
; i
++)
205 if (test_timeout () != 0)
208 if (ACE_Thread_Manager::instance ()->spawn_n
209 (static_cast<size_t> (n_workers
),
210 ACE_THR_FUNC (worker
),
213 ACE_ERROR_RETURN ((LM_ERROR
,
215 ACE_TEXT ("spawn_n")),
218 // Release the first worker.
221 ACE_Thread_Manager::instance ()->wait ();
223 size_t percent
= (timeouts
.value () * 100) / (n_workers
* n_iterations
);
225 ACE_DEBUG ((LM_DEBUG
,
226 ACE_TEXT ("Worker threads timed out %B percent of the time\n"),
229 if (test_result
== 0)
230 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Auto_Event Test successful\n")));
232 ACE_UNUSED_ARG (argc
);
233 ACE_UNUSED_ARG (argv
);
235 ACE_TEXT ("Threads not supported on this platform\n")));
236 #endif /* ACE_HAS_THREADS */