xref: /aosp_15_r20/external/pigweed/pw_rpc/method_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2021 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/method.h"
16 
17 #include <array>
18 
19 #include "pw_rpc/internal/packet.h"
20 #include "pw_rpc/method_type.h"
21 #include "pw_rpc/server.h"
22 #include "pw_rpc_private/test_method.h"
23 #include "pw_unit_test/framework.h"
24 
25 namespace pw::rpc::internal {
26 namespace {
27 
28 // Test the helper functions for the MethodType enum.
29 static_assert(!HasServerStream(MethodType::kUnary));
30 static_assert(HasServerStream(MethodType::kServerStreaming));
31 static_assert(!HasServerStream(MethodType::kClientStreaming));
32 static_assert(HasServerStream(MethodType::kBidirectionalStreaming));
33 
34 static_assert(!HasClientStream(MethodType::kUnary));
35 static_assert(!HasClientStream(MethodType::kServerStreaming));
36 static_assert(HasClientStream(MethodType::kClientStreaming));
37 static_assert(HasClientStream(MethodType::kBidirectionalStreaming));
38 
39 class TestService : public Service {
40  public:
TestService()41   TestService() : Service(5678, kMethods) {}
42 
43   static constexpr std::array<TestMethodUnion, 1> kMethods = {TestMethod(1234)};
44 };
45 
46 const TestMethod& kTestMethod = TestService::kMethods.front().test_method();
47 
TEST(Method,Id)48 TEST(Method, Id) { EXPECT_EQ(kTestMethod.id(), 1234u); }
49 
TEST(Method,Invoke)50 TEST(Method, Invoke) {
51   class NullChannelOutput final : public ChannelOutput {
52    public:
53     constexpr NullChannelOutput() : ChannelOutput("NullChannelOutput") {}
54 
55     Status Send(ConstByteSpan) override { return OkStatus(); }
56   } channel_output;
57 
58   Channel channel = Channel::Create<123>(&channel_output);
59   Server server(span(&channel, 1));
60   TestService service;
61 
62   const CallContext context(server, channel.id(), service, kTestMethod, 0);
63   Packet empty_packet;
64 
65   EXPECT_EQ(kTestMethod.invocations(), 0u);
66   rpc_lock().lock();
67   kTestMethod.Invoke(context, empty_packet);
68   EXPECT_EQ(kTestMethod.invocations(), 1u);
69 }
70 
71 }  // namespace
72 }  // namespace pw::rpc::internal
73