Back to index

lightning-sunbird  0.9+nobinonly
io_timeoutu.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 /*
00040 ** name io_timeoutu.c
00041 ** Description: Test socket IO timeouts (user level)
00042 **
00043 ** Modification History:
00044 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00045 **              The debug mode will print all of the printfs associated with this test.
00046 **                    The regress mode will be the default mode. Since the regress tool limits
00047 **           the output to a one line status:PASS or FAIL,all of the printf statements
00048 **                    have been handled with an if (debug_mode) statement.
00049 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00050 **                   recognize the return code from tha main program.
00051 ***********************************************************************/
00052 /***********************************************************************
00053 ** Includes
00054 ***********************************************************************/
00055 /* Used to get the command line option */
00056 #include "plgetopt.h"
00057 
00058 #include <stdio.h>
00059 #include "nspr.h"
00060 
00061 #define NUM_THREADS 1
00062 #define BASE_PORT   8000
00063 #define DEFAULT_ACCEPT_TIMEOUT 2
00064 
00065 typedef struct threadInfo {
00066     PRInt16 id;
00067     PRInt16 accept_timeout;
00068     PRLock *dead_lock;
00069     PRCondVar *dead_cv;
00070     PRInt32   *alive;
00071 } threadInfo;
00072 PRIntn failed_already=0;
00073 PRIntn debug_mode;
00074 
00075 void 
00076 thread_main(void *_info)
00077 {
00078     threadInfo *info = (threadInfo *)_info;
00079     PRNetAddr listenAddr;
00080     PRNetAddr clientAddr;
00081     PRFileDesc *listenSock = NULL;
00082     PRFileDesc *clientSock;
00083     PRStatus rv;
00084  
00085     if (debug_mode) printf("thread %d is alive\n", info->id);
00086 
00087     listenSock = PR_NewTCPSocket();
00088     if (!listenSock) {
00089         if (debug_mode) printf("unable to create listen socket\n");
00090         goto dead;
00091     }
00092   
00093     listenAddr.inet.family = AF_INET;
00094     listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
00095     listenAddr.inet.ip = PR_htonl(INADDR_ANY);
00096     rv = PR_Bind(listenSock, &listenAddr);
00097     if (rv == PR_FAILURE) {
00098         if (debug_mode) printf("unable to bind\n");
00099         goto dead;
00100     }
00101 
00102     rv = PR_Listen(listenSock, 4);
00103     if (rv == PR_FAILURE) {
00104         if (debug_mode) printf("unable to listen\n");
00105         goto dead;
00106     }
00107 
00108     if (debug_mode) printf("thread %d going into accept for %d seconds\n", 
00109         info->id, info->accept_timeout + info->id);
00110 
00111     clientSock = PR_Accept(
00112               listenSock, &clientAddr, PR_SecondsToInterval(
00113                      info->accept_timeout + info->id));
00114 
00115     if (clientSock == NULL) {
00116         if (PR_GetError() == PR_IO_TIMEOUT_ERROR) 
00117             if (debug_mode) {
00118                             printf("PR_Accept() timeout worked!\n");
00119                 printf("TEST FAILED! PR_Accept() returned error %d\n",
00120                      }
00121                                                            PR_GetError());
00122                      else failed_already=1;
00123     } else {
00124         if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
00125               else failed_already=1;
00126        PR_Close(clientSock);
00127     }
00128 
00129 dead:
00130     if (listenSock) {
00131        PR_Close(listenSock);
00132     }
00133     PR_Lock(info->dead_lock);
00134     (*info->alive)--;
00135     PR_NotifyCondVar(info->dead_cv);
00136     PR_Unlock(info->dead_lock);
00137 
00138     if (debug_mode) printf("thread %d is dead\n", info->id);
00139 }
00140 
00141 void
00142 thread_test(PRInt32 scope, PRInt32 num_threads)
00143 {
00144     PRInt32 index;
00145     PRThread *thr;
00146     PRLock *dead_lock;
00147     PRCondVar *dead_cv;
00148     PRInt32 alive;
00149 
00150     if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
00151 
00152     dead_lock = PR_NewLock();
00153     dead_cv = PR_NewCondVar(dead_lock);
00154     alive = num_threads;
00155     
00156     for (index = 0; index < num_threads; index++) {
00157         threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
00158 
00159         info->id = index;
00160         info->dead_lock = dead_lock;
00161         info->dead_cv = dead_cv;
00162         info->alive = &alive;
00163         info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
00164         
00165         thr = PR_CreateThread( PR_USER_THREAD,
00166                                thread_main,
00167                                (void *)info,
00168                                PR_PRIORITY_NORMAL,
00169                                scope,
00170                                PR_UNJOINABLE_THREAD,
00171                                0);
00172 
00173         if (!thr) {
00174             PR_Lock(dead_lock);
00175             alive--;
00176             PR_Unlock(dead_lock);
00177         }
00178     }
00179 
00180     PR_Lock(dead_lock);
00181     while(alive) {
00182         if (debug_mode) printf("main loop awake; alive = %d\n", alive);
00183         PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
00184     }
00185     PR_Unlock(dead_lock);
00186 }
00187 
00188 int main(int argc, char **argv)
00189 {
00190     PRInt32 num_threads;
00191 
00192        /* The command line argument: -d is used to determine if the test is being run
00193        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00194        All of the printfs associated with this test has been handled with a if (debug_mode)
00195        test.
00196        Usage: test_name -d
00197        */
00198        PLOptStatus os;
00199        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00200        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00201     {
00202               if (PL_OPT_BAD == os) continue;
00203         switch (opt->option)
00204         {
00205         case 'd':  /* debug mode */
00206                      debug_mode = 1;
00207             break;
00208          default:
00209             break;
00210         }
00211     }
00212        PL_DestroyOptState(opt);
00213 
00214  /* main test */
00215        
00216     if (argc > 2)
00217         num_threads = atoi(argv[2]);
00218     else
00219         num_threads = NUM_THREADS;
00220 
00221     PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
00222     PR_STDIO_INIT();
00223 
00224     if (debug_mode) printf("user level test\n");
00225     thread_test(PR_LOCAL_THREAD, num_threads);
00226 
00227      PR_Cleanup();
00228      if(failed_already)    
00229         return 1;
00230      else
00231         return 0;
00232      
00233 
00234 }