Back to index

lightning-sunbird  0.9+nobinonly
io_timeout.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 ** Test socket IO timeouts
00040 **
00041 **
00042 **
00043 **
00044 ** Modification History:
00045 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00046 **              The debug mode will print all of the printfs associated with this test.
00047 **                    The regress mode will be the default mode. Since the regress tool limits
00048 **           the output to a one line status:PASS or FAIL,all of the printf statements
00049 **                    have been handled with an if (debug_mode) statement. 
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 #ifdef XP_MAC
00061 #include "prlog.h"
00062 #define printf PR_LogPrint
00063 extern void SetupMacPrintfLog(char *logFile);
00064 #endif
00065 
00066 #define NUM_THREADS 1
00067 #define BASE_PORT   8000
00068 #define DEFAULT_ACCEPT_TIMEOUT 2
00069 
00070 typedef struct threadInfo {
00071     PRInt16 id;
00072     PRInt16 accept_timeout;
00073     PRLock *dead_lock;
00074     PRCondVar *dead_cv;
00075     PRInt32   *alive;
00076 } threadInfo;
00077 
00078 PRIntn failed_already = 0;
00079 PRIntn debug_mode = 0;
00080 
00081 #define       LOCAL_SCOPE_STRING                 "LOCAL scope"
00082 #define       GLOBAL_SCOPE_STRING                "GLOBAL scope"
00083 #define       GLOBAL_BOUND_SCOPE_STRING   "GLOBAL_BOUND scope"
00084 
00085 void 
00086 thread_main(void *_info)
00087 {
00088     threadInfo *info = (threadInfo *)_info;
00089     PRNetAddr listenAddr;
00090     PRNetAddr clientAddr;
00091     PRFileDesc *listenSock = NULL;
00092     PRFileDesc *clientSock;
00093     PRStatus rv;
00094        PRThreadScope tscope;
00095        char *scope_str;
00096 
00097  
00098        if (debug_mode)
00099        printf("thread %d is alive\n", info->id);
00100        tscope = PR_GetThreadScope(PR_GetCurrentThread());
00101 
00102        switch(tscope) {
00103               case PR_LOCAL_THREAD:
00104                      scope_str = LOCAL_SCOPE_STRING;
00105                      break;
00106               case PR_GLOBAL_THREAD:
00107                      scope_str = GLOBAL_SCOPE_STRING;
00108                      break;
00109               case PR_GLOBAL_BOUND_THREAD:
00110                      scope_str = GLOBAL_BOUND_SCOPE_STRING;
00111                      break;
00112               default:
00113                      PR_ASSERT(!"Invalid thread scope");
00114                      break;
00115        }
00116        printf("thread id %d, scope %s\n", info->id, scope_str);
00117 
00118     listenSock = PR_NewTCPSocket();
00119     if (!listenSock) {
00120               if (debug_mode)
00121               printf("unable to create listen socket\n");
00122               failed_already=1;
00123         goto dead;
00124     }
00125   
00126     listenAddr.inet.family = PR_AF_INET;
00127     listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
00128     listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
00129     rv = PR_Bind(listenSock, &listenAddr);
00130     if (rv == PR_FAILURE) {
00131               if (debug_mode)
00132               printf("unable to bind\n");
00133               failed_already=1;
00134         goto dead;
00135     }
00136 
00137     rv = PR_Listen(listenSock, 4);
00138     if (rv == PR_FAILURE) {
00139               if (debug_mode)
00140               printf("unable to listen\n");
00141               failed_already=1;
00142         goto dead;
00143     }
00144 
00145        if (debug_mode)
00146        printf("thread %d going into accept for %d seconds\n", 
00147               info->id, info->accept_timeout + info->id);
00148 
00149     clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
00150 
00151     if (clientSock == NULL) {
00152         if (PR_GetError() == PR_IO_TIMEOUT_ERROR) {
00153                      if (debug_mode) {    
00154               printf("PR_Accept() timeout worked!\n"); 
00155                             printf("TEST PASSED! PR_Accept() returned error %d\n",
00156                                                  PR_IO_TIMEOUT_ERROR);
00157                      }
00158        } else {
00159                      if (debug_mode)
00160               printf("TEST FAILED! PR_Accept() returned error %d\n",
00161                                                                                                   PR_GetError());
00162                      failed_already=1;
00163               }
00164     } else {
00165               if (debug_mode)
00166               printf ("TEST FAILED! PR_Accept() succeeded?\n");
00167               failed_already=1;
00168               PR_Close(clientSock);
00169     }
00170 
00171 dead:
00172     if (listenSock) {
00173               PR_Close(listenSock);
00174     }
00175     PR_Lock(info->dead_lock);
00176     (*info->alive)--;
00177     PR_NotifyCondVar(info->dead_cv);
00178     PR_Unlock(info->dead_lock);
00179 
00180        if (debug_mode)
00181        printf("thread %d is dead\n", info->id);
00182 
00183     PR_Free(info);
00184 }
00185 
00186 void
00187 thread_test(PRThreadScope scope, PRInt32 num_threads)
00188 {
00189     PRInt32 index;
00190     PRThread *thr;
00191     PRLock *dead_lock;
00192     PRCondVar *dead_cv;
00193     PRInt32 alive;
00194 
00195        if (debug_mode)
00196        printf("IO Timeout test started with %d threads\n", num_threads);
00197 
00198     dead_lock = PR_NewLock();
00199     dead_cv = PR_NewCondVar(dead_lock);
00200     alive = num_threads;
00201     
00202     for (index = 0; index < num_threads; index++) {
00203         threadInfo *info = (threadInfo *)PR_Malloc(sizeof(threadInfo));
00204 
00205         info->id = index;
00206         info->dead_lock = dead_lock;
00207         info->dead_cv = dead_cv;
00208         info->alive = &alive;
00209         info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
00210         
00211         thr = PR_CreateThread( PR_USER_THREAD,
00212                                thread_main,
00213                                (void *)info,
00214                                PR_PRIORITY_NORMAL,
00215                                scope,
00216                                PR_UNJOINABLE_THREAD,
00217                                0);
00218 
00219         if (!thr) {
00220               printf("Failed to create thread, error = %d(%d)\n",
00221                                                                              PR_GetError(), PR_GetOSError());
00222                      failed_already=1;
00223 
00224             PR_Lock(dead_lock);
00225             alive--;
00226             PR_Unlock(dead_lock);
00227         }
00228     }
00229 
00230     PR_Lock(dead_lock);
00231     while(alive) {
00232               if (debug_mode)
00233               printf("main loop awake; alive = %d\n", alive);
00234         PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
00235     }
00236     PR_Unlock(dead_lock);
00237 
00238     PR_DestroyCondVar(dead_cv);
00239     PR_DestroyLock(dead_lock);
00240 }
00241 
00242 int main(int argc, char **argv)
00243 {
00244     PRInt32 num_threads = 0;
00245 
00246        /* The command line argument: -d is used to determine if the test is being run
00247        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00248        All of the printfs associated with this test has been handled with a if (debug_mode)
00249        test.
00250        Usage: test_name [-d] [-t <threads>]
00251        */
00252        PLOptStatus os;
00253        PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
00254        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00255     {
00256               if (PL_OPT_BAD == os) continue;
00257         switch (opt->option)
00258         {
00259         case 'd':  /* debug mode */
00260                      debug_mode = 1;
00261             break;
00262         case 't':  /* threads to involve */
00263                      num_threads = atoi(opt->value);
00264             break;
00265          default:
00266             break;
00267         }
00268     }
00269        PL_DestroyOptState(opt);
00270 
00271  /* main test */
00272        
00273     if (0 == num_threads)
00274         num_threads = NUM_THREADS;
00275 
00276     PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
00277     PR_STDIO_INIT();
00278 
00279 #ifdef XP_MAC
00280        SetupMacPrintfLog("io_timeout.log");
00281        debug_mode = 1;
00282 #endif
00283 
00284     printf("test with global bound thread\n");
00285     thread_test(PR_GLOBAL_BOUND_THREAD, num_threads);
00286 
00287     printf("test with local thread\n");
00288     thread_test(PR_LOCAL_THREAD, num_threads);
00289 
00290     printf("test with global thread\n");
00291     thread_test(PR_GLOBAL_THREAD, num_threads);
00292 
00293     PR_Cleanup();
00294 
00295        if (failed_already)
00296               return 1;
00297        else
00298        return 0;
00299 }