summaryrefslogtreecommitdiff
path: root/ACE/tests/WFMO_Reactor_Test.cpp
blob: ee85fa70acb7affb20a944230032c0848e089dec (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
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

//=============================================================================
/**
 *  @file    WFMO_Reactor_Test.cpp
 *
 *  This is a simple test of the WFMO_Reactor.  It makes sure that
 *  removals and suspensions work correctly.
 *
 *  @author Irfan Pyarali <irfan@oomworks.com>
 */
//=============================================================================

#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/WFMO_Reactor.h"
#include "ace/Pipe.h"

#if defined (ACE_WIN32)

static int number_of_handlers = 6;
static int number_of_closes = 0;

class Event_Handler : public ACE_Event_Handler
{
public:
  Event_Handler (ACE_Reactor &reactor);

  ~Event_Handler (void);

  ACE_Pipe pipe_;
};

Event_Handler::Event_Handler (ACE_Reactor &reactor)
{
  this->reference_counting_policy ().value
    (ACE_Event_Handler::Reference_Counting_Policy::ENABLED);

  ACE_DEBUG ((LM_DEBUG,
              "Reference count in Event_Handler() is %d\n",
              this->reference_count_.value ()));

  this->reactor (&reactor);

  int result =
    this->pipe_.open ();

  ACE_TEST_ASSERT (result == 0);
  ACE_UNUSED_ARG (result);

  this->reactor ()->register_handler (this->pipe_.read_handle (),
                                      this,
                                      ACE_Event_Handler::READ_MASK);
  ACE_TEST_ASSERT (result == 0);

  this->reactor ()->register_handler (this->pipe_.write_handle (),
                                      this,
                                      ACE_Event_Handler::READ_MASK);
  ACE_TEST_ASSERT (result == 0);
}

Event_Handler::~Event_Handler (void)
{
  ACE_DEBUG ((LM_DEBUG,
              "Reference count in ~Event_Handler() is %d\n",
              this->reference_count_.value ()));

  ++number_of_closes;
}

void
test (void)
{
  int result = 0;
  int i = 0;

  ACE_Reactor reactor (new ACE_WFMO_Reactor, true);

  ACE_Event_Handler_var *safe_event_handlers =
    new ACE_Event_Handler_var[number_of_handlers];

  Event_Handler **event_handlers =
    new Event_Handler*[number_of_handlers];

  for (i = 0; i < number_of_handlers; ++i)
    {
      event_handlers[i] =
        new Event_Handler (reactor);

      safe_event_handlers[i] =
        event_handlers[i];
    }

  ACE_Time_Value timeout (0, 500 * 1000);

  result = reactor.run_reactor_event_loop (timeout);
  ACE_TEST_ASSERT (result != -1);

  for (i = 0; i < number_of_handlers; ++i)
    {
      if (i % 2 == 0)
        continue;

      result = reactor.suspend_handler (event_handlers[i]->pipe_.read_handle ());
      ACE_TEST_ASSERT (result == 0);

      result = reactor.suspend_handler (event_handlers[i]->pipe_.write_handle ());
      ACE_TEST_ASSERT (result == 0);
    }

  result = reactor.run_reactor_event_loop (timeout);
  ACE_TEST_ASSERT (result != -1);

  delete[] safe_event_handlers;
  delete[] event_handlers;
}

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("WFMO_Reactor_Test"));

  test ();

  ACE_TEST_ASSERT (number_of_closes == number_of_handlers);

  ACE_END_TEST;

  return 0;
}

#else /* ACE_WIN32 */

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("WFMO_Reactor_Test"));

  ACE_ERROR ((LM_INFO,
              ACE_TEXT ("WFMO_Reactor not supported on this platform\n")));

  ACE_END_TEST;

  return 0;
}

#endif /* ACE_WIN32 */