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

mercurial