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 ***** */
40 ** Description:Test socket IO timeouts (kernel level)
42 ** Modification History:
43 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
44 ** The debug mode will print all of the printfs associated with this test.
45 ** The regress mode will be the default mode. Since the regress tool limits
46 ** the output to a one line status:PASS or FAIL,all of the printf statements
47 ** have been handled with an if (debug_mode) statement.
48 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
49 ** recognize the return code from tha main program.
50 ***********************************************************************/
51 /***********************************************************************
53 ***********************************************************************/
54 /* Used to get the command line option */
61 #define BASE_PORT 8000
62 #define DEFAULT_ACCEPT_TIMEOUT 2
64 typedef struct threadInfo
{
66 PRInt16 accept_timeout
;
72 PRIntn failed_already
=0;
77 thread_main(void *_info
)
79 threadInfo
*info
= (threadInfo
*)_info
;
82 PRFileDesc
*listenSock
= NULL
;
83 PRFileDesc
*clientSock
;
86 if (debug_mode
) printf("thread %d is alive\n", info
->id
);
88 listenSock
= PR_NewTCPSocket();
90 if (debug_mode
) printf("unable to create listen socket\n");
94 listenAddr
.inet
.family
= AF_INET
;
95 listenAddr
.inet
.port
= PR_htons(BASE_PORT
+ info
->id
);
96 listenAddr
.inet
.ip
= PR_htonl(INADDR_ANY
);
97 rv
= PR_Bind(listenSock
, &listenAddr
);
98 if (rv
== PR_FAILURE
) {
99 if (debug_mode
) printf("unable to bind\n");
103 rv
= PR_Listen(listenSock
, 4);
104 if (rv
== PR_FAILURE
) {
105 if (debug_mode
) printf("unable to listen\n");
109 if (debug_mode
) printf("thread %d going into accept for %d seconds\n",
110 info
->id
, info
->accept_timeout
+ info
->id
);
112 clientSock
= PR_Accept(listenSock
, &clientAddr
, PR_SecondsToInterval(info
->accept_timeout
+info
->id
));
114 if (clientSock
== NULL
) {
115 if (PR_GetError() == PR_IO_TIMEOUT_ERROR
)
117 printf("PR_Accept() timeout worked!\n");
118 printf("TEST FAILED! PR_Accept() returned error %d\n",
121 else failed_already
=1;
123 if (debug_mode
) printf ("TEST FAILED! PR_Accept() succeeded?\n");
124 else failed_already
=1;
125 PR_Close(clientSock
);
130 PR_Close(listenSock
);
132 PR_Lock(info
->dead_lock
);
134 PR_NotifyCondVar(info
->dead_cv
);
135 PR_Unlock(info
->dead_lock
);
137 if (debug_mode
) printf("thread %d is dead\n", info
->id
);
141 thread_test(PRInt32 scope
, PRInt32 num_threads
)
149 if (debug_mode
) printf("IO Timeout test started with %d threads\n", num_threads
);
151 dead_lock
= PR_NewLock();
152 dead_cv
= PR_NewCondVar(dead_lock
);
155 for (index
= 0; index
< num_threads
; index
++) {
156 threadInfo
*info
= (threadInfo
*)malloc(sizeof(threadInfo
));
159 info
->dead_lock
= dead_lock
;
160 info
->dead_cv
= dead_cv
;
161 info
->alive
= &alive
;
162 info
->accept_timeout
= DEFAULT_ACCEPT_TIMEOUT
;
164 thr
= PR_CreateThread( PR_USER_THREAD
,
169 PR_UNJOINABLE_THREAD
,
175 PR_Unlock(dead_lock
);
181 if (debug_mode
) printf("main loop awake; alive = %d\n", alive
);
182 PR_WaitCondVar(dead_cv
, PR_INTERVAL_NO_TIMEOUT
);
184 PR_Unlock(dead_lock
);
187 int main(int argc
, char **argv
)
191 /* The command line argument: -d is used to determine if the test is being run
192 in debug mode. The regress tool requires only one line output:PASS or FAIL.
193 All of the printfs associated with this test has been handled with a if (debug_mode)
198 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "d:");
199 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
201 if (PL_OPT_BAD
== os
) continue;
204 case 'd': /* debug mode */
211 PL_DestroyOptState(opt
);
216 num_threads
= atoi(argv
[2]);
218 num_threads
= NUM_THREADS
;
220 PR_Init(PR_USER_THREAD
, PR_PRIORITY_LOW
, 0);
223 if (debug_mode
) printf("kernel level test\n");
224 thread_test(PR_GLOBAL_THREAD
, num_threads
);