VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/TestPipes.cpp@ 84032

Last change on this file since 84032 was 25762, checked in by vboxsync, 15 years ago

shut up gcc.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 mozilla.org code.
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
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Pierre Phaneuf <[email protected]>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39#include "nsIThread.h"
40#include "nsIRunnable.h"
41#include "nsIInputStream.h"
42#include "nsIOutputStream.h"
43#include "nsIServiceManager.h"
44#include "prprf.h"
45#include "prinrval.h"
46#include "plstr.h"
47#include "nsCRT.h"
48#include "nsCOMPtr.h"
49#include <stdio.h>
50#include "nsIPipe.h" // new implementation
51#include "nsAutoLock.h"
52#include <stdlib.h> // for rand
53
54#define KEY 0xa7
55#define ITERATIONS 33333
56char kTestPattern[] = "My hovercraft is full of eels.\n";
57
58PRBool gTrace = PR_FALSE;
59
60static nsresult
61WriteAll(nsIOutputStream *os, const char *buf, PRUint32 bufLen, PRUint32 *lenWritten)
62{
63 const char *p = buf;
64 *lenWritten = 0;
65 while (bufLen) {
66 PRUint32 n;
67 nsresult rv = os->Write(p, bufLen, &n);
68 if (NS_FAILED(rv)) return rv;
69 p += n;
70 bufLen -= n;
71 *lenWritten += n;
72 }
73 return NS_OK;
74}
75
76class nsReceiver : public nsIRunnable {
77public:
78 NS_DECL_ISUPPORTS
79
80 NS_IMETHOD Run() {
81 nsresult rv;
82 char buf[101];
83 PRUint32 count;
84 PRIntervalTime start = PR_IntervalNow();
85 while (PR_TRUE) {
86 rv = mIn->Read(buf, 100, &count);
87 if (NS_FAILED(rv)) {
88 printf("read failed\n");
89 break;
90 }
91 if (count == 0) {
92// printf("EOF count = %d\n", mCount);
93 break;
94 }
95
96 if (gTrace) {
97 printf("read: ");
98 buf[count] = '\0';
99 printf(buf);
100 printf("\n");
101 }
102 mCount += count;
103 }
104 PRIntervalTime end = PR_IntervalNow();
105 printf("read %d bytes, time = %dms\n", mCount,
106 PR_IntervalToMilliseconds(end - start));
107 return rv;
108 }
109
110 nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
111 NS_ADDREF(in);
112 }
113
114 PRUint32 GetBytesRead() { return mCount; }
115
116private:
117 ~nsReceiver() {
118 NS_RELEASE(mIn);
119 }
120
121protected:
122 nsIInputStream* mIn;
123 PRUint32 mCount;
124};
125
126NS_IMPL_THREADSAFE_ISUPPORTS1(nsReceiver, nsIRunnable)
127
128nsresult
129TestPipe(nsIInputStream* in, nsIOutputStream* out)
130{
131 nsresult rv;
132 nsIThread* thread;
133 nsReceiver* receiver = new nsReceiver(in);
134 if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
135 NS_ADDREF(receiver);
136
137 rv = NS_NewThread(&thread, receiver, 0, PR_JOINABLE_THREAD);
138 if (NS_FAILED(rv)) return rv;
139
140 PRUint32 total = 0;
141 PRIntervalTime start = PR_IntervalNow();
142 for (PRUint32 i = 0; i < ITERATIONS; i++) {
143 PRUint32 writeCount;
144 char *buf = PR_smprintf("%d %s", i, kTestPattern);
145 PRUint32 len = strlen(buf);
146 rv = WriteAll(out, buf, len, &writeCount);
147 if (gTrace) {
148 printf("wrote: ");
149 for (PRUint32 j = 0; j < writeCount; j++) {
150 putc(buf[j], stdout);
151 }
152 printf("\n");
153 }
154 PR_smprintf_free(buf);
155 if (NS_FAILED(rv)) return rv;
156 total += writeCount;
157 }
158 rv = out->Close();
159 if (NS_FAILED(rv)) return rv;
160
161 PRIntervalTime end = PR_IntervalNow();
162
163 thread->Join();
164
165 printf("wrote %d bytes, time = %dms\n", total,
166 PR_IntervalToMilliseconds(end - start));
167 NS_ASSERTION(receiver->GetBytesRead() == total, "didn't read everything");
168
169 NS_RELEASE(thread);
170 NS_RELEASE(receiver);
171
172 return NS_OK;
173}
174
175////////////////////////////////////////////////////////////////////////////////
176
177class nsShortReader : public nsIRunnable {
178public:
179 NS_DECL_ISUPPORTS
180
181 NS_IMETHOD Run() {
182 nsresult rv;
183 char buf[101];
184 PRUint32 count;
185 PRUint32 total = 0;
186 while (PR_TRUE) {
187 //if (gTrace)
188 // printf("calling Read\n");
189 rv = mIn->Read(buf, 100, &count);
190 if (NS_FAILED(rv)) {
191 printf("read failed\n");
192 break;
193 }
194 if (count == 0) {
195 break;
196 }
197 buf[count] = '\0';
198 if (gTrace)
199 printf("read %d bytes: %s\n", count, buf);
200 Received(count);
201 total += count;
202 }
203 printf("read %d bytes\n", total);
204 return rv;
205 }
206
207 nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
208 NS_ADDREF(in);
209 }
210
211 void Received(PRUint32 count) {
212 nsAutoCMonitor mon(this);
213 mReceived += count;
214 mon.Notify();
215 }
216
217 PRUint32 WaitForReceipt() {
218 nsAutoCMonitor mon(this);
219 PRUint32 result = mReceived;
220 while (result == 0) {
221 mon.Wait();
222 NS_ASSERTION(mReceived >= 0, "failed to receive");
223 result = mReceived;
224 }
225 mReceived = 0;
226 return result;
227 }
228
229private:
230 ~nsShortReader() {
231 NS_RELEASE(mIn);
232 }
233
234protected:
235 nsIInputStream* mIn;
236 PRUint32 mReceived;
237};
238
239NS_IMPL_THREADSAFE_ISUPPORTS1(nsShortReader, nsIRunnable)
240
241nsresult
242TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
243{
244 nsresult rv;
245 nsIThread* thread;
246 nsShortReader* receiver = new nsShortReader(in);
247 if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
248 NS_ADDREF(receiver);
249
250 rv = NS_NewThread(&thread, receiver, 0, PR_JOINABLE_THREAD);
251 if (NS_FAILED(rv)) return rv;
252
253 PRUint32 total = 0;
254 for (PRUint32 i = 0; i < ITERATIONS; i++) {
255 PRUint32 writeCount;
256 char* buf = PR_smprintf("%d %s", i, kTestPattern);
257 PRUint32 len = strlen(buf);
258 len = len * rand() / RAND_MAX;
259 len = PR_MAX(1, len);
260 rv = WriteAll(out, buf, len, &writeCount);
261 if (NS_FAILED(rv)) return rv;
262 NS_ASSERTION(writeCount == len, "didn't write enough");
263 total += writeCount;
264
265 if (gTrace)
266 printf("wrote %d bytes: %s\n", writeCount, buf);
267 PR_smprintf_free(buf);
268 //printf("calling Flush\n");
269 out->Flush();
270 //printf("calling WaitForReceipt\n");
271 PRUint32 received = receiver->WaitForReceipt();
272 NS_ASSERTION(received == writeCount, "received wrong amount");
273 }
274 rv = out->Close();
275 if (NS_FAILED(rv)) return rv;
276
277 thread->Join();
278 printf("wrote %d bytes\n", total);
279
280 NS_RELEASE(thread);
281 NS_RELEASE(receiver);
282
283 return NS_OK;
284}
285
286////////////////////////////////////////////////////////////////////////////////
287
288#if 0
289
290class nsPipeObserver : public nsIInputStreamObserver,
291 public nsIOutputStreamObserver
292{
293public:
294 NS_DECL_ISUPPORTS
295
296 NS_IMETHOD OnFull(nsIOutputStream *outStr) {
297 printf("OnFull outStr=%p\n", outStr);
298 return NS_OK;
299 }
300
301 NS_IMETHOD OnWrite(nsIOutputStream *outStr, PRUint32 amount) {
302 printf("OnWrite outStr=%p amount=%d\n", outStr, amount);
303 return NS_OK;
304 }
305
306 NS_IMETHOD OnEmpty(nsIInputStream* inStr) {
307 printf("OnEmpty inStr=%p\n", inStr);
308 return NS_OK;
309 }
310
311 NS_IMETHOD OnClose(nsIInputStream* inStr) {
312 printf("OnClose inStr=%p\n", inStr);
313 return NS_OK;
314 }
315
316 nsPipeObserver() { }
317
318private:
319 ~nsPipeObserver() {}
320};
321
322NS_IMPL_ISUPPORTS2(nsPipeObserver, nsIInputStreamObserver, nsIOutputStreamObserver)
323
324nsresult
325TestPipeObserver()
326{
327 nsresult rv;
328 nsPipeObserver* obs = new nsPipeObserver();
329 if (obs == nsnull) return NS_ERROR_OUT_OF_MEMORY;
330 NS_ADDREF(obs);
331
332 printf("TestPipeObserver: OnWrite and OnFull should be called once, OnEmpty should be called twice.\n");
333 nsIInputStream* in;
334 nsIOutputStream* out;
335 rv = NS_NewPipe(&in, &out, 18, 36, PR_TRUE, PR_TRUE);
336 if (NS_FAILED(rv)) return rv;
337
338 nsCOMPtr<nsIObservableInputStream> observableIn(do_QueryInterface(in, &rv));
339 if (NS_FAILED(rv)) return rv;
340 nsCOMPtr<nsIObservableInputStream> observableOut(do_QueryInterface(out, &rv));
341 if (NS_FAILED(rv)) return rv;
342
343 rv = observableIn->SetObserver(obs);
344 if (NS_FAILED(rv)) return rv;
345 rv = observableOut->SetObserver(obs);
346 if (NS_FAILED(rv)) return rv;
347
348 char buf[] = "puirt a beul: a style of Gaelic vocal music intended for dancing.";
349 PRUint32 cnt;
350 printf("> should see OnWrite message:\n");
351 rv = out->Write(buf, 20, &cnt);
352 if (NS_FAILED(rv)) return rv;
353 NS_ASSERTION(cnt == 20, "Write failed");
354
355 printf("> should see OnWrite message followed by OnFull message:\n");
356 rv = out->Write(buf + 20, 20, &cnt);
357 if (NS_FAILED(rv)) return rv;
358 NS_ASSERTION(cnt == 16, "Write failed");
359
360 rv = in->Available(&cnt);
361 if (NS_FAILED(rv)) return rv;
362 printf("available = %u\n", cnt);
363 NS_ASSERTION(cnt == 36, "Available failed");
364
365 char buf2[40];
366 printf("> should see OnEmpty message:\n");
367 rv = in->Read(buf2, 40, &cnt);
368 if (NS_FAILED(rv)) return rv;
369 printf("cnt = %u\n", cnt);
370 NS_ASSERTION(cnt == 36, "Read failed");
371 NS_ASSERTION(nsCRT::strncmp(buf, buf2, 36) == 0, "Read wrong stuff");
372
373 rv = in->Available(&cnt);
374 if (NS_FAILED(rv)) return rv;
375 printf("available = %u\n", cnt);
376 NS_ASSERTION(cnt == 0, "Available failed");
377
378 printf("> should see OnEmpty message:\n");
379 rv = in->Read(buf2, 2, &cnt);
380 if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) return rv;
381 NS_ASSERTION(cnt == 0 && rv == NS_BASE_STREAM_WOULD_BLOCK, "Read failed");
382
383 printf("> should see OnWrite message:\n");
384 rv = out->Write(buf, 20, &cnt);
385 if (NS_FAILED(rv)) return rv;
386 NS_ASSERTION(cnt == 20, "Write failed");
387
388 rv = in->Available(&cnt);
389 if (NS_FAILED(rv)) return rv;
390 printf("available = %u\n", cnt);
391 NS_ASSERTION(cnt == 20, "Available failed");
392
393 printf("> should see OnEmpty message:\n");
394 rv = in->Read(buf2, 20, &cnt);
395 if (NS_FAILED(rv)) return rv;
396 NS_ASSERTION(cnt == 20, "Read failed");
397
398 printf("> should see OnClose message:\n");
399 NS_RELEASE(obs);
400 NS_RELEASE(in);
401 NS_RELEASE(out);
402 return NS_OK;
403}
404#endif
405
406////////////////////////////////////////////////////////////////////////////////
407
408class nsPump : public nsIRunnable
409{
410public:
411 NS_DECL_ISUPPORTS
412
413 NS_IMETHOD Run() {
414 nsresult rv;
415 PRUint32 count;
416 while (PR_TRUE) {
417 nsAutoCMonitor mon(this);
418 rv = mOut->WriteFrom(mIn, ~0U, &count);
419 if (NS_FAILED(rv)) {
420 printf("Write failed\n");
421 break;
422 }
423 if (count == 0) {
424 printf("EOF count = %d\n", mCount);
425 break;
426 }
427
428 if (gTrace) {
429 printf("Wrote: %d\n", count);
430 }
431 mCount += count;
432 }
433 mOut->Close();
434 return rv;
435 }
436
437 nsPump(nsIInputStream* in,
438 nsIOutputStream* out)
439 : mIn(in), mOut(out), mCount(0) {
440 }
441
442private:
443 ~nsPump() {}
444
445protected:
446 nsCOMPtr<nsIInputStream> mIn;
447 nsCOMPtr<nsIOutputStream> mOut;
448 PRUint32 mCount;
449};
450
451NS_IMPL_THREADSAFE_ISUPPORTS1(nsPump,
452 nsIRunnable)
453
454nsresult
455TestChainedPipes()
456{
457 nsresult rv;
458 printf("TestChainedPipes\n");
459
460 nsIInputStream* in1;
461 nsIOutputStream* out1;
462 rv = NS_NewPipe(&in1, &out1, 20, 1999);
463 if (NS_FAILED(rv)) return rv;
464
465 nsIInputStream* in2;
466 nsIOutputStream* out2;
467 rv = NS_NewPipe(&in2, &out2, 200, 401);
468 if (NS_FAILED(rv)) return rv;
469
470 nsIThread* thread;
471 nsPump* pump = new nsPump(in1, out2);
472 if (pump == nsnull) return NS_ERROR_OUT_OF_MEMORY;
473 NS_ADDREF(pump);
474
475 rv = NS_NewThread(&thread, pump, 0, PR_JOINABLE_THREAD);
476 if (NS_FAILED(rv)) return rv;
477
478 nsIThread* receiverThread;
479 nsReceiver* receiver = new nsReceiver(in2);
480 if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
481 NS_ADDREF(receiver);
482
483 rv = NS_NewThread(&receiverThread, receiver, 0, PR_JOINABLE_THREAD);
484 if (NS_FAILED(rv)) return rv;
485
486 PRUint32 total = 0;
487 for (PRUint32 i = 0; i < ITERATIONS; i++) {
488 PRUint32 writeCount;
489 char* buf = PR_smprintf("%d %s", i, kTestPattern);
490 PRUint32 len = strlen(buf);
491 len = len * rand() / RAND_MAX;
492 len = PR_MAX(1, len);
493 rv = WriteAll(out1, buf, len, &writeCount);
494 if (NS_FAILED(rv)) return rv;
495 NS_ASSERTION(writeCount == len, "didn't write enough");
496 total += writeCount;
497
498 if (gTrace)
499 printf("wrote %d bytes: %s\n", writeCount, buf);
500
501 PR_smprintf_free(buf);
502 }
503 printf("wrote total of %d bytes\n", total);
504 rv = out1->Close();
505 if (NS_FAILED(rv)) return rv;
506
507 thread->Join();
508 receiverThread->Join();
509
510 NS_RELEASE(thread);
511 NS_RELEASE(pump);
512 NS_RELEASE(receiverThread);
513 NS_RELEASE(receiver);
514 return NS_OK;
515}
516
517////////////////////////////////////////////////////////////////////////////////
518
519void
520RunTests(PRUint32 segSize, PRUint32 segCount)
521{
522 nsresult rv;
523 nsIInputStream* in = 0; /* bird: initialize to quiet gcc. */
524 nsIOutputStream* out = 0; /* ditto */
525 PRUint32 bufSize;
526
527 bufSize = segSize * segCount;
528 printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize);
529
530 printf("Testing long writes...\n");
531 rv = NS_NewPipe(&in, &out, segSize, bufSize);
532 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
533 rv = TestPipe(in, out);
534 NS_RELEASE(in);
535 NS_RELEASE(out);
536 NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
537
538 printf("Testing short writes...\n");
539 rv = NS_NewPipe(&in, &out, segSize, bufSize);
540 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
541 rv = TestShortWrites(in, out);
542 NS_RELEASE(in);
543 NS_RELEASE(out);
544 NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
545}
546
547////////////////////////////////////////////////////////////////////////////////
548#if 0
549void
550TestSearch(const char* delim, PRUint32 segSize)
551{
552 nsresult rv;
553 // need at least 2 segments to test boundary conditions:
554 PRUint32 bufDataSize = segSize * 2;
555 PRUint32 bufSize = segSize * 2;
556 nsIInputStream* in;
557 nsIOutputStream* out;
558 rv = NS_NewPipe(&in, &out, segSize, bufSize);
559 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
560 out->SetNonBlocking(PR_TRUE);
561
562 PRUint32 i, j, amt;
563 PRUint32 delimLen = nsCRT::strlen(delim);
564 for (i = 0; i < bufDataSize; i++) {
565 // first fill the buffer
566 for (j = 0; j < i; j++) {
567 rv = out->Write("-", 1, &amt);
568 NS_ASSERTION(NS_SUCCEEDED(rv) && amt == 1, "Write failed");
569 }
570 rv = out->Write(delim, delimLen, &amt);
571 NS_ASSERTION(NS_SUCCEEDED(rv), "Write failed");
572 if (i + amt < bufDataSize) {
573 for (j = i + amt; j < bufDataSize; j++) {
574 rv = out->Write("+", 1, &amt);
575 NS_ASSERTION(NS_SUCCEEDED(rv) && amt == 1, "Write failed");
576 }
577 }
578
579 // now search for the delimiter
580 PRBool found;
581 PRUint32 offset;
582 rv = in->Search(delim, PR_FALSE, &found, &offset);
583 NS_ASSERTION(NS_SUCCEEDED(rv), "Search failed");
584
585 // print the results
586 char* bufferContents = new char[bufDataSize + 1];
587 rv = in->Read(bufferContents, bufDataSize, &amt);
588 NS_ASSERTION(NS_SUCCEEDED(rv) && amt == bufDataSize, "Read failed");
589 bufferContents[bufDataSize] = '\0';
590 printf("Buffer: %s\nDelim: %s %s offset: %d\n", bufferContents,
591 delim, (found ? "found" : "not found"), offset);
592 }
593 NS_RELEASE(in);
594 NS_RELEASE(out);
595}
596#endif
597////////////////////////////////////////////////////////////////////////////////
598
599#ifdef DEBUG
600extern NS_COM void
601TestSegmentedBuffer();
602#endif
603
604int
605main(int argc, char* argv[])
606{
607 nsresult rv;
608 nsIServiceManager* servMgr;
609
610 rv = NS_InitXPCOM2(&servMgr, NULL, NULL);
611 if (NS_FAILED(rv)) return rv;
612
613 if (argc > 1 && nsCRT::strcmp(argv[1], "-trace") == 0)
614 gTrace = PR_TRUE;
615
616#ifdef DEBUG
617 TestSegmentedBuffer();
618#endif
619
620#if 0 // obsolete old implementation
621 rv = NS_NewPipe(&in, &out, 4096 * 4);
622 if (NS_FAILED(rv)) {
623 printf("NewPipe failed\n");
624 return -1;
625 }
626
627 rv = TestPipe(in, out);
628 NS_RELEASE(in);
629 NS_RELEASE(out);
630 if (NS_FAILED(rv)) {
631 printf("TestPipe failed\n");
632 return -1;
633 }
634#endif
635#if 0
636 TestSearch("foo", 8);
637 TestSearch("bar", 6);
638 TestSearch("baz", 2);
639#endif
640
641 rv = TestChainedPipes();
642 NS_ASSERTION(NS_SUCCEEDED(rv), "TestChainedPipes failed");
643 RunTests(16, 1);
644 RunTests(4096, 16);
645 NS_RELEASE(servMgr);
646 rv = NS_ShutdownXPCOM( NULL );
647 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
648 return 0;
649}
650
651////////////////////////////////////////////////////////////////////////////////
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