1 //
2 // Copyright 2024 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/cpp/server/orca/orca_service.h"
18
19 #include <grpc/grpc.h>
20 #include <grpcpp/ext/orca_service.h>
21 #include <grpcpp/ext/server_metric_recorder.h>
22 #include <grpcpp/support/byte_buffer.h>
23 #include <grpcpp/support/server_callback.h>
24 #include <grpcpp/support/slice.h>
25 #include <grpcpp/support/status.h>
26 #include <gtest/gtest.h>
27
28 #include <memory>
29 #include <utility>
30
31 #include "absl/strings/string_view.h"
32 #include "absl/time/time.h"
33 #include "src/core/util/notification.h"
34 #include "test/core/test_util/test_config.h"
35
36 namespace grpc {
37 namespace testing {
38
39 using experimental::OrcaService;
40 using experimental::ServerMetricRecorder;
41
42 class OrcaServiceTest : public ::testing::Test {
43 public:
OrcaServiceTest()44 OrcaServiceTest()
45 : server_metric_recorder_(ServerMetricRecorder::Create()),
46 orca_service_(server_metric_recorder_.get(),
47 OrcaService::Options().set_min_report_duration(
48 absl::ZeroDuration())) {};
49 ~OrcaServiceTest() override = default;
50
51 class TestReactorHook : public OrcaService::ReactorHook {
52 public:
TestReactorHook(grpc::Status expected_status)53 explicit TestReactorHook(grpc::Status expected_status)
54 : expected_status_(expected_status) {}
OnFinish(grpc::Status status)55 void OnFinish(grpc::Status status) override {
56 EXPECT_EQ(status.error_code(), expected_status_.error_code());
57 EXPECT_EQ(status.error_message(), expected_status_.error_message());
58 notification_.Notify();
59 }
60
OnStartWrite(const ByteBuffer *)61 void OnStartWrite(const ByteBuffer* /*response*/) override {
62 GTEST_FAIL() << "Unexpected write of response";
63 }
64
AwaitFinish()65 void AwaitFinish() { notification_.WaitForNotification(); }
66
67 private:
68 grpc_core::Notification notification_;
69 grpc::Status expected_status_;
70 };
71
72 protected:
InstantiateReactor(absl::string_view peer,const ByteBuffer * request_buffer,std::shared_ptr<TestReactorHook> hook)73 std::unique_ptr<ServerWriteReactor<ByteBuffer>> InstantiateReactor(
74 absl::string_view peer, const ByteBuffer* request_buffer,
75 std::shared_ptr<TestReactorHook> hook) {
76 return std::make_unique<OrcaService::Reactor>(
77 &orca_service_, peer, request_buffer, std::move(hook));
78 }
79
80 private:
81 std::unique_ptr<ServerMetricRecorder> server_metric_recorder_;
82 OrcaService orca_service_;
83 };
84
TEST_F(OrcaServiceTest,ReactorEmptyInputBufferTest)85 TEST_F(OrcaServiceTest, ReactorEmptyInputBufferTest) {
86 std::shared_ptr<TestReactorHook> hook = std::make_shared<TestReactorHook>(
87 grpc::Status(grpc::StatusCode::INTERNAL, "Buffer not initialized"));
88 ByteBuffer request_buffer;
89 auto reactor = InstantiateReactor("peer", &request_buffer, hook);
90 hook->AwaitFinish();
91 }
92
TEST_F(OrcaServiceTest,ReactorCorruptBufferTest)93 TEST_F(OrcaServiceTest, ReactorCorruptBufferTest) {
94 std::shared_ptr<TestReactorHook> hook =
95 std::make_shared<TestReactorHook>(grpc::Status(
96 grpc::StatusCode::INTERNAL, "could not parse request proto"));
97 Slice data("Hello World");
98 ByteBuffer request_buffer(&data, 1);
99 auto reactor = InstantiateReactor("peer", &request_buffer, hook);
100 hook->AwaitFinish();
101 }
102
103 } // namespace testing
104 } // namespace grpc
105
main(int argc,char ** argv)106 int main(int argc, char** argv) {
107 grpc::testing::TestEnvironment env(&argc, argv);
108 ::testing::InitGoogleTest(&argc, argv);
109 grpc_init();
110 int ret = RUN_ALL_TESTS();
111 grpc_shutdown();
112 return ret;
113 }
114