Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Auto_Event_Test.cpp
blobdd960c75843559cdaf09817f07b903880256bac5
2 //=============================================================================
3 /**
4 * @file Auto_Event_Test.cpp
6 * This test verifies the functionality of the <ACE_Auto_Event>
7 * implementation.
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
39 // state is 0).
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.
55 static int
56 test_timeout ()
58 int status = 0;
60 // milliseconds...
61 long msecs_expected;
62 long msecs_waited;
63 long msecs_diff;
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)
76 if (errno != ETIME)
78 ACE_ERROR ((LM_ERROR,
79 ACE_TEXT ("%p\n"),
80 ACE_TEXT ("test_timeout should be ETIME but is")));
81 status = -1;
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)
92 ACE_ERROR ((LM_ERROR,
93 ACE_TEXT ("Timed wait fails length test\n")));
94 ACE_ERROR ((LM_ERROR,
95 ACE_TEXT ("Expected %d ms, actual %d ms; %d allowed\n"),
96 (int)msecs_expected,
97 (int)msecs_waited,
98 (int)ACE_ALLOWED_SLACK));
99 status = -1;
102 ++wait_secs;
103 return status;
106 // Explain usage and exit.
107 static void
108 print_usage_and_die ()
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("usage: %n [-w n_workers] [-n iteration_count]\n")));
112 ACE_OS::exit (1);
115 static void
116 parse_args (int argc, ACE_TCHAR *argv[])
118 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("w:n:"));
120 int c;
122 while ((c = get_opt ()) != -1)
123 switch (c)
125 case 'w':
126 n_workers = ACE_OS::atoi (get_opt.opt_arg ());
127 break;
128 case 'n':
129 n_iterations = ACE_OS::atoi (get_opt.opt_arg ());
130 break;
131 default:
132 print_usage_and_die ();
133 break;
137 // Worker tries to acquire the semaphore, hold it for a while, and
138 // then releases it.
140 static void *
141 worker (void *)
143 for (int iterations = 1;
144 iterations <= n_iterations;
145 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,
159 ACE_TEXT ("%p\n"),
160 ACE_TEXT ("Worker should be ETIME but is")));
162 else
163 ++timeouts;
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"),
175 (int)diff_msec,
176 (int)ACE_ALLOWED_SLACK));
177 test_result = 1;
179 // Hold the lock for a while.
180 ACE_OS::sleep (ACE_Time_Value (0,
181 (ACE_OS::rand () % 1000) * 1000));
182 evt.signal ();
185 ACE_Thread::yield ();
188 return 0;
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));
203 //Test timed waits.
204 for (size_t i = 0; i < test_timeout_count; i++)
205 if (test_timeout () != 0)
206 test_result = 1;
208 if (ACE_Thread_Manager::instance ()->spawn_n
209 (static_cast<size_t> (n_workers),
210 ACE_THR_FUNC (worker),
212 THR_NEW_LWP) == -1)
213 ACE_ERROR_RETURN ((LM_ERROR,
214 ACE_TEXT ("%p\n"),
215 ACE_TEXT ("spawn_n")),
218 // Release the first worker.
219 evt.signal ();
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"),
227 percent));
229 if (test_result == 0)
230 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Auto_Event Test successful\n")));
231 #else
232 ACE_UNUSED_ARG (argc);
233 ACE_UNUSED_ARG (argv);
234 ACE_ERROR ((LM_INFO,
235 ACE_TEXT ("Threads not supported on this platform\n")));
236 #endif /* ACE_HAS_THREADS */
237 ACE_END_TEST;
238 return test_result;