pw_rpc: Basic client implementation
This implements an RPC client class which is used to make RPC requests
to a server. Like the server, the client exists globally and processes
incoming RPC packets. The client keeps track of active RPC request
contexts through ClientCall objects, and dispatches packets to them when
an expected response is received.
A nanopb implementation of a ClientCall is added as well, supporting
unary and server-streaming RPC calls, with a codegen-ready API.
Change-Id: If9615877199e0d4bc468c33d3d9ecf85da32440a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/15741
Commit-Queue: Alexei Frolov <frolv@google.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_rpc/client_test.cc b/pw_rpc/client_test.cc
new file mode 100644
index 0000000..13a7cbe
--- /dev/null
+++ b/pw_rpc/client_test.cc
@@ -0,0 +1,78 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "pw_rpc/client.h"
+
+#include "gtest/gtest.h"
+#include "pw_rpc/internal/packet.h"
+#include "pw_rpc_private/internal_test_utils.h"
+
+namespace pw::rpc {
+namespace {
+
+using internal::BaseClientCall;
+using internal::Packet;
+using internal::PacketType;
+
+class TestClientCall : public BaseClientCall {
+ public:
+ constexpr TestClientCall(Channel* channel,
+ uint32_t service_id,
+ uint32_t method_id)
+ : BaseClientCall(channel, service_id, method_id, ProcessPacket) {}
+
+ static void ProcessPacket(BaseClientCall& call, const Packet& packet) {
+ static_cast<TestClientCall&>(call).HandlePacket(packet);
+ }
+
+ void HandlePacket(const Packet&) { invoked_ = true; }
+
+ constexpr bool invoked() const { return invoked_; }
+
+ private:
+ bool invoked_ = false;
+};
+
+TEST(Client, ProcessPacket_InvokesARegisteredClientCall) {
+ ClientContextForTest context;
+
+ TestClientCall call(
+ &context.channel(), context.kServiceId, context.kMethodId);
+ EXPECT_EQ(context.SendResponse(Status::Ok(), {}), Status::Ok());
+
+ EXPECT_TRUE(call.invoked());
+}
+
+TEST(Client, ProcessPacket_ReturnsNotFoundOnUnregisteredCall) {
+ ClientContextForTest context;
+ EXPECT_EQ(context.SendResponse(Status::OK, {}), Status::NotFound());
+}
+
+TEST(Client, ProcessPacket_ReturnsDataLossOnBadPacket) {
+ ClientContextForTest context;
+
+ constexpr std::byte bad_packet[]{
+ std::byte{0xab}, std::byte{0xcd}, std::byte{0xef}};
+ EXPECT_EQ(context.client().ProcessPacket(bad_packet), Status::DataLoss());
+}
+
+TEST(Client, ProcessPacket_ReturnsInvalidArgumentOnServerPacket) {
+ ClientContextForTest context;
+ EXPECT_EQ(context.SendPacket(PacketType::REQUEST), Status::InvalidArgument());
+ EXPECT_EQ(context.SendPacket(PacketType::CANCEL_SERVER_STREAM),
+ Status::InvalidArgument());
+}
+
+} // namespace
+} // namespace pw::rpc