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) { |
Wyatt Hepler | 85eb7c9 | 2020-08-06 13:45:20 -0700 | [diff] [blame] | 36 | writer.Write({.chunk = {}, .number = static_cast<uint32_t>(i)}); |
Alexei Frolov | abb0f99 | 2020-07-17 16:34:41 -0700 | [diff] [blame] | 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 | |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | TEST(NanopbCodegen, CompilesProperly) { |
Alexei Frolov | 3ab26ff | 2020-07-21 10:44:58 -0700 | [diff] [blame] | 48 | test::TestService service; |
Wyatt Hepler | 1532e52 | 2020-07-30 11:44:58 -0700 | [diff] [blame] | 49 | EXPECT_EQ(service.id(), internal::Hash("pw.rpc.test.TestService")); |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 50 | EXPECT_STREQ(service.name(), "TestService"); |
| 51 | } |
| 52 | |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 53 | TEST(NanopbCodegen, InvokeUnaryRpc) { |
Wyatt Hepler | 1532e52 | 2020-07-30 11:44:58 -0700 | [diff] [blame] | 54 | TestMethodContext<&test::TestService::TestRpc> context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 55 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 56 | EXPECT_EQ(Status::Ok(), |
| 57 | context.call({.integer = 123, .status_code = Status::Ok()})); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 58 | |
| 59 | EXPECT_EQ(124, context.response().value); |
| 60 | |
| 61 | EXPECT_EQ( |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 62 | Status::InvalidArgument(), |
| 63 | context.call({.integer = 999, .status_code = Status::InvalidArgument()})); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 64 | EXPECT_EQ(1000, context.response().value); |
| 65 | } |
| 66 | |
| 67 | TEST(NanopbCodegen, InvokeStreamingRpc) { |
Wyatt Hepler | 1532e52 | 2020-07-30 11:44:58 -0700 | [diff] [blame] | 68 | TestMethodContext<&test::TestService::TestStreamRpc> context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 69 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 70 | context.call({.integer = 0, .status_code = Status::Aborted()}); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 71 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 72 | EXPECT_EQ(Status::Aborted(), context.status()); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 73 | EXPECT_TRUE(context.done()); |
| 74 | EXPECT_TRUE(context.responses().empty()); |
| 75 | EXPECT_EQ(0u, context.total_responses()); |
| 76 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 77 | context.call({.integer = 4, .status_code = Status::Ok()}); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 78 | |
| 79 | ASSERT_EQ(4u, context.responses().size()); |
| 80 | ASSERT_EQ(4u, context.total_responses()); |
| 81 | |
| 82 | for (size_t i = 0; i < context.responses().size(); ++i) { |
| 83 | EXPECT_EQ(context.responses()[i].number, i); |
| 84 | } |
| 85 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 86 | EXPECT_EQ(Status::Ok(), context.status()); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | TEST(NanopbCodegen, InvokeStreamingRpc_ContextKeepsFixedNumberOfResponses) { |
Wyatt Hepler | 1532e52 | 2020-07-30 11:44:58 -0700 | [diff] [blame] | 90 | TestMethodContext<&test::TestService::TestStreamRpc, 3> context; |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 91 | |
| 92 | ASSERT_EQ(3u, context.responses().max_size()); |
| 93 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 94 | context.call({.integer = 5, .status_code = Status::NotFound()}); |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 95 | |
| 96 | ASSERT_EQ(3u, context.responses().size()); |
| 97 | ASSERT_EQ(5u, context.total_responses()); |
| 98 | |
| 99 | EXPECT_EQ(context.responses()[0].number, 0u); |
| 100 | EXPECT_EQ(context.responses()[1].number, 1u); |
| 101 | EXPECT_EQ(context.responses()[2].number, 4u); |
| 102 | } |
| 103 | |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame] | 104 | TEST(NanopbCodegen, InvokeStreamingRpc_ManualWriting) { |
Wyatt Hepler | 1532e52 | 2020-07-30 11:44:58 -0700 | [diff] [blame] | 105 | TestMethodContext<&test::TestService::TestStreamRpc, 3> context; |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame] | 106 | |
| 107 | ASSERT_EQ(3u, context.responses().max_size()); |
| 108 | |
| 109 | auto writer = context.writer(); |
| 110 | |
Wyatt Hepler | 85eb7c9 | 2020-08-06 13:45:20 -0700 | [diff] [blame] | 111 | writer.Write({.chunk = {}, .number = 3}); |
| 112 | writer.Write({.chunk = {}, .number = 6}); |
| 113 | writer.Write({.chunk = {}, .number = 9}); |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame] | 114 | |
| 115 | EXPECT_FALSE(context.done()); |
| 116 | |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 117 | writer.Finish(Status::Cancelled()); |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame] | 118 | ASSERT_TRUE(context.done()); |
Wyatt Hepler | d78f7c6 | 2020-09-28 14:27:32 -0700 | [diff] [blame^] | 119 | EXPECT_EQ(Status::Cancelled(), context.status()); |
Alexei Frolov | 9117261 | 2020-07-21 17:08:03 -0700 | [diff] [blame] | 120 | |
| 121 | ASSERT_EQ(3u, context.responses().size()); |
| 122 | ASSERT_EQ(3u, context.total_responses()); |
| 123 | |
| 124 | EXPECT_EQ(context.responses()[0].number, 3u); |
| 125 | EXPECT_EQ(context.responses()[1].number, 6u); |
| 126 | EXPECT_EQ(context.responses()[2].number, 9u); |
| 127 | } |
| 128 | |
Alexei Frolov | 7fb63af | 2020-07-09 17:05:52 -0700 | [diff] [blame] | 129 | } // namespace |
Wyatt Hepler | 8aa0292 | 2020-07-17 08:54:37 -0700 | [diff] [blame] | 130 | } // namespace pw::rpc |