1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
37
** Description:Test socket IO timeouts (kernel level)
39
** Modification History:
40
** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
41
** The debug mode will print all of the printfs associated with this test.
42
** The regress mode will be the default mode. Since the regress tool limits
43
** the output to a one line status:PASS or FAIL,all of the printf statements
44
** have been handled with an if (debug_mode) statement.
45
** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
46
** recognize the return code from tha main program.
47
***********************************************************************/
48
/***********************************************************************
50
***********************************************************************/
51
/* Used to get the command line option */
58
#define BASE_PORT 8000
59
#define DEFAULT_ACCEPT_TIMEOUT 2
61
typedef struct threadInfo {
63
PRInt16 accept_timeout;
69
PRIntn failed_already=0;
74
thread_main(void *_info)
76
threadInfo *info = (threadInfo *)_info;
79
PRFileDesc *listenSock = NULL;
80
PRFileDesc *clientSock;
83
if (debug_mode) printf("thread %d is alive\n", info->id);
85
listenSock = PR_NewTCPSocket();
87
if (debug_mode) printf("unable to create listen socket\n");
91
listenAddr.inet.family = AF_INET;
92
listenAddr.inet.port = PR_htons(BASE_PORT + info->id);
93
listenAddr.inet.ip = PR_htonl(INADDR_ANY);
94
rv = PR_Bind(listenSock, &listenAddr);
95
if (rv == PR_FAILURE) {
96
if (debug_mode) printf("unable to bind\n");
100
rv = PR_Listen(listenSock, 4);
101
if (rv == PR_FAILURE) {
102
if (debug_mode) printf("unable to listen\n");
106
if (debug_mode) printf("thread %d going into accept for %d seconds\n",
107
info->id, info->accept_timeout + info->id);
109
clientSock = PR_Accept(listenSock, &clientAddr, PR_SecondsToInterval(info->accept_timeout +info->id));
111
if (clientSock == NULL) {
112
if (PR_GetError() == PR_IO_TIMEOUT_ERROR)
114
printf("PR_Accept() timeout worked!\n");
115
printf("TEST FAILED! PR_Accept() returned error %d\n",
118
else failed_already=1;
120
if (debug_mode) printf ("TEST FAILED! PR_Accept() succeeded?\n");
121
else failed_already=1;
122
PR_Close(clientSock);
127
PR_Close(listenSock);
129
PR_Lock(info->dead_lock);
131
PR_NotifyCondVar(info->dead_cv);
132
PR_Unlock(info->dead_lock);
134
if (debug_mode) printf("thread %d is dead\n", info->id);
138
thread_test(PRInt32 scope, PRInt32 num_threads)
146
if (debug_mode) printf("IO Timeout test started with %d threads\n", num_threads);
148
dead_lock = PR_NewLock();
149
dead_cv = PR_NewCondVar(dead_lock);
152
for (index = 0; index < num_threads; index++) {
153
threadInfo *info = (threadInfo *)malloc(sizeof(threadInfo));
156
info->dead_lock = dead_lock;
157
info->dead_cv = dead_cv;
158
info->alive = &alive;
159
info->accept_timeout = DEFAULT_ACCEPT_TIMEOUT;
161
thr = PR_CreateThread( PR_USER_THREAD,
166
PR_UNJOINABLE_THREAD,
172
PR_Unlock(dead_lock);
178
if (debug_mode) printf("main loop awake; alive = %d\n", alive);
179
PR_WaitCondVar(dead_cv, PR_INTERVAL_NO_TIMEOUT);
181
PR_Unlock(dead_lock);
184
int main(int argc, char **argv)
188
/* The command line argument: -d is used to determine if the test is being run
189
in debug mode. The regress tool requires only one line output:PASS or FAIL.
190
All of the printfs associated with this test has been handled with a if (debug_mode)
195
PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
196
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
198
if (PL_OPT_BAD == os) continue;
201
case 'd': /* debug mode */
208
PL_DestroyOptState(opt);
213
num_threads = atoi(argv[2]);
215
num_threads = NUM_THREADS;
217
PR_Init(PR_USER_THREAD, PR_PRIORITY_LOW, 0);
220
if (debug_mode) printf("kernel level test\n");
221
thread_test(PR_GLOBAL_THREAD, num_threads);