Bug 1313483 - Convert XPCOM test TestThreadUtils to a gtest. r=froydnj
authorEric Rahm <erahm@mozilla.com>
Fri, 04 Nov 2016 16:24:59 -0700
changeset 347922 d0f06f2fdba053344e07fbd5b4e55af117077dd4
parent 347921 c1809c625dcaf04a27de1256b82abd55b7ed2640
child 347923 e9b561d60697a81fa8afe157ff0e52efea3e2636
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1313483
milestone52.0a1
Bug 1313483 - Convert XPCOM test TestThreadUtils to a gtest. r=froydnj MozReview-Commit-ID: EA0SNRX2EXC
testing/cppunittest.ini
xpcom/tests/TestThreadUtils.cpp
xpcom/tests/gtest/TestThreadUtils.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
--- a/testing/cppunittest.ini
+++ b/testing/cppunittest.ini
@@ -65,17 +65,16 @@ skip-if = os == 'android' # Bug 1147630
 [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]
-[TestThreadUtils]
 [TestTuple]
 [TestTypeTraits]
 [TestTypedEnum]
 [TestUDPSocket]
 [TestUniquePtr]
 [TestVariant]
 [TestVector]
 [TestVolatileBuffer]
rename from xpcom/tests/TestThreadUtils.cpp
rename to xpcom/tests/gtest/TestThreadUtils.cpp
--- a/xpcom/tests/TestThreadUtils.cpp
+++ b/xpcom/tests/gtest/TestThreadUtils.cpp
@@ -1,15 +1,16 @@
 /* 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 "nsThreadUtils.h"
 
+#include "gtest/gtest.h"
+
 using namespace mozilla;
 
 enum {
   TEST_CALL_VOID_ARG_VOID_RETURN,
   TEST_CALL_VOID_ARG_VOID_RETURN_CONST,
   TEST_CALL_VOID_ARG_NONVOID_RETURN,
   TEST_CALL_NONVOID_ARG_VOID_RETURN,
   TEST_CALL_NONVOID_ARG_NONVOID_RETURN,
@@ -152,163 +153,156 @@ struct TestCopyMove
   TestCopyMove(const TestCopyMove& a) : mCopyCounter(a.mCopyCounter), mMoveCounter(a.mMoveCounter) { ++mCopyCounter; };
   TestCopyMove(TestCopyMove&& a) : mCopyCounter(a.mCopyCounter), mMoveCounter(a.mMoveCounter) { a.mMoveCounter = nullptr; ++mMoveCounter; }
   ~TestCopyMove() { mCopyCounter = nullptr; mMoveCounter = nullptr; }
   void operator()() { MOZ_RELEASE_ASSERT(mCopyCounter); MOZ_RELEASE_ASSERT(mMoveCounter); }
   int* mCopyCounter;
   int* mMoveCounter;
 };
 
-static int Expect(const char* aContext, int aCounter, int aMaxExpected)
+static void Expect(const char* aContext, int aCounter, int aMaxExpected)
 {
-  if (aCounter > aMaxExpected) {
-    fail("%s: expected %d max, got %d", aContext, aMaxExpected, aCounter);
-    return 1;
-  }
-  passed("%s: got %d <= %d as expected", aContext, aCounter, aMaxExpected);
-  return 0;
+  EXPECT_LE(aCounter, aMaxExpected) << aContext;
 }
 
-int TestNS_NewRunnableFunction()
+TEST(ThreadUtils, NewRunnableFunction)
 {
-  int result = 0;
-
   // Test NS_NewRunnableFunction with copyable-only function object.
   {
     int copyCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyWithNoMove tracker(&copyCounter);
         trackedRunnable = NS_NewRunnableFunction(tracker);
         // Original 'tracker' is destroyed here.
       }
       // Verify that the runnable contains a non-destroyed function object.
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) function, copies",
-                     copyCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable-only (and no move) function, copies",
+           copyCounter, 1);
   }
   {
     int copyCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         // Passing as rvalue, but using copy.
         // (TestCopyWithDeletedMove wouldn't allow this.)
         trackedRunnable = NS_NewRunnableFunction(TestCopyWithNoMove(&copyCounter));
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) function rvalue, copies",
-                     copyCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable-only (and no move) function rvalue, copies",
+           copyCounter, 1);
   }
   {
     int copyCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyWithDeletedMove tracker(&copyCounter);
         trackedRunnable = NS_NewRunnableFunction(tracker);
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable-only (and deleted move) function, copies",
-                     copyCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable-only (and deleted move) function, copies",
+           copyCounter, 1);
   }
 
   // Test NS_NewRunnableFunction with movable-only function object.
   {
     int moveCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestMove tracker(&moveCounter);
         trackedRunnable = NS_NewRunnableFunction(Move(tracker));
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with movable-only function, moves",
-                     moveCounter, 1);
+    Expect("NS_NewRunnableFunction with movable-only function, moves",
+           moveCounter, 1);
   }
   {
     int moveCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         trackedRunnable = NS_NewRunnableFunction(TestMove(&moveCounter));
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with movable-only function rvalue, moves",
-                     moveCounter, 1);
+    Expect("NS_NewRunnableFunction with movable-only function rvalue, moves",
+           moveCounter, 1);
   }
 
   // Test NS_NewRunnableFunction with copyable&movable function object.
   {
     int copyCounter = 0;
     int moveCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyMove tracker(&copyCounter, &moveCounter);
         trackedRunnable = NS_NewRunnableFunction(Move(tracker));
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable&movable function, copies",
-                     copyCounter, 0);
-    result |= Expect("NS_NewRunnableFunction with copyable&movable function, moves",
-                     moveCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable&movable function, copies",
+           copyCounter, 0);
+    Expect("NS_NewRunnableFunction with copyable&movable function, moves",
+           moveCounter, 1);
   }
   {
     int copyCounter = 0;
     int moveCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         trackedRunnable =
           NS_NewRunnableFunction(TestCopyMove(&copyCounter, &moveCounter));
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable&movable function rvalue, copies",
-                     copyCounter, 0);
-    result |= Expect("NS_NewRunnableFunction with copyable&movable function rvalue, moves",
-                     moveCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable&movable function rvalue, copies",
+           copyCounter, 0);
+    Expect("NS_NewRunnableFunction with copyable&movable function rvalue, moves",
+           moveCounter, 1);
   }
 
   // Test NS_NewRunnableFunction with copyable-only lambda capture.
   {
     int copyCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyWithNoMove tracker(&copyCounter);
         // Expect 2 copies (here -> local lambda -> runnable lambda).
         trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable-only (and no move) capture, copies",
-                     copyCounter, 2);
+    Expect("NS_NewRunnableFunction with copyable-only (and no move) capture, copies",
+           copyCounter, 2);
   }
   {
     int copyCounter = 0;
     {
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyWithDeletedMove tracker(&copyCounter);
         // Expect 2 copies (here -> local lambda -> runnable lambda).
         trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable-only (and deleted move) capture, copies",
-                     copyCounter, 2);
+    Expect("NS_NewRunnableFunction with copyable-only (and deleted move) capture, copies",
+           copyCounter, 2);
   }
 
   // Note: Not possible to use move-only captures.
   // (Until we can use C++14 generalized lambda captures)
 
   // Test NS_NewRunnableFunction with copyable&movable lambda capture.
   {
     int copyCounter = 0;
@@ -317,32 +311,25 @@ int TestNS_NewRunnableFunction()
       nsCOMPtr<nsIRunnable> trackedRunnable;
       {
         TestCopyMove tracker(&copyCounter, &moveCounter);
         trackedRunnable = NS_NewRunnableFunction([tracker]() mutable { tracker(); });
         // Expect 1 copy (here -> local lambda) and 1 move (local -> runnable lambda).
       }
       trackedRunnable->Run();
     }
-    result |= Expect("NS_NewRunnableFunction with copyable&movable capture, copies",
-                     copyCounter, 1);
-    result |= Expect("NS_NewRunnableFunction with copyable&movable capture, moves",
-                     moveCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable&movable capture, copies",
+           copyCounter, 1);
+    Expect("NS_NewRunnableFunction with copyable&movable capture, moves",
+           moveCounter, 1);
   }
-
-  return result;
 }
 
-int main(int argc, char** argv)
+TEST(ThreadUtils, RunnableMethod)
 {
-  int result = TestNS_NewRunnableFunction();
-
-  ScopedXPCOM xpcom("ThreadUtils");
-  NS_ENSURE_FALSE(xpcom.failed(), 1);
-
   memset(gRunnableExecuted, false, MAX_TESTS * sizeof(bool));
   // Scope the smart ptrs so that the runnables need to hold on to whatever they need
   {
     RefPtr<nsFoo> foo = new nsFoo();
     RefPtr<nsBar> bar = new nsBar();
     RefPtr<const nsBar> constBar = bar;
 
     // This pointer will be freed at the end of the block
@@ -374,25 +361,18 @@ int main(int argc, char** argv)
   }
 
   // Spin the event loop
   NS_ProcessPendingEvents(nullptr);
 
   // Now test a suicidal event in NS_New(Named)Thread
   nsCOMPtr<nsIThread> thread;
   NS_NewNamedThread("SuicideThread", getter_AddRefs(thread), new TestSuicide());
-  MOZ_RELEASE_ASSERT(thread);
+  ASSERT_TRUE(thread);
 
   while (!gRunnableExecuted[TEST_CALL_NEWTHREAD_SUICIDAL]) {
     NS_ProcessPendingEvents(nullptr);
   }
 
   for (uint32_t i = 0; i < MAX_TESTS; i++) {
-    if (gRunnableExecuted[i]) {
-      passed("Test %d passed",i);
-    } else {
-      fail("Error in test %d", i);
-      result = 1;
-    }
+    EXPECT_TRUE(gRunnableExecuted[i]) << "Error in test " << i;
   }
-
-  return result;
 }
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -31,16 +31,17 @@ UNIFIED_SOURCES += [
     'TestStrings.cpp',
     'TestStringStream.cpp',
     'TestSynchronization.cpp',
     'TestTArray.cpp',
     'TestTArray2.cpp',
     'TestTextFormatter.cpp',
     'TestThreadPool.cpp',
     'TestThreads.cpp',
+    'TestThreadUtils.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
@@ -41,17 +41,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([
-    'TestThreadUtils',
     'TestTimers'
 ])
 
 if CONFIG['MOZ_MEMORY']:
     GeckoCppUnitTests([
         'TestJemalloc',
     ])