Bug 1751995 - Migrate TestAsyncReturns r=ipc-reviewers,nika

Differential Revision: https://phabricator.services.mozilla.com/D200960
This commit is contained in:
Alex Franchuk 2024-03-13 13:10:10 +00:00
parent 67c1f33507
commit 47c20f8b9b
6 changed files with 83 additions and 163 deletions

View File

@ -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<const uint32_t&, const uint32_t&>(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<uint32_t, uint32_t>& 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

View File

@ -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

View File

@ -11,7 +11,6 @@ EXPORTS.mozilla._ipdltest += [
"IPDLUnitTests.h", "IPDLUnitTests.h",
"IPDLUnitTestTypes.h", "IPDLUnitTestTypes.h",
"IPDLUnitTestUtils.h", "IPDLUnitTestUtils.h",
"TestAsyncReturns.h",
"TestBadActor.h", "TestBadActor.h",
"TestCancel.h", "TestCancel.h",
"TestCrashCleanup.h", "TestCrashCleanup.h",
@ -52,7 +51,6 @@ EXPORTS.mozilla._ipdltest += [
] ]
SOURCES += [ SOURCES += [
"TestAsyncReturns.cpp",
"TestBadActor.cpp", "TestBadActor.cpp",
"TestCancel.cpp", "TestCancel.cpp",
"TestCrashCleanup.cpp", "TestCrashCleanup.cpp",
@ -99,7 +97,6 @@ SOURCES += [
] ]
IPDL_SOURCES += [ IPDL_SOURCES += [
"PTestAsyncReturns.ipdl",
"PTestBadActor.ipdl", "PTestBadActor.ipdl",
"PTestBadActorSub.ipdl", "PTestBadActorSub.ipdl",
"PTestCancel.ipdl", "PTestCancel.ipdl",

View File

@ -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 mozilla {
namespace _ipdltest { namespace _ipdltest {
[ChildProc=any, ChildImpl=virtual, ParentImpl=virtual]
[ManualDealloc, ChildImpl="TestAsyncReturnsChild", ParentImpl="TestAsyncReturnsParent"]
protocol PTestAsyncReturns { protocol PTestAsyncReturns {
child: child:
async Ping() returns (bool one); async Ping() returns (bool one);
async NoReturn() returns (bool unused); async NoReturn() returns (bool unused);
@ -15,6 +15,5 @@ parent:
async Pong() returns (uint32_t param1, uint32_t param2); async Pong() returns (uint32_t param1, uint32_t param2);
}; };
} // namespace mozilla } // namespace mozilla
} // namespace _ipdltest } // namespace _ipdltest

View File

@ -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<uint32_t, uint32_t>& 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<const uint32_t&, const uint32_t&>(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

View File

@ -16,6 +16,7 @@ EXPORTS.mozilla._ipdltest += [
SOURCES += [ SOURCES += [
"IPDLUnitTest.cpp", "IPDLUnitTest.cpp",
"TestAsyncReturns.cpp",
"TestBasic.cpp", "TestBasic.cpp",
"TestCrossProcessSemaphore.cpp", "TestCrossProcessSemaphore.cpp",
"TestInduceConnectionError.cpp", "TestInduceConnectionError.cpp",
@ -24,6 +25,7 @@ SOURCES += [
IPDL_SOURCES += [ IPDL_SOURCES += [
"PIPDLUnitTest.ipdl", "PIPDLUnitTest.ipdl",
"PTestAsyncReturns.ipdl",
"PTestBasic.ipdl", "PTestBasic.ipdl",
"PTestCrossProcessSemaphore.ipdl", "PTestCrossProcessSemaphore.ipdl",
"PTestInduceConnectionError.ipdl", "PTestInduceConnectionError.ipdl",