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