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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
41 ** This test, ntioto.c, was designed to reproduce a bug reported by NES
42 ** on WindowsNT (fibers implementation). NSPR was asserting in ntio.c
43 ** after PR_AcceptRead() had timed out. I/O performed subsequent to the
44 ** call to PR_AcceptRead() could complete on a CPU other than the one
45 ** on which it was started. The assert in ntio.c detected this, then
49 ** This test will fail with an assert in ntio.c if the problem it was
50 ** designed to catch occurs. It returns 0 otherwise.
52 ** The main() thread initializes and tears things down. A file is
53 ** opened for writing; this file will be written to by AcceptThread()
54 ** and JitterThread(). Main() creates a socket for reading, listens
55 ** and binds the socket.
57 ** ConnectThread() connects to the socket created by main, then polls
58 ** the "state" variable. When state is AllDone, ConnectThread() exits.
60 ** AcceptThread() calls PR_AcceptRead() on the socket. He fully expects
61 ** it to time out. After the timeout, AccpetThread() interacts with
62 ** JitterThread() via a common condition variable and the state
63 ** variable. The two threads ping-pong back and forth, each thread
64 ** writes the the file opened by main. This should provoke the
65 ** condition reported by NES (if we didn't fix it).
67 ** The failure is not solid. It may fail within a few ping-pongs between
68 ** AcceptThread() and JitterThread() or may take a while. The default
69 ** iteration count, jitter, is set by DEFAULT_JITTER. This may be
70 ** modified at the command line with the -j option.
81 ** Test harness infrastructure
84 PRLogModuleLevel msgLevel
= PR_LOG_NONE
;
87 PRUint32 failed_already
= 0;
88 /* end Test harness infrastructure */
90 /* JITTER_DEFAULT: the number of times AcceptThread() and JitterThread() ping-pong */
91 #define JITTER_DEFAULT 100000
92 #define BASE_PORT 9867
94 PRIntervalTime timeout
;
96 PRFileDesc
*listenSock
;
103 } state
= RunAcceptRead
;
106 PRIntn jitter
= JITTER_DEFAULT
;
107 PRBool resume
= PR_FALSE
;
110 ** Emit help text for this test
112 static void Help( void )
114 printf("Template: Help(): display your help message(s) here");
120 ** static computation of PR_AcceptRead() buffer size.
122 #define ACCEPT_READ_DATASIZE 10
123 #define ACCEPT_READ_BUFSIZE (PR_ACCEPT_READ_BUF_OVERHEAD + ACCEPT_READ_DATASIZE)
125 static void AcceptThread(void *arg
)
128 char dataBuf
[ACCEPT_READ_BUFSIZE
];
132 bytesRead
= PR_AcceptRead( listenSock
,
136 ACCEPT_READ_DATASIZE
,
137 PR_SecondsToInterval(1));
139 if ( bytesRead
== -1 && PR_GetError() == PR_IO_TIMEOUT_ERROR
) {
140 if ( debug
) printf("AcceptRead timed out\n");
142 if ( debug
) printf("Oops! read: %d, error: %d\n", bytesRead
, PR_GetError());
145 while( state
!= AllDone
) {
147 while( state
!= RunAcceptRead
)
148 PR_WaitCondVar( cv
, PR_INTERVAL_NO_TIMEOUT
);
149 if ( ++iCounter
>= jitter
)
153 if ( verbose
) printf(".");
154 PR_NotifyCondVar( cv
);
156 PR_Write( file1
, ".", 1 );
160 } /* end AcceptThread() */
162 static void JitterThread(void *arg
)
164 while( state
!= AllDone
) {
166 while( state
!= RunJitter
&& state
!= AllDone
)
167 PR_WaitCondVar( cv
, PR_INTERVAL_NO_TIMEOUT
);
168 if ( state
!= AllDone
)
169 state
= RunAcceptRead
;
170 if ( verbose
) printf("+");
171 PR_NotifyCondVar( cv
);
173 PR_Write( file1
, "+", 1 );
178 static void ConnectThread( void *arg
)
181 PRFileDesc
*clientSock
;
182 PRNetAddr serverAddress
;
183 clientSock
= PR_NewTCPSocket();
185 PR_ASSERT(clientSock
);
188 if ( debug
) printf("pausing 3 seconds before connect\n");
189 PR_Sleep( PR_SecondsToInterval(3));
192 memset(&serverAddress
, 0, sizeof(serverAddress
));
193 rv
= PR_InitializeNetAddr(PR_IpAddrLoopback
, BASE_PORT
, &serverAddress
);
194 PR_ASSERT( PR_SUCCESS
== rv
);
195 rv
= PR_Connect( clientSock
,
197 PR_SecondsToInterval(1));
198 PR_ASSERT( PR_SUCCESS
== rv
);
200 /* that's all we do. ... Wait for the acceptread() to timeout */
201 while( state
!= AllDone
)
202 PR_Sleep( PR_SecondsToInterval(1));
204 } /* end ConnectThread() */
207 PRIntn
main(PRIntn argc
, char *argv
[])
213 /* This test if valid for WinNT only! */
221 ** Get command line options
224 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "hdrvj:");
226 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
228 if (PL_OPT_BAD
== os
) continue;
231 case 'd': /* debug */
233 msgLevel
= PR_LOG_ERROR
;
235 case 'v': /* verbose mode */
237 msgLevel
= PR_LOG_DEBUG
;
240 jitter
= atoi(opt
->value
);
242 jitter
= JITTER_DEFAULT
;
247 case 'h': /* help message */
254 PL_DestroyOptState(opt
);
257 lm
= PR_NewLogModule("Test"); /* Initialize logging */
259 /* set concurrency */
260 PR_SetConcurrency( 4 );
262 /* setup thread synchronization mechanics */
264 cv
= PR_NewCondVar( ml
);
266 /* setup a tcp socket */
267 memset(&listenAddr
, 0, sizeof(listenAddr
));
268 rv
= PR_InitializeNetAddr(PR_IpAddrAny
, BASE_PORT
, &listenAddr
);
269 PR_ASSERT( PR_SUCCESS
== rv
);
271 listenSock
= PR_NewTCPSocket();
272 PR_ASSERT( listenSock
);
274 rv
= PR_Bind( listenSock
, &listenAddr
);
275 PR_ASSERT( PR_SUCCESS
== rv
);
277 rv
= PR_Listen( listenSock
, 5 );
278 PR_ASSERT( PR_SUCCESS
== rv
);
280 /* open a file for writing, provoke bug */
281 file1
= PR_Open("xxxTestFile", PR_CREATE_FILE
| PR_RDWR
, 666);
283 /* create Connect thread */
284 tConnect
= PR_CreateThread(
285 PR_USER_THREAD
, ConnectThread
, NULL
,
286 PR_PRIORITY_NORMAL
, PR_GLOBAL_THREAD
,
287 PR_JOINABLE_THREAD
, 0 );
288 PR_ASSERT( tConnect
);
290 /* create jitter off thread */
291 tJitter
= PR_CreateThread(
292 PR_USER_THREAD
, JitterThread
, NULL
,
293 PR_PRIORITY_NORMAL
, PR_GLOBAL_THREAD
,
294 PR_JOINABLE_THREAD
, 0 );
295 PR_ASSERT( tJitter
);
297 /* create acceptread thread */
298 tAccept
= PR_CreateThread(
299 PR_USER_THREAD
, AcceptThread
, NULL
,
300 PR_PRIORITY_NORMAL
, PR_LOCAL_THREAD
,
301 PR_JOINABLE_THREAD
, 0 );
302 PR_ASSERT( tAccept
);
304 /* wait for all threads to quit, then terminate gracefully */
305 PR_JoinThread( tConnect
);
306 PR_JoinThread( tAccept
);
307 PR_JoinThread( tJitter
);
308 PR_Close( listenSock
);
309 PR_DestroyCondVar(cv
);
312 PR_Delete( "xxxTestFile");
314 /* test return and exit */
315 if (debug
) printf("%s\n", (failed_already
)? "FAIL" : "PASS");
316 return( (failed_already
== PR_TRUE
)? 1 : 0 );