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 <grpcpp/channel.h>
23 #include <grpcpp/client_context.h>
24 #include <grpcpp/create_channel.h>
25 #include <grpcpp/generic/generic_stub.h>
26 #include <grpcpp/impl/codegen/delegating_channel.h>
27 #include <grpcpp/impl/codegen/proto_utils.h>
28 #include <grpcpp/server.h>
29 #include <grpcpp/server_builder.h>
30 #include <grpcpp/server_context.h>
31 #include <grpcpp/support/client_interceptor.h>
32 
33 #include "src/proto/grpc/testing/echo.grpc.pb.h"
34 #include "test/core/util/port.h"
35 #include "test/core/util/test_config.h"
36 #include "test/cpp/end2end/test_service_impl.h"
37 #include "test/cpp/util/byte_buffer_proto_helper.h"
38 #include "test/cpp/util/string_ref_helper.h"
39 
40 #include <gtest/gtest.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   TestChannel(const std::shared_ptr<ChannelInterface>& delegate_channel)
49       : experimental::DelegatingChannel(delegate_channel) {}
50   // Always returns GRPC_CHANNEL_READY
GetState(bool)51   grpc_connectivity_state GetState(bool /*try_to_connect*/) override {
52     return GRPC_CHANNEL_READY;
53   }
54 };
55 
56 class DelegatingChannelTest : public ::testing::Test {
57  protected:
DelegatingChannelTest()58   DelegatingChannelTest() {
59     int port = grpc_pick_unused_port_or_die();
60     ServerBuilder builder;
61     server_address_ = "localhost:" + std::to_string(port);
62     builder.AddListeningPort(server_address_, InsecureServerCredentials());
63     builder.RegisterService(&service_);
64     server_ = builder.BuildAndStart();
65   }
66 
~DelegatingChannelTest()67   ~DelegatingChannelTest() { server_->Shutdown(); }
68 
69   std::string server_address_;
70   TestServiceImpl service_;
71   std::unique_ptr<Server> server_;
72 };
73 
TEST_F(DelegatingChannelTest,SimpleTest)74 TEST_F(DelegatingChannelTest, SimpleTest) {
75   auto channel = CreateChannel(server_address_, InsecureChannelCredentials());
76   std::shared_ptr<TestChannel> test_channel =
77       std::make_shared<TestChannel>(channel);
78   // gRPC channel should be in idle state at this point but our test channel
79   // will return ready.
80   EXPECT_EQ(channel->GetState(false), GRPC_CHANNEL_IDLE);
81   EXPECT_EQ(test_channel->GetState(false), GRPC_CHANNEL_READY);
82   auto stub = grpc::testing::EchoTestService::NewStub(test_channel);
83   ClientContext ctx;
84   EchoRequest req;
85   req.set_message("Hello");
86   EchoResponse resp;
87   Status s = stub->Echo(&ctx, req, &resp);
88   EXPECT_EQ(s.ok(), true);
89   EXPECT_EQ(resp.message(), "Hello");
90 }
91 
92 }  // namespace
93 }  // namespace testing
94 }  // namespace grpc
95 
main(int argc,char ** argv)96 int main(int argc, char** argv) {
97   grpc::testing::TestEnvironment env(argc, argv);
98   ::testing::InitGoogleTest(&argc, argv);
99   return RUN_ALL_TESTS();
100 }
101