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 */
61 #define BASE_PORT 8000
62 #define DEFAULT_ACCEPT_TIMEOUT 2
64 typedef struct threadInfo {
66 PRInt16 accept_timeout;
72 PRIntn failed_already = 0;
73 PRIntn debug_mode = 0;
75 #define LOCAL_SCOPE_STRING "LOCAL scope"
76 #define GLOBAL_SCOPE_STRING "GLOBAL scope"
77 #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope"
80 thread_main(void *_info)
82 threadInfo *info = (threadInfo *)_info;
85 PRFileDesc *listenSock = NULL;
86 PRFileDesc *clientSock;
93 printf("thread %d is alive\n", info->id);
94 tscope = PR_GetThreadScope(PR_GetCurrentThread());
98 scope_str = LOCAL_SCOPE_STRING;
100 case PR_GLOBAL_THREAD:
101 scope_str = GLOBAL_SCOPE_STRING;
103 case PR_GLOBAL_BOUND_THREAD:
104 scope_str = GLOBAL_BOUND_SCOPE_STRING;
107 PR_ASSERT(!"Invalid thread scope");
110 printf("thread id %d, scope %s\n", info->id, scope_str);
112 listenSock = PR_NewTCPSocket();
115 printf("unable to create listen socket\n");
120 listenAddr.inet.family = PR_AF_INET;
121 listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
122 listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
123 rv = PR_Bind(listenSock, &listenAddr);
124 if (rv == PR_FAILURE) {
126 printf("unable to bind\n");
131 rv = PR_Listen(listenSock, 4);
132 if (rv == PR_FAILURE) {
134 printf("unable to listen\n");
140 printf("thread %d going into accept for %d seconds\n",
141 info->id, info->accept_timeout + info->id);
143 clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
145 if (clientSock == NULL) {
146 if (PR_GetError() == PR_IO_TIMEOUT_ERROR) {
148 printf("PR_Accept() timeout worked!\n");
149 printf("TEST PASSED! PR_Accept() returned error %d\n",
150 PR_IO_TIMEOUT_ERROR);
154 printf("TEST FAILED! PR_Accept() returned error %d\n",
160 printf ("TEST FAILED! PR_Accept() succeeded?\n");
162 PR_Close(clientSock);
167 PR_Close(listenSock);
169 PR_Lock(info->dead_lock);
171 PR_NotifyCondVar(info->dead_cv);
172 PR_Unlock(info->dead_lock);
175 printf("thread %d is dead\n", info->id);
181 thread_test(PRThreadScope scope, PRInt32 num_threads)
190 printf("IO Timeout test started with %d threads\n", num_threads);
192 dead_lock = PR_NewLock();
193 dead_cv = PR_NewCondVar(dead_lock);
196 for (index = 0; index < num_threads; index++) {
197 threadInfo *info = (threadInfo *)PR_Malloc(sizeof(threadInfo));
200 info->dead_lock = dead_lock;
201 info->dead_cv = dead_cv;
202 info->alive = &alive;
203 info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
205 thr = PR_CreateThread( PR_USER_THREAD,
210 PR_UNJOINABLE_THREAD,
214 printf("Failed to create thread, error = %d(%d)\n",
215 PR_GetError(), PR_GetOSError());
220 PR_Unlock(dead_lock);
227 printf("main loop awake; alive = %d\n", alive);
228 PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
230 PR_Unlock(dead_lock);
232 PR_DestroyCondVar(dead_cv);
233 PR_DestroyLock(dead_lock);
236 int main(int argc, char **argv)
238 PRInt32 num_threads = 0;
240 /* The command line argument: -d is used to determine if the test is being run
241 in debug mode. The regress tool requires only one line output:PASS or FAIL.
242 All of the printfs associated with this test has been handled with a if (debug_mode)
244 Usage: test_name [-d] [-t <threads>]
247 PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
248 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
250 if (PL_OPT_BAD == os) continue;
253 case 'd': /* debug mode */
256 case 't': /* threads to involve */
257 num_threads = atoi(opt->value);
263 PL_DestroyOptState(opt);
267 if (0 == num_threads)
268 num_threads = NUM_THREADS;
270 PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
273 printf("test with global bound thread\n");
274 thread_test(PR_GLOBAL_BOUND_THREAD, num_threads);
276 printf("test with local thread\n");
277 thread_test(PR_LOCAL_THREAD, num_threads);
279 printf("test with global thread\n");
280 thread_test(PR_GLOBAL_THREAD, num_threads);