delegating_channel_test.cc 3.1 KB

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