delegating_channel_test.cc
Go to the documentation of this file.
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>
30 #include <grpcpp/server.h>
31 #include <grpcpp/server_builder.h>
32 #include <grpcpp/server_context.h>
34 
35 #include "src/proto/grpc/testing/echo.grpc.pb.h"
36 #include "test/core/util/port.h"
41 
42 namespace grpc {
43 namespace testing {
44 namespace {
45 
46 class TestChannel : public experimental::DelegatingChannel {
47  public:
48  explicit TestChannel(
49  const std::shared_ptr<ChannelInterface>& delegate_channel)
50  : experimental::DelegatingChannel(delegate_channel) {}
51  // Always returns GRPC_CHANNEL_READY
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:
59  DelegatingChannelTest() {
61  ServerBuilder builder;
62  server_address_ = "localhost:" + std::to_string(port);
64  builder.RegisterService(&service_);
65  server_ = builder.BuildAndStart();
66  }
67 
68  ~DelegatingChannelTest() override { server_->Shutdown(); }
69 
72  std::unique_ptr<Server> server_;
73 };
74 
75 TEST_F(DelegatingChannelTest, SimpleTest) {
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 
97 int main(int argc, char** argv) {
99  ::testing::InitGoogleTest(&argc, argv);
100  return RUN_ALL_TESTS();
101 }
testing
Definition: aws_request_signer_test.cc:25
GRPC_CHANNEL_READY
@ GRPC_CHANNEL_READY
Definition: include/grpc/impl/codegen/connectivity_state.h:36
port.h
service_
TestServiceImpl service_
Definition: delegating_channel_test.cc:71
ctx
Definition: benchmark-async.c:30
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
server_
std::unique_ptr< Server > server_
Definition: delegating_channel_test.cc:72
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
absl::FormatConversionChar::s
@ s
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
grpc_connectivity_state
grpc_connectivity_state
Definition: include/grpc/impl/codegen/connectivity_state.h:30
test_service_impl.h
client_interceptor.h
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
req
static uv_connect_t req
Definition: test-connection-fail.c:30
http2_server_health_check.resp
resp
Definition: http2_server_health_check.py:31
GetState
benchmark::State & GetState()
GRPC_CHANNEL_IDLE
@ GRPC_CHANNEL_IDLE
Definition: include/grpc/impl/codegen/connectivity_state.h:32
channel.h
grpc::CreateChannel
std::shared_ptr< Channel > CreateChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds)
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
server_address_
std::string server_address_
Definition: delegating_channel_test.cc:70
generic_stub.h
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
main
int main(int argc, char **argv)
Definition: delegating_channel_test.cc:97
client_context.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc::testing::TEST_F
TEST_F(ChannelArgumentsTest, SetInt)
Definition: channel_arguments_test.cc:134
delegating_channel.h
server_context.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
proto_utils.h
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::testing::EXPECT_EQ
EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange")
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
server.h
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
string_ref_helper.h
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
TestServiceImpl
Definition: interop_server.cc:139
server_builder.h
byte_buffer_proto_helper.h
create_channel.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:09