xpcom/tests/TestTimers.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/tests/TestTimers.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,186 @@
     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 "nsITimer.h"
    1.13 +
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsComponentManagerUtils.h"
    1.16 +#include "nsServiceManagerUtils.h"
    1.17 +#include "nsThreadUtils.h"
    1.18 +#include "prinrval.h"
    1.19 +#include "prmon.h"
    1.20 +#include "prthread.h"
    1.21 +#include "mozilla/Attributes.h"
    1.22 +
    1.23 +#include "mozilla/ReentrantMonitor.h"
    1.24 +using namespace mozilla;
    1.25 +
    1.26 +typedef nsresult(*TestFuncPtr)();
    1.27 +
    1.28 +class AutoTestThread
    1.29 +{
    1.30 +public:
    1.31 +  AutoTestThread() {
    1.32 +    nsCOMPtr<nsIThread> newThread;
    1.33 +    nsresult rv = NS_NewThread(getter_AddRefs(newThread));
    1.34 +    if (NS_FAILED(rv))
    1.35 +      return;
    1.36 +
    1.37 +    newThread.swap(mThread);
    1.38 +  }
    1.39 +
    1.40 +  ~AutoTestThread() {
    1.41 +    mThread->Shutdown();
    1.42 +  }
    1.43 +
    1.44 +  operator nsIThread*() const {
    1.45 +    return mThread;
    1.46 +  }
    1.47 +
    1.48 +  nsIThread* operator->() const {
    1.49 +    return mThread;
    1.50 +  }
    1.51 +
    1.52 +private:
    1.53 +  nsCOMPtr<nsIThread> mThread;
    1.54 +};
    1.55 +
    1.56 +class AutoCreateAndDestroyReentrantMonitor
    1.57 +{
    1.58 +public:
    1.59 +  AutoCreateAndDestroyReentrantMonitor() {
    1.60 +    mReentrantMonitor = new ReentrantMonitor("TestTimers::AutoMon");
    1.61 +    NS_ASSERTION(mReentrantMonitor, "Out of memory!");
    1.62 +  }
    1.63 +
    1.64 +  ~AutoCreateAndDestroyReentrantMonitor() {
    1.65 +    delete mReentrantMonitor;
    1.66 +  }
    1.67 +
    1.68 +  operator ReentrantMonitor* () {
    1.69 +    return mReentrantMonitor;
    1.70 +  }
    1.71 +
    1.72 +private:
    1.73 +  ReentrantMonitor* mReentrantMonitor;
    1.74 +};
    1.75 +
    1.76 +class TimerCallback MOZ_FINAL : public nsITimerCallback
    1.77 +{
    1.78 +public:
    1.79 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.80 +
    1.81 +  TimerCallback(nsIThread** aThreadPtr, ReentrantMonitor* aReentrantMonitor)
    1.82 +  : mThreadPtr(aThreadPtr), mReentrantMonitor(aReentrantMonitor) { }
    1.83 +
    1.84 +  NS_IMETHOD Notify(nsITimer* aTimer) {
    1.85 +    NS_ASSERTION(mThreadPtr, "Callback was not supposed to be called!");
    1.86 +    nsCOMPtr<nsIThread> current(do_GetCurrentThread());
    1.87 +
    1.88 +    ReentrantMonitorAutoEnter mon(*mReentrantMonitor);
    1.89 +
    1.90 +    NS_ASSERTION(!*mThreadPtr, "Timer called back more than once!");
    1.91 +    *mThreadPtr = current;
    1.92 +
    1.93 +    mon.Notify();
    1.94 +
    1.95 +    return NS_OK;
    1.96 +  }
    1.97 +private:
    1.98 +  nsIThread** mThreadPtr;
    1.99 +  ReentrantMonitor* mReentrantMonitor;
   1.100 +};
   1.101 +
   1.102 +NS_IMPL_ISUPPORTS(TimerCallback, nsITimerCallback)
   1.103 +
   1.104 +nsresult
   1.105 +TestTargetedTimers()
   1.106 +{
   1.107 +  AutoCreateAndDestroyReentrantMonitor newMon;
   1.108 +  NS_ENSURE_TRUE(newMon, NS_ERROR_OUT_OF_MEMORY);
   1.109 +
   1.110 +  AutoTestThread testThread;
   1.111 +  NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
   1.112 +
   1.113 +  nsresult rv;
   1.114 +  nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
   1.115 +  NS_ENSURE_SUCCESS(rv, rv);
   1.116 +
   1.117 +  nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
   1.118 +
   1.119 +  rv = timer->SetTarget(target);
   1.120 +  NS_ENSURE_SUCCESS(rv, rv);
   1.121 +
   1.122 +  nsIThread* notifiedThread = nullptr;
   1.123 +
   1.124 +  nsCOMPtr<nsITimerCallback> callback =
   1.125 +    new TimerCallback(&notifiedThread, newMon);
   1.126 +  NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
   1.127 +
   1.128 +  rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(2000),
   1.129 +                               nsITimer::TYPE_ONE_SHOT);
   1.130 +  NS_ENSURE_SUCCESS(rv, rv);
   1.131 +
   1.132 +  ReentrantMonitorAutoEnter mon(*newMon);
   1.133 +  while (!notifiedThread) {
   1.134 +    mon.Wait();
   1.135 +  }
   1.136 +  NS_ENSURE_TRUE(notifiedThread == testThread, NS_ERROR_FAILURE);
   1.137 +
   1.138 +  return NS_OK;
   1.139 +}
   1.140 +
   1.141 +nsresult
   1.142 +TestTimerWithStoppedTarget()
   1.143 +{
   1.144 +  AutoTestThread testThread;
   1.145 +  NS_ENSURE_TRUE(testThread, NS_ERROR_OUT_OF_MEMORY);
   1.146 +
   1.147 +  nsresult rv;
   1.148 +  nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
   1.149 +  NS_ENSURE_SUCCESS(rv, rv);
   1.150 +
   1.151 +  nsIEventTarget* target = static_cast<nsIEventTarget*>(testThread);
   1.152 +
   1.153 +  rv = timer->SetTarget(target);
   1.154 +  NS_ENSURE_SUCCESS(rv, rv);
   1.155 +
   1.156 +  // If this is called, we'll assert
   1.157 +  nsCOMPtr<nsITimerCallback> callback =
   1.158 +    new TimerCallback(nullptr, nullptr);
   1.159 +  NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
   1.160 +
   1.161 +  rv = timer->InitWithCallback(callback, PR_MillisecondsToInterval(100),
   1.162 +                               nsITimer::TYPE_ONE_SHOT);
   1.163 +  NS_ENSURE_SUCCESS(rv, rv);
   1.164 +
   1.165 +  testThread->Shutdown();
   1.166 +
   1.167 +  PR_Sleep(400);
   1.168 +
   1.169 +  return NS_OK;
   1.170 +}
   1.171 +
   1.172 +int main(int argc, char** argv)
   1.173 +{
   1.174 +  ScopedXPCOM xpcom("TestTimers");
   1.175 +  NS_ENSURE_FALSE(xpcom.failed(), 1);
   1.176 +
   1.177 +  static TestFuncPtr testsToRun[] = {
   1.178 +    TestTargetedTimers,
   1.179 +    TestTimerWithStoppedTarget
   1.180 +  };
   1.181 +  static uint32_t testCount = sizeof(testsToRun) / sizeof(testsToRun[0]);
   1.182 +
   1.183 +  for (uint32_t i = 0; i < testCount; i++) {
   1.184 +    nsresult rv = testsToRun[i]();
   1.185 +    NS_ENSURE_SUCCESS(rv, 1);
   1.186 +  }
   1.187 +
   1.188 +  return 0;
   1.189 +}

mercurial