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