<commit_before>/* Copyright 2017 Istio Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/grpc_transport.h"

#include <grpc++/security/server_credentials.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc/grpc.h>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <future>

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReaderWriter;

using ::istio::mixer::v1::CheckRequest;
using ::istio::mixer::v1::CheckResponse;
using ::istio::mixer::v1::ReportRequest;
using ::istio::mixer::v1::ReportResponse;
using ::istio::mixer::v1::QuotaRequest;
using ::istio::mixer::v1::QuotaResponse;
using ::google::protobuf::util::Status;

using ::testing::Invoke;
using ::testing::_;

namespace istio {
namespace mixer_client {

// A fake Mixer gRPC server: just echo a response for each request.
class MockMixerServerImpl final : public ::istio::mixer::v1::Mixer::Service {
 public:
  grpc::Status Check(
      ServerContext* context,
      ServerReaderWriter<CheckResponse, CheckRequest>* stream) override {
    CheckRequest request;
    while (stream->Read(&request)) {
      CheckResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
  grpc::Status Report(
      ServerContext* context,
      ServerReaderWriter<ReportResponse, ReportRequest>* stream) override {
    ReportRequest request;
    while (stream->Read(&request)) {
      ReportResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
  grpc::Status Quota(
      ServerContext* context,
      ServerReaderWriter<QuotaResponse, QuotaRequest>* stream) override {
    QuotaRequest request;
    while (stream->Read(&request)) {
      QuotaResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
};

template <class T>
class MockReader : public ReadInterface<T> {
 public:
  MOCK_METHOD1_T(OnRead, void(const T&));
  MOCK_METHOD1_T(OnClose, void(const Status&));
};

class GrpcTransportTest : public ::testing::Test {
 public:
  void SetUp() {
    // TODO: pick a un-used port. If this port is used, the test will fail.
    std::string server_address("0.0.0.0:50051");
    ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service_);
    server_ = builder.BuildAndStart();
<commit_msg>Add WaitForServer in grpc_transport_test. (#37)<commit_after>/* Copyright 2017 Istio Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/grpc_transport.h"

#include <grpc++/security/server_credentials.h>
#include <grpc++/server.h>
#include <grpc++/server_builder.h>
#include <grpc++/server_context.h>
#include <grpc/grpc.h>
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include <future>

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReaderWriter;

using ::istio::mixer::v1::CheckRequest;
using ::istio::mixer::v1::CheckResponse;
using ::istio::mixer::v1::ReportRequest;
using ::istio::mixer::v1::ReportResponse;
using ::istio::mixer::v1::QuotaRequest;
using ::istio::mixer::v1::QuotaResponse;
using ::google::protobuf::util::Status;

using ::testing::Invoke;
using ::testing::_;

namespace istio {
namespace mixer_client {

// A fake Mixer gRPC server: just echo a response for each request.
class MockMixerServerImpl final : public ::istio::mixer::v1::Mixer::Service {
 public:
  grpc::Status Check(
      ServerContext* context,
      ServerReaderWriter<CheckResponse, CheckRequest>* stream) override {
    CheckRequest request;
    while (stream->Read(&request)) {
      CheckResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
  grpc::Status Report(
      ServerContext* context,
      ServerReaderWriter<ReportResponse, ReportRequest>* stream) override {
    ReportRequest request;
    while (stream->Read(&request)) {
      ReportResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
  grpc::Status Quota(
      ServerContext* context,
      ServerReaderWriter<QuotaResponse, QuotaRequest>* stream) override {
    QuotaRequest request;
    while (stream->Read(&request)) {
      QuotaResponse response;
      // Just echo it back with same request_index.
      response.set_request_index(request.request_index());
      stream->Write(response);
    }
    return grpc::Status::OK;
  }
};

// Wait for up to 10 seconds for channel to be connected.
void WaitForServer(const std::string& server) {
  auto channel = CreateChannel(server, ::grpc::InsecureChannelCredentials());
  channel->WaitForConnected(gpr_time_add(
      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_seconds(10, GPR_TIMESPAN)));
}

template <class T>
class MockReader : public ReadInterface<T> {
 public:
  MOCK_METHOD1_T(OnRead, void(const T&));
  MOCK_METHOD1_T(OnClose, void(const Status&));
};

class GrpcTransportTest : public ::testing::Test {
 public:
  void SetUp() {
    // TODO: pick a un-used port. If this port is used, the test will fail.
    std::string server_address("0.0.0.0:50051");
    ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service_);
    server_ = builder.BuildAndStart();

    WaitForServer(server_address);

{'fraction_non_alphanumeric': 0.12081015546997576, 'fraction_numerical': 0.00827271430609043, 'mean_word_length': 4.521259842519685, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6582515', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2098, 'n_words': 570}