xref: /aosp_15_r20/external/grpc-grpc/test/cpp/ext/gcp/observability_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <grpc++/grpc++.h>
21 #include <grpcpp/ext/gcp_observability.h>
22 
23 #include "src/core/lib/config/core_configuration.h"
24 #include "src/proto/grpc/testing/echo.grpc.pb.h"
25 #include "src/proto/grpc/testing/echo_messages.pb.h"
26 #include "test/core/util/port.h"
27 #include "test/core/util/test_config.h"
28 #include "test/cpp/end2end/test_service_impl.h"
29 
30 namespace grpc {
31 namespace testing {
32 namespace {
33 
TEST(GcpObservabilityTest,Basic)34 TEST(GcpObservabilityTest, Basic) {
35   auto observability = grpc::GcpObservability::Init();
36   EXPECT_EQ(observability.status(),
37             absl::FailedPreconditionError(
38                 "Environment variables GRPC_GCP_OBSERVABILITY_CONFIG_FILE or "
39                 "GRPC_GCP_OBSERVABILITY_CONFIG "
40                 "not defined"));
41   grpc_core::CoreConfiguration::Reset();
42 }
43 
TEST(GcpObservabilityTest,ContinuesWorkingAfterFailure)44 TEST(GcpObservabilityTest, ContinuesWorkingAfterFailure) {
45   auto observability = grpc::GcpObservability::Init();
46   EXPECT_FALSE(observability.ok());
47 
48   // Set up a synchronous server on a different thread to avoid the asynch
49   // interface.
50   grpc::ServerBuilder builder;
51   TestServiceImpl service;
52   int port = grpc_pick_unused_port_or_die();
53   auto server_address = absl::StrCat("localhost:", port);
54   // Use IPv4 here because it's less flaky than IPv6 ("[::]:0") on Travis.
55   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials(),
56                            &port);
57   builder.RegisterService(&service);
58   auto server = builder.BuildAndStart();
59   ASSERT_NE(nullptr, server);
60   auto server_thread = std::thread([&]() { server->Wait(); });
61   // Send a single RPC to make sure that things work.
62   auto stub = EchoTestService::NewStub(
63       grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials()));
64   EchoRequest request;
65   request.set_message("foo");
66   EchoResponse response;
67   grpc::ClientContext context;
68   grpc::Status status = stub->Echo(&context, request, &response);
69   EXPECT_TRUE(status.ok());
70   EXPECT_EQ(response.message(), "foo");
71   server->Shutdown();
72   server_thread.join();
73 }
74 
75 }  // namespace
76 }  // namespace testing
77 }  // namespace grpc
78 
main(int argc,char ** argv)79 int main(int argc, char** argv) {
80   grpc::testing::TestEnvironment env(&argc, argv);
81   ::testing::InitGoogleTest(&argc, argv);
82   return RUN_ALL_TESTS();
83 }
84