• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpcpp/channel.h>
20 #include <grpcpp/client_context.h>
21 #include <grpcpp/create_channel.h>
22 #include <grpcpp/generic/generic_stub.h>
23 #include <grpcpp/impl/delegating_channel.h>
24 #include <grpcpp/impl/proto_utils.h>
25 #include <grpcpp/server.h>
26 #include <grpcpp/server_builder.h>
27 #include <grpcpp/server_context.h>
28 #include <grpcpp/support/client_interceptor.h>
29 #include <gtest/gtest.h>
30 
31 #include <memory>
32 #include <vector>
33 
34 #include "src/proto/grpc/testing/echo.grpc.pb.h"
35 #include "test/core/test_util/port.h"
36 #include "test/core/test_util/test_config.h"
37 #include "test/cpp/end2end/test_service_impl.h"
38 #include "test/cpp/util/byte_buffer_proto_helper.h"
39 #include "test/cpp/util/string_ref_helper.h"
40 
41 namespace grpc {
42 namespace testing {
43 namespace {
44 
45 class TestChannel : public experimental::DelegatingChannel {
46  public:
TestChannel(const std::shared_ptr<ChannelInterface> & delegate_channel)47   explicit TestChannel(
48       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() override { 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