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.

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

mercurial