LCOV - code coverage report
Current view: top level - test/cpp/util - cli_call_test.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 55 55 100.0 %
Date: 2015-10-10 Functions: 15 17 88.2 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "test/cpp/util/cli_call.h"
      35             : 
      36             : #include <grpc/grpc.h>
      37             : #include <grpc++/channel.h>
      38             : #include <grpc++/client_context.h>
      39             : #include <grpc++/create_channel.h>
      40             : #include <grpc++/server.h>
      41             : #include <grpc++/server_builder.h>
      42             : #include <grpc++/server_context.h>
      43             : #include <gtest/gtest.h>
      44             : 
      45             : #include "test/core/util/port.h"
      46             : #include "test/core/util/test_config.h"
      47             : #include "test/cpp/util/echo.grpc.pb.h"
      48             : #include "test/cpp/util/string_ref_helper.h"
      49             : 
      50             : using grpc::cpp::test::util::EchoRequest;
      51             : using grpc::cpp::test::util::EchoResponse;
      52             : 
      53             : namespace grpc {
      54             : namespace testing {
      55             : 
      56           2 : class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
      57             :  public:
      58           2 :   Status Echo(ServerContext* context, const EchoRequest* request,
      59             :               EchoResponse* response) GRPC_OVERRIDE {
      60           2 :     if (!context->client_metadata().empty()) {
      61          18 :       for (std::multimap<grpc::string_ref, grpc::string_ref>::const_iterator
      62           2 :                iter = context->client_metadata().begin();
      63          12 :            iter != context->client_metadata().end(); ++iter) {
      64           4 :         context->AddInitialMetadata(ToString(iter->first),
      65           8 :                                     ToString(iter->second));
      66             :       }
      67             :     }
      68           2 :     context->AddTrailingMetadata("trailing_key", "trailing_value");
      69           2 :     response->set_message(request->message());
      70           2 :     return Status::OK;
      71             :   }
      72             : };
      73             : 
      74           1 : class CliCallTest : public ::testing::Test {
      75             :  protected:
      76           1 :   CliCallTest() {}
      77             : 
      78           1 :   void SetUp() GRPC_OVERRIDE {
      79           1 :     int port = grpc_pick_unused_port_or_die();
      80           1 :     server_address_ << "localhost:" << port;
      81             :     // Setup server
      82           1 :     ServerBuilder builder;
      83             :     builder.AddListeningPort(server_address_.str(),
      84           1 :                              InsecureServerCredentials());
      85           1 :     builder.RegisterService(&service_);
      86           1 :     server_ = builder.BuildAndStart();
      87           1 :   }
      88             : 
      89           1 :   void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
      90             : 
      91           1 :   void ResetStub() {
      92           1 :     channel_ = CreateChannel(server_address_.str(), InsecureCredentials());
      93           1 :     stub_ = grpc::cpp::test::util::TestService::NewStub(channel_);
      94           1 :   }
      95             : 
      96             :   std::shared_ptr<Channel> channel_;
      97             :   std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
      98             :   std::unique_ptr<Server> server_;
      99             :   std::ostringstream server_address_;
     100             :   TestServiceImpl service_;
     101             : };
     102             : 
     103             : // Send a rpc with a normal stub and then a CliCall. Verify they match.
     104           5 : TEST_F(CliCallTest, SimpleRpc) {
     105           1 :   ResetStub();
     106             :   // Normal stub.
     107           1 :   EchoRequest request;
     108           2 :   EchoResponse response;
     109           1 :   request.set_message("Hello");
     110             : 
     111           2 :   ClientContext context;
     112           1 :   context.AddMetadata("key1", "val1");
     113           2 :   Status s = stub_->Echo(&context, request, &response);
     114           1 :   EXPECT_EQ(response.message(), request.message());
     115           1 :   EXPECT_TRUE(s.ok());
     116             : 
     117           1 :   const grpc::string kMethod("/grpc.cpp.test.util.TestService/Echo");
     118           2 :   grpc::string request_bin, response_bin, expected_response_bin;
     119           1 :   EXPECT_TRUE(request.SerializeToString(&request_bin));
     120           1 :   EXPECT_TRUE(response.SerializeToString(&expected_response_bin));
     121           1 :   std::multimap<grpc::string, grpc::string> client_metadata;
     122           2 :   std::multimap<grpc::string_ref, grpc::string_ref> server_initial_metadata,
     123           2 :       server_trailing_metadata;
     124           1 :   client_metadata.insert(std::pair<grpc::string, grpc::string>("key1", "val1"));
     125             :   Status s2 = CliCall::Call(channel_, kMethod, request_bin, &response_bin,
     126             :                             client_metadata, &server_initial_metadata,
     127           2 :                             &server_trailing_metadata);
     128           1 :   EXPECT_TRUE(s2.ok());
     129             : 
     130           1 :   EXPECT_EQ(expected_response_bin, response_bin);
     131           1 :   EXPECT_EQ(context.GetServerInitialMetadata(), server_initial_metadata);
     132           4 :   EXPECT_EQ(context.GetServerTrailingMetadata(), server_trailing_metadata);
     133           1 : }
     134             : 
     135             : }  // namespace testing
     136             : }  // namespace grpc
     137             : 
     138           1 : int main(int argc, char** argv) {
     139           1 :   grpc_test_init(argc, argv);
     140           1 :   ::testing::InitGoogleTest(&argc, argv);
     141           1 :   return RUN_ALL_TESTS();
     142           3 : }

Generated by: LCOV version 1.10