Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / ACE / tests / Reactor_Exceptions_Test.cpp
blobfdb1ade001218d8c55157add413ae8fbe14d0fbe
1 //=============================================================================
2 /**
3 * @file Reactor_Exceptions_Test.cpp
5 * This is a test that makes sure the <ACE_Reactor> works
6 * correctly in the face of C++ exceptions and threads.
8 * @author Luca Priorelli <lucapri@mbox.vol.it> and Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 */
10 //=============================================================================
12 #include "test_config.h"
13 #include "ace/Reactor.h"
14 #include "ace/SOCK_Dgram.h"
15 #include "ace/INET_Addr.h"
16 #include "ace/Thread_Manager.h"
17 #include "ace/Select_Reactor.h"
19 // Just need a simple exception class.
20 class Except {};
22 static void
23 throw_exception ()
25 ACE_DEBUG ((LM_DEBUG,
26 ACE_TEXT ("(%P|%t) throw exception\n")));
27 throw Except ();
30 class My_Handler : public ACE_Event_Handler, public ACE_SOCK_Dgram
32 public:
33 My_Handler (const ACE_INET_Addr &local_addr);
35 ACE_HANDLE get_handle () const override;
36 int handle_input (ACE_HANDLE handle) override;
39 My_Handler::My_Handler (const ACE_INET_Addr &local_addr)
40 : ACE_SOCK_Dgram (local_addr)
44 ACE_HANDLE
45 My_Handler::get_handle () const
47 return ACE_SOCK_Dgram::get_handle ();
50 int
51 My_Handler::handle_input (ACE_HANDLE)
53 ACE_TCHAR buf[BUFSIZ];
54 ACE_INET_Addr from_addr;
56 ACE_DEBUG ((LM_DEBUG,
57 ACE_TEXT ("Activity occurred on handle %d!\n"),
58 ACE_SOCK_Dgram::get_handle ()));
60 ssize_t n = ACE_SOCK_Dgram::recv (buf,
61 sizeof buf,
62 from_addr);
63 if (n == -1)
64 ACE_ERROR ((LM_ERROR,
65 ACE_TEXT ("%p\n"),
66 ACE_TEXT ("handle_input")));
67 else
68 ACE_DEBUG ((LM_DEBUG,
69 ACE_TEXT ("got buf = %s\n"),
70 buf));
72 throw_exception ();
73 return 0;
76 class My_Reactor : public ACE_Select_Reactor
78 public:
79 int handle_events (ACE_Time_Value *max_wait_time) override
81 int ret = 0;
83 try
85 ret = ACE_Select_Reactor::handle_events (max_wait_time);
87 catch (...)
89 ACE_DEBUG ((LM_DEBUG,
90 ACE_TEXT (" (%t) catch exception\n")));
91 ret = -1;
92 // do your thing, etc.
94 return ret;
97 int handle_events (ACE_Time_Value &max_wait_time) override
99 return this->handle_events (&max_wait_time);
103 #if defined (ACE_HAS_THREADS)
104 static void*
105 worker (void*)
107 ACE_Reactor::instance ()->owner (ACE_OS::thr_self ());
109 for (;;)
110 if (ACE_Reactor::instance ()->handle_events () == -1)
112 ACE_DEBUG ((LM_DEBUG, ACE_TEXT (" (%t) exception return\n")));
113 break;
116 return 0;
118 #endif /* ACE_HAS_THREADS */
121 run_main (int argc, ACE_TCHAR *argv[])
123 ACE_START_TEST (ACE_TEXT ("Reactor_Exceptions_Test"));
125 ACE_DEBUG ((LM_DEBUG,
126 ACE_TEXT ("Starting tracing\n")));
128 u_short port = argc > 1 ? ACE_OS::atoi (argv[1]) : ACE_DEFAULT_SERVER_PORT;
130 ACE_INET_Addr local_addr (port);
131 ACE_INET_Addr remote_addr (port, ACE_LOCALHOST, PF_INET);
132 ACE_Reactor reactor (new My_Reactor, true);
134 ACE_Reactor::instance (&reactor);
135 ACE_Thread_Manager *thr_mgr = ACE_Thread_Manager::instance ();
138 // Make sure handler gets cleaned up before reactor by putting it in its
139 // own scope
140 My_Handler handler (local_addr);
142 if (ACE_Reactor::instance ()->register_handler
143 (&handler,
144 ACE_Event_Handler::READ_MASK) == -1)
145 ACE_ERROR_RETURN ((LM_ERROR,
146 ACE_TEXT ("%p\n"),
147 ACE_TEXT ("register_handler")),
148 -1);
150 #if defined (ACE_HAS_THREADS)
151 thr_mgr->spawn (ACE_THR_FUNC (worker));
152 #else
153 // Need to figure out how to implement this test.
154 ACE_ERROR ((LM_INFO,
155 ACE_TEXT ("threads not supported on this platform\n")));
156 #endif /* ACE_HAS_THREADS */
158 ACE_SOCK_Dgram dgram (ACE_sap_any_cast (ACE_INET_Addr &), PF_INET);
160 for (size_t i = 0; i < ACE_MAX_ITERATIONS; i++)
161 dgram.send (ACE_TEXT ("Hello"),
162 sizeof (ACE_TEXT ("Hello")),
163 remote_addr);
164 // Barrier to wait for the other thread to return.
165 thr_mgr->wait ();
167 handler.close ();
168 dgram.close ();
171 ACE_DEBUG ((LM_DEBUG,
172 ACE_TEXT (" (%t) exiting main\n")));
174 ACE_END_TEST;
175 return 0;