Bug 1313484 - Convert XPCOM test TestTimers to a gtest. r=froydnj
☠☠ backed out by 01800003ec3b ☠ ☠
authorEric Rahm <erahm@mozilla.com>
Mon, 07 Nov 2016 14:25:41 -0800
changeset 321581 6a7831fbca696651b68f30cbe0d984d2fc1357c3
parent 321580 fc00f40f2b6ed681952171c5901cc376b64cbb72
child 321582 1b545e55e42aae19c6ba66f316b3b8167732be4b
push id30931
push userkwierso@gmail.com
push dateTue, 08 Nov 2016 21:58:36 +0000
treeherdermozilla-central@783356f1476e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1313484
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1313484 - Convert XPCOM test TestTimers to a gtest. r=froydnj MozReview-Commit-ID: 8sH5fbvv9wZ
testing/cppunittest.ini
xpcom/tests/TestTimers.cpp
xpcom/tests/gtest/TestTimers.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
--- a/testing/cppunittest.ini
+++ b/testing/cppunittest.ini
@@ -54,17 +54,16 @@ skip-if = os == 'android' # Bug 1147630
 [TestSHA1]
 [TestSTSParser]
 [TestSaturate]
 [TestSplayTree]
 [TestStartupCache]
 skip-if = os == 'b2g' || os == 'android'  # Bug 929655
 support-files = TestStartupCacheTelemetry.js TestStartupCacheTelemetry.manifest
 [TestSyncRunnable]
-[TestTimers]
 [TestTXMgr]
 skip-if = os == 'b2g'  #Bug 919595
 [TestTemplateLib]
 [TestTuple]
 [TestTypeTraits]
 [TestTypedEnum]
 [TestUDPSocket]
 [TestUniquePtr]
rename from xpcom/tests/TestTimers.cpp
rename to xpcom/tests/gtest/TestTimers.cpp
--- a/xpcom/tests/TestTimers.cpp
+++ b/xpcom/tests/gtest/TestTimers.cpp
@@ -1,32 +1,31 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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 "nsIThread.h"
 #include "nsITimer.h"
 
 #include "nsCOMPtr.h"
 #include "nsComponentManagerUtils.h"
 #include "nsServiceManagerUtils.h"
 #include "nsThreadUtils.h"
 #include "prinrval.h"
 #include "prmon.h"
 #include "prthread.h"
 #include "mozilla/Attributes.h"
 
 #include "mozilla/ReentrantMonitor.h"
 
 #include <list>
 #include <vector>
-#include <stdio.h>
+
+#include "gtest/gtest.h"
 
 using namespace mozilla;
 
 typedef nsresult(*TestFuncPtr)();
 
 class AutoTestThread
 {
 public:
@@ -100,80 +99,74 @@ private:
   ~TimerCallback() {}
 
   nsIThread** mThreadPtr;
   ReentrantMonitor* mReentrantMonitor;
 };
 
 NS_IMPL_ISUPPORTS(TimerCallback, nsITimerCallback)
 
-nsresult
-TestTargetedTimers()
+TEST(Timers, TargetedTimers)
 {
   AutoCreateAndDestroyReentrantMonitor newMon;
-  NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY);
+  ASSERT_TRUE(newMon);
 
   AutoTestThread testThread;
-  NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
+  ASSERT_TRUE(testThread);
 
   nsresult rv;
   nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
 
   rv = timer->SetTarget(target);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   nsIThread* notifiedThread = nullptr;
 
   nsCOMPtr<nsITimerCallback> callback =
     new TimerCallback(&notifiedThread, newMon);
-  NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
+  ASSERT_TRUE(callback);
 
   rv = timer->InitWithCallback(callback, 2000, nsITimer::TYPE_ONE_SHOT);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   ReentrantMonitorAutoEnter mon(*newMon);
   while (!notifiedThread) {
     mon.Wait();
   }
-  NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
-
-  return NS_OK;
+  ASSERT_EQ(notifiedThread, testThread);
 }
 
