summaryrefslogtreecommitdiff
path: root/examples/Reactor/Misc/test_timer_queue.cpp
blob: 37bc7636a640852959e3ada4efd05482f1840525 (plain)
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
// $Id$

#include "ace/Timer_Heap.h"
#include "ace/Timer_List.h"
#include "ace/Timer_Queue.h"
#include "ace/Log_Msg.h"

ACE_RCSID(Misc, test_timer_queue, "$Id$")

class Example_Handler : public ACE_Event_Handler
{
public:
  Example_Handler (void)
    : count_ (1)
  {}

  virtual int handle_timeout (const ACE_Time_Value &, const void *arg)
  {
    int *times = (int *) arg;

    ACE_DEBUG ((LM_DEBUG,
                "yow, the time has come and gone %d times %d, Horatio!\n",
                this->count_++,
                *times));
    delete times;
    return 0;
  }

private:
  int count_;
};

static void
test_functionality (ACE_Timer_Queue *tq)
{
  Example_Handler eh;

  ACE_ASSERT (tq->is_empty ());
  ACE_ASSERT (ACE_Time_Value::zero == ACE_Time_Value (0));
  const void *timer_act = 0;

  ACE_NEW (timer_act, int (1));
  int timer_id1 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());

  // Use timer_id outside of an assert, so that we don't get compile
  // warnings with ACE_NDEBUG about it being unused.
  if (timer_id1 == -1)
    ACE_ERROR ((LM_ERROR, "%p\n", "schedule () failed"));
  ACE_ASSERT (timer_id1 != -1);

  ACE_NEW (timer_act, int (42));
  long result = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
  ACE_ASSERT (result != -1);
  ACE_NEW (timer_act, int (42));
  result = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
  ACE_ASSERT (result != -1);  

  result = tq->cancel (timer_id1, &timer_act);
  ACE_ASSERT (result == 1);  
  delete (int *) timer_act;
  result = tq->is_empty ();
  ACE_ASSERT (!result);

  result = tq->expire ();
  ACE_ASSERT (result == 2);

  ACE_NEW (timer_act, int (4));
  timer_id1 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
  ACE_ASSERT (timer_id1 != -1);
  ACE_NEW (timer_act, int (5));
  int timer_id2 = tq->schedule (&eh, timer_act, ACE_OS::gettimeofday ());
  ACE_ASSERT (timer_id2 != -1);

  result = tq->cancel (timer_id1, &timer_act);
  ACE_ASSERT (result == 1);
  delete (int *) timer_act;
  result = tq->cancel (timer_id2, &timer_act);
  ACE_ASSERT (result == 1);
  delete (int *) timer_act;
  result = tq->is_empty ();
  ACE_ASSERT (result == 1);
  result = tq->expire ();
  ACE_ASSERT (result == 0);
}

struct Timer_Queues
{
  ACE_Timer_Queue *queue_;
  // Pointer to the subclass of <ACE_Timer_Queue> that we're testing.

  const char *name_;
  // Name of the Queue that we're testing.
};

static Timer_Queues timer_queues[] =
{
  { new ACE_Timer_List, "ACE_Timer_List" },
  { new ACE_Timer_Heap, "ACE_Timer_Heap" },
  { 0, 0 },
};

int
main (int, char *[])
{
  for (int i = 0; timer_queues[i].name_ != 0; i++)
    {
      test_functionality (timer_queues[i].queue_);
      delete timer_queues[i].queue_;
    }

  return 0;
}