home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / io_timeoutu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.2 KB  |  216 lines

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