From acfa245a0af67c71e23cb0bf41390a6b6f299e94 Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Tue, 20 Jan 2015 22:46:48 -0500 Subject: [PATCH] Bug 1124041 - Move TestPipes.cpp to gtest and enable it; r=froydnj --- xpcom/tests/{ => gtest}/TestPipes.cpp | 218 +++++++++----------------- xpcom/tests/gtest/moz.build | 1 + xpcom/tests/moz.build | 1 - 3 files changed, 71 insertions(+), 149 deletions(-) rename xpcom/tests/{ => gtest}/TestPipes.cpp (60%) diff --git a/xpcom/tests/TestPipes.cpp b/xpcom/tests/gtest/TestPipes.cpp similarity index 60% rename from xpcom/tests/TestPipes.cpp rename to xpcom/tests/gtest/TestPipes.cpp index c630e930efed..5ec57e041b62 100644 --- a/xpcom/tests/TestPipes.cpp +++ b/xpcom/tests/gtest/TestPipes.cpp @@ -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 +#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 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 mIn; uint32_t mCount; @@ -151,7 +90,7 @@ NS_IMPL_ISUPPORTS(nsReceiver, nsIRunnable) nsresult TestPipe(nsIInputStream* in, nsIOutputStream* out) { - nsCOMPtr receiver = new nsReceiver(in); + nsRefPtr 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 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 receiver = new nsShortReader(in); + nsRefPtr 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 mIn; nsCOMPtr mOut; @@ -354,35 +299,36 @@ protected: NS_IMPL_ISUPPORTS(nsPump, nsIRunnable) -nsresult -TestChainedPipes() +TEST(Pipes, ChainedPipes) { nsresult rv; - printf("TestChainedPipes\n"); + if (gTrace) { + printf("TestChainedPipes\n"); + } nsCOMPtr in1; nsCOMPtr 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 in2; nsCOMPtr 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 pump = new nsPump(in1, out2); - if (pump == nullptr) return NS_ERROR_OUT_OF_MEMORY; + nsRefPtr pump = new nsPump(in1, out2); + if (pump == nullptr) return; nsCOMPtr thread; rv = NS_NewThread(getter_AddRefs(thread), pump); - if (NS_FAILED(rv)) return rv; + if (NS_FAILED(rv)) return; - nsCOMPtr receiver = new nsReceiver(in2); - if (receiver == nullptr) return NS_ERROR_OUT_OF_MEMORY; + nsRefPtr receiver = new nsReceiver(in2); + if (receiver == nullptr) return; nsCOMPtr 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); } - printf("wrote total of %d bytes\n", total); + 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 in; nsCOMPtr out; uint32_t bufSize = segSize * segCount; - 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"); + if (gTrace) { + printf("Testing New Pipes: segment size %d buffer size %d\n", segSize, bufSize); + printf("Testing long writes...\n"); + } + 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)); - printf("Testing short writes...\n"); - rv = TP_NewPipe(getter_AddRefs(in), getter_AddRefs(out), segSize, bufSize); - NS_ASSERTION(NS_SUCCEEDED(rv), "TP_NewPipe failed"); + if (gTrace) { + printf("Testing short writes...\n"); + } + 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 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; } - -//////////////////////////////////////////////////////////////////////////////// diff --git a/xpcom/tests/gtest/moz.build b/xpcom/tests/gtest/moz.build index 50fffc042896..a244bb605918 100644 --- a/xpcom/tests/gtest/moz.build +++ b/xpcom/tests/gtest/moz.build @@ -8,6 +8,7 @@ UNIFIED_SOURCES += [ 'TestCRT.cpp', 'TestEncoding.cpp', 'TestExpirationTracker.cpp', + 'TestPipes.cpp', 'TestSnappyStreams.cpp', 'TestStrings.cpp', ] diff --git a/xpcom/tests/moz.build b/xpcom/tests/moz.build index 131cacdf5452..de6a36ddeb2a 100644 --- a/xpcom/tests/moz.build +++ b/xpcom/tests/moz.build @@ -80,7 +80,6 @@ if CONFIG['MOZ_MEMORY']: # XXX Make these tests work in libxul builds. #CPP_UNIT_TESTS += [ -# 'TestPipes', # 'TestPriorityQueue', # 'TestStorageStream', # 'TestSynchronization',