Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 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" |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame^] | 18 | #include "pw_rpc/internal/test_utils.h" |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 19 | |
| 20 | namespace pw::rpc::internal { |
| 21 | namespace { |
| 22 | |
| 23 | TEST(BaseClientCall, RegistersAndRemovesItselfFromClient) { |
| 24 | ClientContextForTest context; |
| 25 | EXPECT_EQ(context.client().active_calls(), 0u); |
| 26 | |
| 27 | { |
| 28 | BaseClientCall call(&context.channel(), |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 29 | context.service_id(), |
| 30 | context.method_id(), |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 31 | [](BaseClientCall&, const Packet&) {}); |
| 32 | EXPECT_EQ(context.client().active_calls(), 1u); |
| 33 | } |
| 34 | |
| 35 | EXPECT_EQ(context.client().active_calls(), 0u); |
| 36 | } |
| 37 | |
Alexei Frolov | 2d737bc | 2021-04-27 23:03:09 -0700 | [diff] [blame] | 38 | TEST(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 Pho | 938f08b | 2021-08-11 12:29:49 -0700 | [diff] [blame] | 58 | TEST(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 Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 77 | class 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 | |
| 92 | TEST(BaseClientCall, SendsPacketWithPayload) { |
| 93 | ClientContextForTest context; |
| 94 | FakeClientCall call(&context.channel(), |
Ewout van Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 95 | context.service_id(), |
| 96 | context.method_id(), |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 97 | [](BaseClientCall&, const Packet&) {}); |
| 98 | |
| 99 | constexpr std::byte payload[]{std::byte{0x08}, std::byte{0x39}}; |
Wyatt Hepler | fa6edcc | 2021-08-20 08:30:08 -0700 | [diff] [blame^] | 100 | call.SendPacket(payload).IgnoreError(); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 101 | |
| 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 Bekkum | 5ea3340 | 2021-03-31 11:00:02 -0700 | [diff] [blame] | 105 | EXPECT_EQ(packet.service_id(), context.service_id()); |
| 106 | EXPECT_EQ(packet.method_id(), context.method_id()); |
Alexei Frolov | 4d2adde | 2020-08-04 10:19:24 -0700 | [diff] [blame] | 107 | EXPECT_EQ(std::memcmp(packet.payload().data(), payload, sizeof(payload)), 0); |
| 108 | } |
| 109 | |
| 110 | } // namespace |
| 111 | } // namespace pw::rpc::internal |