Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / Reactor / Misc / test_timer_queue.cpp
blob098128e9172930882676af1d9f2dcb440180a98c
1 #include "ace/OS_NS_sys_time.h"
2 #include "ace/Timer_Heap.h"
3 #include "ace/Timer_List.h"
4 #include "ace/Timer_Queue.h"
5 #include "ace/Log_Msg.h"
6 #include "ace/Recursive_Thread_Mutex.h"
7 #include "ace/Null_Mutex.h"
8 #include "ace/Event_Handler.h"
10 class Example_Handler : public ACE_Event_Handler
12 public:
13 Example_Handler ()
14 : count_ (1)
17 virtual int handle_timeout (const ACE_Time_Value &, const void *arg)
19 int *times = (int *) arg;
21 ACE_DEBUG ((LM_DEBUG,
22 "yow, the time has come and gone %d times %d, Horatio!\n",
23 this->count_++,
24 *times));
25 delete times;
26 return 0;
29 private:
30 int count_;
33 static void
34 test_functionality (ACE_Timer_Queue *tq)
36 Example_Handler eh;
38 ACE_TEST_ASSERT (tq->is_empty ());
39 ACE_TEST_ASSERT (ACE_Time_Value::zero == ACE_Time_Value (0));
40 const void *timer_act = 0;
42 ACE_NEW (timer_act, int (1));
43 long timer_id1 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
45 // Use timer_id outside of an assert, so that we don't get compile
46 // warnings with ACE_NDEBUG about it being unused.
47 if (timer_id1 == -1)
48 ACE_ERROR ((LM_ERROR, "%p\n", "schedule () failed"));
49 ACE_TEST_ASSERT (timer_id1 != -1);
51 ACE_NEW (timer_act, int (42));
52 long result = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
53 ACE_TEST_ASSERT (result != -1);
54 ACE_NEW (timer_act, int (42));
55 result = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
56 ACE_TEST_ASSERT (result != -1);
58 result = tq->cancel (timer_id1, &timer_act);
59 ACE_TEST_ASSERT (result == 1);
60 delete (int *) timer_act;
61 result = tq->is_empty ();
62 ACE_TEST_ASSERT (!result);
64 result = tq->expire ();
65 ACE_TEST_ASSERT (result == 2);
67 ACE_NEW (timer_act, int (4));
68 timer_id1 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
69 ACE_TEST_ASSERT (timer_id1 != -1);
70 ACE_NEW (timer_act, int (5));
71 long timer_id2 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
72 ACE_TEST_ASSERT (timer_id2 != -1);
74 result = tq->cancel (timer_id1, &timer_act);
75 ACE_TEST_ASSERT (result == 1);
76 delete (int *) timer_act;
77 result = tq->cancel (timer_id2, &timer_act);
78 ACE_TEST_ASSERT (result == 1);
79 delete (int *) timer_act;
80 result = tq->is_empty ();
81 ACE_TEST_ASSERT (result == 1);
82 result = tq->expire ();
83 ACE_TEST_ASSERT (result == 0);
86 struct Timer_Queues
88 ACE_Timer_Queue *queue_;
89 // Pointer to the subclass of <ACE_Timer_Queue> that we're testing.
91 const char *name_;
92 // Name of the Queue that we're testing.
95 static Timer_Queues timer_queues[] =
97 { new ACE_Timer_List, "ACE_Timer_List" },
98 { new ACE_Timer_Heap, "ACE_Timer_Heap" },
99 { 0, 0 },
103 ACE_TMAIN (int, ACE_TCHAR *[])
105 for (int i = 0; timer_queues[i].name_ != 0; i++)
107 test_functionality (timer_queues[i].queue_);
108 delete timer_queues[i].queue_;
111 return 0;