2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-04-13 22:17:29 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-04-13 22:17:29 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-04-13 22:17:29 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
1999-12-01 03:21:53 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-29 00:14:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-04-13 22:17:29 +04:00
|
|
|
|
|
|
|
#include "nsIThread.h"
|
1999-09-16 02:33:38 +04:00
|
|
|
#include "nsIRunnable.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
1999-05-07 09:30:30 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include "prprf.h"
|
1999-04-22 11:31:03 +04:00
|
|
|
#include "prinrval.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include "plstr.h"
|
1999-05-01 02:55:12 +04:00
|
|
|
#include "nsCRT.h"
|
1999-08-24 12:45:17 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-04-13 22:17:29 +04:00
|
|
|
#include <stdio.h>
|
1999-08-24 12:45:17 +04:00
|
|
|
#include "nsIPipe.h" // new implementation
|
1999-08-10 23:27:05 +04:00
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include <stdlib.h> // for rand
|
1999-04-13 22:17:29 +04:00
|
|
|
|
|
|
|
#define KEY 0xa7
|
1999-05-01 02:55:12 +04:00
|
|
|
#define ITERATIONS 33333
|
1999-04-13 22:17:29 +04:00
|
|
|
char kTestPattern[] = "My hovercraft is full of eels.\n";
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRBool gTrace = PR_FALSE;
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
static nsresult
|
|
|
|
WriteAll(nsIOutputStream *os, const char *buf, PRUint32 bufLen, PRUint32 *lenWritten)
|
|
|
|
{
|
|
|
|
const char *p = buf;
|
|
|
|
*lenWritten = 0;
|
|
|
|
while (bufLen) {
|
|
|
|
PRUint32 n;
|
|
|
|
nsresult rv = os->Write(p, bufLen, &n);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
p += n;
|
|
|
|
bufLen -= n;
|
|
|
|
*lenWritten += n;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-13 22:17:29 +04:00
|
|
|
class nsReceiver : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv;
|
|
|
|
char buf[101];
|
|
|
|
PRUint32 count;
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
while (PR_TRUE) {
|
|
|
|
rv = mIn->Read(buf, 100, &count);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("read failed\n");
|
1999-04-22 11:31:03 +04:00
|
|
|
break;
|
1999-04-13 22:17:29 +04:00
|
|
|
}
|
1999-09-10 02:05:05 +04:00
|
|
|
if (count == 0) {
|
|
|
|
// printf("EOF count = %d\n", mCount);
|
|
|
|
break;
|
|
|
|
}
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
if (gTrace) {
|
|
|
|
printf("read: ");
|
|
|
|
buf[count] = '\0';
|
|
|
|
printf(buf);
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-04-13 22:17:29 +04:00
|
|
|
mCount += count;
|
|
|
|
}
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime end = PR_IntervalNow();
|
1999-05-01 02:55:12 +04:00
|
|
|
printf("read %d bytes, time = %dms\n", mCount,
|
|
|
|
PR_IntervalToMilliseconds(end - start));
|
1999-04-13 22:17:29 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
|
|
|
|
NS_ADDREF(in);
|
|
|
|
}
|
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
PRUint32 GetBytesRead() { return mCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsReceiver() {
|
1999-04-13 22:17:29 +04:00
|
|
|
NS_RELEASE(mIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsIInputStream* mIn;
|
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
2003-09-08 04:31:50 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsReceiver, nsIRunnable)
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
nsresult
|
|
|
|
TestPipe(nsIInputStream* in, nsIOutputStream* out)
|
1999-04-13 22:17:29 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-05-01 02:55:12 +04:00
|
|
|
nsIThread* thread;
|
|
|
|
nsReceiver* receiver = new nsReceiver(in);
|
|
|
|
if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(receiver);
|
1999-04-13 22:17:29 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_NewThread(&thread, receiver);
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-13 22:17:29 +04:00
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRUint32 total = 0;
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime start = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
for (PRUint32 i = 0; i < ITERATIONS; i++) {
|
|
|
|
PRUint32 writeCount;
|
2003-01-18 05:15:14 +03:00
|
|
|
char *buf = PR_smprintf("%d %s", i, kTestPattern);
|
|
|
|
PRUint32 len = strlen(buf);
|
|
|
|
rv = WriteAll(out, buf, len, &writeCount);
|
1999-05-01 02:55:12 +04:00
|
|
|
if (gTrace) {
|
|
|
|
printf("wrote: ");
|
|
|
|
for (PRUint32 j = 0; j < writeCount; j++) {
|
|
|
|
putc(buf[j], stdout);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
1999-04-13 22:17:29 +04:00
|
|
|
PR_smprintf_free(buf);
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
total += writeCount;
|
1999-04-13 22:17:29 +04:00
|
|
|
}
|
|
|
|
rv = out->Close();
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-22 11:31:03 +04:00
|
|
|
PRIntervalTime end = PR_IntervalNow();
|
1999-04-13 22:17:29 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
thread->Shutdown();
|
1999-05-01 02:55:12 +04:00
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
printf("wrote %d bytes, time = %dms\n", total,
|
1999-05-01 02:55:12 +04:00
|
|
|
PR_IntervalToMilliseconds(end - start));
|
|
|
|
NS_ASSERTION(receiver->GetBytesRead() == total, "didn't read everything");
|
|
|
|
|
|
|
|
NS_RELEASE(thread);
|
1999-04-13 22:17:29 +04:00
|
|
|
NS_RELEASE(receiver);
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsShortReader : public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv;
|
|
|
|
char buf[101];
|
|
|
|
PRUint32 count;
|
1999-08-24 12:45:17 +04:00
|
|
|
PRUint32 total = 0;
|
1999-08-10 23:27:05 +04:00
|
|
|
while (PR_TRUE) {
|
2001-01-24 01:10:34 +03:00
|
|
|
//if (gTrace)
|
|
|
|
// printf("calling Read\n");
|
1999-08-10 23:27:05 +04:00
|
|
|
rv = mIn->Read(buf, 100, &count);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("read failed\n");
|
|
|
|
break;
|
|
|
|
}
|
1999-09-10 02:05:05 +04:00
|
|
|
if (count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
1999-08-10 23:27:05 +04:00
|
|
|
buf[count] = '\0';
|
|
|
|
if (gTrace)
|
|
|
|
printf("read %d bytes: %s\n", count, buf);
|
|
|
|
Received(count);
|
1999-08-24 12:45:17 +04:00
|
|
|
total += count;
|
1999-08-10 23:27:05 +04:00
|
|
|
}
|
1999-08-24 12:45:17 +04:00
|
|
|
printf("read %d bytes\n", total);
|
1999-08-10 23:27:05 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
|
|
|
|
NS_ADDREF(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Received(PRUint32 count) {
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
mReceived += count;
|
|
|
|
mon.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 WaitForReceipt() {
|
|
|
|
nsAutoCMonitor mon(this);
|
2003-01-18 05:15:14 +03:00
|
|
|
PRUint32 result = mReceived;
|
|
|
|
while (result == 0) {
|
1999-08-10 23:27:05 +04:00
|
|
|
mon.Wait();
|
|
|
|
NS_ASSERTION(mReceived >= 0, "failed to receive");
|
|
|
|
result = mReceived;
|
|
|
|
}
|
|
|
|
mReceived = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
|
|
|
~nsShortReader() {
|
|
|
|
NS_RELEASE(mIn);
|
|
|
|
}
|
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
protected:
|
|
|
|
nsIInputStream* mIn;
|
|
|
|
PRUint32 mReceived;
|
|
|
|
};
|
|
|
|
|
2003-09-08 04:31:50 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsShortReader, nsIRunnable)
|
1999-08-10 23:27:05 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
|
1999-05-01 02:55:12 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-08-10 23:27:05 +04:00
|
|
|
nsIThread* thread;
|
|
|
|
nsShortReader* receiver = new nsShortReader(in);
|
|
|
|
if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(receiver);
|
1999-05-07 09:30:30 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_NewThread(&thread, receiver);
|
1999-05-07 09:30:30 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
PRUint32 total = 0;
|
|
|
|
for (PRUint32 i = 0; i < ITERATIONS; i++) {
|
1999-08-10 23:27:05 +04:00
|
|
|
PRUint32 writeCount;
|
|
|
|
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
2002-02-20 09:38:38 +03:00
|
|
|
PRUint32 len = strlen(buf);
|
1999-08-10 23:27:05 +04:00
|
|
|
len = len * rand() / RAND_MAX;
|
|
|
|
len = PR_MAX(1, len);
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = WriteAll(out, buf, len, &writeCount);
|
1999-08-10 23:27:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(writeCount == len, "didn't write enough");
|
1999-08-24 12:45:17 +04:00
|
|
|
total += writeCount;
|
1999-08-10 23:27:05 +04:00
|
|
|
|
|
|
|
if (gTrace)
|
|
|
|
printf("wrote %d bytes: %s\n", writeCount, buf);
|
|
|
|
PR_smprintf_free(buf);
|
2001-01-24 01:10:34 +03:00
|
|
|
//printf("calling Flush\n");
|
1999-08-10 23:27:05 +04:00
|
|
|
out->Flush();
|
2001-01-24 01:10:34 +03:00
|
|
|
//printf("calling WaitForReceipt\n");
|
1999-08-10 23:27:05 +04:00
|
|
|
PRUint32 received = receiver->WaitForReceipt();
|
|
|
|
NS_ASSERTION(received == writeCount, "received wrong amount");
|
1999-05-01 02:55:12 +04:00
|
|
|
}
|
1999-08-10 23:27:05 +04:00
|
|
|
rv = out->Close();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
thread->Shutdown();
|
1999-08-24 12:45:17 +04:00
|
|
|
printf("wrote %d bytes\n", total);
|
1999-08-10 23:27:05 +04:00
|
|
|
|
|
|
|
NS_RELEASE(thread);
|
|
|
|
NS_RELEASE(receiver);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-10-09 05:54:07 +04:00
|
|
|
class nsPump : public nsIRunnable
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
1999-08-24 12:45:17 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 count;
|
|
|
|
while (PR_TRUE) {
|
|
|
|
nsAutoCMonitor mon(this);
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = mOut->WriteFrom(mIn, ~0U, &count);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("Write failed\n");
|
|
|
|
break;
|
|
|
|
}
|
1999-09-10 02:05:05 +04:00
|
|
|
if (count == 0) {
|
|
|
|
printf("EOF count = %d\n", mCount);
|
|
|
|
break;
|
|
|
|
}
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
if (gTrace) {
|
|
|
|
printf("Wrote: %d\n", count);
|
|
|
|
}
|
|
|
|
mCount += count;
|
|
|
|
}
|
|
|
|
mOut->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsPump(nsIInputStream* in,
|
|
|
|
nsIOutputStream* out)
|
1999-08-24 12:45:17 +04:00
|
|
|
: mIn(in), mOut(out), mCount(0) {
|
|
|
|
}
|
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
|
|
|
~nsPump() {}
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
protected:
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIInputStream> mIn;
|
|
|
|
nsCOMPtr<nsIOutputStream> mOut;
|
1999-08-24 12:45:17 +04:00
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsPump,
|
|
|
|
nsIRunnable)
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestChainedPipes()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
printf("TestChainedPipes\n");
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsIInputStream* in1;
|
|
|
|
nsIOutputStream* out1;
|
|
|
|
rv = NS_NewPipe(&in1, &out1, 20, 1999);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
nsIInputStream* in2;
|
|
|
|
nsIOutputStream* out2;
|
|
|
|
rv = NS_NewPipe(&in2, &out2, 200, 401);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsIThread* thread;
|
|
|
|
nsPump* pump = new nsPump(in1, out2);
|
|
|
|
if (pump == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(pump);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_NewThread(&thread, pump);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsIThread* receiverThread;
|
|
|
|
nsReceiver* receiver = new nsReceiver(in2);
|
|
|
|
if (receiver == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(receiver);
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
rv = NS_NewThread(&receiverThread, receiver);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 total = 0;
|
|
|
|
for (PRUint32 i = 0; i < ITERATIONS; i++) {
|
|
|
|
PRUint32 writeCount;
|
|
|
|
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
2002-02-20 09:38:38 +03:00
|
|
|
PRUint32 len = strlen(buf);
|
1999-08-24 12:45:17 +04:00
|
|
|
len = len * rand() / RAND_MAX;
|
|
|
|
len = PR_MAX(1, len);
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = WriteAll(out1, buf, len, &writeCount);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(writeCount == len, "didn't write enough");
|
|
|
|
total += writeCount;
|
|
|
|
|
|
|
|
if (gTrace)
|
|
|
|
printf("wrote %d bytes: %s\n", writeCount, buf);
|
|
|
|
|
|
|
|
PR_smprintf_free(buf);
|
|
|
|
}
|
|
|
|
printf("wrote total of %d bytes\n", total);
|
|
|
|
rv = out1->Close();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
thread->Shutdown();
|
|
|
|
receiverThread->Shutdown();
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
NS_RELEASE(thread);
|
|
|
|
NS_RELEASE(pump);
|
|
|
|
NS_RELEASE(receiverThread);
|
|
|
|
NS_RELEASE(receiver);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
void
|
|
|
|
RunTests(PRUint32 segSize, PRUint32 segCount)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-08-22 11:03:33 +04:00
|
|
|
nsIInputStream* in;
|
|
|
|
nsIOutputStream* out;
|
1999-08-10 23:27:05 +04:00
|
|
|
PRUint32 bufSize;
|
|
|
|
|
|
|
|
bufSize = segSize * segCount;
|
|
|
|
printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize);
|
1999-06-09 08:52:47 +04:00
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
printf("Testing long writes...\n");
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = NS_NewPipe(&in, &out, segSize, bufSize);
|
1999-08-10 23:27:05 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
|
1999-06-09 08:52:47 +04:00
|
|
|
rv = TestPipe(in, out);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
1999-08-10 23:27:05 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
|
|
|
|
|
|
|
|
printf("Testing short writes...\n");
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = NS_NewPipe(&in, &out, segSize, bufSize);
|
1999-08-10 23:27:05 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
|
|
|
|
rv = TestShortWrites(in, out);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2000-08-22 11:03:33 +04:00
|
|
|
#if 0
|
1999-08-27 14:32:51 +04:00
|
|
|
void
|
|
|
|
TestSearch(const char* delim, PRUint32 segSize)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
// need at least 2 segments to test boundary conditions:
|
|
|
|
PRUint32 bufDataSize = segSize * 2;
|
|
|
|
PRUint32 bufSize = segSize * 2;
|
2000-08-22 11:03:33 +04:00
|
|
|
nsIInputStream* in;
|
|
|
|
nsIOutputStream* out;
|
|
|
|
rv = NS_NewPipe(&in, &out, segSize, bufSize);
|
1999-08-27 14:32:51 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
|
|
|
|
out->SetNonBlocking(PR_TRUE);
|
|
|
|
|
|
|
|
PRUint32 i, j, amt;
|
|
|
|
PRUint32 delimLen = nsCRT::strlen(delim);
|
|
|
|
for (i = 0; i < bufDataSize; i++) {
|
|
|
|
// first fill the buffer
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
rv = out->Write("-", 1, &amt);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv) && amt == 1, "Write failed");
|
|
|
|
}
|
|
|
|
rv = out->Write(delim, delimLen, &amt);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Write failed");
|
|
|
|
if (i + amt < bufDataSize) {
|
|
|
|
for (j = i + amt; j < bufDataSize; j++) {
|
|
|
|
rv = out->Write("+", 1, &amt);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv) && amt == 1, "Write failed");
|
|
|
|
}
|
|
|
|
}
|
2000-08-22 11:03:33 +04:00
|
|
|
|
1999-08-27 14:32:51 +04:00
|
|
|
// now search for the delimiter
|
|
|
|
PRBool found;
|
|
|
|
PRUint32 offset;
|
|
|
|
rv = in->Search(delim, PR_FALSE, &found, &offset);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Search failed");
|
|
|
|
|
|
|
|
// print the results
|
|
|
|
char* bufferContents = new char[bufDataSize + 1];
|
|
|
|
rv = in->Read(bufferContents, bufDataSize, &amt);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv) && amt == bufDataSize, "Read failed");
|
|
|
|
bufferContents[bufDataSize] = '\0';
|
|
|
|
printf("Buffer: %s\nDelim: %s %s offset: %d\n", bufferContents,
|
|
|
|
delim, (found ? "found" : "not found"), offset);
|
|
|
|
}
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
}
|
2000-08-22 11:03:33 +04:00
|
|
|
#endif
|
1999-08-27 14:32:51 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
extern NS_COM void
|
|
|
|
TestSegmentedBuffer();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsIServiceManager* servMgr;
|
|
|
|
|
2001-10-16 07:35:52 +04:00
|
|
|
rv = NS_InitXPCOM2(&servMgr, NULL, NULL);
|
1999-08-10 23:27:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (argc > 1 && nsCRT::strcmp(argv[1], "-trace") == 0)
|
|
|
|
gTrace = PR_TRUE;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
TestSegmentedBuffer();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0 // obsolete old implementation
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = NS_NewPipe(&in, &out, 4096 * 4);
|
1999-05-01 02:55:12 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("NewPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = TestPipe(in, out);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("TestPipe failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1999-08-10 23:27:05 +04:00
|
|
|
#endif
|
2000-08-22 11:03:33 +04:00
|
|
|
#if 0
|
1999-08-27 14:32:51 +04:00
|
|
|
TestSearch("foo", 8);
|
|
|
|
TestSearch("bar", 6);
|
|
|
|
TestSearch("baz", 2);
|
2000-08-22 11:03:33 +04:00
|
|
|
#endif
|
1999-08-27 14:32:51 +04:00
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
rv = TestChainedPipes();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestChainedPipes failed");
|
1999-08-10 23:27:05 +04:00
|
|
|
RunTests(16, 1);
|
|
|
|
RunTests(4096, 16);
|
2002-07-04 18:29:25 +04:00
|
|
|
NS_RELEASE(servMgr);
|
|
|
|
rv = NS_ShutdownXPCOM( NULL );
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
1999-04-13 22:17:29 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1999-08-10 23:27:05 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|