| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "test/cpp/end2end/rls_server.h" |
| 18 |
|
| 19 |
#include <gmock/gmock.h> |
| 20 |
|
| 21 |
#include "src/proto/grpc/lookup/v1/rls.grpc.pb.h" |
| 22 |
#include "src/proto/grpc/lookup/v1/rls.pb.h" |
| 23 |
#include "test/core/util/test_config.h" |
| 24 |
|
| 25 |
using ::grpc::lookup::v1::RouteLookupRequest; |
| 26 |
using ::grpc::lookup::v1::RouteLookupResponse; |
| 27 |
|
| 28 |
namespace grpc { |
| 29 |
namespace testing { |
| 30 |
|
| 31 |
::grpc::Status RlsServiceImpl::RouteLookup(grpc::ServerContext* context, |
| 32 |
const RouteLookupRequest* request, |
| 33 |
RouteLookupResponse* response) { |
| 34 |
gpr_log(GPR_INFO, "RLS: Received request: %s", |
| 35 |
request->DebugString().c_str()); |
| 36 |
if (context_proc_ != nullptr) { |
| 37 |
context_proc_(context); |
| 38 |
} |
| 39 |
IncreaseRequestCount(); |
| 40 |
EXPECT_EQ(request->target_type(), "grpc"); |
| 41 |
// See if we have a configured response for this request. |
| 42 |
ResponseData res; |
| 43 |
{ |
| 44 |
grpc::internal::MutexLock lock(&mu_); |
| 45 |
auto it = responses_.find(*request); |
| 46 |
if (it == responses_.end()) { |
| 47 |
gpr_log(GPR_INFO, "RLS: no matching request, returning INTERNAL"); |
| 48 |
unmatched_requests_.push_back(*request); |
| 49 |
return Status(StatusCode::INTERNAL, "no response entry"); |
| 50 |
} |
| 51 |
res = it->second; |
| 52 |
} |
| 53 |
// Configured response found, so use it. |
| 54 |
if (res.response_delay > grpc_core::Duration::Zero()) { |
| 55 |
gpr_sleep_until( |
| 56 |
grpc_timeout_milliseconds_to_deadline(res.response_delay.millis())); |
| 57 |
} |
| 58 |
IncreaseResponseCount(); |
| 59 |
*response = res.response; |
| 60 |
gpr_log(GPR_INFO, "RLS: returning configured response: %s", |
| 61 |
response->DebugString().c_str()); |
| 62 |
return Status::OK; |
| 63 |
} |
| 64 |
|
| 65 |
void RlsServiceImpl::SetResponse(RouteLookupRequest request, |
| 66 |
RouteLookupResponse response, |
| 67 |
grpc_core::Duration response_delay) { |
| 68 |
grpc::internal::MutexLock lock(&mu_); |
| 69 |
responses_[std::move(request)] = {std::move(response), response_delay}; |
| 70 |
} |
| 71 |
|
| 72 |
void RlsServiceImpl::RemoveResponse(const RouteLookupRequest& request) { |
| 73 |
grpc::internal::MutexLock lock(&mu_); |
| 74 |
responses_.erase(request); |
| 75 |
} |
| 76 |
|
| 77 |
std::vector<RouteLookupRequest> RlsServiceImpl::GetUnmatchedRequests() { |
| 78 |
grpc::internal::MutexLock lock(&mu_); |
| 79 |
return std::move(unmatched_requests_); |
| 80 |
} |
| 81 |
|
| 82 |
grpc::lookup::v1::RouteLookupRequest BuildRlsRequest( |
| 83 |
std::map<std::string, std::string> key, |
| 84 |
grpc::lookup::v1::RouteLookupRequest::Reason reason, |
| 85 |
const char* stale_header_data) { |
| 86 |
grpc::lookup::v1::RouteLookupRequest request; |
| 87 |
request.set_target_type("grpc"); |
| 88 |
request.mutable_key_map()->insert(key.begin(), key.end()); |
| 89 |
request.set_reason(reason); |
| 90 |
request.set_stale_header_data(stale_header_data); |
| 91 |
return request; |
| 92 |
} |
| 93 |
|
| 94 |
grpc::lookup::v1::RouteLookupResponse BuildRlsResponse( |
| 95 |
std::vector<std::string> targets, const char* header_data) { |
| 96 |
grpc::lookup::v1::RouteLookupResponse response; |
| 97 |
response.mutable_targets()->Add(targets.begin(), targets.end()); |
| 98 |
response.set_header_data(header_data); |
| 99 |
return response; |
| 100 |
} |
| 101 |
|
| 102 |
} // namespace testing |
| 103 |
} // namespace grpc |