summaryrefslogtreecommitdiff
path: root/ACE/tests/Bug_2815_Regression_Test.cpp
blob: 23baade0054d0928129446ef1b5c0580c80a75f6 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/**
 * @file Bug_2815_Regression_Test.cpp
 *
 * Verify that the notification queue can be used with large numbers
 * of event handlers.
 *
 * Normally the ACE_Reactor uses a pipe to implement the notify()
 * methods.  ACE can be compiled with
 * ACE_HAS_REACTOR_NOTIFICATION_QUEUE, with this configuration flag
 * the Reactor uses a user-space queue to contain the notifications.
 * A single message is sent through the pipe to indicate "pipe not
 * empty."
 *
 * In this configuration, if an event handler is removed
 * from the Reactor the user-space queue has to be searched for
 * pending notifications and the notifications must be removed.
 *
 * The original implementation used a naive algorithm to search and
 * remove the handlers, which resulted in very high overhead when
 * removing handlers while having a very long notification queue.
 *
 * @author Carlos O'Ryan <coryan@atdesk.com>
 */

#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/TP_Reactor.h"
#include "ace/Select_Reactor.h"

class One_Shot_Handler;

/**
 * @class Driver
 *
 * @brief Main driver for the test, generates notification events and
 * verifies they are received correctly.
 */
class Driver
{
public:
  Driver(ACE_Reactor * reactor,
         int max_notifications,
         char const *test_name);

  /// Run the test
  void run ();

  /// One of the sub-handlers has received a notification
  void notification_received ();

  /// One of the sub-handlers has decided to skip several notifications
  void notifications_skipped (int skip_count);

  /**
   * @brief Return the reactor configured for this test
   */
  ACE_Reactor * reactor ();

private:
  /**
   * @brief Implement a single iteration.
   *
   * Each iteration of the test consists of sending multiple
   * notifications simultaneously.
   */
  void send_notifications ();

  /**
   * @brief Return true if the test is finished.
   */
  bool done () const;

  /**
   * @brief Return true if there are more iterations to run.
   */
  bool more_iterations () const;

  /**
   * @brief Return true if the current iteration is completed.
   */
  bool current_iteration_done () const;

  /**
   * @brief Run one iteration of the test, each iteration doubles
   * the number of events.
   */
  int run_one_iteration ();

  /**
   * @brief Initialize a bunch of One_Shot_Handlers
   */
  void initialize_handlers(
    int nhandlers, One_Shot_Handler ** handlers);

  /**
   * @brief Dispatch events to the One_Shot_Handlers
   */
  void notify_handlers(
    int nhandlers, One_Shot_Handler ** handlers);

  /**
   * @brief Helpful for debugging
   *
   * The number of notifications received, skipped and sent are
   * subject to simple invariants.  During debugging any violation of
   * those invariants indicates a problem in the application or the
   * Reactor.
   */
  void check_notification_invariants();

  /// A good place to set break points.
  void invariant_failed();

private:
  /**
   * @brief The reactor used in this test
   */
  ACE_Reactor * reactor_;

  /**
   * @brief The maximum number of notifications in any single
   * iteration.
   */
  int max_notifications_;

  /**
   * @brief The name of the test
   */
  char const * test_name_;

  /**
   * @brief Number of notifications received
   */
  int notifications_sent_;

  /**
   * @brief Number of notifications sent
   */
  int notifications_recv_;

  /**
   * @brief Number of notifications skipped because
   * the handler was removed
   */
  int notifications_skipped_;

  /**
   * @brief Number of notifications sent on each iteration
   */
  int notifications_curr_;
};

/**
 * @class One_Shot_Handler
 *
 * @brief A handler that removes itself from the reactor
 * after its first notification.
 *
 * To demonstrate the problems with the first implementation of
 * the notification queue we generate multiple event handlers.
 * Then we generate multiple notifications for each, but the handlers
 * remove themselves from the reactor when the first notification
 * is delivered.  This causes a lot of activity in the notification
 * queue.
 */
class One_Shot_Handler : public ACE_Event_Handler
{
public:
  One_Shot_Handler(
    Driver * master_handler,
    char const * test_name,
    int id);

  /// Increase the number of expected notifications
  void notification_queued();

  /// Receive the notifications, but remove itself from the reactor on
  /// on the first one.
  int handle_exception(ACE_HANDLE) override;

private:
  /// The driver for this test, communicate results to it
  Driver * master_handler_;

  /// The number of expected notifications
  int expected_notifications_;

  /// Identify the test and handler for debugging and better error output
  char const * test_name_;
  int id_;
};

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

#if !defined(ACE_HAS_REACTOR_NOTIFICATION_QUEUE)
  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("Notification queue disabled. ")
              ACE_TEXT ("This test depends on purge_pending_notifications. ")
              ACE_TEXT ("Thus, the test is disabled in this case\n")));

#else

# ifdef ACE_LYNXOS_MAJOR
  const int max_notifications = 512;
# else
  const int max_notifications = 512 * 1024;
# endif

  {
    ACE_Reactor select_reactor (
        new ACE_Select_Reactor,
        1);

    Driver handler(&select_reactor,
                   max_notifications,
                   "Select_Reactor");

    handler.run ();
  }

  {
    ACE_Reactor tp_reactor (new ACE_TP_Reactor,
                            1);
    Driver handler(&tp_reactor,
                   max_notifications,
                   "TP_Reactor");
    handler.run();
  }

#endif /* ACE_HAS_THREADS */
  ACE_END_TEST;

  return 0;
}