-nsresult
-TestTimerWithStoppedTarget()
+TEST(Timers, TimerWithStoppedTarget)
 {
   AutoTestThread testThread;
-  NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
+  ASSERT_TRUE(testThread);
 
   nsresult rv;
   nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
 
   rv = timer->SetTarget(target);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   // If this is called, we'll assert
   nsCOMPtr<nsITimerCallback> callback =
     new TimerCallback(nullptr, nullptr);
-  NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
+  ASSERT_TRUE(callback);
 
   rv = timer->InitWithCallback(callback, 100, nsITimer::TYPE_ONE_SHOT);
-  NS_ENSURE_SUCCESS(rv, rv);
+  ASSERT_TRUE(NS_SUCCEEDED(rv));
 
   testThread->Shutdown();
 
   PR_Sleep(400);
-
-  return NS_OK;
 }
 
 #define FUZZ_MAX_TIMEOUT 9
 class FuzzTestThreadState final : public nsITimerCallback {
   public:
     NS_DECL_THREADSAFE_ISUPPORTS
 
     explicit FuzzTestThreadState(nsIThread* thread) :
@@ -410,18 +403,17 @@ class FuzzTestThreadState final : public
     std::list<nsCOMPtr<nsITimer>> mOneShotTimersByDelay[FUZZ_MAX_TIMEOUT + 1];
     std::vector<nsCOMPtr<nsITimer>> mRepeatingTimers;
     Atomic<bool> mStopped;
     Atomic<size_t> mTimersOutstanding;
 };
 
 NS_IMPL_ISUPPORTS(FuzzTestThreadState, nsITimerCallback)
 
-nsresult
-FuzzTestTimers()
+TEST(Timers, FuzzTestTimers)
 {
   static const size_t kNumThreads(10);
   AutoTestThread threads[kNumThreads];
   RefPtr<FuzzTestThreadState> threadStates[kNumThreads];
 
   for (size_t i = 0; i < kNumThreads; ++i) {
     threadStates[i] = new FuzzTestThreadState(&*threads[i]);
     threadStates[i]->Start();
@@ -433,36 +425,13 @@ FuzzTestTimers()
     threadStates[i]->Stop();
   }
 
   // Wait at most 10 seconds for all outstanding timers to pop
   PRIntervalTime start = PR_IntervalNow();
   for (auto& threadState : threadStates) {
     while (threadState->HasTimersOutstanding()) {
       uint32_t elapsedMs = PR_IntervalToMilliseconds(PR_IntervalNow() - start);
-      MOZ_RELEASE_ASSERT(elapsedMs <= 10000,
-                         "Timed out waiting for all timers to pop");
+      ASSERT_LE(elapsedMs, uint32_t(10000)) << "Timed out waiting for all timers to pop";
       PR_Sleep(PR_MillisecondsToInterval(10));
     }
   }
-
-  return NS_OK;
 }
-
-int main(int argc, char** argv)
-{
-  ScopedXPCOM xpcom("TestTimers");
-  NS_ENSURE_FALSE(xpcom.failed(), 1);
-
-  static TestFuncPtr testsToRun[] = {
-    TestTargetedTimers,
-    TestTimerWithStoppedTarget,
-    FuzzTestTimers
-  };
-  static uint32_t testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
-
-  for (uint32_t i = 0; i < testCount; i++) {
-    nsresult rv = testsToRun[i]();
-    NS_ENSURE_SUCCESS(rv, 1);
-  }
-
-  return 0;
-}
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -32,16 +32,17 @@ UNIFIED_SOURCES += [
     'TestStringStream.cpp',
     'TestSynchronization.cpp',
     'TestTArray.cpp',
     'TestTArray2.cpp',
     'TestTextFormatter.cpp',
     'TestThreadPool.cpp',
     'TestThreads.cpp',
     'TestThreadUtils.cpp',
+    'TestTimers.cpp',
     'TestTimeStamp.cpp',
     'TestTokenizer.cpp',
     'TestUTF.cpp',
     'TestXPIDLString.cpp',
 ]
 
 # Compile TestAllocReplacement separately so Windows headers don't pollute
 # the global namespace for other files.
--- a/xpcom/tests/moz.build
+++ b/xpcom/tests/moz.build
@@ -40,20 +40,16 @@ if CONFIG['OS_TARGET'] == 'WINNT':
 
 if CONFIG['WRAP_STL_INCLUDES'] and not CONFIG['CLANG_CL']:
     GeckoSimplePrograms([
         'TestSTLWrappers',
     ])
 
 XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
 
-GeckoCppUnitTests([
-    'TestTimers'
-])
-
 if CONFIG['MOZ_MEMORY']:
     GeckoCppUnitTests([
         'TestJemalloc',
     ])
 
 # FIXME: bug 577500 TestStaticAtoms fails when run in dist/bin
 #CPP_UNIT_TESTS += [
 #    'TestStaticAtoms',