зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1124041 - Move TestPipes.cpp to gtest and enable it; r=froydnj
This commit is contained in:
Родитель
4dd401ec2f
Коммит
acfa245a0a
|
@ -3,8 +3,9 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "TestHarness.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include "nsIAsyncInputStream.h"
|
||||
#include "nsIAsyncOutputStream.h"
|
||||
#include "nsIThread.h"
|
||||
#include "nsIRunnable.h"
|
||||
#include "nsThreadUtils.h"
|
||||
|
@ -13,76 +14,11 @@
|
|||
#include "nsCRT.h"
|
||||
#include "nsIPipe.h" // new implementation
|
||||
|
||||
#include "mozilla/Monitor.h"
|
||||
#include "mozilla/ReentrantMonitor.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
using namespace mozilla;
|
||||
|
||||
/** NS_NewPipe2 reimplemented, because it's not exported by XPCOM */
|
||||
nsresult TP_NewPipe2(nsIAsyncInputStream** input,
|
||||
nsIAsyncOutputStream** output,
|
||||
bool nonBlockingInput,
|
||||
bool nonBlockingOutput,
|
||||
uint32_t segmentSize,
|
||||
uint32_t segmentCount,
|
||||
nsIMemory* segmentAlloc)
|
||||
{
|
||||
nsCOMPtr<nsIPipe> pipe = do_CreateInstance("@mozilla.org/pipe;1");
|
||||
if (!pipe)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
nsresult rv = pipe->Init(nonBlockingInput,
|
||||
nonBlockingOutput,
|
||||
segmentSize,
|
||||
segmentCount,
|
||||
segmentAlloc);
|
||||
|
||||
if (NS_FAILED(rv))
|
||||
return rv;
|
||||
|
||||
pipe->GetInputStream(input);
|
||||
pipe->GetOutputStream(output);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/** NS_NewPipe reimplemented, because it's not exported by XPCOM */
|
||||
#define TP_DEFAULT_SEGMENT_SIZE 4096
|
||||
nsresult TP_NewPipe(nsIInputStream **pipeIn,
|
||||
nsIOutputStream **pipeOut,
|
||||
uint32_t segmentSize = 0,
|
||||
uint32_t maxSize = 0,
|
||||
bool nonBlockingInput = false,
|
||||
bool nonBlockingOutput = false,
|
||||
nsIMemory *segmentAlloc = nullptr);
|
||||
nsresult TP_NewPipe(nsIInputStream **pipeIn,
|
||||
nsIOutputStream **pipeOut,
|
||||
uint32_t segmentSize,
|
||||
uint32_t maxSize,
|
||||
bool nonBlockingInput,
|
||||
bool nonBlockingOutput,
|
||||
nsIMemory *segmentAlloc)
|
||||
{
|
||||
if (segmentSize == 0)
|
||||
segmentSize = TP_DEFAULT_SEGMENT_SIZE;
|
||||
|
||||
// Handle maxSize of UINT32_MAX as a special case
|
||||
uint32_t segmentCount;
|
||||
if (maxSize == UINT32_MAX)
|
||||
segmentCount = UINT32_MAX;
|
||||
else
|
||||
segmentCount = maxSize / segmentSize;
|
||||
|
||||
nsIAsyncInputStream *in;
|
||||
nsIAsyncOutputStream *out;
|
||||
nsresult rv = TP_NewPipe2(&in, &out, nonBlockingInput, nonBlockingOutput,
|
||||
segmentSize, segmentCount, segmentAlloc);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
*pipeIn = in;
|
||||
*pipeOut = out;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
#define KEY 0xa7
|
||||
#define ITERATIONS 33333
|
||||
char kTestPattern[] = "My hovercraft is full of eels.\n";
|
||||
|
||||
|
@ -104,11 +40,11 @@ WriteAll(nsIOutputStream *os, const char *buf, uint32_t bufLen, uint32_t *lenWri
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
class nsReceiver : public nsIRunnable {
|
||||
class nsReceiver MOZ_FINAL : public nsIRunnable {
|
||||
public:
|
||||
NS_DECL_THREADSAFE_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Run() {
|
||||
NS_IMETHOD Run() MOZ_OVERRIDE {
|
||||
nsresult rv;
|
||||
char buf[101];
|
||||
uint32_t count;
|
||||
|
@ -136,11 +72,14 @@ public:
|
|||
return rv;
|
||||
}
|
||||
|
||||
nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
|
||||
explicit nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
|
||||
}
|
||||
|
||||
uint32_t GetBytesRead() { return mCount; }
|
||||
|
||||
private:
|
||||
~nsReceiver() {}
|
||||
|
||||
protected:
|
||||
nsCOMPtr<nsIInputStream> mIn;
|
||||
uint32_t mCount;
|
||||
|
@ -151,7 +90,7 @@ NS_IMPL_ISUPPORTS(nsReceiver, nsIRunnable)
|
|||
nsresult
|
||||
TestPipe(nsIInputStream* in, nsIOutputStream* out)
|
||||
{
|
||||
nsCOMPtr<nsReceiver> receiver = new nsReceiver(in);
|
||||
nsRefPtr<nsReceiver> receiver = new nsReceiver(in);
|
||||
if (!receiver)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
|
@ -188,18 +127,18 @@ TestPipe(nsIInputStream* in, nsIOutputStream* out)
|
|||
|
||||
printf("wrote %d bytes, time = %dms\n", total,
|
||||
PR_IntervalToMilliseconds(end - start));
|
||||
NS_ASSERTION(receiver->GetBytesRead() == total, "didn't read everything");
|
||||
EXPECT_EQ(receiver->GetBytesRead(), total);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class nsShortReader : public nsIRunnable {
|
||||
class nsShortReader MOZ_FINAL : public nsIRunnable {
|
||||
public:
|
||||
NS_DECL_THREADSAFE_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Run() {
|
||||
NS_IMETHOD Run() MOZ_OVERRIDE {
|
||||
nsresult rv;
|
||||
char buf[101];
|
||||
uint32_t count;
|
||||
|
@ -230,24 +169,24 @@ public:
|
|||
return rv;
|
||||
}
|
||||
|
||||
nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
|
||||
mMon = new Monitor("nsShortReader");
|
||||
explicit nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
|
||||
mMon = new ReentrantMonitor("nsShortReader");
|
||||
}
|
||||
|
||||
void Received(uint32_t count) {
|
||||
MonitorAutoEnter mon(*mMon);
|
||||
ReentrantMonitorAutoEnter mon(*mMon);
|
||||
mReceived += count;
|
||||
mon.Notify();
|
||||
}
|
||||
|
||||
uint32_t WaitForReceipt(const uint32_t aWriteCount) {
|
||||
MonitorAutoEnter mon(*mMon);
|
||||
ReentrantMonitorAutoEnter mon(*mMon);
|
||||
uint32_t result = mReceived;
|
||||
|
||||
while (result < aWriteCount) {
|
||||
mon.Wait();
|
||||
|
||||
NS_ASSERTION(mReceived > result, "failed to receive");
|
||||
EXPECT_TRUE(mReceived > result);
|
||||
result = mReceived;
|
||||
}
|
||||
|
||||
|
@ -255,10 +194,13 @@ public:
|
|||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
~nsShortReader() {}
|
||||
|
||||
protected:
|
||||
nsCOMPtr<nsIInputStream> mIn;
|
||||
uint32_t mReceived;
|
||||
Monitor* mMon;
|
||||
ReentrantMonitor* mMon;
|
||||
};
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable)
|
||||
|
@ -266,7 +208,7 @@ NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable)
|
|||
nsresult
|
||||
TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
|
||||
{
|
||||
nsCOMPtr<nsShortReader> receiver = new nsShortReader(in);
|
||||
nsRefPtr<nsShortReader> receiver = new nsShortReader(in);
|
||||
if (!receiver)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
|
@ -282,10 +224,10 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
|
|||
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
||||
uint32_t len = strlen(buf);
|
||||
len = len * rand() / RAND_MAX;
|
||||
len = XPCOM_MAX(1, len);
|
||||
len = std::min(1u, len);
|
||||
rv = WriteAll(out, buf, len, &writeCount);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
NS_ASSERTION(writeCount == len, "didn't write enough");
|
||||
EXPECT_EQ(writeCount, len);
|
||||
total += writeCount;
|
||||
|
||||
if (gTrace)
|
||||
|
@ -297,9 +239,9 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
|
|||
|
||||
#ifdef DEBUG
|
||||
const uint32_t received =
|
||||
#endif
|
||||
receiver->WaitForReceipt(writeCount);
|
||||
NS_ASSERTION(received == writeCount, "received wrong amount");
|
||||
EXPECT_EQ(received, writeCount);
|
||||
#endif
|
||||
}
|
||||
rv = out->Close();
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
@ -313,12 +255,12 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
|
|||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class nsPump : public nsIRunnable
|
||||
class nsPump MOZ_FINAL : public nsIRunnable
|
||||
{
|
||||
public:
|
||||
NS_DECL_THREADSAFE_ISUPPORTS
|
||||
|
||||
NS_IMETHOD Run() {
|
||||
NS_IMETHOD Run() MOZ_OVERRIDE {
|
||||
nsresult rv;
|
||||
uint32_t count;
|
||||
while (true) {
|
||||
|
@ -346,6 +288,9 @@ public:
|
|||
: mIn(in), mOut(out), mCount(0) {
|
||||
}
|
||||
|
||||
private:
|
||||
~nsPump() {}
|
||||
|
||||
protected:
|
||||
nsCOMPtr<nsIInputStream> mIn;
|
||||
nsCOMPtr<nsIOutputStream> mOut;
|
||||
|
@ -354,35 +299,36 @@ protected:
|
|||
|
||||
NS_IMPL_ISUPPORTS(nsPump, nsIRunnable)
|
||||
|
||||
nsresult
|
||||
TestChainedPipes()
|
||||
TEST(Pipes, ChainedPipes)
|
||||
{
|
||||
nsresult rv;
|
||||
if (gTrace) {
|
||||
printf("TestChainedPipes\n");
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIInputStream> in1;
|
||||
nsCOMPtr<nsIOutputStream> out1;
|
||||
rv = TP_NewPipe(getter_AddRefs(in1), getter_AddRefs(out1), 20, 1999);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
rv = NS_NewPipe(getter_AddRefs(in1), getter_AddRefs(out1), 20, 1999);
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
nsCOMPtr<nsIInputStream> in2;
|
||||
nsCOMPtr<nsIOutputStream> out2;
|
||||
rv = TP_NewPipe(getter_AddRefs(in2), getter_AddRefs(out2), 200, 401);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
rv = NS_NewPipe(getter_AddRefs(in2), getter_AddRefs(out2), 200, 401);
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
nsCOMPtr<nsPump> pump = new nsPump(in1, out2);
|
||||
if (pump == nullptr) return NS_ERROR_OUT_OF_MEMORY;
|
||||
nsRefPtr<nsPump> pump = new nsPump(in1, out2);
|
||||
if (pump == nullptr) return;
|
||||
|
||||
nsCOMPtr<nsIThread> thread;
|
||||
rv = NS_NewThread(getter_AddRefs(thread), pump);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
nsCOMPtr<nsReceiver> receiver = new nsReceiver(in2);
|
||||
if (receiver == nullptr) return NS_ERROR_OUT_OF_MEMORY;
|
||||
nsRefPtr<nsReceiver> receiver = new nsReceiver(in2);
|
||||
if (receiver == nullptr) return;
|
||||
|
||||
nsCOMPtr<nsIThread> receiverThread;
|
||||
rv = NS_NewThread(getter_AddRefs(receiverThread), receiver);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
uint32_t total = 0;
|
||||
for (uint32_t i = 0; i < ITERATIONS; i++) {
|
||||
|
@ -390,10 +336,10 @@ TestChainedPipes()
|
|||
char* buf = PR_smprintf("%d %s", i, kTestPattern);
|
||||
uint32_t len = strlen(buf);
|
||||
len = len * rand() / RAND_MAX;
|
||||
len = XPCOM_MAX(1, len);
|
||||
len = std::max(1u, len);
|
||||
rv = WriteAll(out1, buf, len, &writeCount);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
NS_ASSERTION(writeCount == len, "didn't write enough");
|
||||
if (NS_FAILED(rv)) return;
|
||||
EXPECT_EQ(writeCount, len);
|
||||
total += writeCount;
|
||||
|
||||
if (gTrace)
|
||||
|
@ -401,14 +347,14 @@ TestChainedPipes()
|
|||
|
||||
PR_smprintf_free(buf);
|
||||
}
|
||||
if (gTrace) {
|
||||
printf("wrote total of %d bytes\n", total);
|
||||
}
|
||||
rv = out1->Close();
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
if (NS_FAILED(rv)) return;
|
||||
|
||||
thread->Shutdown();
|
||||
receiverThread->Shutdown();
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -420,50 +366,26 @@ RunTests(uint32_t segSize, uint32_t segCount)
|
|||
nsCOMPtr<nsIInputStream> in;
|
||||
nsCOMPtr<nsIOutputStream> out;
|
||||
uint32_t bufSize = segSize * segCount;
|
||||
if (gTrace) {
|
||||
printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize);
|
||||
|
||||
printf("Testing long writes...\n");
|
||||
rv = TP_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TP_NewPipe failed");
|
||||
}
|
||||
rv = NS_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize);
|
||||
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
||||
rv = TestPipe(in, out);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
|
||||
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
||||
|
||||
if (gTrace) {
|
||||
printf("Testing short writes...\n");
|
||||
rv = TP_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TP_NewPipe failed");
|
||||
}
|
||||
rv = NS_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize);
|
||||
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
||||
rv = TestShortWrites(in, out);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed");
|
||||
EXPECT_TRUE(NS_SUCCEEDED(rv));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#if 0
|
||||
extern void
|
||||
TestSegmentedBuffer();
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
TEST(Pipes, Main)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsIServiceManager> servMgr;
|
||||
rv = NS_InitXPCOM2(getter_AddRefs(servMgr), nullptr, nullptr);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
if (argc > 1 && nsCRT::strcmp(argv[1], "-trace") == 0)
|
||||
gTrace = true;
|
||||
|
||||
rv = TestChainedPipes();
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "TestChainedPipes failed");
|
||||
RunTests(16, 1);
|
||||
RunTests(4096, 16);
|
||||
|
||||
servMgr = 0;
|
||||
rv = NS_ShutdownXPCOM(nullptr);
|
||||
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
|
@ -8,6 +8,7 @@ UNIFIED_SOURCES += [
|
|||
'TestCRT.cpp',
|
||||
'TestEncoding.cpp',
|
||||
'TestExpirationTracker.cpp',
|
||||
'TestPipes.cpp',
|
||||
'TestSnappyStreams.cpp',
|
||||
'TestStrings.cpp',
|
||||
]
|
||||
|
|
|
@ -80,7 +80,6 @@ if CONFIG['MOZ_MEMORY']:
|
|||
|
||||
# XXX Make these tests work in libxul builds.
|
||||
#CPP_UNIT_TESTS += [
|
||||
# 'TestPipes',
|
||||
# 'TestPriorityQueue',
|
||||
# 'TestStorageStream',
|
||||
# 'TestSynchronization',
|
||||
|
|
Загрузка…
Ссылка в новой задаче