Driver::Driver (
    ACE_Reactor * reactor,
    int max_notifications,
    char const * test_name)
  : reactor_(reactor)
  , max_notifications_(max_notifications)
  , test_name_(test_name)
  , notifications_sent_(0)
  , notifications_recv_(0)
  , notifications_skipped_(0)
  , notifications_curr_(1)
{
}

void
Driver::run ()
{
  while(more_iterations())
  {
    if(run_one_iteration() == -1)
      {
        return;
      }

    notifications_curr_ *= 2;
  }

  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("Test %C passed sent=%d, recv=%d, skip=%d\n"),
              test_name_,
              notifications_sent_,
              notifications_recv_,
              notifications_skipped_));
}

void
Driver::notification_received ()
{
  ++notifications_recv_;
  check_notification_invariants();
}

void
Driver::notifications_skipped (int skip_count)
{
  notifications_skipped_ += skip_count;
  check_notification_invariants();
}

ACE_Reactor *
Driver::reactor()
{
  return reactor_;
}

void
Driver::send_notifications ()
{
  int const nhandlers = 16;
  One_Shot_Handler * handlers[nhandlers];
  initialize_handlers(nhandlers, handlers);

  for (int i = 0; i != notifications_curr_; ++i)
  {
    notify_handlers(nhandlers, handlers);
  }
}

bool
Driver::done () const
{
  return !more_iterations() && current_iteration_done();
}

bool
Driver::more_iterations() const
{
  return notifications_curr_ < max_notifications_;
}

bool
Driver::current_iteration_done() const
{
  return notifications_sent_ == (notifications_recv_ + notifications_skipped_);
}

int
Driver::run_one_iteration ()
{
  ACE_DEBUG ((LM_INFO,
              ACE_TEXT ("Running iteration with %d events for %C test\n"),
              notifications_curr_,
              test_name_));

  send_notifications ();

  // Run for 30 seconds or until the test is done.

  ACE_Time_Value const timeout(30,0);

  while (!current_iteration_done())
  {
    ACE_Time_Value start = ACE_OS::gettimeofday();
    ACE_Time_Value interval(1,0);
    reactor()->run_reactor_event_loop(interval);
    ACE_Time_Value end = ACE_OS::gettimeofday();

    if (end - start >= timeout)
      {
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("Test %C failed due to timeout ")
                    ACE_TEXT (" sent=%d,recv=%d,skip=%d\n"),
                    test_name_,
                    notifications_sent_,
                    notifications_recv_,
                    notifications_skipped_));
        return -1;
      }
  }

  return 0;
}

void
Driver::initialize_handlers(
  int nhandlers, One_Shot_Handler ** handlers)
{
  for (int j = 0; j != nhandlers; ++j)
  {
    handlers[j] = new One_Shot_Handler(this, test_name_, j);
  }
}

void
Driver::notify_handlers(
  int nhandlers, One_Shot_Handler ** handlers)
{
  for(int i = 0; i != nhandlers; ++i)
  {
    if(reactor()->notify (handlers[i]) == -1)
      {
        ACE_ERROR((LM_ERROR,
                   ACE_TEXT ("notify %d/%d in %C %p\n"),
                   i, notifications_curr_, test_name_, ACE_TEXT ("test")));
        return;
      }
    handlers[i]->notification_queued();

    ++notifications_sent_;
  }
}

void Driver::
check_notification_invariants()
{
  if (notifications_sent_ < 0)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("The number of notifications sent (%d)")
                  ACE_TEXT(" should be positive\n"),
                  notifications_sent_));
      invariant_failed();
    }

  if (notifications_recv_ < 0)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("The number of notifications received (%d)")
                  ACE_TEXT(" should be positive\n"),
                  notifications_recv_));
      invariant_failed();
    }

  if (notifications_skipped_ < 0)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("The number of notifications skipped (%d)")
                  ACE_TEXT(" should be positive\n"),
                  notifications_skipped_));
      invariant_failed();
    }

  if (notifications_sent_ < notifications_recv_)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("Too many notifications received (%d)")
                  ACE_TEXT(" vs sent (%d)\n"),
                  notifications_recv_, notifications_sent_));
      invariant_failed();
    }

  if (notifications_sent_ < notifications_skipped_)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("Too many notifications skipped (%d)")
                  ACE_TEXT(" vs sent (%d)\n"),
                  notifications_skipped_, notifications_sent_));
      invariant_failed();
    }

  if (notifications_skipped_ + notifications_recv_ > notifications_sent_)
    {
      ACE_ERROR ((LM_ERROR,
                  ACE_TEXT("Too many notifications skipped (%d)")
                  ACE_TEXT(" and received (%d) vs sent (%d)\n"),
                  notifications_skipped_, notifications_recv_,
                  notifications_sent_));
      invariant_failed();
    }
}

void Driver::
invariant_failed()
{
  // Just a good place to set a breakpoint
}

// ============================================

One_Shot_Handler::One_Shot_Handler(
  Driver * master_handler,
  char const * test_name, int id)
  : ACE_Event_Handler(master_handler->reactor())
  , master_handler_(master_handler)
  , expected_notifications_(0)
  , test_name_(test_name)
  , id_(id)
{
}

void One_Shot_Handler::
notification_queued()
{
  ++expected_notifications_;
}

int One_Shot_Handler::
handle_exception(ACE_HANDLE)
{
  --expected_notifications_;
  master_handler_->notification_received();

  int r = reactor()->purge_pending_notifications(this);
  if (r >= 0)
    {
      master_handler_->notifications_skipped(r);
      delete this;
      return 0;
    }

  ACE_ERROR((LM_ERROR,
             ACE_TEXT ("Cannot remove handler %d in %C test\n"),
             id_, test_name_));

  delete this;
  return 0;
}