Bug 1124041 - Move TestPipes.cpp to gtest and enable it; r=froydnj

This commit is contained in:
Ehsan Akhgari 2015-01-20 22:46:48 -05:00
Родитель 4dd401ec2f
Коммит acfa245a0a
3 изменённых файлов: 71 добавлений и 149 удалений

Просмотреть файл

@ -3,8 +3,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * 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 "nsIThread.h"
#include "nsIRunnable.h" #include "nsIRunnable.h"
#include "nsThreadUtils.h" #include "nsThreadUtils.h"
@ -13,76 +14,11 @@
#include "nsCRT.h" #include "nsCRT.h"
#include "nsIPipe.h" // new implementation #include "nsIPipe.h" // new implementation
#include "mozilla/Monitor.h" #include "mozilla/ReentrantMonitor.h"
#include "gtest/gtest.h"
using namespace mozilla; 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 #define ITERATIONS 33333
char kTestPattern[] = "My hovercraft is full of eels.\n"; 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; return NS_OK;
} }
class nsReceiver : public nsIRunnable { class nsReceiver MOZ_FINAL : public nsIRunnable {
public: public:
NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() { NS_IMETHOD Run() MOZ_OVERRIDE {
nsresult rv; nsresult rv;
char buf[101]; char buf[101];
uint32_t count; uint32_t count;
@ -136,11 +72,14 @@ public:
return rv; return rv;
} }
nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) { explicit nsReceiver(nsIInputStream* in) : mIn(in), mCount(0) {
} }
uint32_t GetBytesRead() { return mCount; } uint32_t GetBytesRead() { return mCount; }
private:
~nsReceiver() {}
protected: protected:
nsCOMPtr<nsIInputStream> mIn; nsCOMPtr<nsIInputStream> mIn;
uint32_t mCount; uint32_t mCount;
@ -151,7 +90,7 @@ NS_IMPL_ISUPPORTS(nsReceiver, nsIRunnable)
nsresult nsresult
TestPipe(nsIInputStream* in, nsIOutputStream* out) TestPipe(nsIInputStream* in, nsIOutputStream* out)
{ {
nsCOMPtr<nsReceiver> receiver = new nsReceiver(in); nsRefPtr<nsReceiver> receiver = new nsReceiver(in);
if (!receiver) if (!receiver)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
@ -188,18 +127,18 @@ TestPipe(nsIInputStream* in, nsIOutputStream* out)
printf("wrote %d bytes, time = %dms\n", total, printf("wrote %d bytes, time = %dms\n", total,
PR_IntervalToMilliseconds(end - start)); PR_IntervalToMilliseconds(end - start));
NS_ASSERTION(receiver->GetBytesRead() == total, "didn't read everything"); EXPECT_EQ(receiver->GetBytesRead(), total);
return NS_OK; return NS_OK;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
class nsShortReader : public nsIRunnable { class nsShortReader MOZ_FINAL : public nsIRunnable {
public: public:
NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() { NS_IMETHOD Run() MOZ_OVERRIDE {
nsresult rv; nsresult rv;
char buf[101]; char buf[101];
uint32_t count; uint32_t count;
@ -230,24 +169,24 @@ public:
return rv; return rv;
} }
nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) { explicit nsShortReader(nsIInputStream* in) : mIn(in), mReceived(0) {
mMon = new Monitor("nsShortReader"); mMon = new ReentrantMonitor("nsShortReader");
} }
void Received(uint32_t count) { void Received(uint32_t count) {
MonitorAutoEnter mon(*mMon); ReentrantMonitorAutoEnter mon(*mMon);
mReceived += count; mReceived += count;
mon.Notify(); mon.Notify();
} }
uint32_t WaitForReceipt(const uint32_t aWriteCount) { uint32_t WaitForReceipt(const uint32_t aWriteCount) {
MonitorAutoEnter mon(*mMon); ReentrantMonitorAutoEnter mon(*mMon);
uint32_t result = mReceived; uint32_t result = mReceived;
while (result < aWriteCount) { while (result < aWriteCount) {
mon.Wait(); mon.Wait();
NS_ASSERTION(mReceived > result, "failed to receive"); EXPECT_TRUE(mReceived > result);
result = mReceived; result = mReceived;
} }
@ -255,10 +194,13 @@ public:
return result; return result;
} }
private:
~nsShortReader() {}
protected: protected:
nsCOMPtr<nsIInputStream> mIn; nsCOMPtr<nsIInputStream> mIn;
uint32_t mReceived; uint32_t mReceived;
Monitor* mMon; ReentrantMonitor* mMon;
}; };
NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable) NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable)
@ -266,7 +208,7 @@ NS_IMPL_ISUPPORTS(nsShortReader, nsIRunnable)
nsresult nsresult
TestShortWrites(nsIInputStream* in, nsIOutputStream* out) TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
{ {
nsCOMPtr<nsShortReader> receiver = new nsShortReader(in); nsRefPtr<nsShortReader> receiver = new nsShortReader(in);
if (!receiver) if (!receiver)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
@ -282,10 +224,10 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
char* buf = PR_smprintf("%d %s", i, kTestPattern); char* buf = PR_smprintf("%d %s", i, kTestPattern);
uint32_t len = strlen(buf); uint32_t len = strlen(buf);
len = len * rand() / RAND_MAX; len = len * rand() / RAND_MAX;
len = XPCOM_MAX(1, len); len = std::min(1u, len);
rv = WriteAll(out, buf, len, &writeCount); rv = WriteAll(out, buf, len, &writeCount);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return rv;
NS_ASSERTION(writeCount == len, "didn't write enough"); EXPECT_EQ(writeCount, len);
total += writeCount; total += writeCount;
if (gTrace) if (gTrace)
@ -297,9 +239,9 @@ TestShortWrites(nsIInputStream* in, nsIOutputStream* out)
#ifdef DEBUG #ifdef DEBUG
const uint32_t received = const uint32_t received =
#endif
receiver->WaitForReceipt(writeCount); receiver->WaitForReceipt(writeCount);
NS_ASSERTION(received == writeCount, "received wrong amount"); EXPECT_EQ(received, writeCount);
#endif
} }
rv = out->Close(); rv = out->Close();
if (NS_FAILED(rv)) return rv; 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: public:
NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_THREADSAFE_ISUPPORTS
NS_IMETHOD Run() { NS_IMETHOD Run() MOZ_OVERRIDE {
nsresult rv; nsresult rv;
uint32_t count; uint32_t count;
while (true) { while (true) {
@ -346,6 +288,9 @@ public:
: mIn(in), mOut(out), mCount(0) { : mIn(in), mOut(out), mCount(0) {
} }
private:
~nsPump() {}
protected: protected:
nsCOMPtr<nsIInputStream> mIn; nsCOMPtr<nsIInputStream> mIn;
nsCOMPtr<nsIOutputStream> mOut; nsCOMPtr<nsIOutputStream> mOut;
@ -354,35 +299,36 @@ protected:
NS_IMPL_ISUPPORTS(nsPump, nsIRunnable) NS_IMPL_ISUPPORTS(nsPump, nsIRunnable)
nsresult TEST(Pipes, ChainedPipes)
TestChainedPipes()
{ {
nsresult rv; nsresult rv;
printf("TestChainedPipes\n"); if (gTrace) {
printf("TestChainedPipes\n");
}
nsCOMPtr<nsIInputStream> in1; nsCOMPtr<nsIInputStream> in1;
nsCOMPtr<nsIOutputStream> out1; nsCOMPtr<nsIOutputStream> out1;
rv = TP_NewPipe(getter_AddRefs(in1), getter_AddRefs(out1), 20, 1999); rv = NS_NewPipe(getter_AddRefs(in1), getter_AddRefs(out1), 20, 1999);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
nsCOMPtr<nsIInputStream> in2; nsCOMPtr<nsIInputStream> in2;
nsCOMPtr<nsIOutputStream> out2; nsCOMPtr<nsIOutputStream> out2;
rv = TP_NewPipe(getter_AddRefs(in2), getter_AddRefs(out2), 200, 401); rv = NS_NewPipe(getter_AddRefs(in2), getter_AddRefs(out2), 200, 401);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
nsCOMPtr<nsPump> pump = new nsPump(in1, out2); nsRefPtr<nsPump> pump = new nsPump(in1, out2);
if (pump == nullptr) return NS_ERROR_OUT_OF_MEMORY; if (pump == nullptr) return;
nsCOMPtr<nsIThread> thread; nsCOMPtr<nsIThread> thread;
rv = NS_NewThread(getter_AddRefs(thread), pump); rv = NS_NewThread(getter_AddRefs(thread), pump);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
nsCOMPtr<nsReceiver> receiver = new nsReceiver(in2); nsRefPtr<nsReceiver> receiver = new nsReceiver(in2);
if (receiver == nullptr) return NS_ERROR_OUT_OF_MEMORY; if (receiver == nullptr) return;
nsCOMPtr<nsIThread> receiverThread; nsCOMPtr<nsIThread> receiverThread;
rv = NS_NewThread(getter_AddRefs(receiverThread), receiver); rv = NS_NewThread(getter_AddRefs(receiverThread), receiver);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
uint32_t total = 0; uint32_t total = 0;
for (uint32_t i = 0; i < ITERATIONS; i++) { for (uint32_t i = 0; i < ITERATIONS; i++) {
@ -390,10 +336,10 @@ TestChainedPipes()
char* buf = PR_smprintf("%d %s", i, kTestPattern); char* buf = PR_smprintf("%d %s", i, kTestPattern);
uint32_t len = strlen(buf); uint32_t len = strlen(buf);
len = len * rand() / RAND_MAX; len = len * rand() / RAND_MAX;
len = XPCOM_MAX(1, len); len = std::max(1u, len);
rv = WriteAll(out1, buf, len, &writeCount); rv = WriteAll(out1, buf, len, &writeCount);
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
NS_ASSERTION(writeCount == len, "didn't write enough"); EXPECT_EQ(writeCount, len);
total += writeCount; total += writeCount;
if (gTrace) if (gTrace)
@ -401,14 +347,14 @@ TestChainedPipes()
PR_smprintf_free(buf); PR_smprintf_free(buf);
} }
printf("wrote total of %d bytes\n", total); if (gTrace) {
printf("wrote total of %d bytes\n", total);
}
rv = out1->Close(); rv = out1->Close();
if (NS_FAILED(rv)) return rv; if (NS_FAILED(rv)) return;
thread->Shutdown(); thread->Shutdown();
receiverThread->Shutdown(); receiverThread->Shutdown();
return NS_OK;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -420,50 +366,26 @@ RunTests(uint32_t segSize, uint32_t segCount)
nsCOMPtr<nsIInputStream> in; nsCOMPtr<nsIInputStream> in;
nsCOMPtr<nsIOutputStream> out; nsCOMPtr<nsIOutputStream> out;
uint32_t bufSize = segSize * segCount; uint32_t bufSize = segSize * segCount;
printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize); if (gTrace) {
printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize);
printf("Testing long writes...\n"); 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); rv = TestPipe(in, out);
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed"); EXPECT_TRUE(NS_SUCCEEDED(rv));
printf("Testing short writes...\n"); if (gTrace) {
rv = TP_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize); printf("Testing short writes...\n");
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); rv = TestShortWrites(in, out);
NS_ASSERTION(NS_SUCCEEDED(rv), "TestPipe failed"); EXPECT_TRUE(NS_SUCCEEDED(rv));
} }
//////////////////////////////////////////////////////////////////////////////// TEST(Pipes, Main)
#if 0
extern void
TestSegmentedBuffer();
#endif
int
main(int argc, char* argv[])
{ {
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(16, 1);
RunTests(4096, 16); 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', 'TestCRT.cpp',
'TestEncoding.cpp', 'TestEncoding.cpp',
'TestExpirationTracker.cpp', 'TestExpirationTracker.cpp',
'TestPipes.cpp',
'TestSnappyStreams.cpp', 'TestSnappyStreams.cpp',
'TestStrings.cpp', 'TestStrings.cpp',
] ]

Просмотреть файл

@ -80,7 +80,6 @@ if CONFIG['MOZ_MEMORY']:
# XXX Make these tests work in libxul builds. # XXX Make these tests work in libxul builds.
#CPP_UNIT_TESTS += [ #CPP_UNIT_TESTS += [
# 'TestPipes',
# 'TestPriorityQueue', # 'TestPriorityQueue',
# 'TestStorageStream', # 'TestStorageStream',
# 'TestSynchronization', # 'TestSynchronization',