TestCrossProcessSemaphore.cpp (2253B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 9 #include "mozilla/_ipdltest/IPDLUnitTest.h" 10 #include "mozilla/_ipdltest/PTestCrossProcessSemaphoreChild.h" 11 #include "mozilla/_ipdltest/PTestCrossProcessSemaphoreParent.h" 12 13 #include "mozilla/ipc/CrossProcessSemaphore.h" 14 15 using namespace mozilla::ipc; 16 17 namespace mozilla::_ipdltest { 18 19 class TestCrossProcessSemaphoreChild : public PTestCrossProcessSemaphoreChild { 20 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestCrossProcessSemaphoreChild, 21 override) 22 23 public: 24 IPCResult RecvCrossProcessSemaphore( 25 CrossProcessSemaphoreHandle&& aSem) override { 26 UniquePtr<CrossProcessSemaphore> cps( 27 CrossProcessSemaphore::Create(std::move(aSem))); 28 EXPECT_TRUE(bool(cps)); 29 cps->Signal(); 30 Close(); 31 return IPC_OK(); 32 } 33 34 private: 35 ~TestCrossProcessSemaphoreChild() = default; 36 }; 37 38 class TestCrossProcessSemaphoreParent 39 : public PTestCrossProcessSemaphoreParent { 40 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestCrossProcessSemaphoreParent, 41 override) 42 43 private: 44 ~TestCrossProcessSemaphoreParent() = default; 45 }; 46 47 IPDL_TEST(TestCrossProcessSemaphore) { 48 // Create a semaphore with an initial count of 1. The test will then try to 49 // send the semaphore over IPDL to the other end which will signal, increasing 50 // the count to 2. The test will then try to wait on (decrement) the semaphore 51 // twice, which should succeed only if the semaphore was properly signaled. 52 UniquePtr<CrossProcessSemaphore> cps( 53 CrossProcessSemaphore::Create("TestCrossProcessSemaphore", 1)); 54 ASSERT_TRUE(bool(cps)); 55 CrossProcessSemaphoreHandle handle = cps->CloneHandle(); 56 ASSERT_TRUE(bool(handle)); 57 bool ok = mActor->SendCrossProcessSemaphore(std::move(handle)); 58 ASSERT_TRUE(ok); 59 EXPECT_TRUE(cps->Wait()); 60 EXPECT_TRUE(cps->Wait(Some(TimeDuration::FromSeconds(10)))); 61 } 62 63 } // namespace mozilla::_ipdltest