/**
 * @file Bug_1890_Regression_Test.cpp
 *
 * Bug_1890_Regression_Test.cpp,v 1.1 2006/05/26 10:23:28 jwillemsen Exp
 *
 * 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,
           "Bug_1890_Regression_Test.cpp,v 1.1 2006/05/26 10:23:28 jwillemsen Exp")

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];
}
