orca_service_end2end_test.cc
Go to the documentation of this file.
1 //
2 // Copyright 2022 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "absl/strings/str_cat.h"
21 #include "absl/types/optional.h"
22 
23 #include <grpc/grpc.h>
24 #include <grpcpp/channel.h>
25 #include <grpcpp/client_context.h>
26 #include <grpcpp/create_channel.h>
28 #include <grpcpp/server.h>
29 #include <grpcpp/server_builder.h>
30 #include <grpcpp/server_context.h>
31 
33 #include "src/proto/grpc/testing/xds/v3/orca_service.grpc.pb.h"
34 #include "src/proto/grpc/testing/xds/v3/orca_service.pb.h"
35 #include "test/core/util/port.h"
37 
38 using xds::data::orca::v3::OrcaLoadReport;
39 using xds::service::orca::v3::OpenRcaService;
40 using xds::service::orca::v3::OrcaLoadReportRequest;
41 
42 namespace grpc {
43 namespace testing {
44 namespace {
45 
46 using experimental::OrcaService;
47 
48 class OrcaServiceEnd2endTest : public ::testing::Test {
49  protected:
50  // A wrapper for the client stream that ensures that responses come
51  // back at the requested interval.
52  class Stream {
53  public:
54  Stream(OpenRcaService::Stub* stub, grpc_core::Duration requested_interval)
55  : requested_interval_(requested_interval) {
56  OrcaLoadReportRequest request;
57  gpr_timespec timespec = requested_interval.as_timespec();
58  auto* interval_proto = request.mutable_report_interval();
59  interval_proto->set_seconds(timespec.tv_sec);
60  interval_proto->set_nanos(timespec.tv_nsec);
61  stream_ = stub->StreamCoreMetrics(&context_, request);
62  }
63 
64  ~Stream() { context_.TryCancel(); }
65 
66  OrcaLoadReport ReadResponse() {
67  OrcaLoadReport response;
68  EXPECT_TRUE(stream_->Read(&response));
72  // Allow a small fudge factor to avoid test flakiness.
73  const grpc_core::Duration fudge_factor =
76  auto elapsed = now - *last_response_time_;
77  EXPECT_GE(elapsed, requested_interval_ - fudge_factor)
78  << elapsed.ToString();
79  EXPECT_LE(elapsed, requested_interval_ + fudge_factor)
80  << elapsed.ToString();
81  }
83  return response;
84  }
85 
86  private:
88  ClientContext context_;
89  std::unique_ptr<grpc::ClientReaderInterface<OrcaLoadReport>> stream_;
91  };
92 
93  OrcaServiceEnd2endTest()
94  : orca_service_(OrcaService::Options().set_min_report_duration(
95  absl::ZeroDuration())) {
98  ServerBuilder builder;
100  builder.RegisterService(&orca_service_);
101  server_ = builder.BuildAndStart();
102  gpr_log(GPR_INFO, "server started on %s", server_address_.c_str());
104  stub_ = OpenRcaService::NewStub(channel);
105  }
106 
107  ~OrcaServiceEnd2endTest() override { server_->Shutdown(); }
108 
110  OrcaService orca_service_;
111  std::unique_ptr<Server> server_;
112  std::unique_ptr<OpenRcaService::Stub> stub_;
113 };
114 
115 TEST_F(OrcaServiceEnd2endTest, Basic) {
116  constexpr char kMetricName1[] = "foo";
117  constexpr char kMetricName2[] = "bar";
118  constexpr char kMetricName3[] = "baz";
119  constexpr char kMetricName4[] = "quux";
120  // Start stream1 with 5s interval and stream2 with 2.5s interval.
121  // Throughout the test, we should get two responses on stream2 for
122  // every one response on stream1.
123  Stream stream1(stub_.get(), grpc_core::Duration::Milliseconds(5000));
124  Stream stream2(stub_.get(), grpc_core::Duration::Milliseconds(2500));
125  auto ReadResponses = [&](std::function<void(const OrcaLoadReport&)> checker) {
126  gpr_log(GPR_INFO, "reading response from stream1");
127  OrcaLoadReport response = stream1.ReadResponse();
128  checker(response);
129  gpr_log(GPR_INFO, "reading response from stream2");
130  response = stream2.ReadResponse();
131  checker(response);
132  gpr_log(GPR_INFO, "reading response from stream2");
133  response = stream2.ReadResponse();
134  checker(response);
135  };
136  // Initial response should not have any values populated.
137  ReadResponses([](const OrcaLoadReport& response) {
138  EXPECT_EQ(response.cpu_utilization(), 0);
139  EXPECT_EQ(response.mem_utilization(), 0);
141  });
142  // Now set CPU utilization on the server.
143  orca_service_.SetCpuUtilization(0.5);
144  ReadResponses([](const OrcaLoadReport& response) {
145  EXPECT_EQ(response.cpu_utilization(), 0.5);
146  EXPECT_EQ(response.mem_utilization(), 0);
148  });
149  // Update CPU utilization and set memory utilization.
150  orca_service_.SetCpuUtilization(0.8);
151  orca_service_.SetMemoryUtilization(0.4);
152  ReadResponses([](const OrcaLoadReport& response) {
153  EXPECT_EQ(response.cpu_utilization(), 0.8);
154  EXPECT_EQ(response.mem_utilization(), 0.4);
156  });
157  // Unset CPU and memory utilization and set a named utilization.
158  orca_service_.DeleteCpuUtilization();
159  orca_service_.DeleteMemoryUtilization();
160  orca_service_.SetNamedUtilization(kMetricName1, 0.3);
161  ReadResponses([&](const OrcaLoadReport& response) {
162  EXPECT_EQ(response.cpu_utilization(), 0);
163  EXPECT_EQ(response.mem_utilization(), 0);
164  EXPECT_THAT(
165  response.utilization(),
166  ::testing::UnorderedElementsAre(::testing::Pair(kMetricName1, 0.3)));
167  });
168  // Unset the previous named utilization and set two new ones.
169  orca_service_.DeleteNamedUtilization(kMetricName1);
170  orca_service_.SetNamedUtilization(kMetricName2, 0.2);
171  orca_service_.SetNamedUtilization(kMetricName3, 0.1);
172  ReadResponses([&](const OrcaLoadReport& response) {
173  EXPECT_EQ(response.cpu_utilization(), 0);
174  EXPECT_EQ(response.mem_utilization(), 0);
175  EXPECT_THAT(
176  response.utilization(),
177  ::testing::UnorderedElementsAre(::testing::Pair(kMetricName2, 0.2),
178  ::testing::Pair(kMetricName3, 0.1)));
179  });
180  // Replace the entire named metric map at once.
181  orca_service_.SetAllNamedUtilization(
182  {{kMetricName2, 0.5}, {kMetricName4, 0.9}});
183  ReadResponses([&](const OrcaLoadReport& response) {
184  EXPECT_EQ(response.cpu_utilization(), 0);
185  EXPECT_EQ(response.mem_utilization(), 0);
186  EXPECT_THAT(
187  response.utilization(),
188  ::testing::UnorderedElementsAre(::testing::Pair(kMetricName2, 0.5),
189  ::testing::Pair(kMetricName4, 0.9)));
190  });
191 }
192 
193 } // namespace
194 } // namespace testing
195 } // namespace grpc
196 
197 int main(int argc, char** argv) {
198  grpc::testing::TestEnvironment env(&argc, argv);
199  ::testing::InitGoogleTest(&argc, argv);
200  return RUN_ALL_TESTS();
201 }
gpr_timespec::tv_nsec
int32_t tv_nsec
Definition: gpr_types.h:52
grpc::EXPECT_THAT
EXPECT_THAT(status.error_message(), ::testing::HasSubstr("subject_token_type"))
Stream
Definition: bm_chttp2_transport.cc:199
gpr_timespec::tv_sec
int64_t tv_sec
Definition: gpr_types.h:51
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
testing
Definition: aws_request_signer_test.cc:25
now
static double now(void)
Definition: test/core/fling/client.cc:130
port.h
generate.env
env
Definition: generate.py:37
absl::StrCat
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: abseil-cpp/absl/strings/str_cat.cc:98
absl::ZeroDuration
constexpr Duration ZeroDuration()
Definition: third_party/abseil-cpp/absl/time/time.h:308
grpc
Definition: grpcpp/alarm.h:33
context_
ClientContext context_
Definition: orca_service_end2end_test.cc:88
stream_
std::unique_ptr< grpc::ClientReaderInterface< OrcaLoadReport > > stream_
Definition: orca_service_end2end_test.cc:89
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
server_address
std::string server_address("0.0.0.0:10000")
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
stub_
std::unique_ptr< OpenRcaService::Stub > stub_
Definition: orca_service_end2end_test.cc:112
main
int main(int argc, char **argv)
Definition: orca_service_end2end_test.cc:197
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
absl::optional::has_value
constexpr bool has_value() const noexcept
Definition: abseil-cpp/absl/types/optional.h:461
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
absl::optional< grpc_core::Timestamp >
grpc_test_slowdown_factor
int64_t grpc_test_slowdown_factor()
Definition: test/core/util/test_config.cc:76
channel.h
time.h
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
last_response_time_
absl::optional< grpc_core::Timestamp > last_response_time_
Definition: orca_service_end2end_test.cc:90
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
requested_interval_
const grpc_core::Duration requested_interval_
Definition: orca_service_end2end_test.cc:87
testing::Pair
internal::PairMatcher< FirstMatcher, SecondMatcher > Pair(FirstMatcher first_matcher, SecondMatcher second_matcher)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:9152
test_config.h
server_
std::unique_ptr< Server > server_
Definition: orca_service_end2end_test.cc:111
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
orca_service_
OrcaService orca_service_
Definition: orca_service_end2end_test.cc:110
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
orca_service.h
grpc_core::Duration::as_timespec
gpr_timespec as_timespec() const
Definition: src/core/lib/gprpp/time.cc:171
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc_core::Timestamp::FromTimespecRoundDown
static Timestamp FromTimespecRoundDown(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:141
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
testing::UnorderedElementsAre
internal::UnorderedElementsAreMatcher< ::testing::tuple<> > UnorderedElementsAre()
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:13255
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
server_address_
std::string server_address_
Definition: orca_service_end2end_test.cc:109
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
grpc::testing::EXPECT_TRUE
EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(minimum_valid_json, &options) .ok())
server.h
gpr_timespec
Definition: gpr_types.h:50
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
server_builder.h
create_channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:36