1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 ** Test socket IO timeouts
44 ** Modification History:
45 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
46 ** The debug mode will print all of the printfs associated with this test.
47 ** The regress mode will be the default mode. Since the regress tool limits
48 ** the output to a one line status:PASS or FAIL,all of the printf statements
49 ** have been handled with an if (debug_mode) statement.
50 ***********************************************************************/
51 /***********************************************************************
53 ***********************************************************************/
54 /* Used to get the command line option */
62 #define printf PR_LogPrint
63 extern void SetupMacPrintfLog(char *logFile
);
67 #define BASE_PORT 8000
68 #define DEFAULT_ACCEPT_TIMEOUT 2
70 typedef struct threadInfo
{
72 PRInt16 accept_timeout
;
78 PRIntn failed_already
= 0;
79 PRIntn debug_mode
= 0;
81 #define LOCAL_SCOPE_STRING "LOCAL scope"
82 #define GLOBAL_SCOPE_STRING "GLOBAL scope"
83 #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope"
86 thread_main(void *_info
)
88 threadInfo
*info
= (threadInfo
*)_info
;
91 PRFileDesc
*listenSock
= NULL
;
92 PRFileDesc
*clientSock
;
99 printf("thread %d is alive\n", info
->id
);
100 tscope
= PR_GetThreadScope(PR_GetCurrentThread());
103 case PR_LOCAL_THREAD
:
104 scope_str
= LOCAL_SCOPE_STRING
;
106 case PR_GLOBAL_THREAD
:
107 scope_str
= GLOBAL_SCOPE_STRING
;
109 case PR_GLOBAL_BOUND_THREAD
:
110 scope_str
= GLOBAL_BOUND_SCOPE_STRING
;
113 PR_ASSERT(!"Invalid thread scope");
116 printf("thread id %d, scope %s\n", info
->id
, scope_str
);
118 listenSock
= PR_NewTCPSocket();
121 printf("unable to create listen socket\n");
126 listenAddr
.inet
.family
= PR_AF_INET
;
127 listenAddr
.inet
.port
= PR_htons(BASE_PORT
+ info
->id
);
128 listenAddr
.inet
.ip
= PR_htonl(PR_INADDR_ANY
);
129 rv
= PR_Bind(listenSock
, &listenAddr
);
130 if (rv
== PR_FAILURE
) {
132 printf("unable to bind\n");
137 rv
= PR_Listen(listenSock
, 4);
138 if (rv
== PR_FAILURE
) {
140 printf("unable to listen\n");
146 printf("thread %d going into accept for %d seconds\n",
147 info
->id
, info
->accept_timeout
+ info
->id
);
149 clientSock
= PR_Accept(listenSock
, &clientAddr
, PR_SecondsToInterval(info
->accept_timeout
+info
->id
));
151 if (clientSock
== NULL
) {
152 if (PR_GetError() == PR_IO_TIMEOUT_ERROR
) {
154 printf("PR_Accept() timeout worked!\n");
155 printf("TEST PASSED! PR_Accept() returned error %d\n",
156 PR_IO_TIMEOUT_ERROR
);
160 printf("TEST FAILED! PR_Accept() returned error %d\n",
166 printf ("TEST FAILED! PR_Accept() succeeded?\n");
168 PR_Close(clientSock
);
173 PR_Close(listenSock
);
175 PR_Lock(info
->dead_lock
);
177 PR_NotifyCondVar(info
->dead_cv
);
178 PR_Unlock(info
->dead_lock
);
181 printf("thread %d is dead\n", info
->id
);
187 thread_test(PRThreadScope scope
, PRInt32 num_threads
)
196 printf("IO Timeout test started with %d threads\n", num_threads
);
198 dead_lock
= PR_NewLock();
199 dead_cv
= PR_NewCondVar(dead_lock
);
202 for (index
= 0; index
< num_threads
; index
++) {
203 threadInfo
*info
= (threadInfo
*)PR_Malloc(sizeof(threadInfo
));
206 info
->dead_lock
= dead_lock
;
207 info
->dead_cv
= dead_cv
;
208 info
->alive
= &alive
;
209 info
->accept_timeout
= DEFAULT_ACCEPT_TIMEOUT
;
211 thr
= PR_CreateThread( PR_USER_THREAD
,
216 PR_UNJOINABLE_THREAD
,
220 printf("Failed to create thread, error = %d(%d)\n",
221 PR_GetError(), PR_GetOSError());
226 PR_Unlock(dead_lock
);
233 printf("main loop awake; alive = %d\n", alive
);
234 PR_WaitCondVar(dead_cv
, PR_INTERVAL_NO_TIMEOUT
);
236 PR_Unlock(dead_lock
);
238 PR_DestroyCondVar(dead_cv
);
239 PR_DestroyLock(dead_lock
);
242 int main(int argc
, char **argv
)
244 PRInt32 num_threads
= 0;
246 /* The command line argument: -d is used to determine if the test is being run
247 in debug mode. The regress tool requires only one line output:PASS or FAIL.
248 All of the printfs associated with this test has been handled with a if (debug_mode)
250 Usage: test_name [-d] [-t <threads>]
253 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dt:");
254 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
256 if (PL_OPT_BAD
== os
) continue;
259 case 'd': /* debug mode */
262 case 't': /* threads to involve */
263 num_threads
= atoi(opt
->value
);
269 PL_DestroyOptState(opt
);
273 if (0 == num_threads
)
274 num_threads
= NUM_THREADS
;
276 PR_Init(PR_USER_THREAD
, PR_PRIORITY_LOW
, 0);
280 SetupMacPrintfLog("io_timeout.log");
284 printf("test with global bound thread\n");
285 thread_test(PR_GLOBAL_BOUND_THREAD
, num_threads
);
287 printf("test with local thread\n");
288 thread_test(PR_LOCAL_THREAD
, num_threads
);
290 printf("test with global thread\n");
291 thread_test(PR_GLOBAL_THREAD
, num_threads
);