home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / io_timeoutk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.2 KB  |  215 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. ** name io_timeoutk.c
  21. ** Description:Test socket IO timeouts (kernel level)
  22. **
  23. ** Modification History:
  24. ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  25. **             The debug mode will print all of the printfs associated with this test.
  26. **             The regress mode will be the default mode. Since the regress tool limits
  27. **           the output to a one line status:PASS or FAIL,all of the printf statements
  28. **             have been handled with an if (debug_mode) statement.
  29. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  30. **            recognize the return code from tha main program.
  31. ***********************************************************************/
  32. /***********************************************************************
  33. ** Includes
  34. ***********************************************************************/
  35. /* Used to get the command line option */
  36. #include "plgetopt.h"
  37.  
  38. #include <stdio.h>
  39. #include "nspr.h"
  40.  
  41. #define NUM_THREADS 1
  42. #define BASE_PORT   8000
  43. #define DEFAULT_ACCEPT_TIMEOUT 2
  44.  
  45. typedef struct threadInfo {
  46.     PRInt16 id;
  47.     PRInt16 accept_timeout;
  48.     PRLock *dead_lock;
  49.     PRCondVar *dead_cv;
  50.     PRInt32   *alive;
  51. } threadInfo;
  52.  
  53. PRIntn failed_already=0;
  54. PRIntn debug_mode;
  55.  
  56.  
  57. void 
  58. thread_main(void *_info)
  59. {
  60.     threadInfo *info = (threadInfo *)_info;
  61.     PRNetAddr listenAddr;
  62.     PRNetAddr clientAddr;
  63.     PRFileDesc *listenSock = NULL;
  64.     PRFileDesc *clientSock;
  65.     PRStatus rv;
  66.  
  67.     if (debug_mode) printf("thread %d is alive\n", info->id);
  68.  
  69.     listenSock = PR_NewTCPSocket();
  70.     if (!listenSock) {
  71.         if (debug_mode) printf("unable to create listen socket\n");
  72.         goto dead;
  73.     }
  74.   
  75.     listenAddr.inet.family = AF_INET;
  76.     listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
  77.     listenAddr.inet.ip = PR_htonl(INADDR_ANY);
  78.     rv = PR_Bind(listenSock, &listenAddr);
  79.     if (rv == PR_FAILURE) {
  80.         if (debug_mode) printf("unable to bind\n");
  81.         goto dead;
  82.     }
  83.  
  84.     rv = PR_Listen(listenSock, 4);
  85.     if (rv == PR_FAILURE) {
  86.         if (debug_mode) printf("unable to listen\n");
  87.         goto dead;
  88.     }
  89.  
  90.     if (debug_mode) printf("thread %d going into accept for %d seconds\n", 
  91.         info->id, info->accept_timeout + info->id);
  92.  
  93.     clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
  94.  
  95.     if (clientSock == NULL) {
  96.         if (PR_GetError() == PR_IO_TIMEOUT_ERROR) 
  97.             if (debug_mode) {
  98.                 printf("PR_Accept() timeout worked!\n");
  99.                 printf("TEST FAILED! PR_Accept() returned error %d\n",
  100.                                    PR_GetError());
  101.             }
  102.             else failed_already=1;
  103.     } else {
  104.         if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
  105.         else failed_already=1;
  106.     PR_Close(clientSock);
  107.     }
  108.  
  109. dead:
  110.     if (listenSock) {
  111.     PR_Close(listenSock);
  112.     }
  113.     PR_Lock(info->dead_lock);
  114.     (*info->alive)--;
  115.     PR_NotifyCondVar(info->dead_cv);
  116.     PR_Unlock(info->dead_lock);
  117.  
  118.     if (debug_mode) printf("thread %d is dead\n", info->id);
  119. }
  120.  
  121. void
  122. thread_test(PRInt32 scope, PRInt32 num_threads)
  123. {
  124.     PRInt32 index;
  125.     PRThread *thr;
  126.     PRLock *dead_lock;
  127.     PRCondVar *dead_cv;
  128.     PRInt32 alive;
  129.  
  130.     if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
  131.  
  132.     dead_lock = PR_NewLock();
  133.     dead_cv = PR_NewCondVar(dead_lock);
  134.     alive = num_threads;
  135.     
  136.     for (index = 0; index < num_threads; index++) {
  137.         threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
  138.  
  139.         info->id = index;
  140.         info->dead_lock = dead_lock;
  141.         info->dead_cv = dead_cv;
  142.         info->alive = &alive;
  143.         info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
  144.         
  145.         thr = PR_CreateThread( PR_USER_THREAD,
  146.                                thread_main,
  147.                                (void *)info,
  148.                                PR_PRIORITY_NORMAL,
  149.                                scope,
  150.                                PR_UNJOINABLE_THREAD,
  151.                                0);
  152.  
  153.         if (!thr) {
  154.             PR_Lock(dead_lock);
  155.             alive--;
  156.             PR_Unlock(dead_lock);
  157.         }
  158.     }
  159.  
  160.     PR_Lock(dead_lock);
  161.     while(alive) {
  162.         if (debug_mode) printf("main loop awake; alive = %d\n", alive);
  163.         PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
  164.     }
  165.     PR_Unlock(dead_lock);
  166. }
  167.  
  168. int main(int argc, char **argv)
  169. {
  170.     PRInt32 num_threads;
  171.  
  172.     /* The command line argument: -d is used to determine if the test is being run
  173.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  174.     All of the printfs associated with this test has been handled with a if (debug_mode)
  175.     test.
  176.     Usage: test_name -d
  177.     */
  178.     PLOptStatus os;
  179.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  180.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  181.     {
  182.         if (PL_OPT_BAD == os) continue;
  183.         switch (opt->option)
  184.         {
  185.         case 'd':  /* debug mode */
  186.             debug_mode = 1;
  187.             break;
  188.          default:
  189.             break;
  190.         }
  191.     }
  192.     PL_DestroyOptState(opt);
  193.  
  194.  /* main test */
  195.     
  196.     if (argc > 2)
  197.         num_threads = atoi(argv[2]);
  198.     else
  199.         num_threads = NUM_THREADS;
  200.  
  201.     PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
  202.     PR_STDIO_INIT();
  203.  
  204.     if (debug_mode)     printf("kernel level test\n");
  205.     thread_test(PR_GLOBAL_THREAD, num_threads);
  206.  
  207.      PR_Cleanup();
  208.      
  209.      if(failed_already)    
  210.         return 1;
  211.     else
  212.         return 0;
  213.  
  214. }
  215.