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
|
// $Id: Thread_Timer_Queue_Adapter_Test.cpp 92900 2010-12-17 14:45:11Z mcorino $
/**
* @file Thread_Timer_Queue_Adapter_Test.cpp
*
* @author Alon Diamant <diamant.alon@gmail.com>
*
* This test verifies the functionality of the ACE_Timer_Queue_Thread_Adapter.
* It also shows the usage of custom event handlers.
*
*/
//=============================================================================
#include "ace/Timer_Wheel.h"
#include "ace/Timer_Queue_Adapters.h"
#include "test_config.h"
#if defined (ACE_HAS_THREADS)
/// ICustomEventHandler
///
/// Used to demonstrate the usage of custom event handlers. This is called by the
/// timer queue thread adapter.
class ICustomEventHandler
{
public:
/// Default constructor.
///
/// @return
ICustomEventHandler()
{
}
/// Default destructor.
///
/// @return
virtual ~ICustomEventHandler()
{
}
/// Main functor method.
///
/// @return int
/// @param p_vParameter
virtual int operator() (void* p_vParameter) = 0;
};
/// CCustomEventHandlerUpcall
///
/// Implements the Upcall interface used by the ACE_Timer_Queue, specifically for the
/// ICustomEventHandler interface.
class CCustomEventHandlerUpcall
{
public:
typedef ACE_Timer_Queue_T<ICustomEventHandler*,
CCustomEventHandlerUpcall,
ACE_Null_Mutex> TTimerQueue;
/// Default constructor
CCustomEventHandlerUpcall()
{
}
/// Destructor.
~CCustomEventHandlerUpcall()
{
}
/// This method is called when the timer expires.
int timeout (TTimerQueue&,
ICustomEventHandler* p_Handler,
const void* p_vParameter,
int /*recurring_timer*/,
const ACE_Time_Value& /*cur_time*/)
{
ACE_TRACE(ACE_TEXT ("timeout"));
return (*p_Handler)(const_cast<void*> (p_vParameter));
}
/// This method is called when a timer is registered.
int registration(TTimerQueue&, ICustomEventHandler*, const void*) { return 0; }
/// This method is called before the timer expires.
int preinvoke(TTimerQueue&, ICustomEventHandler*, const void*,
int, const ACE_Time_Value&, const void*&) { return 0; }
/// This method is called after the timer expires.
int postinvoke(TTimerQueue&, ICustomEventHandler*, const void*,
int, const ACE_Time_Value&, const void*) { return 0; }
/// This method is called when a handler is canceled
int cancel_type(TTimerQueue&, ICustomEventHandler*, int, int&) { return 0; }
/// This method is called when a timer is canceled
int cancel_timer(TTimerQueue&, ICustomEventHandler* p_Handler, int, int)
{
ACE_TRACE(ACE_TEXT ("cancel_timer"));
delete p_Handler;
return 0;
}
/// This method is called when the timer queue is destroyed and
/// the timer is still contained in it
int deletion(TTimerQueue&, ICustomEventHandler* p_Handler, const void*)
{
ACE_TRACE(ACE_TEXT ("deletion"));
delete p_Handler;
return 0;
}
};
/// ICustomEventHandler
///
/// Used to demonstrate the usage of custom event handlers. This is called by the
/// timer queue thread adapter.
class CTestEventHandler : public ICustomEventHandler
{
public:
/// Default constructor.
///
/// @return
CTestEventHandler(long* p_iCallCount)
: m_p_iCallCount(p_iCallCount)
{
ACE_DEBUG((LM_DEBUG, ACE_TEXT("%I(%t) Initializing test event handler.\n")));
}
/// Default destructor.
///
/// @return
virtual ~CTestEventHandler()
{
ACE_DEBUG((LM_DEBUG, ACE_TEXT("%I(%t) Destroying test event handler.\n")));
}
/// Main functor method.
///
/// @return int
/// @param p_vParameter
virtual int operator() (void* p_vParameter)
{
long iParameter = (long) p_vParameter;
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Incrementing test event handler call count by %d.\n"),
iParameter));
m_Mutex.acquire();
*m_p_iCallCount += iParameter;
m_Mutex.release();
// Success
return 0;
}
private:
long* m_p_iCallCount;
ACE_Thread_Mutex m_Mutex;
};
// Used for the actual timer queue thread adapter
typedef ACE_Timer_Wheel_T <ICustomEventHandler*,
CCustomEventHandlerUpcall,
ACE_Null_Mutex> TTimerWheel;
typedef ACE_Timer_Wheel_Iterator_T <ICustomEventHandler*,
CCustomEventHandlerUpcall,
ACE_Null_Mutex> TTimerWheelIterator;
typedef ACE_Thread_Timer_Queue_Adapter<TTimerWheel,
ICustomEventHandler*> TTimerWheelThreadAdapter;
#endif /* ACE_HAS_THREADS */
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Thread_Timer_Queue_Adapter_Test"));
#if defined (ACE_HAS_THREADS)
// Start the thread adapter
TTimerWheelThreadAdapter TimerWheelThreadAdapter;
TimerWheelThreadAdapter.activate();
// Single timer
{
// Create a test event handler
long iCallCount = 0;
CTestEventHandler* p_TestEventHandler = 0;
ACE_NEW_RETURN(p_TestEventHandler, CTestEventHandler(&iCallCount), -1);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Scheduling timer...\n")));
TimerWheelThreadAdapter.schedule(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(1, 0));
ACE_OS::sleep(ACE_Time_Value(1, 100 * 1000));
ACE_TEST_ASSERT(iCallCount == 1);
delete p_TestEventHandler;
ACE_DEBUG((LM_DEBUG, ACE_TEXT("%I(%t) Success in Single timer test.\n")));
}
// Single timer with cancellation
{
// Create a test event handler
long iCallCount = 0;
CTestEventHandler* p_TestEventHandler = 0;
ACE_NEW_RETURN(p_TestEventHandler, CTestEventHandler(&iCallCount), -1);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Scheduling timer...\n")));
long lTimerHandle =
TimerWheelThreadAdapter.schedule(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(1, 0));
// Cancel the repeating timer
TimerWheelThreadAdapter.cancel(lTimerHandle);
ACE_OS::sleep(ACE_Time_Value(1, 100 * 1000));
ACE_TEST_ASSERT(iCallCount == 0);
// Test event handler was deleted by the timer.
ACE_DEBUG((LM_DEBUG, ACE_TEXT("%I(%t) Success in Single timer with cancellation test.\n")));
}
// Repeating timer with cancellation
{
// Create a test event handler
long iCallCount = 0;
CTestEventHandler* p_TestEventHandler = 0;
ACE_NEW_RETURN(p_TestEventHandler, CTestEventHandler(&iCallCount), -1);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Scheduling timer...\n")));
long lTimerHandle =
TimerWheelThreadAdapter.schedule
(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(1, 0),
ACE_Time_Value(1, 0));
ACE_OS::sleep(ACE_Time_Value(3, 500 * 1000));
ACE_TEST_ASSERT(iCallCount == 3);
// Cancel the repeating timer
TimerWheelThreadAdapter.cancel(lTimerHandle);
ACE_TEST_ASSERT(iCallCount == 3);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Success in Repeating timer with cancellation test.\n")));
}
// Multiple timers
{
// Create a test event handler
long iCallCount = 0;
CTestEventHandler* p_TestEventHandler = 0;
ACE_NEW_RETURN(p_TestEventHandler, CTestEventHandler(&iCallCount), -1);
ACE_DEBUG((LM_DEBUG,
ACE_TEXT("%I(%t) Scheduling timer...\n")));
TimerWheelThreadAdapter.schedule
(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(1, 0));
TimerWheelThreadAdapter.schedule
(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(1, 0));
TimerWheelThreadAdapter.schedule
(p_TestEventHandler,
(void*) 1,
ACE_OS::gettimeofday() + ACE_Time_Value(2, 0));
ACE_OS::sleep(ACE_Time_Value(3, 0));
ACE_TEST_ASSERT(iCallCount == 3);
delete p_TestEventHandler;
ACE_DEBUG((LM_DEBUG, ACE_TEXT("%I(%t) Success in Multiple timers test.\n")));
}
#else
ACE_ERROR ((LM_INFO,
ACE_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
ACE_END_TEST;
return 0;
}
|