From 47c20f8b9baf04e4a90c4f9a5cbaca53d170f2d4 Mon Sep 17 00:00:00 2001 From: Alex Franchuk Date: Wed, 13 Mar 2024 13:10:10 +0000 Subject: [PATCH] Bug 1751995 - Migrate TestAsyncReturns r=ipc-reviewers,nika Differential Revision: https://phabricator.services.mozilla.com/D200960 --- ipc/ipdl/test/cxx/TestAsyncReturns.cpp | 101 ------------------ ipc/ipdl/test/cxx/TestAsyncReturns.h | 54 ---------- ipc/ipdl/test/cxx/moz.build | 3 - .../{cxx => gtest}/PTestAsyncReturns.ipdl | 9 +- ipc/ipdl/test/gtest/TestAsyncReturns.cpp | 77 +++++++++++++ ipc/ipdl/test/gtest/moz.build | 2 + 6 files changed, 83 insertions(+), 163 deletions(-) delete mode 100644 ipc/ipdl/test/cxx/TestAsyncReturns.cpp delete mode 100644 ipc/ipdl/test/cxx/TestAsyncReturns.h rename ipc/ipdl/test/{cxx => gtest}/PTestAsyncReturns.ipdl (52%) create mode 100644 ipc/ipdl/test/gtest/TestAsyncReturns.cpp diff --git a/ipc/ipdl/test/cxx/TestAsyncReturns.cpp b/ipc/ipdl/test/cxx/TestAsyncReturns.cpp deleted file mode 100644 index 76597143c0b2..000000000000 --- a/ipc/ipdl/test/cxx/TestAsyncReturns.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "TestAsyncReturns.h" - -#include "IPDLUnitTests.h" // fail etc. - -#include "mozilla/AbstractThread.h" -#include "mozilla/Unused.h" - -namespace mozilla { -namespace _ipdltest { - -static uint32_t sMagic1 = 0x105b59fb; -static uint32_t sMagic2 = 0x09b6f5e3; - -//----------------------------------------------------------------------------- -// parent - -TestAsyncReturnsParent::TestAsyncReturnsParent() { - MOZ_COUNT_CTOR(TestAsyncReturnsParent); -} - -TestAsyncReturnsParent::~TestAsyncReturnsParent() { - MOZ_COUNT_DTOR(TestAsyncReturnsParent); -} - -void TestAsyncReturnsParent::Main() { - SendNoReturn()->Then( - MessageLoop::current()->SerialEventTarget(), __func__, - [](bool unused) { fail("resolve handler should not be called"); }, - [](ResponseRejectReason&& aReason) { - // MozPromise asserts in debug build if the - // handler is not called - if (aReason != ResponseRejectReason::ChannelClosed) { - fail("reject with wrong reason"); - } - passed("reject handler called on channel close"); - }); - SendPing()->Then( - MessageLoop::current()->SerialEventTarget(), __func__, - [this](bool one) { - if (one) { - passed("take one argument"); - } else { - fail("get one argument but has wrong value"); - } - - // Also try with the callback-based API. - SendPing( - [this](bool one) { - if (one) { - passed("take one argument"); - } else { - fail("get one argument but has wrong value"); - } - Close(); - }, - [](ResponseRejectReason&& aReason) { fail("sending Ping"); }); - }, - [](ResponseRejectReason&& aReason) { fail("sending Ping"); }); -} - -mozilla::ipc::IPCResult TestAsyncReturnsParent::RecvPong( - PongResolver&& aResolve) { - aResolve(std::tuple(sMagic1, sMagic2)); - return IPC_OK(); -} - -//----------------------------------------------------------------------------- -// child - -TestAsyncReturnsChild::TestAsyncReturnsChild() { - MOZ_COUNT_CTOR(TestAsyncReturnsChild); -} - -TestAsyncReturnsChild::~TestAsyncReturnsChild() { - MOZ_COUNT_DTOR(TestAsyncReturnsChild); -} - -mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvNoReturn( - NoReturnResolver&& aResolve) { - // Not resolving the promise intentionally - return IPC_OK(); -} - -mozilla::ipc::IPCResult TestAsyncReturnsChild::RecvPing( - PingResolver&& aResolve) { - SendPong()->Then( - MessageLoop::current()->SerialEventTarget(), __func__, - [aResolve](const std::tuple& aParam) { - if (std::get<0>(aParam) == sMagic1 && std::get<1>(aParam) == sMagic2) { - passed("take two arguments"); - } else { - fail("get two argument but has wrong value"); - } - aResolve(true); - }, - [](ResponseRejectReason&& aReason) { fail("sending Pong"); }); - return IPC_OK(); -} - -} // namespace _ipdltest -} // namespace mozilla diff --git a/ipc/ipdl/test/cxx/TestAsyncReturns.h b/ipc/ipdl/test/cxx/TestAsyncReturns.h deleted file mode 100644 index 5dad3da0abaa..000000000000 --- a/ipc/ipdl/test/cxx/TestAsyncReturns.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef mozilla__ipdltest_TestAsyncReturns_h -#define mozilla__ipdltest_TestAsyncReturns_h 1 - -#include "mozilla/_ipdltest/IPDLUnitTests.h" - -#include "mozilla/_ipdltest/PTestAsyncReturnsParent.h" -#include "mozilla/_ipdltest/PTestAsyncReturnsChild.h" - -namespace mozilla { -namespace _ipdltest { - -class TestAsyncReturnsParent : public PTestAsyncReturnsParent { - friend class PTestAsyncReturnsParent; - - public: - TestAsyncReturnsParent(); - virtual ~TestAsyncReturnsParent(); - - static bool RunTestInProcesses() { return true; } - static bool RunTestInThreads() { return true; } - - void Main(); - - protected: - mozilla::ipc::IPCResult RecvPong(PongResolver&& aResolve); - - virtual void ActorDestroy(ActorDestroyReason why) override { - if (NormalShutdown != why) fail("unexpected destruction!"); - passed("ok"); - QuitParent(); - } -}; - -class TestAsyncReturnsChild : public PTestAsyncReturnsChild { - friend class PTestAsyncReturnsChild; - - public: - TestAsyncReturnsChild(); - virtual ~TestAsyncReturnsChild(); - - protected: - mozilla::ipc::IPCResult RecvPing(PingResolver&& aResolve); - mozilla::ipc::IPCResult RecvNoReturn(NoReturnResolver&& aResolve); - - virtual void ActorDestroy(ActorDestroyReason why) override { - if (NormalShutdown != why) fail("unexpected destruction!"); - QuitChild(); - } -}; - -} // namespace _ipdltest -} // namespace mozilla - -#endif // ifndef mozilla__ipdltest_TestAsyncReturns_h diff --git a/ipc/ipdl/test/cxx/moz.build b/ipc/ipdl/test/cxx/moz.build index 61e2143d59da..7eba7157ce35 100644 --- a/ipc/ipdl/test/cxx/moz.build +++ b/ipc/ipdl/test/cxx/moz.build @@ -11,7 +11,6 @@ EXPORTS.mozilla._ipdltest += [ "IPDLUnitTests.h", "IPDLUnitTestTypes.h", "IPDLUnitTestUtils.h", - "TestAsyncReturns.h", "TestBadActor.h", "TestCancel.h", "TestCrashCleanup.h", @@ -52,7 +51,6 @@ EXPORTS.mozilla._ipdltest += [ ] SOURCES += [ - "TestAsyncReturns.cpp", "TestBadActor.cpp", "TestCancel.cpp", "TestCrashCleanup.cpp", @@ -99,7 +97,6 @@ SOURCES += [ ] IPDL_SOURCES += [ - "PTestAsyncReturns.ipdl", "PTestBadActor.ipdl", "PTestBadActorSub.ipdl", "PTestCancel.ipdl", diff --git a/ipc/ipdl/test/cxx/PTestAsyncReturns.ipdl b/ipc/ipdl/test/gtest/PTestAsyncReturns.ipdl similarity index 52% rename from ipc/ipdl/test/cxx/PTestAsyncReturns.ipdl rename to ipc/ipdl/test/gtest/PTestAsyncReturns.ipdl index 42ef17d1e84c..5cae489b4a8f 100644 --- a/ipc/ipdl/test/cxx/PTestAsyncReturns.ipdl +++ b/ipc/ipdl/test/gtest/PTestAsyncReturns.ipdl @@ -1,12 +1,12 @@ -include "mozilla/_ipdltest/TestAsyncReturns.h"; +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ namespace mozilla { namespace _ipdltest { - -[ManualDealloc, ChildImpl="TestAsyncReturnsChild", ParentImpl="TestAsyncReturnsParent"] +[ChildProc=any, ChildImpl=virtual, ParentImpl=virtual] protocol PTestAsyncReturns { - child: async Ping() returns (bool one); async NoReturn() returns (bool unused); @@ -15,6 +15,5 @@ parent: async Pong() returns (uint32_t param1, uint32_t param2); }; - } // namespace mozilla } // namespace _ipdltest diff --git a/ipc/ipdl/test/gtest/TestAsyncReturns.cpp b/ipc/ipdl/test/gtest/TestAsyncReturns.cpp new file mode 100644 index 000000000000..960a2954db05 --- /dev/null +++ b/ipc/ipdl/test/gtest/TestAsyncReturns.cpp @@ -0,0 +1,77 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* + * These tests ensure that async function return values work as expected. + */ + +#include "gtest/gtest.h" + +#include "mozilla/_ipdltest/IPDLUnitTest.h" +#include "mozilla/_ipdltest/PTestAsyncReturnsChild.h" +#include "mozilla/_ipdltest/PTestAsyncReturnsParent.h" + +using namespace mozilla::ipc; + +namespace mozilla::_ipdltest { + +static uint32_t sMagic1 = 0x105b59fb; +static uint32_t sMagic2 = 0x09b6f5e3; + +class TestAsyncReturnsChild : public PTestAsyncReturnsChild { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestAsyncReturnsChild, override) + private: + IPCResult RecvPing(PingResolver&& resolve) final override { + SendPong( + [resolve](const std::tuple& aParam) { + EXPECT_EQ(std::get<0>(aParam), sMagic1); + EXPECT_EQ(std::get<1>(aParam), sMagic2); + resolve(true); + }, + [](ResponseRejectReason&& aReason) { FAIL() << "sending Pong"; }); + return IPC_OK(); + } + + IPCResult RecvNoReturn(NoReturnResolver&& resolve) final override { + // Not calling `resolve` intentionally + return IPC_OK(); + } + + ~TestAsyncReturnsChild() = default; +}; + +class TestAsyncReturnsParent : public PTestAsyncReturnsParent { + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestAsyncReturnsParent, override) + private: + IPCResult RecvPong(PongResolver&& resolve) final override { + resolve(std::tuple(sMagic1, sMagic2)); + return IPC_OK(); + } + + ~TestAsyncReturnsParent() = default; +}; + +IPDL_TEST(TestAsyncReturns, NoReturn) { + mActor->SendNoReturn( + [](bool unused) { FAIL() << "resolve handler should not be called"; }, + [=](ResponseRejectReason&& aReason) { + if (aReason != ResponseRejectReason::ResolverDestroyed) { + FAIL() << "reject with wrong reason"; + } + mActor->Close(); + }); +} + +IPDL_TEST(TestAsyncReturns, PingPong) { + mActor->SendPing( + [=](bool one) { + EXPECT_TRUE(one); + mActor->Close(); + }, + [](ResponseRejectReason&& aReason) { FAIL() << "sending Ping"; }); +} + +} // namespace mozilla::_ipdltest diff --git a/ipc/ipdl/test/gtest/moz.build b/ipc/ipdl/test/gtest/moz.build index 129e366a4bc6..531723d2f863 100644 --- a/ipc/ipdl/test/gtest/moz.build +++ b/ipc/ipdl/test/gtest/moz.build @@ -16,6 +16,7 @@ EXPORTS.mozilla._ipdltest += [ SOURCES += [ "IPDLUnitTest.cpp", + "TestAsyncReturns.cpp", "TestBasic.cpp", "TestCrossProcessSemaphore.cpp", "TestInduceConnectionError.cpp", @@ -24,6 +25,7 @@ SOURCES += [ IPDL_SOURCES += [ "PIPDLUnitTest.ipdl", + "PTestAsyncReturns.ipdl", "PTestBasic.ipdl", "PTestCrossProcessSemaphore.ipdl", "PTestInduceConnectionError.ipdl",