1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/io_timeoutu.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,202 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 + 1.10 +/* 1.11 +** name io_timeoutu.c 1.12 +** Description: Test socket IO timeouts (user level) 1.13 +** 1.14 +** Modification History: 1.15 +** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. 1.16 +** The debug mode will print all of the printfs associated with this test. 1.17 +** The regress mode will be the default mode. Since the regress tool limits 1.18 +** the output to a one line status:PASS or FAIL,all of the printf statements 1.19 +** have been handled with an if (debug_mode) statement. 1.20 +** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to 1.21 +** recognize the return code from tha main program. 1.22 +***********************************************************************/ 1.23 +/*********************************************************************** 1.24 +** Includes 1.25 +***********************************************************************/ 1.26 +/* Used to get the command line option */ 1.27 +#include "plgetopt.h" 1.28 + 1.29 +#include <stdio.h> 1.30 +#include "nspr.h" 1.31 + 1.32 +#define NUM_THREADS 1 1.33 +#define BASE_PORT 8000 1.34 +#define DEFAULT_ACCEPT_TIMEOUT 2 1.35 + 1.36 +typedef struct threadInfo { 1.37 + PRInt16 id; 1.38 + PRInt16 accept_timeout; 1.39 + PRLock *dead_lock; 1.40 + PRCondVar *dead_cv; 1.41 + PRInt32 *alive; 1.42 +} threadInfo; 1.43 +PRIntn failed_already=0; 1.44 +PRIntn debug_mode; 1.45 + 1.46 +void 1.47 +thread_main(void *_info) 1.48 +{ 1.49 + threadInfo *info = (threadInfo *)_info; 1.50 + PRNetAddr listenAddr; 1.51 + PRNetAddr clientAddr; 1.52 + PRFileDesc *listenSock = NULL; 1.53 + PRFileDesc *clientSock; 1.54 + PRStatus rv; 1.55 + 1.56 + if (debug_mode) printf("thread %d is alive\n", info->id); 1.57 + 1.58 + listenSock = PR_NewTCPSocket(); 1.59 + if (!listenSock) { 1.60 + if (debug_mode) printf("unable to create listen socket\n"); 1.61 + goto dead; 1.62 + } 1.63 + 1.64 + listenAddr.inet.family = AF_INET; 1.65 + listenAddr.inet.port = PR_htons(BASE_PORT + info->id); 1.66 + listenAddr.inet.ip = PR_htonl(INADDR_ANY); 1.67 + rv = PR_Bind(listenSock, &listenAddr); 1.68 + if (rv == PR_FAILURE) { 1.69 + if (debug_mode) printf("unable to bind\n"); 1.70 + goto dead; 1.71 + } 1.72 + 1.73 + rv = PR_Listen(listenSock, 4); 1.74 + if (rv == PR_FAILURE) { 1.75 + if (debug_mode) printf("unable to listen\n"); 1.76 + goto dead; 1.77 + } 1.78 + 1.79 + if (debug_mode) printf("thread %d going into accept for %d seconds\n", 1.80 + info->id, info->accept_timeout + info->id); 1.81 + 1.82 + clientSock = PR_Accept( 1.83 + listenSock, &clientAddr, PR_SecondsToInterval( 1.84 + info->accept_timeout + info->id)); 1.85 + 1.86 + if (clientSock == NULL) { 1.87 + if (PR_GetError() == PR_IO_TIMEOUT_ERROR) 1.88 + if (debug_mode) { 1.89 + printf("PR_Accept() timeout worked!\n"); 1.90 + printf("TEST FAILED! PR_Accept() returned error %d\n", 1.91 + } 1.92 + PR_GetError()); 1.93 + else failed_already=1; 1.94 + } else { 1.95 + if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n"); 1.96 + else failed_already=1; 1.97 + PR_Close(clientSock); 1.98 + } 1.99 + 1.100 +dead: 1.101 + if (listenSock) { 1.102 + PR_Close(listenSock); 1.103 + } 1.104 + PR_Lock(info->dead_lock); 1.105 + (*info->alive)--; 1.106 + PR_NotifyCondVar(info->dead_cv); 1.107 + PR_Unlock(info->dead_lock); 1.108 + 1.109 + if (debug_mode) printf("thread %d is dead\n", info->id); 1.110 +} 1.111 + 1.112 +void 1.113 +thread_test(PRInt32 scope, PRInt32 num_threads) 1.114 +{ 1.115 + PRInt32 index; 1.116 + PRThread *thr; 1.117 + PRLock *dead_lock; 1.118 + PRCondVar *dead_cv; 1.119 + PRInt32 alive; 1.120 + 1.121 + if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads); 1.122 + 1.123 + dead_lock = PR_NewLock(); 1.124 + dead_cv = PR_NewCondVar(dead_lock); 1.125 + alive = num_threads; 1.126 + 1.127 + for (index = 0; index < num_threads; index++) { 1.128 + threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo)); 1.129 + 1.130 + info->id = index; 1.131 + info->dead_lock = dead_lock; 1.132 + info->dead_cv = dead_cv; 1.133 + info->alive = &alive; 1.134 + info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT; 1.135 + 1.136 + thr = PR_CreateThread( PR_USER_THREAD, 1.137 + thread_main, 1.138 + (void *)info, 1.139 + PR_PRIORITY_NORMAL, 1.140 + scope, 1.141 + PR_UNJOINABLE_THREAD, 1.142 + 0); 1.143 + 1.144 + if (!thr) { 1.145 + PR_Lock(dead_lock); 1.146 + alive--; 1.147 + PR_Unlock(dead_lock); 1.148 + } 1.149 + } 1.150 + 1.151 + PR_Lock(dead_lock); 1.152 + while(alive) { 1.153 + if (debug_mode) printf("main loop awake; alive = %d\n", alive); 1.154 + PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT); 1.155 + } 1.156 + PR_Unlock(dead_lock); 1.157 +} 1.158 + 1.159 +int main(int argc, char **argv) 1.160 +{ 1.161 + PRInt32 num_threads; 1.162 + 1.163 + /* The command line argument: -d is used to determine if the test is being run 1.164 + in debug mode. The regress tool requires only one line output:PASS or FAIL. 1.165 + All of the printfs associated with this test has been handled with a if (debug_mode) 1.166 + test. 1.167 + Usage: test_name -d 1.168 + */ 1.169 + PLOptStatus os; 1.170 + PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); 1.171 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.172 + { 1.173 + if (PL_OPT_BAD == os) continue; 1.174 + switch (opt->option) 1.175 + { 1.176 + case 'd': /* debug mode */ 1.177 + debug_mode = 1; 1.178 + break; 1.179 + default: 1.180 + break; 1.181 + } 1.182 + } 1.183 + PL_DestroyOptState(opt); 1.184 + 1.185 + /* main test */ 1.186 + 1.187 + if (argc > 2) 1.188 + num_threads = atoi(argv[2]); 1.189 + else 1.190 + num_threads = NUM_THREADS; 1.191 + 1.192 + PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0); 1.193 + PR_STDIO_INIT(); 1.194 + 1.195 + if (debug_mode) printf("user level test\n"); 1.196 + thread_test(PR_LOCAL_THREAD, num_threads); 1.197 + 1.198 + PR_Cleanup(); 1.199 + if(failed_already) 1.200 + return 1; 1.201 + else 1.202 + return 0; 1.203 + 1.204 + 1.205 +}