Bug 1313484 - Convert XPCOM test TestTimers to a gtest. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Wed, 09 Nov 2016 10:23:43 -0800
changeset 351977 0d61fccdd9f60e9a95b70e5f8bd9e130e7b173a1
parent 351976 058b48a72e502bc73e1a85d5a0f2a197ed6a5c61
child 351978 ea97ec1c574330bc76f453058f2fa8e55027db3d
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1313484
milestone52.0a1
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
@@ -51,17 +51,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
@@ -43,20 +43,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',