blob: 7f15146139894f20763fbad939811b63c03a3778 [file] [log] [blame]
Alexei Frolov4d2adde2020-08-04 10:19:24 -07001// 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
20namespace pw::rpc::internal {
21namespace {
22
23TEST(BaseClientCall, RegistersAndRemovesItselfFromClient) {
24 ClientContextForTest context;
25 EXPECT_EQ(context.client().active_calls(), 0u);
26
27 {
28 BaseClientCall call(&context.channel(),
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070029 context.service_id(),
30 context.method_id(),
Alexei Frolov4d2adde2020-08-04 10:19:24 -070031 [](BaseClientCall&, const Packet&) {});
32 EXPECT_EQ(context.client().active_calls(), 1u);
33 }
34
35 EXPECT_EQ(context.client().active_calls(), 0u);
36}
37
38class FakeClientCall : public BaseClientCall {
39 public:
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
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
53TEST(BaseClientCall, SendsPacketWithPayload) {
54 ClientContextForTest context;
55 FakeClientCall call(&context.channel(),
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070056 context.service_id(),
57 context.method_id(),
Alexei Frolov4d2adde2020-08-04 10:19:24 -070058 [](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());
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070066 EXPECT_EQ(packet.service_id(), context.service_id());
67 EXPECT_EQ(packet.method_id(), context.method_id());
Alexei Frolov4d2adde2020-08-04 10:19:24 -070068 EXPECT_EQ(std::memcmp(packet.payload().data(), payload, sizeof(payload)), 0);
69}
70
71} // namespace
72} // namespace pw::rpc::internal