summaryrefslogtreecommitdiff
path: root/ACE/tests/Bug_1890_Regression_Test.cpp
blob: a28c8302b778fab12b6e99207fb0c65c6571e6cf (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
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
/**
 * @file Bug_1890_Regression_Test.cpp
 *
 * $Id$
 *
 * Reproduces the problems reported in bug 1890
 *   http://deuce.doc.wustl.edu/bugzilla/show_bug.cgi?id=1890
 *
 * @author Carlos O'Ryan <coryan@atdesk.com>
 * Based on a test provided by "Vadim" (no further details available)
 */

#include "test_config.h"

#include "ace/Pipe.h"
#include "ace/Event_Handler.h"
#include "ace/Reactor.h"

ACE_RCSID (tests,
           Bug_1890_Regression_Test,
           "$Id$")

int const nhandlers = 3;

/**
 * This class is used to create real I/O in the test.  To keep the I/O under
 * control and keep the test to a single process we use ACE_Pipe.  This class
 * is known to work with the Reactor, in fact, that is its main function.
 *
 * Handler counts how many calls to handle_input() has the reactor performed.
 * When bug 1890 is triggered the Reactor continues to call the timers, but it
 * stops calling select() and the handle_input() functions.
 */
class Handler : public ACE_Event_Handler
{
public:
    Handler();

    /// Initialize the pipe and register with the reactor
    int open(ACE_Reactor * reactor);

    /// Return the current count
    size_t handle_input_count() const;

    /// Write some data
    void send_dummy_data();

    /// Reactor callback
    virtual ACE_HANDLE get_handle() const;
    virtual int handle_input(ACE_HANDLE);

private:
    size_t handle_input_count_;

    ACE_Pipe the_pipe_;

    ACE_HANDLE handles_[2];
};

/**
 * This is the main driver for the test.  This timer is called by the reactor
 * in a repeating interval.  On the first @c initial_iterations the Timer
 * writes data through all of its handlers.  On iteration @c initial_iteration
 * it triggers bug 1890 by removing all the handlers from the reactor, and
 * then re-adding one handler.
 *
 */
class Timer : public ACE_Event_Handler
{
public:
    Timer();

    int open(ACE_Reactor * reactor);
    void close();
    bool check_expected_results() const;

    virtual int handle_timeout(ACE_Time_Value const &, void const*);

private:
    void send_data_through_handlers();
    void remove_some_handlers();

    Handler & special_handler();
    Handler const & special_handler() const;

private:
    Handler handler_[nhandlers];
    int iteration_;

    size_t recorded_count_;
};

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

  ACE_Reactor * reactor = ACE_Reactor::instance();

  // Create the timer, this is the main driver for the test
  Timer * timer = new Timer;

  // Initialize the timer and register with the reactor
  if (-1 == timer->open(reactor))
  {
      ACE_ERROR_RETURN ((LM_ERROR, "Cannot initialize timer\n"), -1);
  }

  reactor->run_reactor_event_loop();

  // Verify that the results are what we expect
  if (!timer->check_expected_results())
  {
      ACE_ERROR_RETURN ((LM_ERROR, "Test failed\n"), -1);
  }

  // Cleanup
  timer->close();

  ACE_END_TEST;

  return 0;
}

Handler::Handler()
    : handle_input_count_(0)
    , the_pipe_()
{
}

int Handler::
open(ACE_Reactor * r)
{
  if(-1 == the_pipe_.open(handles_))
    {
      return -1;
    }
  if(-1 == r->register_handler(this, ACE_Event_Handler::READ_MASK))
    {
      return -1;
    }
  return 0;
}

size_t Handler::handle_input_count() const
{
    return handle_input_count_;
}

void Handler::send_dummy_data()
{
    char buf[] = "dummy";
    (void) the_pipe_.send(buf, sizeof(buf));
}

ACE_HANDLE Handler::get_handle() const
{
    return the_pipe_.read_handle();
}

int Handler::handle_input(ACE_HANDLE)
{
    ++handle_input_count_;
    // ACE_DEBUG((LM_DEBUG, "Handler::handle_input called for %d\n", h));
    return 0;
}

int const initial_iterations = 5;
int const total_iterations = 10;

int const special_handler_index = nhandlers - 1;

Timer::Timer()
    : iteration_(0)
    , recorded_count_(0)
{
}

int Timer::open(ACE_Reactor * r)
{
  this->reactor(r);

  // Initialize both handles and register them with the reactor for reading.
  for(int i = 0; i != nhandlers; ++i)
  {
      if (-1 == handler_[i].open(r))
      {
        ACE_ERROR_RETURN ((LM_ERROR, "Could not open dummy handler %d\n", i), -1);
      }
  }

  ACE_Time_Value const interval(0, ACE_ONE_SECOND_IN_USECS / 10);
  ACE_Time_Value const startup (0, ACE_ONE_SECOND_IN_USECS / 20);

  if ( -1 == r->schedule_timer(
	   this, 0, startup, interval))
  {
      ACE_ERROR_RETURN((LM_ERROR, "Could not schedule timer\n"), -1);
  }

  return 0;
}

void Timer::close()
{
  for(int i = 0; i != nhandlers; ++i)
  {
      reactor()->remove_handler(&handler_[i], ACE_Event_Handler::ALL_EVENTS_MASK);
  }
  reactor()->cancel_timer(this);
}

bool Timer::check_expected_results() const
{
    if(recorded_count_ < special_handler().handle_input_count())
    {
	return true;
    }
    return false;
}

int Timer::handle_timeout(ACE_Time_Value const &, void const *)
{
    if (iteration_ == 0)
    {
      send_data_through_handlers();
    }

    ++iteration_;
    if (iteration_ < initial_iterations)
    {
      return 0;
    }

    if (iteration_ == initial_iterations)
    {
      remove_some_handlers();
      recorded_count_  = special_handler().handle_input_count();
      return 0;
    }

    if (iteration_ < total_iterations)
    {
      return 0;
    }

    reactor()->end_event_loop();

    return 0;
}

void Timer::send_data_through_handlers()
{
    for(int i = 0; i != nhandlers; ++i)
    {
      handler_[i].send_dummy_data();
    }
}

void Timer::remove_some_handlers()
{
    for(int i = 0; i != nhandlers; ++i)
    {
	if (-1 == reactor()->remove_handler(&handler_[i], ACE_Event_Handler::ALL_EVENTS_MASK))
	{
	    ACE_ERROR((LM_ERROR, "Cannot remove handler %d in timeout\n", i));
	}
    }

    if (-1 == reactor()->register_handler(&special_handler(), ACE_Event_Handler::ALL_EVENTS_MASK))
    {
	ACE_ERROR((LM_ERROR, "Cannot add back special handler in timeout\n"));
    }
}

Handler & Timer::special_handler()
{
    return handler_[special_handler_index];
}

Handler const & Timer::special_handler() const
{
    return handler_[special_handler_index];
}