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: ** Test socket IO timeouts michael@0: ** michael@0: ** michael@0: ** michael@0: ** michael@0: ** Modification History: michael@0: ** 14-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: ***********************************************************************/ 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 = 0; michael@0: michael@0: #define LOCAL_SCOPE_STRING "LOCAL scope" michael@0: #define GLOBAL_SCOPE_STRING "GLOBAL scope" michael@0: #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope" 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: PRThreadScope tscope; michael@0: char *scope_str; michael@0: michael@0: michael@0: if (debug_mode) michael@0: printf("thread %d is alive\n", info->id); michael@0: tscope = PR_GetThreadScope(PR_GetCurrentThread()); michael@0: michael@0: switch(tscope) { michael@0: case PR_LOCAL_THREAD: michael@0: scope_str = LOCAL_SCOPE_STRING; michael@0: break; michael@0: case PR_GLOBAL_THREAD: michael@0: scope_str = GLOBAL_SCOPE_STRING; michael@0: break; michael@0: case PR_GLOBAL_BOUND_THREAD: michael@0: scope_str = GLOBAL_BOUND_SCOPE_STRING; michael@0: break; michael@0: default: michael@0: PR_ASSERT(!"Invalid thread scope"); michael@0: break; michael@0: } michael@0: printf("thread id %d, scope %s\n", info->id, scope_str); michael@0: michael@0: listenSock = PR_NewTCPSocket(); michael@0: if (!listenSock) { michael@0: if (debug_mode) michael@0: printf("unable to create listen socket\n"); michael@0: failed_already=1; michael@0: goto dead; michael@0: } michael@0: michael@0: listenAddr.inet.family = PR_AF_INET; michael@0: listenAddr.inet.port = PR_htons(BASE_PORT + info->id); michael@0: listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY); michael@0: rv = PR_Bind(listenSock, &listenAddr); michael@0: if (rv == PR_FAILURE) { michael@0: if (debug_mode) michael@0: printf("unable to bind\n"); michael@0: failed_already=1; 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) michael@0: printf("unable to listen\n"); michael@0: failed_already=1; michael@0: goto dead; michael@0: } michael@0: michael@0: if (debug_mode) michael@0: 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 PASSED! PR_Accept() returned error %d\n", michael@0: PR_IO_TIMEOUT_ERROR); michael@0: } michael@0: } else { michael@0: if (debug_mode) michael@0: printf("TEST FAILED! PR_Accept() returned error %d\n", michael@0: PR_GetError()); michael@0: failed_already=1; michael@0: } michael@0: } else { michael@0: if (debug_mode) michael@0: printf ("TEST FAILED! PR_Accept() succeeded?\n"); michael@0: 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) michael@0: printf("thread %d is dead\n", info->id); michael@0: michael@0: PR_Free(info); michael@0: } michael@0: michael@0: void michael@0: thread_test(PRThreadScope 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) michael@0: 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 *)PR_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: printf("Failed to create thread, error = %d(%d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: 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) michael@0: 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: PR_DestroyCondVar(dead_cv); michael@0: PR_DestroyLock(dead_lock); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRInt32 num_threads = 0; 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] [-t ] michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "dt:"); 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: case 't': /* threads to involve */ michael@0: num_threads = atoi(opt->value); 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 (0 == num_threads) 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: printf("test with global bound thread\n"); michael@0: thread_test(PR_GLOBAL_BOUND_THREAD, num_threads); michael@0: michael@0: printf("test with local thread\n"); michael@0: thread_test(PR_LOCAL_THREAD, num_threads); michael@0: michael@0: printf("test with global thread\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: }