Back to index

lightning-sunbird  0.9+nobinonly
io_timeoutk.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** name io_timeoutk.c
00040 ** Description:Test socket IO timeouts (kernel level)
00041 **
00042 ** Modification History:
00043 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00044 **              The debug mode will print all of the printfs associated with this test.
00045 **                    The regress mode will be the default mode. Since the regress tool limits
00046 **           the output to a one line status:PASS or FAIL,all of the printf statements
00047 **                    have been handled with an if (debug_mode) statement.
00048 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00049 **                   recognize the return code from tha main program.
00050 ***********************************************************************/
00051 /***********************************************************************
00052 ** Includes
00053 ***********************************************************************/
00054 /* Used to get the command line option */
00055 #include "plgetopt.h"
00056 
00057 #include <stdio.h>
00058 #include "nspr.h"
00059 
00060 #define NUM_THREADS 1
00061 #define BASE_PORT   8000
00062 #define DEFAULT_ACCEPT_TIMEOUT 2
00063 
00064 typedef struct threadInfo {
00065     PRInt16 id;
00066     PRInt16 accept_timeout;
00067     PRLock *dead_lock;
00068     PRCondVar *dead_cv;
00069     PRInt32   *alive;
00070 } threadInfo;
00071 
00072 PRIntn failed_already=0;
00073 PRIntn debug_mode;
00074 
00075 
00076 void 
00077 thread_main(void *_info)
00078 {
00079     threadInfo *info = (threadInfo *)_info;
00080     PRNetAddr listenAddr;
00081     PRNetAddr clientAddr;
00082     PRFileDesc *listenSock = NULL;
00083     PRFileDesc *clientSock;
00084     PRStatus rv;
00085  
00086     if (debug_mode) printf("thread %d is alive\n", info->id);
00087 
00088     listenSock = PR_NewTCPSocket();
00089     if (!listenSock) {
00090         if (debug_mode) printf("unable to create listen socket\n");
00091         goto dead;
00092     }
00093   
00094     listenAddr.inet.family = AF_INET;
00095     listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
00096     listenAddr.inet.ip = PR_htonl(INADDR_ANY);
00097     rv = PR_Bind(listenSock, &listenAddr);
00098     if (rv == PR_FAILURE) {
00099         if (debug_mode) printf("unable to bind\n");
00100         goto dead;
00101     }
00102 
00103     rv = PR_Listen(listenSock, 4);
00104     if (rv == PR_FAILURE) {
00105         if (debug_mode) printf("unable to listen\n");
00106         goto dead;
00107     }
00108 
00109     if (debug_mode) printf("thread %d going into accept for %d seconds\n", 
00110         info->id, info->accept_timeout + info->id);
00111 
00112     clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
00113 
00114     if (clientSock == NULL) {
00115         if (PR_GetError() == PR_IO_TIMEOUT_ERROR) 
00116             if (debug_mode) {
00117                             printf("PR_Accept() timeout worked!\n");
00118                 printf("TEST FAILED! PR_Accept() returned error %d\n",
00119                                                            PR_GetError());
00120                      }
00121                      else failed_already=1;
00122     } else {
00123         if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
00124               else failed_already=1;
00125        PR_Close(clientSock);
00126     }
00127 
00128 dead:
00129     if (listenSock) {
00130        PR_Close(listenSock);
00131     }
00132     PR_Lock(info->dead_lock);
00133     (*info->alive)--;
00134     PR_NotifyCondVar(info->dead_cv);
00135     PR_Unlock(info->dead_lock);
00136 
00137     if (debug_mode) printf("thread %d is dead\n", info->id);
00138 }
00139 
00140 void
00141 thread_test(PRInt32 scope, PRInt32 num_threads)
00142 {
00143     PRInt32 index;
00144     PRThread *thr;
00145     PRLock *dead_lock;
00146     PRCondVar *dead_cv;
00147     PRInt32 alive;
00148 
00149     if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
00150 
00151     dead_lock = PR_NewLock();
00152     dead_cv = PR_NewCondVar(dead_lock);
00153     alive = num_threads;
00154     
00155     for (index = 0; index < num_threads; index++) {
00156         threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
00157 
00158         info->id = index;
00159         info->dead_lock = dead_lock;
00160         info->dead_cv = dead_cv;
00161         info->alive = &alive;
00162         info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
00163         
00164         thr = PR_CreateThread( PR_USER_THREAD,
00165                                thread_main,
00166                                (void *)info,
00167                                PR_PRIORITY_NORMAL,
00168                                scope,
00169                                PR_UNJOINABLE_THREAD,
00170                                0);
00171 
00172         if (!thr) {
00173             PR_Lock(dead_lock);
00174             alive--;
00175             PR_Unlock(dead_lock);
00176         }
00177     }
00178 
00179     PR_Lock(dead_lock);
00180     while(alive) {
00181         if (debug_mode) printf("main loop awake; alive = %d\n", alive);
00182         PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
00183     }
00184     PR_Unlock(dead_lock);
00185 }
00186 
00187 int main(int argc, char **argv)
00188 {
00189     PRInt32 num_threads;
00190 
00191        /* The command line argument: -d is used to determine if the test is being run
00192        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00193        All of the printfs associated with this test has been handled with a if (debug_mode)
00194        test.
00195        Usage: test_name -d
00196        */
00197        PLOptStatus os;
00198        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00199        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00200     {
00201               if (PL_OPT_BAD == os) continue;
00202         switch (opt->option)
00203         {
00204         case 'd':  /* debug mode */
00205                      debug_mode = 1;
00206             break;
00207          default:
00208             break;
00209         }
00210     }
00211        PL_DestroyOptState(opt);
00212 
00213  /* main test */
00214        
00215     if (argc > 2)
00216         num_threads = atoi(argv[2]);
00217     else
00218         num_threads = NUM_THREADS;
00219 
00220     PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
00221     PR_STDIO_INIT();
00222 
00223     if (debug_mode)     printf("kernel level test\n");
00224     thread_test(PR_GLOBAL_THREAD, num_threads);
00225 
00226      PR_Cleanup();
00227      
00228      if(failed_already)    
00229         return 1;
00230     else
00231         return 0;
00232 
00233 }