nsprpub/pr/tests/io_timeoutk.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 ** name io_timeoutk.c
michael@0 8 ** Description:Test socket IO timeouts (kernel level)
michael@0 9 **
michael@0 10 ** Modification History:
michael@0 11 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
michael@0 12 ** The debug mode will print all of the printfs associated with this test.
michael@0 13 ** The regress mode will be the default mode. Since the regress tool limits
michael@0 14 ** the output to a one line status:PASS or FAIL,all of the printf statements
michael@0 15 ** have been handled with an if (debug_mode) statement.
michael@0 16 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
michael@0 17 ** recognize the return code from tha main program.
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;
michael@0 42
michael@0 43
michael@0 44 void
michael@0 45 thread_main(void *_info)
michael@0 46 {
michael@0 47 threadInfo *info = (threadInfo *)_info;
michael@0 48 PRNetAddr listenAddr;
michael@0 49 PRNetAddr clientAddr;
michael@0 50 PRFileDesc *listenSock = NULL;
michael@0 51 PRFileDesc *clientSock;
michael@0 52 PRStatus rv;
michael@0 53
michael@0 54 if (debug_mode) printf("thread %d is alive\n", info->id);
michael@0 55
michael@0 56 listenSock = PR_NewTCPSocket();
michael@0 57 if (!listenSock) {
michael@0 58 if (debug_mode) printf("unable to create listen socket\n");
michael@0 59 goto dead;
michael@0 60 }
michael@0 61
michael@0 62 listenAddr.inet.family = AF_INET;
michael@0 63 listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
michael@0 64 listenAddr.inet.ip = PR_htonl(INADDR_ANY);
michael@0 65 rv = PR_Bind(listenSock, &listenAddr);
michael@0 66 if (rv == PR_FAILURE) {
michael@0 67 if (debug_mode) printf("unable to bind\n");
michael@0 68 goto dead;
michael@0 69 }
michael@0 70
michael@0 71 rv = PR_Listen(listenSock, 4);
michael@0 72 if (rv == PR_FAILURE) {
michael@0 73 if (debug_mode) printf("unable to listen\n");
michael@0 74 goto dead;
michael@0 75 }
michael@0 76
michael@0 77 if (debug_mode) printf("thread %d going into accept for %d seconds\n",
michael@0 78 info->id, info->accept_timeout + info->id);
michael@0 79
michael@0 80 clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
michael@0 81
michael@0 82 if (clientSock == NULL) {
michael@0 83 if (PR_GetError() == PR_IO_TIMEOUT_ERROR)
michael@0 84 if (debug_mode) {
michael@0 85 printf("PR_Accept() timeout worked!\n");
michael@0 86 printf("TEST FAILED! PR_Accept() returned error %d\n",
michael@0 87 PR_GetError());
michael@0 88 }
michael@0 89 else failed_already=1;
michael@0 90 } else {
michael@0 91 if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
michael@0 92 else failed_already=1;
michael@0 93 PR_Close(clientSock);
michael@0 94 }
michael@0 95
michael@0 96 dead:
michael@0 97 if (listenSock) {
michael@0 98 PR_Close(listenSock);
michael@0 99 }
michael@0 100 PR_Lock(info->dead_lock);
michael@0 101 (*info->alive)--;
michael@0 102 PR_NotifyCondVar(info->dead_cv);
michael@0 103 PR_Unlock(info->dead_lock);
michael@0 104
michael@0 105 if (debug_mode) printf("thread %d is dead\n", info->id);
michael@0 106 }
michael@0 107
michael@0 108 void
michael@0 109 thread_test(PRInt32 scope, PRInt32 num_threads)
michael@0 110 {
michael@0 111 PRInt32 index;
michael@0 112 PRThread *thr;
michael@0 113 PRLock *dead_lock;
michael@0 114 PRCondVar *dead_cv;
michael@0 115 PRInt32 alive;
michael@0 116
michael@0 117 if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
michael@0 118
michael@0 119 dead_lock = PR_NewLock();
michael@0 120 dead_cv = PR_NewCondVar(dead_lock);
michael@0 121 alive = num_threads;
michael@0 122
michael@0 123 for (index = 0; index < num_threads; index++) {
michael@0 124 threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
michael@0 125
michael@0 126 info->id = index;
michael@0 127 info->dead_lock = dead_lock;
michael@0 128 info->dead_cv = dead_cv;
michael@0 129 info->alive = &alive;
michael@0 130 info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
michael@0 131
michael@0 132 thr = PR_CreateThread( PR_USER_THREAD,
michael@0 133 thread_main,
michael@0 134 (void *)info,
michael@0 135 PR_PRIORITY_NORMAL,
michael@0 136 scope,
michael@0 137 PR_UNJOINABLE_THREAD,
michael@0 138 0);
michael@0 139
michael@0 140 if (!thr) {
michael@0 141 PR_Lock(dead_lock);
michael@0 142 alive--;
michael@0 143 PR_Unlock(dead_lock);
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 PR_Lock(dead_lock);
michael@0 148 while(alive) {
michael@0 149 if (debug_mode) printf("main loop awake; alive = %d\n", alive);
michael@0 150 PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
michael@0 151 }
michael@0 152 PR_Unlock(dead_lock);
michael@0 153 }
michael@0 154
michael@0 155 int main(int argc, char **argv)
michael@0 156 {
michael@0 157 PRInt32 num_threads;
michael@0 158
michael@0 159 /* The command line argument: -d is used to determine if the test is being run
michael@0 160 in debug mode. The regress tool requires only one line output:PASS or FAIL.
michael@0 161 All of the printfs associated with this test has been handled with a if (debug_mode)
michael@0 162 test.
michael@0 163 Usage: test_name -d
michael@0 164 */
michael@0 165 PLOptStatus os;
michael@0 166 PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
michael@0 167 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 168 {
michael@0 169 if (PL_OPT_BAD == os) continue;
michael@0 170 switch (opt->option)
michael@0 171 {
michael@0 172 case 'd': /* debug mode */
michael@0 173 debug_mode = 1;
michael@0 174 break;
michael@0 175 default:
michael@0 176 break;
michael@0 177 }
michael@0 178 }
michael@0 179 PL_DestroyOptState(opt);
michael@0 180
michael@0 181 /* main test */
michael@0 182
michael@0 183 if (argc > 2)
michael@0 184 num_threads = atoi(argv[2]);
michael@0 185 else
michael@0 186 num_threads = NUM_THREADS;
michael@0 187
michael@0 188 PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
michael@0 189 PR_STDIO_INIT();
michael@0 190
michael@0 191 if (debug_mode) printf("kernel level test\n");
michael@0 192 thread_test(PR_GLOBAL_THREAD, num_threads);
michael@0 193
michael@0 194 PR_Cleanup();
michael@0 195
michael@0 196 if(failed_already)
michael@0 197 return 1;
michael@0 198 else
michael@0 199 return 0;
michael@0 200
michael@0 201 }

mercurial