blob: e4f0dff63c48296ff3d09db90b338c27f99cf412 [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"
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -070018#include "pw_rpc/internal/test_utils.h"
Alexei Frolov4d2adde2020-08-04 10:19:24 -070019
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
Alexei Frolov2d737bc2021-04-27 23:03:09 -070038TEST(BaseClientCall, Move_UnregistersOriginal) {
39 ClientContextForTest context;
40 EXPECT_EQ(context.client().active_calls(), 0u);
41
42 BaseClientCall moved(&context.channel(),
43 context.service_id(),
44 context.method_id(),
45 [](BaseClientCall&, const Packet&) {});
46 EXPECT_EQ(context.client().active_calls(), 1u);
47
48 BaseClientCall call(std::move(moved));
49 EXPECT_EQ(context.client().active_calls(), 1u);
50
51// Ignore use-after-move.
52#ifndef __clang_analyzer__
53 EXPECT_FALSE(moved.active());
54#endif // __clang_analyzer__
55 EXPECT_TRUE(call.active());
56}
57
Tri Pho938f08b2021-08-11 12:29:49 -070058TEST(BaseClientCall, TwoConcurrentClientCallsAssigned) {
59 // Mostly null/placeholder data to exercise the call registration path.
60 constexpr uint32_t kServiceId = 16;
61 constexpr uint32_t kMethodId = 111;
62 std::array<pw::rpc::Channel, 2> channels = {
63 Channel::Create<1>(nullptr),
64 Channel::Create<2>(nullptr),
65 };
66 Client client(channels);
67
68 // Specifically testing the assignment operator.
69 BaseClientCall call1;
70 BaseClientCall call2;
71 call1 = BaseClientCall(&channels[0], kServiceId, kMethodId, nullptr);
72 call2 = BaseClientCall(&channels[1], kServiceId, kMethodId, nullptr);
73
74 EXPECT_EQ(client.active_calls(), 2u);
75}
76
Alexei Frolov4d2adde2020-08-04 10:19:24 -070077class FakeClientCall : public BaseClientCall {
78 public:
79 constexpr FakeClientCall(rpc::Channel* channel,
80 uint32_t service_id,
81 uint32_t method_id,
82 ResponseHandler handler)
83 : BaseClientCall(channel, service_id, method_id, handler) {}
84
85 Status SendPacket(std::span<const std::byte> payload) {
86 std::span buffer = AcquirePayloadBuffer();
87 std::memcpy(buffer.data(), payload.data(), payload.size());
88 return ReleasePayloadBuffer(buffer.first(payload.size()));
89 }
90};
91
92TEST(BaseClientCall, SendsPacketWithPayload) {
93 ClientContextForTest context;
94 FakeClientCall call(&context.channel(),
Ewout van Bekkum5ea33402021-03-31 11:00:02 -070095 context.service_id(),
96 context.method_id(),
Alexei Frolov4d2adde2020-08-04 10:19:24 -070097 [](BaseClientCall&, const Packet&) {});
98
99 constexpr std::byte payload[]{std::byte{0x08}, std::byte{0x39}};
Wyatt Heplerfa6edcc2021-08-20 08:30:08 -0700100 call.SendPacket(payload).IgnoreError();
Alexei Frolov4d2adde2020-08-04 10:19:24 -0700101
102 EXPECT_EQ(context.output().packet_count(), 1u);
103 Packet packet = context.output().sent_packet();
104 EXPECT_EQ(packet.channel_id(), context.channel().id());
Ewout van Bekkum5ea33402021-03-31 11:00:02 -0700105 EXPECT_EQ(packet.service_id(), context.service_id());
106 EXPECT_EQ(packet.method_id(), context.method_id());
Alexei Frolov4d2adde2020-08-04 10:19:24 -0700107 EXPECT_EQ(std::memcmp(packet.payload().data(), payload, sizeof(payload)), 0);
108}
109
110} // namespace
111} // namespace pw::rpc::internal