VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/io_timeoutu.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette