Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Max_Default_Port_Test.cpp
blob90641fcb1543e311cc993d6b9447e046c16daae1
1 //=============================================================================
2 /**
3 * @file Max_Default_Port_Test.cpp
5 * This is a test for ACE_MAX_DEFAULT_PORT value. The test tests the
6 * highest value of the port number at which an event handler can be
7 * registered and a Connector can be connected to.
9 * Some weird behaviour has been reported on Windows NT (sp 3) when
10 * the port number exceeds 65279 resulting ACE_MAX_DEFAULT_PORT to set
11 * to zero on that platform.
13 * In this test, the event handler is started at the port value
14 * USHRT_MAX and decremented for 'ports_to_test' port values and tested
15 * if the highest port number used agrees with ACE_MAX_DEFAULT_PORT value.
17 * @author Chanaka Liyanaarachchi <chanaka@ociweb.com>
19 //=============================================================================
22 #include "test_config.h"
23 #include "ace/Reactor.h"
24 #include "ace/SOCK_Connector.h"
25 #include "ace/Thread_Manager.h"
26 #include "ace/OS_NS_unistd.h"
27 #include "ace/Time_Value.h"
29 #include "Max_Default_Port_Test.h"
31 // implement a retry and recuperation mechanism for VxWorks because test will otherwise fail
32 // on a non-optimized kernel with several ETIME errors on the client connects.
33 #if defined (ACE_VXWORKS)
34 int retry_port_ = 0;
35 #endif
37 My_Accept_Handler::My_Accept_Handler (ACE_INET_Addr &addr)
38 : addr_ (addr)
40 if (addr.get_port_number() != 0)
41 this->open (addr);
45 My_Accept_Handler::~My_Accept_Handler ()
47 this->peer_acceptor_.close (); // Prevent handle leaks
51 int
52 My_Accept_Handler::open (ACE_INET_Addr &addr)
54 if (this->peer_acceptor_.open (addr, 1) == -1)
56 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"),
57 ACE_TEXT ("My_Accept_Handler open")));
58 ACE_OS::exit (1);
61 return 0;
64 ACE_HANDLE
65 My_Accept_Handler::get_handle () const
67 return this->peer_acceptor_.get_handle ();
70 int
71 My_Accept_Handler::handle_input (ACE_HANDLE)
73 if (this->peer_acceptor_.accept(this->stream_, 0) == -1) {
74 ACE_ERROR((LM_ERROR, ACE_TEXT ("%p\n"),
75 ACE_TEXT ("peer_acceptor.accept")));
76 ACE_OS::exit(1);
79 ACE_DEBUG ((LM_DEBUG,
80 ACE_TEXT ("My_Accept_Handler::handle_input\n")));
82 // Close the opened stream, else it'll leak a handle. Don't close
83 // the acceptor here, though, because get_handle() needs it to
84 // correctly allow removal from the reactor later. It gets closed
85 // in the destructor.
86 this->stream_.close ();
88 return 0;
91 u_short
92 My_Accept_Handler::port ()
94 return this->addr_.get_port_number();
97 long max_connected_port = 0;
99 void *
100 client (void *arg)
102 ACE_INET_Addr *remote_addr = reinterpret_cast<ACE_INET_Addr *> (arg);
104 ACE_INET_Addr server_addr (remote_addr->get_port_number (),
105 ACE_LOCALHOST,
106 AF_INET);
108 ACE_SOCK_Stream cli_stream;
109 ACE_SOCK_Connector con;
110 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
112 ACE_DEBUG ((LM_DEBUG,
113 ACE_TEXT ("(%P|%t) Connecting to port %d\n"),
114 server_addr.get_port_number()));
116 // Initiate connection with server; don't wait forever
117 if (con.connect (cli_stream,
118 server_addr,
119 &timeout) == -1)
121 #if defined (ACE_VXWORKS)
122 if (errno == ETIME)
124 if ( ++retry_port_<6 )
126 ACE_DEBUG ((LM_DEBUG,
127 ACE_TEXT ("(%P|%t) Going to retry port %d\n"),
128 server_addr.get_port_number()));
131 if ( retry_port_>5 )
133 retry_port_ = 0;
134 #endif
135 ACE_ERROR ((LM_ERROR,
136 ACE_TEXT ("(%P|%t) %p\n"),
137 ACE_TEXT ("connection failed")));
139 #if defined (ACE_VXWORKS)
141 #endif
142 return 0;
145 #if defined (ACE_VXWORKS)
146 retry_port_ = 0;
147 #endif
149 // if connect succesful, what is the max port number we connected
150 // up to now.
151 int connected_port = server_addr.get_port_number ();
153 if (connected_port > max_connected_port)
154 max_connected_port = connected_port;
156 cli_stream.close ();
158 return 0;
162 run_main (int argc, ACE_TCHAR *argv[])
164 ACE_START_TEST (ACE_TEXT ("Max_Default_Port_Test"));
166 ACE_UNUSED_ARG (argc);
167 ACE_UNUSED_ARG (argv);
169 #ifndef ACE_LACKS_ACCEPT
171 u_short max_listened_port = 0;
173 #if defined (__Lynx__)
174 // LynxOS can handle only 80 test iterations.
175 // This needs to be investigated further -- olli 12.11.2007
176 const u_short ports_to_test = 80;
177 #else
178 const u_short ports_to_test = 300;
179 #endif
181 //Ports beyond 65279 were said to bad on NT sp 3.
182 for (u_short idx = USHRT_MAX; idx != USHRT_MAX - ports_to_test; --idx)
184 #if defined (ACE_VXWORKS)
185 if (retry_port_>0)
187 ++idx;
188 ACE_OS::sleep (ACE_Time_Value (2*ACE_DEFAULT_TIMEOUT));
190 #endif
192 ACE_INET_Addr addr (idx);
194 My_Accept_Handler *eh = new My_Accept_Handler (addr);
197 if ( ACE_Reactor::instance()->register_handler (
199 ACE_Event_Handler::ACCEPT_MASK) == -1)
201 ACE_ERROR_RETURN ((LM_ERROR,
202 ACE_TEXT ("%p\n"),
203 ACE_TEXT ("Failed to register event handler")),
207 ACE_DEBUG ((LM_DEBUG, "Registered event handler at %d\n", idx));
209 ACE_Time_Value tv (1);
211 #if defined (ACE_HAS_THREADS)
213 if (ACE_Thread_Manager::instance ()->spawn_n
215 ACE_THR_FUNC (client),
216 reinterpret_cast <void *> (&addr),
217 THR_NEW_LWP | THR_DETACHED) == -1)
219 ACE_ERROR_RETURN ((LM_ERROR,
220 ACE_TEXT ("(%P|%t) %p\n"),
221 ACE_TEXT ("thread create failed")),
224 ACE_Thread_Manager::instance ()->wait ();
226 #else
227 ACE_UNUSED_ARG (client);
228 ACE_ERROR ((LM_ERROR,
229 ACE_TEXT ("(%P|%t) only one thread may be run in a process on this platform\n%a"),
230 1));
231 #endif //ACE_HAS_THREADS
233 if (ACE_Reactor::instance()->handle_events (tv) == -1 )
235 ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"),
236 ACE_TEXT ("Reactor::handle_events")),
240 // see if I can register a reactor at this port.
241 if (eh->port () == idx)
243 if (idx > max_listened_port)
244 max_listened_port = idx;
246 else
248 ACE_ERROR_RETURN ((LM_ERROR,
249 ACE_TEXT ("Test Fail, listening port %d\n"),
250 eh->port()),
254 ACE_Reactor::instance()->remove_handler (
256 ACE_Event_Handler::ACCEPT_MASK |
257 ACE_Event_Handler::DONT_CALL);
258 delete eh;
261 ACE_DEBUG ((LM_DEBUG,
262 "Value of ACE_MAX_DEFAULT_PORT %d\n",
263 ACE_MAX_DEFAULT_PORT));
265 ACE_DEBUG ((LM_DEBUG,
266 "Highest port value I can listen at %d\n",
267 max_listened_port));
269 ACE_DEBUG ((LM_DEBUG,
270 "Highest port value I can connect to %d\n",
271 max_connected_port));
273 if ((max_listened_port == ACE_MAX_DEFAULT_PORT) &&
274 (max_connected_port == ACE_MAX_DEFAULT_PORT))
276 ACE_DEBUG ((LM_DEBUG,
277 ACE_TEXT ("Valid ACE_MAX_DEFAULT_PORT value: %d\n"),
278 max_listened_port));
280 else
282 ACE_ERROR ((LM_ERROR,
283 ACE_TEXT ("Invalid ACE_MAX_DEFAULT_PORT ")
284 ACE_TEXT ("or %d port may be busy; got to %d\n"),
285 ACE_MAX_DEFAULT_PORT, max_listened_port));
288 #endif // ACE_LACKS_ACCEPT
289 ACE_END_TEST;
290 return 0;