diff options
Diffstat (limited to 'pw_rpc/client_integration_test.cc')
-rw-r--r-- | pw_rpc/client_integration_test.cc | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/pw_rpc/client_integration_test.cc b/pw_rpc/client_integration_test.cc index 53bff084a..8f1021176 100644 --- a/pw_rpc/client_integration_test.cc +++ b/pw_rpc/client_integration_test.cc @@ -14,6 +14,8 @@ #include <sys/socket.h> +#include <algorithm> +#include <array> #include <cstring> #include "gtest/gtest.h" @@ -47,28 +49,35 @@ Benchmark::Client kServiceClient(pw::rpc::integration_test::client(), class StringReceiver { public: const char* Wait() { - PW_CHECK(sem_.try_acquire_for(1500ms)); - return buffer_; + PW_CHECK(sem_.try_acquire_for(10s)); + return reinterpret_cast<const char*>(buffer_.begin()); } Function<void(ConstByteSpan, Status)> UnaryOnCompleted() { - return [this](ConstByteSpan data, Status) { CopyPayload(data); }; + return [this](ConstByteSpan data, Status) { CopyStringPayload(data); }; } Function<void(ConstByteSpan)> OnNext() { - return [this](ConstByteSpan data) { CopyPayload(data); }; + return [this](ConstByteSpan data) { CopyStringPayload(data); }; } - private: - void CopyPayload(ConstByteSpan data) { - std::memset(buffer_, 0, sizeof(buffer_)); - PW_CHECK_UINT_LE(data.size(), sizeof(buffer_)); - std::memcpy(buffer_, data.data(), data.size()); + void CopyStringPayload(ConstByteSpan data) { + std::memset(buffer_.data(), 0, buffer_.size()); + PW_CHECK_UINT_LE(data.size(), buffer_.size()); + std::copy(data.begin(), data.end(), buffer_.begin()); sem_.release(); } + void ReverseCopyStringPayload(ConstByteSpan data) { + std::memset(buffer_.data(), 0, buffer_.size()); + PW_CHECK_UINT_LE(data.size(), buffer_.size()); + std::reverse_copy(data.begin(), data.end() - 1, buffer_.begin()); + sem_.release(); + } + + private: pw::sync::BinarySemaphore sem_; - char buffer_[64]; + std::array<std::byte, 64> buffer_; }; TEST(RawRpcIntegrationTest, Unary) { @@ -96,6 +105,38 @@ TEST(RawRpcIntegrationTest, BidirectionalStreaming) { } } +// This test sometimes fails due to a server stream packet being dropped. +// TODO: b/290048137 - Enable this test after the flakiness is fixed. +TEST(RawRpcIntegrationTest, DISABLED_OnNextOverwritesItsOwnCall) { + for (int i = 0; i < kIterations; ++i) { + struct { + StringReceiver receiver; + pw::rpc::RawClientReaderWriter call; + } ctx; + + // Chain together three calls. The first and third copy the string in normal + // order, while the second copies the string in reverse order. + ctx.call = kServiceClient.BidirectionalEcho([&ctx](ConstByteSpan data) { + ctx.call = kServiceClient.BidirectionalEcho([&ctx](ConstByteSpan data) { + ctx.receiver.ReverseCopyStringPayload(data); + ctx.call = kServiceClient.BidirectionalEcho(ctx.receiver.OnNext()); + }); + ctx.receiver.CopyStringPayload(data); + }); + + ASSERT_EQ(OkStatus(), ctx.call.Write(pw::as_bytes(pw::span("Window")))); + EXPECT_STREQ(ctx.receiver.Wait(), "Window"); + + ASSERT_EQ(OkStatus(), ctx.call.Write(pw::as_bytes(pw::span("Door")))); + EXPECT_STREQ(ctx.receiver.Wait(), "rooD"); + + ASSERT_EQ(OkStatus(), ctx.call.Write(pw::as_bytes(pw::span("Roof")))); + EXPECT_STREQ(ctx.receiver.Wait(), "Roof"); + + ASSERT_EQ(OkStatus(), ctx.call.Cancel()); + } +} + } // namespace } // namespace rpc_test |