1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <memory>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 
24 #include <grpcpp/channel.h>
25 #include <grpcpp/client_context.h>
26 #include <grpcpp/create_channel.h>
27 #include <grpcpp/generic/generic_stub.h>
28 #include <grpcpp/impl/codegen/delegating_channel.h>
29 #include <grpcpp/impl/codegen/proto_utils.h>
30 #include <grpcpp/server.h>
31 #include <grpcpp/server_builder.h>
32 #include <grpcpp/server_context.h>
33 #include <grpcpp/support/client_interceptor.h>
34 
35 #include "src/proto/grpc/testing/echo.grpc.pb.h"
36 #include "test/core/util/port.h"
37 #include "test/core/util/test_config.h"
38 #include "test/cpp/end2end/test_service_impl.h"
39 #include "test/cpp/util/byte_buffer_proto_helper.h"
40 #include "test/cpp/util/string_ref_helper.h"
41 
42 namespace grpc {
43 namespace testing {
44 namespace {
45 
46 class TestChannel : public experimental::DelegatingChannel {
47  public:
TestChannel(const std::shared_ptr<ChannelInterface> & delegate_channel)48   explicit TestChannel(
49       const std::shared_ptr<ChannelInterface>& delegate_channel)
50       : experimental::DelegatingChannel(delegate_channel) {}
51   // Always returns GRPC_CHANNEL_READY
GetState(bool)52   grpc_connectivity_state GetState(bool /*try_to_connect*/) override {
53     return GRPC_CHANNEL_READY;
54   }
55 };
56 
57 class DelegatingChannelTest : public ::testing::Test {
58  protected:
DelegatingChannelTest()59   DelegatingChannelTest() {
60     int port = grpc_pick_unused_port_or_die();
61     ServerBuilder builder;
62     server_address_ = "localhost:" + std::to_string(port);
63     builder.AddListeningPort(server_address_, InsecureServerCredentials());
64     builder.RegisterService(&service_);
65     server_ = builder.BuildAndStart();
66   }
67 
~DelegatingChannelTest()68   ~DelegatingChannelTest() override { server_->Shutdown(); }
69 
70   std::string server_address_;
71   TestServiceImpl service_;
72   std::unique_ptr<Server> server_;
73 };
74 
TEST_F(DelegatingChannelTest,SimpleTest)75 TEST_F(DelegatingChannelTest, SimpleTest) {
76   auto channel = CreateChannel(server_address_, InsecureChannelCredentials());
77   std::shared_ptr<TestChannel> test_channel =
78       std::make_shared<TestChannel>(channel);
79   // gRPC channel should be in idle state at this point but our test channel
80   // will return ready.
81   EXPECT_EQ(channel->GetState(false), GRPC_CHANNEL_IDLE);
82   EXPECT_EQ(test_channel->GetState(false), GRPC_CHANNEL_READY);
83   auto stub = grpc::testing::EchoTestService::NewStub(test_channel);
84   ClientContext ctx;
85   EchoRequest req;
86   req.set_message("Hello");
87   EchoResponse resp;
88   Status s = stub->Echo(&ctx, req, &resp);
89   EXPECT_EQ(s.ok(), true);
90   EXPECT_EQ(resp.message(), "Hello");
91 }
92 
93 }  // namespace
94 }  // namespace testing
95 }  // namespace grpc
96 
main(int argc,char ** argv)97 int main(int argc, char** argv) {
98   grpc::testing::TestEnvironment env(argc, argv);
99   ::testing::InitGoogleTest(&argc, argv);
100   return RUN_ALL_TESTS();
101 }
102