Bug 462294 - Add "View Video" to context menu for <video> elements. r=gavin, ui...
[wine-gecko.git] / nsprpub / pr / tests / ntioto.c
blob0dc0072affbbe0cc5202452c79bfa4eda5cb9b52
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
39 ** File: ntioto.c
40 ** Description:
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
46 ** asserted.
48 ** Design:
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.
51 **
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.
56 **
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.
71 **
74 #include <plgetopt.h>
75 #include <nspr.h>
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <string.h>
81 ** Test harness infrastructure
83 PRLogModuleInfo *lm;
84 PRLogModuleLevel msgLevel = PR_LOG_NONE;
85 PRIntn debug = 0;
86 PRIntn verbose = 0;
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;
95 PRNetAddr listenAddr;
96 PRFileDesc *listenSock;
97 PRLock *ml;
98 PRCondVar *cv;
99 volatile enum {
100 RunJitter,
101 RunAcceptRead,
102 AllDone
103 } state = RunAcceptRead;
104 PRFileDesc *file1;
105 PRIntn iCounter = 0;
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");
115 exit(1);
116 } /* end Help() */
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)
127 PRIntn bytesRead;
128 char dataBuf[ACCEPT_READ_BUFSIZE];
129 PRFileDesc *arSock;
130 PRNetAddr *arAddr;
132 bytesRead = PR_AcceptRead( listenSock,
133 &arSock,
134 &arAddr,
135 dataBuf,
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");
141 } else {
142 if ( debug ) printf("Oops! read: %d, error: %d\n", bytesRead, PR_GetError());
145 while( state != AllDone ) {
146 PR_Lock( ml );
147 while( state != RunAcceptRead )
148 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
149 if ( ++iCounter >= jitter )
150 state = AllDone;
151 else
152 state = RunJitter;
153 if ( verbose ) printf(".");
154 PR_NotifyCondVar( cv );
155 PR_Unlock( ml );
156 PR_Write( file1, ".", 1 );
159 return;
160 } /* end AcceptThread() */
162 static void JitterThread(void *arg)
164 while( state != AllDone ) {
165 PR_Lock( ml );
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 );
172 PR_Unlock( ml );
173 PR_Write( file1, "+", 1 );
175 return;
176 } /* end Goofy() */
178 static void ConnectThread( void *arg )
180 PRStatus rv;
181 PRFileDesc *clientSock;
182 PRNetAddr serverAddress;
183 clientSock = PR_NewTCPSocket();
185 PR_ASSERT(clientSock);
187 if ( resume ) {
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,
196 &serverAddress,
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));
203 return;
204 } /* end ConnectThread() */
207 PRIntn main(PRIntn argc, char *argv[])
209 PRThread *tJitter;
210 PRThread *tAccept;
211 PRThread *tConnect;
212 PRStatus rv;
213 /* This test if valid for WinNT only! */
215 #if !defined(WINNT)
216 return 0;
217 #endif
221 ** Get command line options
223 PLOptStatus os;
224 PLOptState *opt = PL_CreateOptState(argc, argv, "hdrvj:");
226 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
228 if (PL_OPT_BAD == os) continue;
229 switch (opt->option)
231 case 'd': /* debug */
232 debug = 1;
233 msgLevel = PR_LOG_ERROR;
234 break;
235 case 'v': /* verbose mode */
236 verbose = 1;
237 msgLevel = PR_LOG_DEBUG;
238 break;
239 case 'j':
240 jitter = atoi(opt->value);
241 if ( jitter == 0)
242 jitter = JITTER_DEFAULT;
243 break;
244 case 'r':
245 resume = PR_TRUE;
246 break;
247 case 'h': /* help message */
248 Help();
249 break;
250 default:
251 break;
254 PL_DestroyOptState(opt);
257 lm = PR_NewLogModule("Test"); /* Initialize logging */
259 /* set concurrency */
260 PR_SetConcurrency( 4 );
262 /* setup thread synchronization mechanics */
263 ml = PR_NewLock();
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);
310 PR_DestroyLock(ml);
311 PR_Close( file1 );
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 );
317 } /* main() */
318 /* end ntioto.c */