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