io_timeout.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** Test socket IO timeouts
  7. **
  8. **
  9. **
  10. **
  11. ** Modification History:
  12. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  13. ** The debug mode will print all of the printfs associated with this test.
  14. ** The regress mode will be the default mode. Since the regress tool limits
  15. ** the output to a one line status:PASS or FAIL,all of the printf statements
  16. ** have been handled with an if (debug_mode) statement.
  17. ***********************************************************************/
  18. /***********************************************************************
  19. ** Includes
  20. ***********************************************************************/
  21. /* Used to get the command line option */
  22. #include "plgetopt.h"
  23. #include <stdio.h>
  24. #include "nspr.h"
  25. #define NUM_THREADS 1
  26. #define BASE_PORT 8000
  27. #define DEFAULT_ACCEPT_TIMEOUT 2
  28. typedef struct threadInfo {
  29. PRInt16 id;
  30. PRInt16 accept_timeout;
  31. PRLock *dead_lock;
  32. PRCondVar *dead_cv;
  33. PRInt32 *alive;
  34. } threadInfo;
  35. PRIntn failed_already = 0;
  36. PRIntn debug_mode = 0;
  37. #define LOCAL_SCOPE_STRING "LOCAL scope"
  38. #define GLOBAL_SCOPE_STRING "GLOBAL scope"
  39. #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope"
  40. void
  41. thread_main(void *_info)
  42. {
  43. threadInfo *info = (threadInfo *)_info;
  44. PRNetAddr listenAddr;
  45. PRNetAddr clientAddr;
  46. PRFileDesc *listenSock = NULL;
  47. PRFileDesc *clientSock;
  48. PRStatus rv;
  49. PRThreadScope tscope;
  50. char *scope_str;
  51. if (debug_mode) {
  52. printf("thread %d is alive\n", info->id);
  53. }
  54. tscope = PR_GetThreadScope(PR_GetCurrentThread());
  55. switch(tscope) {
  56. case PR_LOCAL_THREAD:
  57. scope_str = LOCAL_SCOPE_STRING;
  58. break;
  59. case PR_GLOBAL_THREAD:
  60. scope_str = GLOBAL_SCOPE_STRING;
  61. break;
  62. case PR_GLOBAL_BOUND_THREAD:
  63. scope_str = GLOBAL_BOUND_SCOPE_STRING;
  64. break;
  65. default:
  66. PR_NOT_REACHED("Invalid thread scope");
  67. break;
  68. }
  69. printf("thread id %d, scope %s\n", info->id, scope_str);
  70. listenSock = PR_NewTCPSocket();
  71. if (!listenSock) {
  72. if (debug_mode) {
  73. printf("unable to create listen socket\n");
  74. }
  75. failed_already=1;
  76. goto dead;
  77. }
  78. listenAddr.inet.family = PR_AF_INET;
  79. listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
  80. listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
  81. rv = PR_Bind(listenSock, &listenAddr);
  82. if (rv == PR_FAILURE) {
  83. if (debug_mode) {
  84. printf("unable to bind\n");
  85. }
  86. failed_already=1;
  87. goto dead;
  88. }
  89. rv = PR_Listen(listenSock, 4);
  90. if (rv == PR_FAILURE) {
  91. if (debug_mode) {
  92. printf("unable to listen\n");
  93. }
  94. failed_already=1;
  95. goto dead;
  96. }
  97. if (debug_mode)
  98. printf("thread %d going into accept for %d seconds\n",
  99. info->id, info->accept_timeout + info->id);
  100. clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
  101. if (clientSock == NULL) {
  102. if (PR_GetError() == PR_IO_TIMEOUT_ERROR) {
  103. if (debug_mode) {
  104. printf("PR_Accept() timeout worked!\n");
  105. printf("TEST PASSED! PR_Accept() returned error %d\n",
  106. PR_IO_TIMEOUT_ERROR);
  107. }
  108. } else {
  109. if (debug_mode)
  110. printf("TEST FAILED! PR_Accept() returned error %d\n",
  111. PR_GetError());
  112. failed_already=1;
  113. }
  114. } else {
  115. if (debug_mode) {
  116. printf ("TEST FAILED! PR_Accept() succeeded?\n");
  117. }
  118. failed_already=1;
  119. PR_Close(clientSock);
  120. }
  121. dead:
  122. if (listenSock) {
  123. PR_Close(listenSock);
  124. }
  125. PR_Lock(info->dead_lock);
  126. (*info->alive)--;
  127. PR_NotifyCondVar(info->dead_cv);
  128. PR_Unlock(info->dead_lock);
  129. if (debug_mode) {
  130. printf("thread %d is dead\n", info->id);
  131. }
  132. PR_Free(info);
  133. }
  134. void
  135. thread_test(PRThreadScope scope, PRInt32 num_threads)
  136. {
  137. PRInt32 index;
  138. PRThread *thr;
  139. PRLock *dead_lock;
  140. PRCondVar *dead_cv;
  141. PRInt32 alive;
  142. if (debug_mode) {
  143. printf("IO Timeout test started with %d threads\n", num_threads);
  144. }
  145. dead_lock = PR_NewLock();
  146. dead_cv = PR_NewCondVar(dead_lock);
  147. alive = num_threads;
  148. for (index = 0; index < num_threads; index++) {
  149. threadInfo *info = (threadInfo *)PR_Malloc(sizeof(threadInfo));
  150. info->id = index;
  151. info->dead_lock = dead_lock;
  152. info->dead_cv = dead_cv;
  153. info->alive = &alive;
  154. info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
  155. thr = PR_CreateThread( PR_USER_THREAD,
  156. thread_main,
  157. (void *)info,
  158. PR_PRIORITY_NORMAL,
  159. scope,
  160. PR_UNJOINABLE_THREAD,
  161. 0);
  162. if (!thr) {
  163. printf("Failed to create thread, error = %d(%d)\n",
  164. PR_GetError(), PR_GetOSError());
  165. failed_already=1;
  166. PR_Lock(dead_lock);
  167. alive--;
  168. PR_Unlock(dead_lock);
  169. }
  170. }
  171. PR_Lock(dead_lock);
  172. while(alive) {
  173. if (debug_mode) {
  174. printf("main loop awake; alive = %d\n", alive);
  175. }
  176. PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
  177. }
  178. PR_Unlock(dead_lock);
  179. PR_DestroyCondVar(dead_cv);
  180. PR_DestroyLock(dead_lock);
  181. }
  182. int main(int argc, char **argv)
  183. {
  184. PRInt32 num_threads = 0;
  185. /* The command line argument: -d is used to determine if the test is being run
  186. in debug mode. The regress tool requires only one line output:PASS or FAIL.
  187. All of the printfs associated with this test has been handled with a if (debug_mode)
  188. test.
  189. Usage: test_name [-d] [-t <threads>]
  190. */
  191. PLOptStatus os;
  192. PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
  193. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  194. {
  195. if (PL_OPT_BAD == os) {
  196. continue;
  197. }
  198. switch (opt->option)
  199. {
  200. case 'd': /* debug mode */
  201. debug_mode = 1;
  202. break;
  203. case 't': /* threads to involve */
  204. num_threads = atoi(opt->value);
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. PL_DestroyOptState(opt);
  211. /* main test */
  212. if (0 == num_threads) {
  213. num_threads = NUM_THREADS;
  214. }
  215. PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
  216. PR_STDIO_INIT();
  217. printf("test with global bound thread\n");
  218. thread_test(PR_GLOBAL_BOUND_THREAD, num_threads);
  219. printf("test with local thread\n");
  220. thread_test(PR_LOCAL_THREAD, num_threads);
  221. printf("test with global thread\n");
  222. thread_test(PR_GLOBAL_THREAD, num_threads);
  223. PR_Cleanup();
  224. if (failed_already) {
  225. return 1;
  226. }
  227. else {
  228. return 0;
  229. }
  230. }