1999-04-13 22:17:29 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
1999-04-13 22:17:29 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03: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.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-13 22:17:29 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-12-01 03:21:53 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-04-13 22:17:29 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIThread.h"
|
1999-09-16 02:33:38 +04:00
|
|
|
#include "nsIRunnable.h"
|
1999-06-09 00:57:32 +04:00
|
|
|
#if 0 // obsolete old implementation
|
1999-04-13 22:17:29 +04:00
|
|
|
#include "nsIByteBufferInputStream.h"
|
1999-06-09 00:57:32 +04:00
|
|
|
#endif
|
1999-09-10 02:05:05 +04:00
|
|
|
#ifdef OLD_BUFFERS
|
1999-05-01 02:55:12 +04:00
|
|
|
#include "nsIBuffer.h"
|
1999-09-10 02:05:05 +04:00
|
|
|
#endif
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBufferInputStream.h"
|
|
|
|
#include "nsIBufferOutputStream.h"
|
1999-05-07 09:30:30 +04:00
|
|
|
#include "nsIServiceManager.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;
|
|
|
|
|
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_INIT_REFCNT();
|
|
|
|
NS_ADDREF(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsReceiver() {
|
|
|
|
NS_RELEASE(mIn);
|
|
|
|
}
|
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
PRUint32 GetBytesRead() { return mCount; }
|
|
|
|
|
1999-04-13 22:17:29 +04:00
|
|
|
protected:
|
|
|
|
nsIInputStream* mIn;
|
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
1999-12-01 03:21:53 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsReceiver, NS_GET_IID(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
|
|
|
|
1999-10-02 03:30:06 +04:00
|
|
|
rv = NS_NewThread(&thread, receiver, 0, PR_JOINABLE_THREAD);
|
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;
|
|
|
|
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
1999-05-01 02:55:12 +04:00
|
|
|
rv = out->Write(buf, nsCRT::strlen(buf), &writeCount);
|
|
|
|
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
|
|
|
|
1999-05-01 02:55:12 +04:00
|
|
|
thread->Join();
|
|
|
|
|
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) {
|
|
|
|
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_INIT_REFCNT();
|
|
|
|
NS_ADDREF(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsShortReader() {
|
|
|
|
NS_RELEASE(mIn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Received(PRUint32 count) {
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
mReceived += count;
|
|
|
|
mon.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 WaitForReceipt() {
|
|
|
|
PRUint32 result = mReceived;
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
if (mReceived == 0) {
|
|
|
|
mon.Wait();
|
|
|
|
NS_ASSERTION(mReceived >= 0, "failed to receive");
|
|
|
|
result = mReceived;
|
|
|
|
}
|
|
|
|
mReceived = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsIInputStream* mIn;
|
|
|
|
PRUint32 mReceived;
|
|
|
|
};
|
|
|
|
|
1999-12-01 03:21:53 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsShortReader, NS_GET_IID(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
|
|
|
|
1999-10-02 03:30:06 +04:00
|
|
|
rv = NS_NewThread(&thread, receiver, 0, PR_JOINABLE_THREAD);
|
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);
|
|
|
|
PRUint32 len = nsCRT::strlen(buf);
|
|
|
|
len = len * rand() / RAND_MAX;
|
|
|
|
len = PR_MAX(1, len);
|
|
|
|
rv = out->Write(buf, len, &writeCount);
|
|
|
|
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);
|
|
|
|
out->Flush();
|
|
|
|
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;
|
|
|
|
|
|
|
|
thread->Join();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
class nsPipeObserver : public nsIPipeObserver {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD OnFull(nsIPipe *pipe) {
|
|
|
|
printf("OnFull pipe=%p\n", pipe);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnWrite(nsIPipe *pipe, PRUint32 amount) {
|
|
|
|
printf("OnWrite pipe=%p amount=%d\n", pipe, amount);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnEmpty(nsIPipe *pipe) {
|
|
|
|
printf("OnEmpty pipe=%p\n", pipe);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-05 08:07:46 +03:00
|
|
|
NS_IMETHOD OnClose(nsIPipe *pipe) {
|
|
|
|
printf("OnClose pipe=%p\n", pipe);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
nsPipeObserver() { NS_INIT_REFCNT(); }
|
|
|
|
virtual ~nsPipeObserver() {}
|
|
|
|
};
|
|
|
|
|
1999-12-01 03:21:53 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsPipeObserver, NS_GET_IID(nsIPipeObserver));
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestPipeObserver()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsPipeObserver* obs = new nsPipeObserver();
|
|
|
|
if (obs == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(obs);
|
|
|
|
|
1999-11-13 09:18:34 +03:00
|
|
|
printf("TestPipeObserver: OnWrite and OnFull should be called once, OnEmpty should be called twice.\n");
|
1999-08-24 12:45:17 +04:00
|
|
|
nsIBufferInputStream* in;
|
|
|
|
nsIBufferOutputStream* out;
|
|
|
|
rv = NS_NewPipe(&in, &out, obs, 20, 20);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = in->SetNonBlocking(PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = out->SetNonBlocking(PR_TRUE);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
char buf[] = "puirt a beul: a style of Gaelic vocal music intended for dancing.";
|
|
|
|
PRUint32 cnt;
|
|
|
|
// this should print OnWrite message:
|
|
|
|
rv = out->Write(buf, 20, &cnt);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(cnt == 20, "Write failed");
|
|
|
|
|
|
|
|
// this should print OnFull message:
|
|
|
|
rv = out->Write(buf + 20, 1, &cnt);
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) return rv;
|
|
|
|
NS_ASSERTION(cnt == 0 && rv == NS_BASE_STREAM_WOULD_BLOCK, "Write failed");
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
char buf2[20];
|
|
|
|
rv = in->Read(buf2, 20, &cnt);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_ASSERTION(cnt == 20, "Read failed");
|
|
|
|
NS_ASSERTION(nsCRT::strncmp(buf, buf2, 20) == 0, "Read wrong stuff");
|
|
|
|
|
|
|
|
// this should print OnEmpty message:
|
|
|
|
rv = in->Read(buf2, 1, &cnt);
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) return rv;
|
|
|
|
NS_ASSERTION(cnt == 0 && rv == NS_BASE_STREAM_WOULD_BLOCK, "Read failed");
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
NS_RELEASE(obs);
|
|
|
|
NS_RELEASE(in);
|
|
|
|
NS_RELEASE(out);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class nsPump : public nsIPipeObserver, public nsIRunnable {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD OnFull(nsIPipe *pipe) {
|
|
|
|
printf("OnFull pipe=%p\n", pipe);
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
mon.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnWrite(nsIPipe *pipe, PRUint32 amount) {
|
|
|
|
printf("OnWrite pipe=%p amount=%d\n", pipe, amount);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnEmpty(nsIPipe *pipe) {
|
|
|
|
printf("OnEmpty pipe=%p\n", pipe);
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
mon.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-05 08:07:46 +03:00
|
|
|
NS_IMETHOD OnClose(nsIPipe *pipe) {
|
|
|
|
printf("OnClose pipe=%p\n", pipe);
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
mon.Notify();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
NS_IMETHOD Run() {
|
|
|
|
nsresult rv;
|
|
|
|
PRUint32 count;
|
|
|
|
while (PR_TRUE) {
|
|
|
|
nsAutoCMonitor mon(this);
|
|
|
|
rv = mOut->WriteFrom(mIn, -1, &count);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPump(nsIBufferInputStream* in,
|
|
|
|
nsIBufferOutputStream* out)
|
|
|
|
: mIn(in), mOut(out), mCount(0) {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~nsPump() {
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIBufferInputStream> mIn;
|
|
|
|
nsCOMPtr<nsIBufferOutputStream> mOut;
|
|
|
|
PRUint32 mCount;
|
|
|
|
};
|
|
|
|
|
1999-11-13 09:18:34 +03:00
|
|
|
NS_IMPL_ISUPPORTS2(nsPump, nsIPipeObserver, nsIRunnable)
|
1999-08-24 12:45:17 +04:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TestChainedPipes()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
printf("TestChainedPipes\n");
|
|
|
|
|
|
|
|
nsIBufferInputStream* in1;
|
|
|
|
nsIBufferOutputStream* out1;
|
|
|
|
rv = NS_NewPipe(&in1, &out1, nsnull, 20, 1999);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsIBufferInputStream* in2;
|
|
|
|
nsIBufferOutputStream* out2;
|
|
|
|
rv = NS_NewPipe(&in2, &out2, nsnull, 200, 401);
|
|
|
|
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);
|
|
|
|
|
1999-10-02 03:30:06 +04:00
|
|
|
rv = NS_NewThread(&thread, pump, 0, PR_JOINABLE_THREAD);
|
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);
|
|
|
|
|
1999-10-02 03:30:06 +04:00
|
|
|
rv = NS_NewThread(&receiverThread, receiver, 0, PR_JOINABLE_THREAD);
|
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);
|
|
|
|
PRUint32 len = nsCRT::strlen(buf);
|
|
|
|
len = len * rand() / RAND_MAX;
|
|
|
|
len = PR_MAX(1, len);
|
|
|
|
rv = out1->Write(buf, len, &writeCount);
|
|
|
|
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);
|
|
|
|
//out1->Flush(); // wakes up the pump
|
|
|
|
|
|
|
|
PR_smprintf_free(buf);
|
|
|
|
}
|
|
|
|
printf("wrote total of %d bytes\n", total);
|
|
|
|
rv = out1->Close();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
receiverThread->Join();
|
|
|
|
|
|
|
|
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;
|
|
|
|
nsIBufferInputStream* in;
|
|
|
|
nsIBufferOutputStream* out;
|
|
|
|
PRUint32 bufSize;
|
1999-09-10 02:05:05 +04:00
|
|
|
#ifdef OLD_BUFFERS
|
1999-08-10 23:27:05 +04:00
|
|
|
bufSize = (segSize + nsIBuffer::SEGMENT_OVERHEAD) * segCount;
|
|
|
|
printf("Testing Old Pipes: segment size %d buffer size %d\n", segSize, segSize * segCount);
|
1999-05-01 02:55:12 +04:00
|
|
|
|
1999-08-10 23:27:05 +04:00
|
|
|
printf("Testing long writes...\n");
|
|
|
|
rv = NS_NewPipe(&in, &out, segSize + nsIBuffer::SEGMENT_OVERHEAD, bufSize, PR_TRUE, nsnull);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewPipe failed");
|
1999-05-01 02:55:12 +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");
|
|
|
|
rv = NS_NewPipe(&in, &out, segSize + nsIBuffer::SEGMENT_OVERHEAD, bufSize, PR_TRUE, nsnull);
|
|
|
|
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");
|
1999-06-09 00:57:32 +04:00
|
|
|
#endif
|
1999-08-10 23:27:05 +04:00
|
|
|
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");
|
|
|
|
rv = NS_NewPipe(&in, &out, nsnull, segSize, bufSize);
|
|
|
|
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");
|
|
|
|
rv = NS_NewPipe(&in, &out, nsnull, segSize, bufSize);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-06-09 08:52:47 +04:00
|
|
|
|
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;
|
|
|
|
nsIBufferInputStream* in;
|
|
|
|
nsIBufferOutputStream* out;
|
|
|
|
rv = NS_NewPipe(&in, &out, nsnull, segSize, bufSize);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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;
|
|
|
|
|
2000-01-06 04:05:13 +03:00
|
|
|
rv = NS_InitXPCOM(&servMgr, 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
|
|
|
|
rv = NS_NewPipe(&in, &out, PR_TRUE, 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
|
1999-08-27 14:32:51 +04:00
|
|
|
TestSearch("foo", 8);
|
|
|
|
TestSearch("bar", 6);
|
|
|
|
TestSearch("baz", 2);
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
rv = TestPipeObserver();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipeObserver failed");
|
|
|
|
rv = TestChainedPipes();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "TestChainedPipes failed");
|
1999-08-10 23:27:05 +04:00
|
|
|
RunTests(16, 1);
|
|
|
|
RunTests(4096, 16);
|
1999-04-13 22:17:29 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1999-08-10 23:27:05 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|