Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Timer_Cancellation_Test.cpp
blobb48f2a8849ce10ee78de1eaffc96c96cccff8390
1 //=============================================================================
2 /**
3 * @file Timer_Cancellation_Test.cpp
5 * A test to ensure the timer cancellation works correctly.
7 * @author Irfan Pyarali <irfan@oomworks.com>
8 */
9 //=============================================================================
11 #include "test_config.h"
12 #include "ace/OS_NS_unistd.h"
13 #include "ace/Reactor.h"
14 #include "ace/TP_Reactor.h"
15 #include "ace/Task.h"
17 #if defined (ACE_HAS_THREADS)
19 class Deadlock : public ACE_Task_Base
21 public:
23 int svc (void);
25 int handle_timeout (const ACE_Time_Value &current_time,
26 const void *act);
29 int
30 Deadlock::svc (void)
32 ACE_DEBUG ((LM_DEBUG,
33 ACE_TEXT ("Deadlock starts accessing Reactor and Timer Queue....\n")));
35 this->reactor ()->schedule_timer (this,
37 ACE_Time_Value (1));
39 ACE_DEBUG ((LM_DEBUG,
40 ACE_TEXT ("Deadlock completes accessing Reactor and Timer Queue....\n")));
42 return 0;
45 int
46 Deadlock::handle_timeout (const ACE_Time_Value &,
47 const void *)
49 ACE_DEBUG ((LM_DEBUG,
50 ACE_TEXT ("Deadlock timeout\n")));
52 return 0;
55 class Event_Handler : public ACE_Event_Handler
57 public:
59 Event_Handler (Deadlock &deadlock);
61 int handle_timeout (const ACE_Time_Value &current_time,
62 const void *act);
64 int handle_close (ACE_HANDLE handle,
65 ACE_Reactor_Mask close_mask);
67 Deadlock &deadlock_;
70 Event_Handler::Event_Handler (Deadlock &deadlock)
71 : deadlock_ (deadlock)
75 int
76 Event_Handler::handle_timeout (const ACE_Time_Value &,
77 const void *)
79 ACE_DEBUG ((LM_DEBUG,
80 ACE_TEXT ("Event_Handler timeout\n")));
82 return -1;
85 int
86 Event_Handler::handle_close (ACE_HANDLE,
87 ACE_Reactor_Mask)
89 ACE_DEBUG ((LM_DEBUG,
90 ACE_TEXT ("Event_Handler closed\n")));
92 // Activate Deadlock.
93 this->deadlock_.activate ();
95 // Give Deadlock a chance to deadlock... ;-)
96 ACE_OS::sleep (1);
98 ACE_DEBUG ((LM_DEBUG,
99 ACE_TEXT ("Event Handler starts accessing Reactor....\n")));
101 // This is a superfluous call to the Reactor to acquire its lock.
102 this->reactor ()->max_notify_iterations ();
104 ACE_DEBUG ((LM_DEBUG,
105 ACE_TEXT ("Event Handler completes accessing Reactor....\n")));
107 return 0;
111 run_main (int, ACE_TCHAR *[])
113 ACE_START_TEST (ACE_TEXT ("Timer_Cancellation_Test"));
115 ACE_Reactor reactor (new ACE_TP_Reactor, true);
117 Deadlock deadlock;
118 deadlock.reactor (&reactor);
120 Event_Handler handler (deadlock);
122 // Scheduler a timer to kick things off.
123 reactor.schedule_timer (&handler,
125 ACE_Time_Value (1));
127 // Run the event loop for a while.
128 ACE_Time_Value timeout (4);
129 reactor.run_reactor_event_loop (timeout);
131 ACE_END_TEST;
133 return 0;
136 #else /* ACE_HAS_THREADS */
139 run_main (int, ACE_TCHAR *[])
141 ACE_START_TEST (ACE_TEXT ("Timer_Cancellation_Test"));
143 ACE_ERROR ((LM_INFO,
144 ACE_TEXT ("threads not supported on this platform\n")));
146 ACE_END_TEST;
148 return 0;
151 #endif /* ACE_HAS_THREADS */