Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -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 "gtest/gtest.h" |
Alexei Frolov | 15255c5 | 2020-07-17 08:25:49 -0700 | [diff] [blame] | 16 | #include "pw_rpc/internal/hash.h" |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 17 | #include "pw_rpc/test_method_context.h" |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 18 | #include "pw_rpc_test_protos/test.rpc.pb.h" |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 19 | |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 20 | namespace pw::rpc { |
| 21 | namespace test { |
| 22 | |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 23 | class TestService final : public generated::TestService<TestService> { |
Alexei Frolov | abb0f99 | 2020-07-17 16:34:41 -0700 | [diff] [blame] | 24 | public: |
| 25 | Status TestRpc(ServerContext&, |
| 26 | const pw_rpc_test_TestRequest& request, |
| 27 | pw_rpc_test_TestResponse& response) { |
| 28 | response.value = request.integer + 1; |
| 29 | return static_cast<Status::Code>(request.status_code); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Alexei Frolov | abb0f99 | 2020-07-17 16:34:41 -0700 | [diff] [blame] | 32 | void TestStreamRpc(ServerContext&, |
| 33 | const pw_rpc_test_TestRequest& request, |
| 34 | ServerWriter<pw_rpc_test_TestStreamResponse>& writer) { |
| 35 | for (int i = 0; i < request.integer; ++i) { |
| 36 | writer.Write({.number = static_cast<uint32_t>(i)}); |
| 37 | } |
| 38 | |
| 39 | writer.Finish(static_cast<Status::Code>(request.status_code)); |
| 40 | } |
| 41 | }; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 42 | |
| 43 | } // namespace test |
| 44 | |
| 45 | namespace internal { |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 46 | namespace { |
| 47 | |
| 48 | TEST(NanopbCodegen, CompilesProperly) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 49 | test::TestService service; |
Alexei Frolov | 15255c5 | 2020-07-17 08:25:49 -0700 | [diff] [blame] | 50 | EXPECT_EQ(service.id(), Hash("pw.rpc.test.TestService")); |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 51 | EXPECT_STREQ(service.name(), "TestService"); |
| 52 | } |
| 53 | |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 54 | TEST(NanopbCodegen, InvokeUnaryRpc) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 55 | PW_RPC_TEST_METHOD_CONTEXT(test::TestService, TestRpc) context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 56 | |
| 57 | EXPECT_EQ(Status::OK, |
| 58 | context.call({.integer = 123, .status_code = Status::OK})); |
| 59 | |
| 60 | EXPECT_EQ(124, context.response().value); |
| 61 | |
| 62 | EXPECT_EQ( |
| 63 | Status::INVALID_ARGUMENT, |
| 64 | context.call({.integer = 999, .status_code = Status::INVALID_ARGUMENT})); |
| 65 | EXPECT_EQ(1000, context.response().value); |
| 66 | } |
| 67 | |
| 68 | TEST(NanopbCodegen, InvokeStreamingRpc) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 69 | PW_RPC_TEST_METHOD_CONTEXT(test::TestService, TestStreamRpc) context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 70 | |
| 71 | context.call({.integer = 0, .status_code = Status::ABORTED}); |
| 72 | |
| 73 | EXPECT_EQ(Status::ABORTED, context.status()); |
| 74 | EXPECT_TRUE(context.done()); |
| 75 | EXPECT_TRUE(context.responses().empty()); |
| 76 | EXPECT_EQ(0u, context.total_responses()); |
| 77 | |
| 78 | context.call({.integer = 4, .status_code = Status::OK}); |
| 79 | |
| 80 | ASSERT_EQ(4u, context.responses().size()); |
| 81 | ASSERT_EQ(4u, context.total_responses()); |
| 82 | |
| 83 | for (size_t i = 0; i < context.responses().size(); ++i) { |
| 84 | EXPECT_EQ(context.responses()[i].number, i); |
| 85 | } |
| 86 | |
| 87 | EXPECT_EQ(Status::OK, context.status()); |
| 88 | } |
| 89 | |
| 90 | TEST(NanopbCodegen, InvokeStreamingRpc_ContextKeepsFixedNumberOfResponses) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 91 | PW_RPC_TEST_METHOD_CONTEXT(test::TestService, TestStreamRpc, 3) context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 92 | |
| 93 | ASSERT_EQ(3u, context.responses().max_size()); |
| 94 | |
| 95 | context.call({.integer = 5, .status_code = Status::NOT_FOUND}); |
| 96 | |
| 97 | ASSERT_EQ(3u, context.responses().size()); |
| 98 | ASSERT_EQ(5u, context.total_responses()); |
| 99 | |
| 100 | EXPECT_EQ(context.responses()[0].number, 0u); |
| 101 | EXPECT_EQ(context.responses()[1].number, 1u); |
| 102 | EXPECT_EQ(context.responses()[2].number, 4u); |
| 103 | } |
| 104 | |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame^] | 105 | TEST(NanopbCodegen, InvokeStreamingRpc_ManualWriting) { |
| 106 | PW_RPC_TEST_METHOD_CONTEXT(test::TestService, TestStreamRpc, 3) context; |
| 107 | |
| 108 | ASSERT_EQ(3u, context.responses().max_size()); |
| 109 | |
| 110 | auto writer = context.writer(); |
| 111 | |
| 112 | writer.Write({.number = 3}); |
| 113 | writer.Write({.number = 6}); |
| 114 | writer.Write({.number = 9}); |
| 115 | |
| 116 | EXPECT_FALSE(context.done()); |
| 117 | |
| 118 | writer.Finish(Status::CANCELLED); |
| 119 | ASSERT_TRUE(context.done()); |
| 120 | EXPECT_EQ(Status::CANCELLED, context.status()); |
| 121 | |
| 122 | ASSERT_EQ(3u, context.responses().size()); |
| 123 | ASSERT_EQ(3u, context.total_responses()); |
| 124 | |
| 125 | EXPECT_EQ(context.responses()[0].number, 3u); |
| 126 | EXPECT_EQ(context.responses()[1].number, 6u); |
| 127 | EXPECT_EQ(context.responses()[2].number, 9u); |
| 128 | } |
| 129 | |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 130 | } // namespace |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 131 | } // namespace internal |
| 132 | } // namespace pw::rpc |