LCOV - code coverage report
Current view: top level - test/cpp/util - cli_call.cc (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 41 43 95.3 %
Date: 2015-10-10 Functions: 4 4 100.0 %

          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 <iostream>
      37             : 
      38             : #include <grpc/grpc.h>
      39             : #include <grpc/support/log.h>
      40             : #include <grpc/support/slice.h>
      41             : #include <grpc++/support/byte_buffer.h>
      42             : #include <grpc++/channel.h>
      43             : #include <grpc++/client_context.h>
      44             : #include <grpc++/generic/generic_stub.h>
      45             : 
      46             : namespace grpc {
      47             : namespace testing {
      48             : namespace {
      49           5 : void* tag(int i) { return (void*)(gpr_intptr)i; }
      50             : }  // namespace
      51             : 
      52           1 : Status CliCall::Call(std::shared_ptr<grpc::Channel> channel,
      53             :                      const grpc::string& method, const grpc::string& request,
      54             :                      grpc::string* response,
      55             :                      const OutgoingMetadataContainer& metadata,
      56             :                      IncomingMetadataContainer* server_initial_metadata,
      57             :                      IncomingMetadataContainer* server_trailing_metadata) {
      58           1 :   std::unique_ptr<grpc::GenericStub> stub(new grpc::GenericStub(channel));
      59           2 :   grpc::ClientContext ctx;
      60           1 :   if (!metadata.empty()) {
      61           6 :     for (OutgoingMetadataContainer::const_iterator iter = metadata.begin();
      62           4 :          iter != metadata.end(); ++iter) {
      63           1 :       ctx.AddMetadata(iter->first, iter->second);
      64             :     }
      65             :   }
      66           2 :   grpc::CompletionQueue cq;
      67             :   std::unique_ptr<grpc::GenericClientAsyncReaderWriter> call(
      68           2 :       stub->Call(&ctx, method, &cq, tag(1)));
      69             :   void* got_tag;
      70             :   bool ok;
      71           1 :   cq.Next(&got_tag, &ok);
      72           1 :   GPR_ASSERT(ok);
      73             : 
      74           1 :   gpr_slice s = gpr_slice_from_copied_string(request.c_str());
      75           2 :   grpc::Slice req_slice(s, grpc::Slice::STEAL_REF);
      76           2 :   grpc::ByteBuffer send_buffer(&req_slice, 1);
      77           1 :   call->Write(send_buffer, tag(2));
      78           1 :   cq.Next(&got_tag, &ok);
      79           1 :   GPR_ASSERT(ok);
      80           1 :   call->WritesDone(tag(3));
      81           1 :   cq.Next(&got_tag, &ok);
      82           1 :   GPR_ASSERT(ok);
      83           2 :   grpc::ByteBuffer recv_buffer;
      84           1 :   call->Read(&recv_buffer, tag(4));
      85           1 :   cq.Next(&got_tag, &ok);
      86           1 :   if (!ok) {
      87           0 :     std::cout << "Failed to read response." << std::endl;
      88           0 :     return Status(StatusCode::INTERNAL, "Failed to read response");
      89             :   }
      90           2 :   grpc::Status status;
      91           1 :   call->Finish(&status, tag(5));
      92           1 :   cq.Next(&got_tag, &ok);
      93           1 :   GPR_ASSERT(ok);
      94             : 
      95           1 :   if (status.ok()) {
      96           1 :     std::vector<grpc::Slice> slices;
      97           1 :     recv_buffer.Dump(&slices);
      98             : 
      99           1 :     response->clear();
     100           2 :     for (size_t i = 0; i < slices.size(); i++) {
     101           1 :       response->append(reinterpret_cast<const char*>(slices[i].begin()),
     102           2 :                        slices[i].size());
     103           1 :     }
     104             :   }
     105           1 :   *server_initial_metadata = ctx.GetServerInitialMetadata();
     106           1 :   *server_trailing_metadata = ctx.GetServerTrailingMetadata();
     107           2 :   return status;
     108             : }
     109             : 
     110             : }  // namespace testing
     111           3 : }  // namespace grpc

Generated by: LCOV version 1.10