1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_rpc/internal/base_client_call.h"
16
17 #include "gtest/gtest.h"
18 #include "pw_rpc_private/internal_test_utils.h"
19
20 namespace pw::rpc::internal {
21 namespace {
22
TEST(BaseClientCall,RegistersAndRemovesItselfFromClient)23 TEST(BaseClientCall, RegistersAndRemovesItselfFromClient) {
24 ClientContextForTest context;
25 EXPECT_EQ(context.client().active_calls(), 0u);
26
27 {
28 BaseClientCall call(&context.channel(),
29 context.service_id(),
30 context.method_id(),
31 [](BaseClientCall&, const Packet&) {});
32 EXPECT_EQ(context.client().active_calls(), 1u);
33 }
34
35 EXPECT_EQ(context.client().active_calls(), 0u);
36 }
37
38 class FakeClientCall : public BaseClientCall {
39 public:
FakeClientCall(rpc::Channel * channel,uint32_t service_id,uint32_t method_id,ResponseHandler handler)40 constexpr FakeClientCall(rpc::Channel* channel,
41 uint32_t service_id,
42 uint32_t method_id,
43 ResponseHandler handler)
44 : BaseClientCall(channel, service_id, method_id, handler) {}
45
SendPacket(std::span<const std::byte> payload)46 Status SendPacket(std::span<const std::byte> payload) {
47 std::span buffer = AcquirePayloadBuffer();
48 std::memcpy(buffer.data(), payload.data(), payload.size());
49 return ReleasePayloadBuffer(buffer.first(payload.size()));
50 }
51 };
52
TEST(BaseClientCall,SendsPacketWithPayload)53 TEST(BaseClientCall, SendsPacketWithPayload) {
54 ClientContextForTest context;
55 FakeClientCall call(&context.channel(),
56 context.service_id(),
57 context.method_id(),
58 [](BaseClientCall&, const Packet&) {});
59
60 constexpr std::byte payload[]{std::byte{0x08}, std::byte{0x39}};
61 call.SendPacket(payload);
62
63 EXPECT_EQ(context.output().packet_count(), 1u);
64 Packet packet = context.output().sent_packet();
65 EXPECT_EQ(packet.channel_id(), context.channel().id());
66 EXPECT_EQ(packet.service_id(), context.service_id());
67 EXPECT_EQ(packet.method_id(), context.method_id());
68 EXPECT_EQ(std::memcmp(packet.payload().data(), payload, sizeof(payload)), 0);
69 }
70
71 } // namespace
72 } // namespace pw::rpc::internal
73