1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ** Test socket IO timeouts
|
---|
40 | **
|
---|
41 | **
|
---|
42 | **
|
---|
43 | **
|
---|
44 | ** Modification History:
|
---|
45 | ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
|
---|
46 | ** The debug mode will print all of the printfs associated with this test.
|
---|
47 | ** The regress mode will be the default mode. Since the regress tool limits
|
---|
48 | ** the output to a one line status:PASS or FAIL,all of the printf statements
|
---|
49 | ** have been handled with an if (debug_mode) statement.
|
---|
50 | ***********************************************************************/
|
---|
51 | /***********************************************************************
|
---|
52 | ** Includes
|
---|
53 | ***********************************************************************/
|
---|
54 | /* Used to get the command line option */
|
---|
55 | #include "plgetopt.h"
|
---|
56 |
|
---|
57 | #include <stdio.h>
|
---|
58 | #include "nspr.h"
|
---|
59 |
|
---|
60 | #ifdef XP_MAC
|
---|
61 | #include "prlog.h"
|
---|
62 | #define printf PR_LogPrint
|
---|
63 | extern void SetupMacPrintfLog(char *logFile);
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #define NUM_THREADS 1
|
---|
67 | #define BASE_PORT 8000
|
---|
68 | #define DEFAULT_ACCEPT_TIMEOUT 2
|
---|
69 |
|
---|
70 | typedef struct threadInfo {
|
---|
71 | PRInt16 id;
|
---|
72 | PRInt16 accept_timeout;
|
---|
73 | PRLock *dead_lock;
|
---|
74 | PRCondVar *dead_cv;
|
---|
75 | PRInt32 *alive;
|
---|
76 | } threadInfo;
|
---|
77 |
|
---|
78 | PRIntn failed_already = 0;
|
---|
79 | PRIntn debug_mode = 0;
|
---|
80 |
|
---|
81 | #define LOCAL_SCOPE_STRING "LOCAL scope"
|
---|
82 | #define GLOBAL_SCOPE_STRING "GLOBAL scope"
|
---|
83 | #define GLOBAL_BOUND_SCOPE_STRING "GLOBAL_BOUND scope"
|
---|
84 |
|
---|
85 | void
|
---|
86 | thread_main(void *_info)
|
---|
87 | {
|
---|
88 | threadInfo *info = (threadInfo *)_info;
|
---|
89 | PRNetAddr listenAddr;
|
---|
90 | PRNetAddr clientAddr;
|
---|
91 | PRFileDesc *listenSock = NULL;
|
---|
92 | PRFileDesc *clientSock;
|
---|
93 | PRStatus rv;
|
---|
94 | PRThreadScope tscope;
|
---|
95 | char *scope_str;
|
---|
96 |
|
---|
97 |
|
---|
98 | if (debug_mode)
|
---|
99 | printf("thread %d is alive\n", info->id);
|
---|
100 | tscope = PR_GetThreadScope(PR_GetCurrentThread());
|
---|
101 |
|
---|
102 | switch(tscope) {
|
---|
103 | case PR_LOCAL_THREAD:
|
---|
104 | scope_str = LOCAL_SCOPE_STRING;
|
---|
105 | break;
|
---|
106 | case PR_GLOBAL_THREAD:
|
---|
107 | scope_str = GLOBAL_SCOPE_STRING;
|
---|
108 | break;
|
---|
109 | case PR_GLOBAL_BOUND_THREAD:
|
---|
110 | scope_str = GLOBAL_BOUND_SCOPE_STRING;
|
---|
111 | break;
|
---|
112 | default:
|
---|
113 | PR_ASSERT(!"Invalid thread scope");
|
---|
114 | break;
|
---|
115 | }
|
---|
116 | printf("thread id %d, scope %s\n", info->id, scope_str);
|
---|
117 |
|
---|
118 | listenSock = PR_NewTCPSocket();
|
---|
119 | if (!listenSock) {
|
---|
120 | if (debug_mode)
|
---|
121 | printf("unable to create listen socket\n");
|
---|
122 | failed_already=1;
|
---|
123 | goto dead;
|
---|
124 | }
|
---|
125 |
|
---|
126 | listenAddr.inet.family = PR_AF_INET;
|
---|
127 | listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
|
---|
128 | listenAddr.inet.ip = PR_htonl(PR_INADDR_ANY);
|
---|
129 | rv = PR_Bind(listenSock, &listenAddr);
|
---|
130 | if (rv == PR_FAILURE) {
|
---|
131 | if (debug_mode)
|
---|
132 | printf("unable to bind\n");
|
---|
133 | failed_already=1;
|
---|
134 | goto dead;
|
---|
135 | }
|
---|
136 |
|
---|
137 | rv = PR_Listen(listenSock, 4);
|
---|
138 | if (rv == PR_FAILURE) {
|
---|
139 | if (debug_mode)
|
---|
140 | printf("unable to listen\n");
|
---|
141 | failed_already=1;
|
---|
142 | goto dead;
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (debug_mode)
|
---|
146 | printf("thread %d going into accept for %d seconds\n",
|
---|
147 | info->id, info->accept_timeout + info->id);
|
---|
148 |
|
---|
149 | clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
|
---|
150 |
|
---|
151 | if (clientSock == NULL) {
|
---|
152 | if (PR_GetError() == PR_IO_TIMEOUT_ERROR) {
|
---|
153 | if (debug_mode) {
|
---|
154 | printf("PR_Accept() timeout worked!\n");
|
---|
155 | printf("TEST PASSED! PR_Accept() returned error %d\n",
|
---|
156 | PR_IO_TIMEOUT_ERROR);
|
---|
157 | }
|
---|
158 | } else {
|
---|
159 | if (debug_mode)
|
---|
160 | printf("TEST FAILED! PR_Accept() returned error %d\n",
|
---|
161 | PR_GetError());
|
---|
162 | failed_already=1;
|
---|
163 | }
|
---|
164 | } else {
|
---|
165 | if (debug_mode)
|
---|
166 | printf ("TEST FAILED! PR_Accept() succeeded?\n");
|
---|
167 | failed_already=1;
|
---|
168 | PR_Close(clientSock);
|
---|
169 | }
|
---|
170 |
|
---|
171 | dead:
|
---|
172 | if (listenSock) {
|
---|
173 | PR_Close(listenSock);
|
---|
174 | }
|
---|
175 | PR_Lock(info->dead_lock);
|
---|
176 | (*info->alive)--;
|
---|
177 | PR_NotifyCondVar(info->dead_cv);
|
---|
178 | PR_Unlock(info->dead_lock);
|
---|
179 |
|
---|
180 | if (debug_mode)
|
---|
181 | printf("thread %d is dead\n", info->id);
|
---|
182 |
|
---|
183 | PR_Free(info);
|
---|
184 | }
|
---|
185 |
|
---|
186 | void
|
---|
187 | thread_test(PRThreadScope scope, PRInt32 num_threads)
|
---|
188 | {
|
---|
189 | PRInt32 index;
|
---|
190 | PRThread *thr;
|
---|
191 | PRLock *dead_lock;
|
---|
192 | PRCondVar *dead_cv;
|
---|
193 | PRInt32 alive;
|
---|
194 |
|
---|
195 | if (debug_mode)
|
---|
196 | printf("IO Timeout test started with %d threads\n", num_threads);
|
---|
197 |
|
---|
198 | dead_lock = PR_NewLock();
|
---|
199 | dead_cv = PR_NewCondVar(dead_lock);
|
---|
200 | alive = num_threads;
|
---|
201 |
|
---|
202 | for (index = 0; index < num_threads; index++) {
|
---|
203 | threadInfo *info = (threadInfo *)PR_Malloc(sizeof(threadInfo));
|
---|
204 |
|
---|
205 | info->id = index;
|
---|
206 | info->dead_lock = dead_lock;
|
---|
207 | info->dead_cv = dead_cv;
|
---|
208 | info->alive = &alive;
|
---|
209 | info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
|
---|
210 |
|
---|
211 | thr = PR_CreateThread( PR_USER_THREAD,
|
---|
212 | thread_main,
|
---|
213 | (void *)info,
|
---|
214 | PR_PRIORITY_NORMAL,
|
---|
215 | scope,
|
---|
216 | PR_UNJOINABLE_THREAD,
|
---|
217 | 0);
|
---|
218 |
|
---|
219 | if (!thr) {
|
---|
220 | printf("Failed to create thread, error = %d(%d)\n",
|
---|
221 | PR_GetError(), PR_GetOSError());
|
---|
222 | failed_already=1;
|
---|
223 |
|
---|
224 | PR_Lock(dead_lock);
|
---|
225 | alive--;
|
---|
226 | PR_Unlock(dead_lock);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | PR_Lock(dead_lock);
|
---|
231 | while(alive) {
|
---|
232 | if (debug_mode)
|
---|
233 | printf("main loop awake; alive = %d\n", alive);
|
---|
234 | PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
|
---|
235 | }
|
---|
236 | PR_Unlock(dead_lock);
|
---|
237 |
|
---|
238 | PR_DestroyCondVar(dead_cv);
|
---|
239 | PR_DestroyLock(dead_lock);
|
---|
240 | }
|
---|
241 |
|
---|
242 | int main(int argc, char **argv)
|
---|
243 | {
|
---|
244 | PRInt32 num_threads = 0;
|
---|
245 |
|
---|
246 | /* The command line argument: -d is used to determine if the test is being run
|
---|
247 | in debug mode. The regress tool requires only one line output:PASS or FAIL.
|
---|
248 | All of the printfs associated with this test has been handled with a if (debug_mode)
|
---|
249 | test.
|
---|
250 | Usage: test_name [-d] [-t <threads>]
|
---|
251 | */
|
---|
252 | PLOptStatus os;
|
---|
253 | PLOptState *opt = PL_CreateOptState(argc, argv, "dt:");
|
---|
254 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
255 | {
|
---|
256 | if (PL_OPT_BAD == os) continue;
|
---|
257 | switch (opt->option)
|
---|
258 | {
|
---|
259 | case 'd': /* debug mode */
|
---|
260 | debug_mode = 1;
|
---|
261 | break;
|
---|
262 | case 't': /* threads to involve */
|
---|
263 | num_threads = atoi(opt->value);
|
---|
264 | break;
|
---|
265 | default:
|
---|
266 | break;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | PL_DestroyOptState(opt);
|
---|
270 |
|
---|
271 | /* main test */
|
---|
272 |
|
---|
273 | if (0 == num_threads)
|
---|
274 | num_threads = NUM_THREADS;
|
---|
275 |
|
---|
276 | PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
|
---|
277 | PR_STDIO_INIT();
|
---|
278 |
|
---|
279 | #ifdef XP_MAC
|
---|
280 | SetupMacPrintfLog("io_timeout.log");
|
---|
281 | debug_mode = 1;
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | printf("test with global bound thread\n");
|
---|
285 | thread_test(PR_GLOBAL_BOUND_THREAD, num_threads);
|
---|
286 |
|
---|
287 | printf("test with local thread\n");
|
---|
288 | thread_test(PR_LOCAL_THREAD, num_threads);
|
---|
289 |
|
---|
290 | printf("test with global thread\n");
|
---|
291 | thread_test(PR_GLOBAL_THREAD, num_threads);
|
---|
292 |
|
---|
293 | PR_Cleanup();
|
---|
294 |
|
---|
295 | if (failed_already)
|
---|
296 | return 1;
|
---|
297 | else
|
---|
298 | return 0;
|
---|
299 | }
|
---|