2 //=============================================================================
4 * @file Thread_Pool_Reactor_Test.cpp
6 * This program is a torture test of thread pool reactors. It
7 * starts by spawning several server threads waiting to handle
8 * events. Several other client threads are spawned right after
9 * to initiate connections to server threads. Each connection
10 * adds a new Svc_Handler into the TP_Reactor and sends out
11 * several "requests" to the server thread. After the connection
12 * is closed, the Svc_Handler is removed from the TP_Reactor.
13 * Each message is treated as a separate request by the server so
14 * two consecutive requests might be serviced by two different
17 * Usage: Thread_Pool_Reactor_Test [-r <hostname:port#>]
18 * [-s <server thr#>] [-c <client thr#>] [-d <delay>]
19 * [-i <client conn attempt#>] [-n <client request# per conn>]
22 * <hostname:port#>: ACE_DEFAULT_RENDEZVOUS
23 * <server thr#>: ACE_MAX_THREADS
24 * <client thr#>: ACE_MAX_ITERATIONS
25 * <client conn attempt#>: ACE_MAX_ITERATIONS
26 * <client req# per conn>: ACE_MAX_THREADS
29 * @author Irfan Pyarali <irfan@cs.wustl.edu> and Nanbor Wang <nanbor@cs.wustl.edu>
31 //=============================================================================
34 #include "test_config.h"
35 #include "ace/OS_NS_string.h"
36 #include "ace/OS_NS_unistd.h"
37 #include "ace/Get_Opt.h"
38 #include "ace/SOCK_Connector.h"
39 #include "ace/SOCK_Acceptor.h"
40 #include "ace/Acceptor.h"
41 #include "ace/Thread_Manager.h"
42 #include "ace/TP_Reactor.h"
45 #if defined (ACE_HAS_THREADS) && !defined ACE_LACKS_ACCEPT
47 #include "Thread_Pool_Reactor_Test.h"
48 using ACCEPTOR
= ACE_Strategy_Acceptor
<Request_Handler
, ACE_SOCK_Acceptor
>;
50 // Accepting end point. This is actually "localhost:10010", but some
51 // platform couldn't resolve the name so we use the IP address
53 static const ACE_TCHAR
*rendezvous
= ACE_TEXT ("127.0.0.1:10010");
55 // Total number of server threads.
56 static size_t svr_thrno
= ACE_MAX_THREADS
;
58 // Default network parameters (MAX_BINDS and system buffers) are too small
59 // for full test on some platforms; add platforms that can't handle too many
60 // connection simultaneously here.
61 #if defined (ACE_VXWORKS)
62 #define ACE_LOAD_FACTOR /2
64 #define ACE_LOAD_FACTOR
67 // Total number of client threads.
68 static size_t cli_thrno
= ACE_MAX_THREADS ACE_LOAD_FACTOR
;
70 // Total connection attempts of a client thread.
71 static size_t cli_conn_no
= ACE_MAX_ITERATIONS ACE_LOAD_FACTOR
;
73 // Total requests a client thread sends.
74 static size_t cli_req_no
= ACE_MAX_THREADS ACE_LOAD_FACTOR
;
76 // Delay before a thread sending the next request (in msec.)
77 static int req_delay
= 50;
80 parse_arg (int argc
, ACE_TCHAR
*argv
[])
82 //FUZZ: disable check_for_lack_ACE_OS
83 ACE_Get_Opt
getopt (argc
, argv
, ACE_TEXT ("r:s:c:d:i:n:"));
87 while ((c
= getopt ()) != -1)
89 //FUZZ: enable check_for_lack_ACE_OS
92 case 'r': // hostname:port
93 rendezvous
= getopt
.opt_arg ();
96 svr_thrno
= ACE_OS::atoi (getopt
.opt_arg ());
99 cli_thrno
= ACE_OS::atoi (getopt
.opt_arg ());
102 req_delay
= ACE_OS::atoi (getopt
.opt_arg ());
105 cli_conn_no
= ACE_OS::atoi (getopt
.opt_arg ());
108 cli_req_no
= ACE_OS::atoi (getopt
.opt_arg ());
111 ACE_ERROR ((LM_ERROR
,
112 "Usage: Thread_Pool_Reactor_Test [-r <hostname:port#>]"
113 "\t[-s <server thr#>] [-c <client thr#>] [-d <delay>]"
114 "\t[-i <client conn attempt#>]"
115 "[-n <client request# per conn>]\n"));
121 Request_Handler::Request_Handler (ACE_Thread_Manager
*thr_mgr
)
122 : ACE_Svc_Handler
<ACE_SOCK_STREAM
, ACE_MT_SYNCH
> (thr_mgr
),
125 // Make sure we use TP_Reactor with this class (that's the whole
127 this->reactor (ACE_Reactor::instance ());
131 Request_Handler::handle_input (ACE_HANDLE fd
)
133 ACE_TCHAR buffer
[BUFSIZ
];
135 ssize_t result
= this->peer ().recv (&len
, sizeof (ACE_TCHAR
));
138 && this->peer ().recv_n (buffer
, len
* sizeof (ACE_TCHAR
))
139 == static_cast<ssize_t
> (len
* sizeof (ACE_TCHAR
)))
141 ++this->nr_msgs_rcvd_
;
143 ACE_DEBUG ((LM_DEBUG
,
144 "(%t) svr input; fd: 0x%x; input: %s\n",
147 if (ACE_OS::strcmp (buffer
, ACE_TEXT ("shutdown")) == 0)
148 ACE_Reactor::instance()->end_reactor_event_loop ();
152 ACE_DEBUG ((LM_DEBUG
,
153 "(%t) Request_Handler: 0x%x peer closed (0x%x)\n",
159 Request_Handler::handle_close (ACE_HANDLE fd
, ACE_Reactor_Mask
)
161 ACE_DEBUG ((LM_DEBUG
,
162 "(%t) svr close; fd: 0x%x, rcvd %d msgs\n",
164 this->nr_msgs_rcvd_
));
165 if (this->nr_msgs_rcvd_
!= cli_req_no
)
167 "(%t) Handler 0x%x: Expected %d messages; got %d\n",
170 this->nr_msgs_rcvd_
));
176 reactor_event_hook (ACE_Reactor
*)
178 ACE_DEBUG ((LM_DEBUG
,
179 "(%t) handling events ....\n"));
184 static ACE_THR_FUNC_RETURN
187 // Server thread function.
189 ACE_Reactor::instance ()->run_reactor_event_loop (&reactor_event_hook
);
192 ACE_ERROR_RETURN ((LM_ERROR
,
194 "Error handling events"),
197 ACE_DEBUG ((LM_DEBUG
,
198 "(%t) I am done handling events. Bye, bye\n"));
203 static ACE_THR_FUNC_RETURN
204 cli_worker (void *arg
)
206 // Client thread function.
207 ACE_INET_Addr
addr (rendezvous
);
208 ACE_SOCK_Stream stream
;
209 ACE_SOCK_Connector connect
;
210 ACE_Time_Value
delay (0, req_delay
);
211 size_t len
= * reinterpret_cast<ACE_TCHAR
*> (arg
);
213 for (size_t i
= 0 ; i
< cli_conn_no
; i
++)
215 if (connect
.connect (stream
, addr
) < 0)
217 ACE_ERROR ((LM_ERROR
,
223 for (size_t j
= 0; j
< cli_req_no
; j
++)
225 ACE_DEBUG ((LM_DEBUG
,
226 "(%t) conn_worker handle 0x%x, req %d\n",
227 stream
.get_handle (),
229 if (stream
.send_n (arg
,
230 (len
+ 1) * sizeof (ACE_TCHAR
)) == -1)
232 ACE_ERROR ((LM_ERROR
,
237 ACE_OS::sleep (delay
);
246 static ACE_THR_FUNC_RETURN
250 const ACE_TCHAR
*msg
= ACE_TEXT ("Message from Connection worker");
251 ACE_TCHAR buf
[BUFSIZ
];
252 buf
[0] = static_cast<ACE_TCHAR
> ((ACE_OS::strlen (msg
) + 1));
253 ACE_OS::strcpy (&buf
[1], msg
);
255 ACE_INET_Addr
addr (rendezvous
);
258 "(%t) Spawning %d client threads...\n",
260 int grp
= ACE_Thread_Manager::instance ()->spawn_n (cli_thrno
,
263 ACE_TEST_ASSERT (grp
!= -1);
265 ACE_Thread_Manager::instance ()->wait_grp (grp
);
267 ACE_DEBUG ((LM_DEBUG
,
268 "(%t) Client threads done; shutting down...\n"));
269 ACE_SOCK_Stream stream
;
270 ACE_SOCK_Connector connect
;
272 if (connect
.connect (stream
, addr
) == -1)
273 ACE_ERROR ((LM_ERROR
,
274 "(%t) %p Error while connecting\n",
277 const ACE_TCHAR
*sbuf
= ACE_TEXT ("\011shutdown");
279 ACE_DEBUG ((LM_DEBUG
,
280 "shutdown stream handle = %x\n",
281 stream
.get_handle ()));
283 if (stream
.send_n (sbuf
, (ACE_OS::strlen (sbuf
) + 1) * sizeof (ACE_TCHAR
)) == -1)
284 ACE_ERROR ((LM_ERROR
,
294 run_main (int argc
, ACE_TCHAR
*argv
[])
296 ACE_START_TEST (ACE_TEXT ("Thread_Pool_Reactor_Test"));
297 parse_arg (argc
, argv
);
299 // Changed the default
301 ACE_Reactor
new_reactor (&sr
);
302 ACE_Reactor::instance (&new_reactor
);
305 ACE_INET_Addr
accept_addr (rendezvous
);
307 if (acceptor
.open (accept_addr
) == -1)
308 ACE_ERROR_RETURN ((LM_ERROR
,
314 ACE_TEXT ("(%t) Spawning %d server threads...\n"),
316 ACE_Thread_Manager::instance ()->spawn_n (svr_thrno
,
318 ACE_Thread_Manager::instance ()->spawn (worker
);
320 ACE_Thread_Manager::instance ()->wait ();
328 run_main (int, ACE_TCHAR
*[])
330 ACE_START_TEST (ACE_TEXT ("Thread_Pool_Reactor_Test"));
333 "threads/accept not supported on this platform\n"));
338 #endif /* ACE_HAS_THREADS */