xpcom/tests/TestThreadPoolListener.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestThreadPoolListener.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,219 @@
     1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "TestHarness.h"
    1.10 +
    1.11 +#include "nsIThread.h"
    1.12 +#include "nsIThreadPool.h"
    1.13 +
    1.14 +#include "nsThreadUtils.h"
    1.15 +#include "nsXPCOMCIDInternal.h"
    1.16 +#include "pratom.h"
    1.17 +#include "prinrval.h"
    1.18 +#include "prmon.h"
    1.19 +#include "prthread.h"
    1.20 +#include "mozilla/Attributes.h"
    1.21 +
    1.22 +#include "mozilla/ReentrantMonitor.h"
    1.23 +using namespace mozilla;
    1.24 +
    1.25 +#define NUMBER_OF_THREADS 4
    1.26 +
    1.27 +// One hour... because test boxes can be slow!
    1.28 +#define IDLE_THREAD_TIMEOUT 3600000
    1.29 +
    1.30 +static nsIThread** gCreatedThreadList = nullptr;
    1.31 +static nsIThread** gShutDownThreadList = nullptr;
    1.32 +
    1.33 +static ReentrantMonitor* gReentrantMonitor = nullptr;
    1.34 +
    1.35 +static bool gAllRunnablesPosted = false;
    1.36 +static bool gAllThreadsCreated = false;
    1.37 +static bool gAllThreadsShutDown = false;
    1.38 +
    1.39 +#ifdef DEBUG
    1.40 +#define TEST_ASSERTION(_test, _msg) \
    1.41 +    NS_ASSERTION(_test, _msg);
    1.42 +#else
    1.43 +#define TEST_ASSERTION(_test, _msg) \
    1.44 +  PR_BEGIN_MACRO \
    1.45 +    if (!(_test)) { \
    1.46 +      NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
    1.47 +    } \
    1.48 +  PR_END_MACRO
    1.49 +#endif
    1.50 +
    1.51 +class Listener MOZ_FINAL : public nsIThreadPoolListener
    1.52 +{
    1.53 +public:
    1.54 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.55 +  NS_DECL_NSITHREADPOOLLISTENER
    1.56 +};
    1.57 +
    1.58 +NS_IMPL_ISUPPORTS(Listener, nsIThreadPoolListener)
    1.59 +
    1.60 +NS_IMETHODIMP
    1.61 +Listener::OnThreadCreated()
    1.62 +{
    1.63 +  nsCOMPtr<nsIThread> current(do_GetCurrentThread());
    1.64 +  TEST_ASSERTION(current, "Couldn't get current thread!");
    1.65 +
    1.66 +  ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
    1.67 +
    1.68 +  while (!gAllRunnablesPosted) {
    1.69 +    mon.Wait();
    1.70 +  }
    1.71 +
    1.72 +  for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
    1.73 +    nsIThread* thread = gCreatedThreadList[i];
    1.74 +    TEST_ASSERTION(thread != current, "Saw the same thread twice!");
    1.75 +
    1.76 +    if (!thread) {
    1.77 +      gCreatedThreadList[i] = current;
    1.78 +      if (i == (NUMBER_OF_THREADS - 1)) {
    1.79 +        gAllThreadsCreated = true;
    1.80 +        mon.NotifyAll();
    1.81 +      }
    1.82 +      return NS_OK;
    1.83 +    }
    1.84 +  }
    1.85 +
    1.86 +  TEST_ASSERTION(false, "Too many threads!");
    1.87 +  return NS_ERROR_FAILURE;
    1.88 +}
    1.89 +
    1.90 +NS_IMETHODIMP
    1.91 +Listener::OnThreadShuttingDown()
    1.92 +{
    1.93 +  nsCOMPtr<nsIThread> current(do_GetCurrentThread());
    1.94 +  TEST_ASSERTION(current, "Couldn't get current thread!");
    1.95 +
    1.96 +  ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
    1.97 +
    1.98 +  for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
    1.99 +    nsIThread* thread = gShutDownThreadList[i];
   1.100 +    TEST_ASSERTION(thread != current, "Saw the same thread twice!");
   1.101 +
   1.102 +    if (!thread) {
   1.103 +      gShutDownThreadList[i] = current;
   1.104 +      if (i == (NUMBER_OF_THREADS - 1)) {
   1.105 +        gAllThreadsShutDown = true;
   1.106 +        mon.NotifyAll();
   1.107 +      }
   1.108 +      return NS_OK;
   1.109 +    }
   1.110 +  }
   1.111 +
   1.112 +  TEST_ASSERTION(false, "Too many threads!");
   1.113 +  return NS_ERROR_FAILURE;
   1.114 +}
   1.115 +
   1.116 +class AutoCreateAndDestroyReentrantMonitor
   1.117 +{
   1.118 +public:
   1.119 +  AutoCreateAndDestroyReentrantMonitor(ReentrantMonitor** aReentrantMonitorPtr)
   1.120 +  : mReentrantMonitorPtr(aReentrantMonitorPtr) {
   1.121 +    *aReentrantMonitorPtr = new ReentrantMonitor("TestThreadPoolListener::AutoMon");
   1.122 +    TEST_ASSERTION(*aReentrantMonitorPtr, "Out of memory!");
   1.123 +  }
   1.124 +
   1.125 +  ~AutoCreateAndDestroyReentrantMonitor() {
   1.126 +    if (*mReentrantMonitorPtr) {
   1.127 +      delete *mReentrantMonitorPtr;
   1.128 +      *mReentrantMonitorPtr = nullptr;
   1.129 +    }
   1.130 +  }
   1.131 +
   1.132 +private:
   1.133 +  ReentrantMonitor** mReentrantMonitorPtr;
   1.134 +};
   1.135 +
   1.136 +int main(int argc, char** argv)
   1.137 +{
   1.138 +  ScopedXPCOM xpcom("ThreadPoolListener");
   1.139 +  NS_ENSURE_FALSE(xpcom.failed(), 1);
   1.140 +
   1.141 +  nsIThread* createdThreadList[NUMBER_OF_THREADS] = { nullptr };
   1.142 +  gCreatedThreadList = createdThreadList;
   1.143 +
   1.144 +  nsIThread* shutDownThreadList[NUMBER_OF_THREADS] = { nullptr };
   1.145 +  gShutDownThreadList = shutDownThreadList;
   1.146 +
   1.147 +  AutoCreateAndDestroyReentrantMonitor newMon(&gReentrantMonitor);
   1.148 +  NS_ENSURE_TRUE(gReentrantMonitor, 1);
   1.149 +
   1.150 +  nsresult rv;
   1.151 +
   1.152 +  nsCOMPtr<nsIThreadPool> pool =
   1.153 +    do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
   1.154 +  NS_ENSURE_SUCCESS(rv, 1);
   1.155 +
   1.156 +  rv = pool->SetThreadLimit(NUMBER_OF_THREADS);
   1.157 +  NS_ENSURE_SUCCESS(rv, 1);
   1.158 +
   1.159 +  rv = pool->SetIdleThreadLimit(NUMBER_OF_THREADS);
   1.160 +  NS_ENSURE_SUCCESS(rv, 1);
   1.161 +
   1.162 +  rv = pool->SetIdleThreadTimeout(IDLE_THREAD_TIMEOUT);
   1.163 +  NS_ENSURE_SUCCESS(rv, 1);
   1.164 +
   1.165 +  nsCOMPtr<nsIThreadPoolListener> listener = new Listener();
   1.166 +  NS_ENSURE_TRUE(listener, 1);
   1.167 +
   1.168 +  rv = pool->SetListener(listener);
   1.169 +  NS_ENSURE_SUCCESS(rv, 1);
   1.170 +
   1.171 +  {
   1.172 +    ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
   1.173 +
   1.174 +    for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
   1.175 +      nsCOMPtr<nsIRunnable> runnable = new nsRunnable();
   1.176 +      NS_ENSURE_TRUE(runnable, 1);
   1.177 +
   1.178 +      rv = pool->Dispatch(runnable, NS_DISPATCH_NORMAL);
   1.179 +      NS_ENSURE_SUCCESS(rv, 1);
   1.180 +    }
   1.181 +
   1.182 +    gAllRunnablesPosted = true;
   1.183 +    mon.NotifyAll();
   1.184 +  }
   1.185 +
   1.186 +  {
   1.187 +    ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
   1.188 +    while (!gAllThreadsCreated) {
   1.189 +      mon.Wait();
   1.190 +    }
   1.191 +  }
   1.192 +
   1.193 +  rv = pool->Shutdown();
   1.194 +  NS_ENSURE_SUCCESS(rv, 1);
   1.195 +
   1.196 +  {
   1.197 +    ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
   1.198 +    while (!gAllThreadsShutDown) {
   1.199 +      mon.Wait();
   1.200 +    }
   1.201 +  }
   1.202 +
   1.203 +  for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
   1.204 +    nsIThread* created = gCreatedThreadList[i];
   1.205 +    NS_ENSURE_TRUE(created, 1);
   1.206 +
   1.207 +    bool match = false;
   1.208 +    for (uint32_t j = 0; j < NUMBER_OF_THREADS; j++) {
   1.209 +      nsIThread* destroyed = gShutDownThreadList[j];
   1.210 +      NS_ENSURE_TRUE(destroyed, 1);
   1.211 +
   1.212 +      if (destroyed == created) {
   1.213 +        match = true;
   1.214 +        break;
   1.215 +      }
   1.216 +    }
   1.217 +
   1.218 +    NS_ENSURE_TRUE(match, 1);
   1.219 +  }
   1.220 +
   1.221 +  return 0;
   1.222 +}

mercurial