michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** name io_timeoutk.c michael@0: ** Description:Test socket IO timeouts (kernel level) michael@0: ** michael@0: ** Modification History: michael@0: ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag. michael@0: ** The debug mode will print all of the printfs associated with this test. michael@0: ** The regress mode will be the default mode. Since the regress tool limits michael@0: ** the output to a one line status:PASS or FAIL,all of the printf statements michael@0: ** have been handled with an if (debug_mode) statement. michael@0: ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to michael@0: ** recognize the return code from tha main program. michael@0: ***********************************************************************/ michael@0: /*********************************************************************** michael@0: ** Includes michael@0: ***********************************************************************/ michael@0: /* Used to get the command line option */ michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include "nspr.h" michael@0: michael@0: #define NUM_THREADS 1 michael@0: #define BASE_PORT 8000 michael@0: #define DEFAULT_ACCEPT_TIMEOUT 2 michael@0: michael@0: typedef struct threadInfo { michael@0: PRInt16 id; michael@0: PRInt16 accept_timeout; michael@0: PRLock *dead_lock; michael@0: PRCondVar *dead_cv; michael@0: PRInt32 *alive; michael@0: } threadInfo; michael@0: michael@0: PRIntn failed_already=0; michael@0: PRIntn debug_mode; michael@0: michael@0: michael@0: void michael@0: thread_main(void *_info) michael@0: { michael@0: threadInfo *info = (threadInfo *)_info; michael@0: PRNetAddr listenAddr; michael@0: PRNetAddr clientAddr; michael@0: PRFileDesc *listenSock = NULL; michael@0: PRFileDesc *clientSock; michael@0: PRStatus rv; michael@0: michael@0: if (debug_mode) printf("thread %d is alive\n", info->id); michael@0: michael@0: listenSock = PR_NewTCPSocket(); michael@0: if (!listenSock) { michael@0: if (debug_mode) printf("unable to create listen socket\n"); michael@0: goto dead; michael@0: } michael@0: michael@0: listenAddr.inet.family = AF_INET; michael@0: listenAddr.inet.port = PR_htons(BASE_PORT + info->id); michael@0: listenAddr.inet.ip = PR_htonl(INADDR_ANY); michael@0: rv = PR_Bind(listenSock, &listenAddr); michael@0: if (rv == PR_FAILURE) { michael@0: if (debug_mode) printf("unable to bind\n"); michael@0: goto dead; michael@0: } michael@0: michael@0: rv = PR_Listen(listenSock, 4); michael@0: if (rv == PR_FAILURE) { michael@0: if (debug_mode) printf("unable to listen\n"); michael@0: goto dead; michael@0: } michael@0: michael@0: if (debug_mode) printf("thread %d going into accept for %d seconds\n", michael@0: info->id, info->accept_timeout + info->id); michael@0: michael@0: clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id)); michael@0: michael@0: if (clientSock == NULL) { michael@0: if (PR_GetError() == PR_IO_TIMEOUT_ERROR) michael@0: if (debug_mode) { michael@0: printf("PR_Accept() timeout worked!\n"); michael@0: printf("TEST FAILED! PR_Accept() returned error %d\n", michael@0: PR_GetError()); michael@0: } michael@0: else failed_already=1; michael@0: } else { michael@0: if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n"); michael@0: else failed_already=1; michael@0: PR_Close(clientSock); michael@0: } michael@0: michael@0: dead: michael@0: if (listenSock) { michael@0: PR_Close(listenSock); michael@0: } michael@0: PR_Lock(info->dead_lock); michael@0: (*info->alive)--; michael@0: PR_NotifyCondVar(info->dead_cv); michael@0: PR_Unlock(info->dead_lock); michael@0: michael@0: if (debug_mode) printf("thread %d is dead\n", info->id); michael@0: } michael@0: michael@0: void michael@0: thread_test(PRInt32 scope, PRInt32 num_threads) michael@0: { michael@0: PRInt32 index; michael@0: PRThread *thr; michael@0: PRLock *dead_lock; michael@0: PRCondVar *dead_cv; michael@0: PRInt32 alive; michael@0: michael@0: if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads); michael@0: michael@0: dead_lock = PR_NewLock(); michael@0: dead_cv = PR_NewCondVar(dead_lock); michael@0: alive = num_threads; michael@0: michael@0: for (index = 0; index < num_threads; index++) { michael@0: threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo)); michael@0: michael@0: info->id = index; michael@0: info->dead_lock = dead_lock; michael@0: info->dead_cv = dead_cv; michael@0: info->alive = &alive; michael@0: info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT; michael@0: michael@0: thr = PR_CreateThread( PR_USER_THREAD, michael@0: thread_main, michael@0: (void *)info, michael@0: PR_PRIORITY_NORMAL, michael@0: scope, michael@0: PR_UNJOINABLE_THREAD, michael@0: 0); michael@0: michael@0: if (!thr) { michael@0: PR_Lock(dead_lock); michael@0: alive--; michael@0: PR_Unlock(dead_lock); michael@0: } michael@0: } michael@0: michael@0: PR_Lock(dead_lock); michael@0: while(alive) { michael@0: if (debug_mode) printf("main loop awake; alive = %d\n", alive); michael@0: PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT); michael@0: } michael@0: PR_Unlock(dead_lock); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRInt32 num_threads; michael@0: michael@0: /* The command line argument: -d is used to determine if the test is being run michael@0: in debug mode. The regress tool requires only one line output:PASS or FAIL. michael@0: All of the printfs associated with this test has been handled with a if (debug_mode) michael@0: test. michael@0: Usage: test_name -d michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "d:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: debug_mode = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: /* main test */ michael@0: michael@0: if (argc > 2) michael@0: num_threads = atoi(argv[2]); michael@0: else michael@0: num_threads = NUM_THREADS; michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: if (debug_mode) printf("kernel level test\n"); michael@0: thread_test(PR_GLOBAL_THREAD, num_threads); michael@0: michael@0: PR_Cleanup(); michael@0: michael@0: if(failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: michael@0: }