summaryrefslogtreecommitdiff
path: root/ACE/tests/Sig_Handlers_Test.cpp
blob: 9378667c33c8b7da14004c39096b7a3a09cc2b46 (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

//=============================================================================
/**
 *  @file    Sig_Handlers_Test.cpp
 *
 *  This is a simple program that tests whether the ACE_Sig_Handlers
 *  class works properly.  To run this test, start the application
 *  and then type ^C.  If everything is working properly the test
 *  will shutdown gracefully.
 *
 *  @author Douglas C. Schmidt <schmidt@dre.vanderbilt.edu> and Andreas Drescher <ace at anticat dot ch>
 */
//=============================================================================


#include "test_config.h"
#include "ace/Reactor.h"
#include "ace/WFMO_Reactor.h"
#include "ace/Select_Reactor.h"
#include "ace/Log_Msg.h"
#include "ace/Signal.h"
#include "ace/Assert.h"
#include "ace/SString.h"


class Test_SIGINT_Handler : public ACE_Event_Handler
{
public:
  Test_SIGINT_Handler (ACE_Reactor *reactor, const char *message)
    : message_ (message)
  {
    int result = reactor->register_handler (SIGINT, this);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT("Main::Test_SIGINT_Handler (%u) - Result %i\n"),
                this,
                result));
    Test_SIGINT_Handler::registration_count_++;
  }

  ~Test_SIGINT_Handler() override
  {
    ACE_TEST_ASSERT (Test_SIGINT_Handler::handle_signal_count_ == Test_SIGINT_Handler::registration_count_);
  }

  int handle_signal (int signal, siginfo_t *, ucontext_t *) override
  {
    ACE_TEST_ASSERT (signal == SIGINT);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT("Main::Test_SIGINT_Handler (%u) - %s\n"),
                this,
                this->message_.c_str()));
    Test_SIGINT_Handler::handle_signal_count_++;
    return 0;
  }

private:
  ACE_CString message_;

  static int handle_signal_count_;

  static int registration_count_;
};

int Test_SIGINT_Handler::handle_signal_count_ = 0;
int Test_SIGINT_Handler::registration_count_ = 0;

class Test_SIGINT_Shutdown_Handler : public ACE_Event_Handler
{
public:
  Test_SIGINT_Shutdown_Handler (ACE_Reactor *reactor)
  {
    int result = reactor->register_handler (SIGINT, this);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT("Main::Test_SIGINT_Shutdown_Handler (%u) - Result %i\n"),
                this,
                result));
  }

  int handle_signal (int signal, siginfo_t *, ucontext_t *) override
  {
    ACE_TEST_ASSERT (signal == SIGINT);
    ACE_DEBUG ((LM_DEBUG,
                ACE_TEXT("Main::Test_SIGINT_Shutdown_Handler (%u)\n"),
                this));
    ACE_Reactor::instance ()->end_reactor_event_loop ();
    return 0;
  }
};

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

  ACE_Sig_Handlers multi_handlers;
  ACE_Select_Reactor reactor_impl (&multi_handlers);
  ACE_Reactor reactor (&reactor_impl);
  ACE_Reactor::instance (&reactor);

  // Create a bevy of handlers.

  Test_SIGINT_Handler h1 (ACE_Reactor::instance (), "h1");
  Test_SIGINT_Handler h2 (ACE_Reactor::instance (), "h2");
  Test_SIGINT_Handler h3 (ACE_Reactor::instance (), "h3");
  Test_SIGINT_Handler h4 (ACE_Reactor::instance (), "h4");
  Test_SIGINT_Handler h5 (ACE_Reactor::instance (), "h5");
  Test_SIGINT_Handler h6 (ACE_Reactor::instance (), "h6");
  Test_SIGINT_Handler h7 (ACE_Reactor::instance (), "h7");
  Test_SIGINT_Handler h8 (ACE_Reactor::instance (), "h8");

  Test_SIGINT_Shutdown_Handler h0 (ACE_Reactor::instance ());

  // Wait for user to type SIGINT.
  while (!ACE_Reactor::instance ()->reactor_event_loop_done ())
    {
      ACE_DEBUG ((LM_DEBUG,"\nwaiting for SIGINT\n"));
      if (ACE_Reactor::instance ()->handle_events () == -1)
        ACE_ERROR ((LM_ERROR,"%p\n","handle_events"));
    }

  ACE_END_TEST;
  return 0;
}