Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / gtest / TestCommon.h
blob76620fbb447cb940c5eabe11d9b67b6ca3e8698a
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef TestCommon_h__
6 #define TestCommon_h__
8 #include <stdlib.h>
9 #include "nsThreadUtils.h"
10 #include "mozilla/Attributes.h"
11 #include "mozilla/SpinEventLoopUntil.h"
13 //-----------------------------------------------------------------------------
15 class WaitForCondition final : public nsIRunnable {
16 public:
17 NS_DECL_THREADSAFE_ISUPPORTS
19 void Wait(int pending) {
20 MOZ_RELEASE_ASSERT(NS_IsMainThread());
21 MOZ_RELEASE_ASSERT(mPending == 0);
23 mPending = pending;
24 mozilla::SpinEventLoopUntil("TestCommon.h:WaitForCondition::Wait"_ns,
25 [&]() { return !mPending; });
26 NS_ProcessPendingEvents(nullptr);
29 void Notify() { NS_DispatchToMainThread(this); }
31 private:
32 virtual ~WaitForCondition() = default;
34 NS_IMETHOD Run() override {
35 MOZ_RELEASE_ASSERT(NS_IsMainThread());
36 MOZ_RELEASE_ASSERT(mPending);
38 --mPending;
39 return NS_OK;
42 uint32_t mPending = 0;
45 #endif