TestAsyncReturns.cpp (2392B)
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 /* 8 * These tests ensure that async function return values work as expected. 9 */ 10 11 #include "gtest/gtest.h" 12 13 #include "mozilla/_ipdltest/IPDLUnitTest.h" 14 #include "mozilla/_ipdltest/PTestAsyncReturnsChild.h" 15 #include "mozilla/_ipdltest/PTestAsyncReturnsParent.h" 16 17 using namespace mozilla::ipc; 18 19 namespace mozilla::_ipdltest { 20 21 static uint32_t sMagic1 = 0x105b59fb; 22 static uint32_t sMagic2 = 0x09b6f5e3; 23 24 class TestAsyncReturnsChild : public PTestAsyncReturnsChild { 25 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestAsyncReturnsChild, override) 26 private: 27 IPCResult RecvPing(PingResolver&& resolve) final override { 28 SendPong( 29 [resolve](const std::tuple<uint32_t, uint32_t>& aParam) { 30 EXPECT_EQ(std::get<0>(aParam), sMagic1); 31 EXPECT_EQ(std::get<1>(aParam), sMagic2); 32 resolve(true); 33 }, 34 [](ResponseRejectReason&& aReason) { FAIL() << "sending Pong"; }); 35 return IPC_OK(); 36 } 37 38 IPCResult RecvNoReturn(NoReturnResolver&& resolve) final override { 39 // Not calling `resolve` intentionally 40 return IPC_OK(); 41 } 42 43 ~TestAsyncReturnsChild() = default; 44 }; 45 46 class TestAsyncReturnsParent : public PTestAsyncReturnsParent { 47 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TestAsyncReturnsParent, override) 48 private: 49 IPCResult RecvPong(PongResolver&& resolve) final override { 50 resolve(std::tuple<const uint32_t&, const uint32_t&>(sMagic1, sMagic2)); 51 return IPC_OK(); 52 } 53 54 ~TestAsyncReturnsParent() = default; 55 }; 56 57 IPDL_TEST(TestAsyncReturns, NoReturn) { 58 mActor->SendNoReturn( 59 [](bool unused) { FAIL() << "resolve handler should not be called"; }, 60 [=](ResponseRejectReason&& aReason) { 61 if (aReason != ResponseRejectReason::ResolverDestroyed) { 62 FAIL() << "reject with wrong reason"; 63 } 64 mActor->Close(); 65 }); 66 } 67 68 IPDL_TEST(TestAsyncReturns, PingPong) { 69 mActor->SendPing( 70 [=](bool one) { 71 EXPECT_TRUE(one); 72 mActor->Close(); 73 }, 74 [](ResponseRejectReason&& aReason) { FAIL() << "sending Ping"; }); 75 } 76 77 } // namespace mozilla::_ipdltest