nsprpub/pr/tests/io_timeout.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** Test socket IO timeouts
michael@0 8 **
michael@0 9 **
michael@0 10 **
michael@0 11 **
michael@0 12 ** Modification History:
michael@0 13 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 14 ** The debug mode will print all of the printfs associated with this test.
michael@0 15 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 16 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 17 ** have been handled with an if (debug_mode) statement.
michael@0 18 ***********************************************************************/
michael@0 19 /***********************************************************************
michael@0 20 ** Includes
michael@0 21 ***********************************************************************/
michael@0 22 /* Used to get the command line option */
michael@0 23 #include "plgetopt.h"
michael@0 24
michael@0 25 #include <stdio.h>
michael@0 26 #include "nspr.h"
michael@0 27
michael@0 28 #define NUM_THREADS 1
michael@0 29 #define BASE_PORT 8000
michael@0 30 #define DEFAULT_ACCEPT_TIMEOUT 2
michael@0 31
michael@0 32 typedef struct threadInfo {
michael@0 33 PRInt16 id;
michael@0 34 PRInt16 accept_timeout;
michael@0 35 PRLock *dead_lock;
michael@0 36 PRCondVar *dead_cv;
michael@0 37 PRInt32 *alive;
michael@0 38 } threadInfo;
michael@0 39
michael@0 40 PRIntn failed_already = 0;
michael@0 41 PRIntn debug_mode = 0;
michael@0 42
michael@0 43 #define LOCAL_SCOPE_STRING "LOCAL scope"
michael@0 44 #define GLOBAL_SCOPE_STRING "GLOBAL scope"
michael@0 45 #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope"
michael@0 46
michael@0 47 void
michael@0 48 thread_main(void *_info)
michael@0 49 {
michael@0 50 threadInfo *info = (threadInfo *)_info;
michael@0 51 PRNetAddr listenAddr;
michael@0 52 PRNetAddr clientAddr;
michael@0 53 PRFileDesc *listenSock = NULL;
michael@0 54 PRFileDesc *clientSock;
michael@0 55 PRStatus rv;
michael@0 56 PRThreadScope tscope;
michael@0 57 char *scope_str;
michael@0 58
michael@0 59
michael@0 60 if (debug_mode)
michael@0 61 printf("thread %d is alive\n", info->id);
michael@0 62 tscope = PR_GetThreadScope(PR_GetCurrentThread());
michael@0 63
michael@0 64 switch(tscope) {
michael@0 65 case PR_LOCAL_THREAD:
michael@0 66 scope_str = LOCAL_SCOPE_STRING;
michael@0 67 break;
michael@0 68 case PR_GLOBAL_THREAD:
michael@0 69 scope_str = GLOBAL_SCOPE_STRING;
michael@0 70 break;
michael@0 71 case PR_GLOBAL_BOUND_THREAD:
michael@0 72 scope_str = GLOBAL_BOUND_SCOPE_STRING;
michael@0 73 break;
michael@0 74 default:
michael@0 75 PR_ASSERT(!"Invalid thread scope");
michael@0 76 break;
michael@0 77 }
michael@0 78 printf("thread id %d, scope %s\n", info->id, scope_str);
michael@0 79
michael@0 80 listenSock = PR_NewTCPSocket();
michael@0 81 if (!listenSock) {
michael@0 82 if (debug_mode)
michael@0 83 printf("unable to create listen socket\n");
michael@0 84 failed_already=1;
michael@0 85 goto dead;
michael@0 86 }
michael@0 87
michael@0 88 listenAddr.inet.family = PR_AF_INET;
michael@0 89 listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
michael@0 90 listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
michael@0 91 rv = PR_Bind(listenSock, &listenAddr);
michael@0 92 if (rv == PR_FAILURE) {
michael@0 93 if (debug_mode)
michael@0 94 printf("unable to bind\n");
michael@0 95 failed_already=1;
michael@0 96 goto dead;
michael@0 97 }
michael@0 98
michael@0 99 rv = PR_Listen(listenSock, 4);
michael@0 100 if (rv == PR_FAILURE) {
michael@0 101 if (debug_mode)
michael@0 102 printf("unable to listen\n");
michael@0 103 failed_already=1;
michael@0 104 goto dead;
michael@0 105 }
michael@0 106
michael@0 107 if (debug_mode)
michael@0 108 printf("thread %d going into accept for %d seconds\n",
michael@0 109 info->id, info->accept_timeout + info->id);
michael@0 110
michael@0 111 clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
michael@0 112
michael@0 113 if (clientSock == NULL) {
michael@0 114 if (PR_GetError() == PR_IO_TIMEOUT_ERROR) {
michael@0 115 if (debug_mode) {
michael@0 116 printf("PR_Accept() timeout worked!\n");
michael@0 117 printf("TEST PASSED! PR_Accept() returned error %d\n",
michael@0 118 PR_IO_TIMEOUT_ERROR);
michael@0 119 }
michael@0 120 } else {
michael@0 121 if (debug_mode)
michael@0 122 printf("TEST FAILED! PR_Accept() returned error %d\n",
michael@0 123 PR_GetError());
michael@0 124 failed_already=1;
michael@0 125 }
michael@0 126 } else {
michael@0 127 if (debug_mode)
michael@0 128 printf ("TEST FAILED! PR_Accept() succeeded?\n");
michael@0 129 failed_already=1;
michael@0 130 PR_Close(clientSock);
michael@0 131 }
michael@0 132
michael@0 133 dead:
michael@0 134 if (listenSock) {
michael@0 135 PR_Close(listenSock);
michael@0 136 }
michael@0 137 PR_Lock(info->dead_lock);
michael@0 138 (*info->alive)--;
michael@0 139 PR_NotifyCondVar(info->dead_cv);
michael@0 140 PR_Unlock(info->dead_lock);
michael@0 141
michael@0 142 if (debug_mode)
michael@0 143 printf("thread %d is dead\n", info->id);
michael@0 144
michael@0 145 PR_Free(info);
michael@0 146 }
michael@0 147
michael@0 148 void
michael@0 149 thread_test(PRThreadScope scope, PRInt32 num_threads)
michael@0 150 {
michael@0 151 PRInt32 index;
michael@0 152 PRThread *thr;
michael@0 153 PRLock *dead_lock;
michael@0 154 PRCondVar *dead_cv;
michael@0 155 PRInt32 alive;
michael@0 156
michael@0 157 if (debug_mode)
michael@0 158 printf("IO Timeout test started with %d threads\n", num_threads);
michael@0 159
michael@0 160 dead_lock = PR_NewLock();
michael@0 161 dead_cv = PR_NewCondVar(dead_lock);
michael@0 162 alive = num_threads;
michael@0 163
michael@0 164 for (index = 0; index < num_threads; index++) {
michael@0 165 threadInfo *info = (threadInfo *)PR_Malloc(sizeof(threadInfo));
michael@0 166
michael@0 167 info->id = index;
michael@0 168 info->dead_lock = dead_lock;
michael@0 169 info->dead_cv = dead_cv;
michael@0 170 info->alive = &alive;
michael@0 171 info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
michael@0 172
michael@0 173 thr = PR_CreateThread( PR_USER_THREAD,
michael@0 174 thread_main,
michael@0 175 (void *)info,
michael@0 176 PR_PRIORITY_NORMAL,
michael@0 177 scope,
michael@0 178 PR_UNJOINABLE_THREAD,
michael@0 179 0);
michael@0 180
michael@0 181 if (!thr) {
michael@0 182 printf("Failed to create thread, error = %d(%d)\n",
michael@0 183 PR_GetError(), PR_GetOSError());
michael@0 184 failed_already=1;
michael@0 185
michael@0 186 PR_Lock(dead_lock);
michael@0 187 alive--;
michael@0 188 PR_Unlock(dead_lock);
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 PR_Lock(dead_lock);
michael@0 193 while(alive) {
michael@0 194 if (debug_mode)
michael@0 195 printf("main loop awake; alive = %d\n", alive);
michael@0 196 PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
michael@0 197 }
michael@0 198 PR_Unlock(dead_lock);
michael@0 199
michael@0 200 PR_DestroyCondVar(dead_cv);
michael@0 201 PR_DestroyLock(dead_lock);
michael@0 202 }
michael@0 203
michael@0 204 int main(int argc, char **argv)
michael@0 205 {
michael@0 206 PRInt32 num_threads = 0;
michael@0 207
michael@0 208 /* The command line argument: -d is used to determine if the test is being run
michael@0 209 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 210 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 211 test.
michael@0 212 Usage: test_name [-d] [-t <threads>]
michael@0 213 */
michael@0 214 PLOptStatus os;
michael@0 215 PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
michael@0 216 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 217 {
michael@0 218 if (PL_OPT_BAD == os) continue;
michael@0 219 switch (opt->option)
michael@0 220 {
michael@0 221 case 'd': /* debug mode */
michael@0 222 debug_mode = 1;
michael@0 223 break;
michael@0 224 case 't': /* threads to involve */
michael@0 225 num_threads = atoi(opt->value);
michael@0 226 break;
michael@0 227 default:
michael@0 228 break;
michael@0 229 }
michael@0 230 }
michael@0 231 PL_DestroyOptState(opt);
michael@0 232
michael@0 233 /* main test */
michael@0 234
michael@0 235 if (0 == num_threads)
michael@0 236 num_threads = NUM_THREADS;
michael@0 237
michael@0 238 PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
michael@0 239 PR_STDIO_INIT();
michael@0 240
michael@0 241 printf("test with global bound thread\n");
michael@0 242 thread_test(PR_GLOBAL_BOUND_THREAD, num_threads);
michael@0 243
michael@0 244 printf("test with local thread\n");
michael@0 245 thread_test(PR_LOCAL_THREAD, num_threads);
michael@0 246
michael@0 247 printf("test with global thread\n");
michael@0 248 thread_test(PR_GLOBAL_THREAD, num_threads);
michael@0 249
michael@0 250 PR_Cleanup();
michael@0 251
michael@0 252 if (failed_already)
michael@0 253 return 1;
michael@0 254 else
michael@0 255 return 0;
michael@0 256 }

mercurial