From 85df64a76c5c6c1f137feebfbccd94046ad7a844 Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Sun, 17 Apr 2022 23:47:24 +0100 Subject: [PATCH] [+] Added async pipe test --- Tests/Public/21. Hello IPC/Main.cpp | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Tests/Public/21. Hello IPC/Main.cpp b/Tests/Public/21. Hello IPC/Main.cpp index bdeef1b..4ac10c6 100755 --- a/Tests/Public/21. Hello IPC/Main.cpp +++ b/Tests/Public/21. Hello IPC/Main.cpp @@ -170,7 +170,75 @@ TEST(IPC, Pipe) pipeImported.reset(); ASSERT_FALSE(pipe->AsReadChannelIsOpen()->IsSignaled()); +} +TEST(IPC, AsyncPipe) +{ + auto pipe = Aurora::IPC::NewPipe(); + ASSERT_TRUE(bool(pipe)); + + ASSERT_FALSE(pipe->AsReadChannelIsOpen()->IsSignaled()); + + auto handleString = pipe->ExportToString(); + ASSERT_TRUE(bool(handleString.size())); + AuLogDbg("Exported pipe handle: {}", handleString); + + auto pipeImported = Aurora::IPC::ImportPipe(handleString); + ASSERT_TRUE(bool(pipeImported)); + + ASSERT_TRUE(pipe->AsReadChannelIsOpen()->IsSignaled()); + + static const AuString kHelloWorldClient = "Hello Client"; + static const AuString kHelloWorldServer = "Hello Server"; + + auto transactionA = pipe->NewAsyncTransaction(); + auto transactionB = pipeImported->NewAsyncTransaction(); + + + // Set callback + transactionA->SetCallback(AuMakeShared([](AuUInt64 offset, AuUInt32 length) + { + AuLogDbg("IPC server callback: {} {}", offset, length); + })); + + transactionB->SetCallback(AuMakeShared([](AuUInt64 offset, AuUInt32 length) + { + AuLogDbg("IPC client callback: {} {}", offset, length); + })); + + + // + AuByteBuffer writeBuffer(512); + AuMemoryViewRead writeView(writeBuffer); + + AuRng::RngFillRange(writeBuffer); + ASSERT_TRUE(transactionA->StartWrite(0, AuUnsafeRaiiToShared(&writeView))); + + AuByteBuffer readBuffer(512); + AuMemoryViewWrite readView(readBuffer.begin(), readBuffer.end()); + ASSERT_TRUE(transactionB->StartRead(0, AuUnsafeRaiiToShared(&readView))); + + // Create loop to sync against the two outstanding IO requests + auto loop = Aurora::Loop::NewLoopQueue(); + + // Add initial loop sources + ASSERT_TRUE(loop->SourceAdd(transactionA->NewLoopSource())); + ASSERT_TRUE(loop->SourceAdd(transactionB->NewLoopSource())); + + ASSERT_TRUE(loop->Commit()); + + // Wait for 100 MS + ASSERT_TRUE(loop->WaitAll(0)); + + ASSERT_EQ(writeBuffer, readBuffer); + + // Reset client pipe + pipeImported.reset(); + transactionB.reset(); + transactionA.reset(); + + // Assert dead + ASSERT_FALSE(pipe->AsReadChannelIsOpen()->IsSignaled()); } void RunTests()