Bug 1125559 - Move TestThreadPool.cpp to gtest and enable it; r=froydnj
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 26 Jan 2015 20:49:22 -0500
changeset 225918 f5c6c57fc11a3da2627648a654fcdf3cca525fa5
parent 225917 4c573585e7dc400ffd320a1918fe296a51365b06
child 225919 05dfcf108b4fc66ded06fcb9b4d653b7e63ed2af
push id28179
push usercbook@mozilla.com
push dateTue, 27 Jan 2015 14:24:11 +0000
treeherdermozilla-central@232401a6d1cc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1125559
milestone38.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 1125559 - Move TestThreadPool.cpp to gtest and enable it; r=froydnj
xpcom/tests/TestThreadPool.cpp
xpcom/tests/gtest/TestThreadPool.cpp
xpcom/tests/gtest/moz.build
xpcom/tests/moz.build
rename from xpcom/tests/TestThreadPool.cpp
rename to xpcom/tests/gtest/TestThreadPool.cpp
--- a/xpcom/tests/TestThreadPool.cpp
+++ b/xpcom/tests/gtest/TestThreadPool.cpp
@@ -7,56 +7,54 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include "nsXPCOM.h"
 #include "nsXPCOMCIDInternal.h"
 #include "nsIThreadPool.h"
 #include "nsComponentManagerUtils.h"
 #include "nsCOMPtr.h"
 #include "nsIRunnable.h"
+#include "mozilla/Atomics.h"
+#include "gtest/gtest.h"
 
-class Task : public nsIRunnable
+class Task MOZ_FINAL : public nsIRunnable
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
 
-  Task(int i) : mIndex(i) {}
+  explicit Task(int i) : mIndex(i) {}
 
-  NS_IMETHOD Run()
+  NS_IMETHOD Run() MOZ_OVERRIDE
   {
     printf("###(%d) running from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
     int r = (int) ((float) rand() * 200 / RAND_MAX);
     PR_Sleep(PR_MillisecondsToInterval(r));
     printf("###(%d) exiting from thread: %p\n", mIndex, (void *) PR_GetCurrentThread());
+    ++sCount;
     return NS_OK;
   }
 
+  static mozilla::Atomic<int> sCount;
+
 private:
+  ~Task() {}
+
   int mIndex;
 };
 NS_IMPL_ISUPPORTS(Task, nsIRunnable)
 
-static nsresult
-RunTests()
+mozilla::Atomic<int> Task::sCount;
+
+TEST(ThreadPool, Main)
 {
   nsCOMPtr<nsIThreadPool> pool = do_CreateInstance(NS_THREADPOOL_CONTRACTID);
-  NS_ENSURE_STATE(pool);
+  EXPECT_TRUE(pool);
 
   for (int i = 0; i < 100; ++i) {
     nsCOMPtr<nsIRunnable> task = new Task(i);
-    NS_ENSURE_TRUE(task, NS_ERROR_OUT_OF_MEMORY);
+    EXPECT_TRUE(task);
 
     pool->Dispatch(task, NS_DISPATCH_NORMAL);
   }
 
   pool->Shutdown();
-  return NS_OK;
+  EXPECT_EQ(Task::sCount, 100);
 }
-
-int
-main(int argc, char **argv)
-{
-  if (NS_FAILED(NS_InitXPCOM2(nullptr, nullptr, nullptr)))
-    return -1;
-  RunTests();
-  NS_ShutdownXPCOM(nullptr);
-  return 0;
-}
--- a/xpcom/tests/gtest/moz.build
+++ b/xpcom/tests/gtest/moz.build
@@ -9,13 +9,14 @@ UNIFIED_SOURCES += [
     'TestEncoding.cpp',
     'TestExpirationTracker.cpp',
     'TestPipes.cpp',
     'TestPriorityQueue.cpp',
     'TestSnappyStreams.cpp',
     'TestStorageStream.cpp',
     'TestStrings.cpp',
     'TestSynchronization.cpp',
+    'TestThreadPool.cpp',
 ]
 
 FINAL_LIBRARY = 'xul-gtest'
 
 FAIL_ON_WARNINGS = True
--- a/xpcom/tests/moz.build
+++ b/xpcom/tests/moz.build
@@ -75,17 +75,16 @@ GeckoCppUnitTests([
 
 if CONFIG['MOZ_MEMORY']:
     GeckoCppUnitTests([
         'TestJemalloc',
     ])
 
 # XXX Make these tests work in libxul builds.
 #CPP_UNIT_TESTS += [
-#    'TestThreadPool',
 #    'TestThreads',
 #    'TestTimeStamp',
 #    'TestXPIDLString',
 #    'TestUTF',
 #    'TestAtoms',
 #]
 
 # FIXME: bug 577500 TestStaticAtoms fails when run in dist/bin