cli_call_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 "test/cpp/util/cli_call.h"
20 
21 #include <gtest/gtest.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpcpp/channel.h>
25 #include <grpcpp/client_context.h>
26 #include <grpcpp/create_channel.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29 #include <grpcpp/server_context.h>
30 
31 #include "src/proto/grpc/testing/echo.grpc.pb.h"
32 #include "test/core/util/port.h"
35 
36 using grpc::testing::EchoRequest;
37 using grpc::testing::EchoResponse;
38 
39 namespace grpc {
40 namespace testing {
41 
42 class TestServiceImpl : public grpc::testing::EchoTestService::Service {
43  public:
44  Status Echo(ServerContext* context, const EchoRequest* request,
45  EchoResponse* response) override {
46  if (!context->client_metadata().empty()) {
47  for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
48  iter = context->client_metadata().begin();
49  iter != context->client_metadata().end(); ++iter) {
50  context->AddInitialMetadata(ToString(iter->first),
51  ToString(iter->second));
52  }
53  }
54  context->AddTrailingMetadata("trailing_key", "trailing_value");
55  response->set_message(request->message());
56  return Status::OK;
57  }
58 };
59 
60 class CliCallTest : public ::testing::Test {
61  protected:
63 
64  void SetUp() override {
66  server_address_ << "localhost:" << port;
67  // Setup server
69  builder.AddListeningPort(server_address_.str(),
71  builder.RegisterService(&service_);
72  server_ = builder.BuildAndStart();
73  }
74 
75  void TearDown() override { server_->Shutdown(); }
76 
77  void ResetStub() {
80  stub_ = grpc::testing::EchoTestService::NewStub(channel_);
81  }
82 
83  std::shared_ptr<Channel> channel_;
84  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
85  std::unique_ptr<Server> server_;
86  std::ostringstream server_address_;
88 };
89 
90 // Send a rpc with a normal stub and then a CliCall. Verify they match.
91 TEST_F(CliCallTest, SimpleRpc) {
92  ResetStub();
93  // Normal stub.
94  EchoRequest request;
95  EchoResponse response;
96  request.set_message("Hello");
97 
99  context.AddMetadata("key1", "val1");
100  Status s = stub_->Echo(&context, request, &response);
101  EXPECT_EQ(response.message(), request.message());
102  EXPECT_TRUE(s.ok());
103 
104  const std::string kMethod("/grpc.testing.EchoTestService/Echo");
105  std::string request_bin, response_bin, expected_response_bin;
106  EXPECT_TRUE(request.SerializeToString(&request_bin));
107  EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
108  std::multimap<std::string, std::string> client_metadata;
109  std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
110  server_trailing_metadata;
111  client_metadata.insert(std::pair<std::string, std::string>("key1", "val1"));
112  CliCall call(channel_, kMethod, client_metadata);
113  Status s2 = call.Call(request_bin, &response_bin, &server_initial_metadata,
114  &server_trailing_metadata);
115  EXPECT_TRUE(s2.ok());
116 
117  EXPECT_EQ(expected_response_bin, response_bin);
118  EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
119  EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
120 }
121 
122 } // namespace testing
123 } // namespace grpc
124 
125 int main(int argc, char** argv) {
126  grpc::testing::TestEnvironment env(&argc, argv);
127  ::testing::InitGoogleTest(&argc, argv);
128  return RUN_ALL_TESTS();
129 }
testing
Definition: aws_request_signer_test.cc:25
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
port.h
grpc::testing::CliCallTest::service_
TestServiceImpl service_
Definition: cli_call_test.cc:87
generate.env
env
Definition: generate.py:37
grpc
Definition: grpcpp/alarm.h:33
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: include/grpcpp/impl/codegen/status.h:126
grpc::testing::TestServiceImpl::Echo
Status Echo(ServerContext *context, const EchoRequest *request, EchoResponse *response) override
Definition: cli_call_test.cc:44
benchmark.request
request
Definition: benchmark.py:77
grpc::testing::CliCallTest::stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: cli_call_test.cc:84
grpc::testing::CliCallTest::SetUp
void SetUp() override
Definition: cli_call_test.cc:64
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cli_call.h
grpc::testing::CliCallTest::server_address_
std::ostringstream server_address_
Definition: cli_call_test.cc:86
grpc::testing::TestServiceImpl
TestMultipleServiceImpl< grpc::testing::EchoTestService::Service > TestServiceImpl
Definition: test_service_impl.h:498
grpc::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Definition: client_context.cc:121
grpc::testing::CliCallTest::channel_
std::shared_ptr< Channel > channel_
Definition: cli_call_test.cc:83
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
call
FilterStackCall * call
Definition: call.cc:750
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
main
int main(int argc, char **argv)
Definition: cli_call_test.cc:125
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpc::testing::CliCallTest::ResetStub
void ResetStub()
Definition: cli_call_test.cc:77
grpc::testing::CliCallTest::TearDown
void TearDown() override
Definition: cli_call_test.cc:75
grpc.h
channel_
RefCountedPtr< Channel > channel_
Definition: channel_connectivity.cc:209
channel.h
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
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)
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
tests.unit._exit_scenarios.port
port
Definition: _exit_scenarios.py:179
test_config.h
grpc::ClientContext::GetServerTrailingMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerTrailingMetadata() const
Definition: grpcpp/impl/codegen/client_context.h:262
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
grpc::testing::ToString
std::string ToString(const grpc::string_ref &r)
Definition: string_ref_helper.cc:24
server_context.h
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
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
grpc::testing::CliCall
Definition: cli_call.h:43
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
iter
Definition: test_winkernel.cpp:47
grpc::testing::EXPECT_TRUE
EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(minimum_valid_json, &options) .ok())
grpc::ClientContext::GetServerInitialMetadata
const std::multimap< grpc::string_ref, grpc::string_ref > & GetServerInitialMetadata() const
Definition: grpcpp/impl/codegen/client_context.h:250
server.h
grpc::testing::CliCallTest
Definition: cli_call_test.cc:60
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
grpc::testing::CliCallTest::CliCallTest
CliCallTest()
Definition: cli_call_test.cc:62
string_ref_helper.h
TestServiceImpl
Definition: interop_server.cc:139
server_builder.h
create_channel.h
grpc::testing::CliCallTest::server_
std::unique_ptr< Server > server_
Definition: cli_call_test.cc:85


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:46