| 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 "absl/types/optional.h" |
| 18 |
|
| 19 |
#include "src/core/lib/gprpp/time.h" |
| 20 |
#include "src/proto/grpc/lookup/v1/rls.grpc.pb.h" |
| 21 |
#include "src/proto/grpc/lookup/v1/rls.pb.h" |
| 22 |
#include "test/cpp/end2end/counted_service.h" |
| 23 |
|
| 24 |
namespace grpc { |
| 25 |
namespace testing { |
| 26 |
|
| 27 |
using RlsService = |
| 28 |
CountedService<grpc::lookup::v1::RouteLookupService::Service>; |
| 29 |
|
| 30 |
class RlsServiceImpl : public RlsService { |
| 31 |
public: |
| 32 |
using ContextProcessingFunc = std::function<void(grpc::ServerContext*)>; |
| 33 |
|
| 34 |
explicit RlsServiceImpl(ContextProcessingFunc context_proc = nullptr) |
| 35 |
: context_proc_(std::move(context_proc)) {} |
| 36 |
|
| 37 |
grpc::Status RouteLookup( |
| 38 |
grpc::ServerContext* context, |
| 39 |
const grpc::lookup::v1::RouteLookupRequest* request, |
| 40 |
grpc::lookup::v1::RouteLookupResponse* response) override; |
| 41 |
|
| 42 |
void Start() {} |
| 43 |
|
| 44 |
void Shutdown() {} |
| 45 |
|
| 46 |
void SetResponse(grpc::lookup::v1::RouteLookupRequest request, |
| 47 |
grpc::lookup::v1::RouteLookupResponse response, |
| 48 |
grpc_core::Duration response_delay = grpc_core::Duration()); |
| 49 |
|
| 50 |
void RemoveResponse(const grpc::lookup::v1::RouteLookupRequest& request); |
| 51 |
|
| 52 |
std::vector<grpc::lookup::v1::RouteLookupRequest> GetUnmatchedRequests(); |
| 53 |
|
| 54 |
private: |
| 55 |
// Sorting thunk for RouteLookupRequest. |
| 56 |
struct RlsRequestLessThan { |
| 57 |
bool operator()(const grpc::lookup::v1::RouteLookupRequest& req1, |
| 58 |
const grpc::lookup::v1::RouteLookupRequest& req2) const { |
| 59 |
std::map<absl::string_view, absl::string_view> key_map1( |
| 60 |
req1.key_map().begin(), req1.key_map().end()); |
| 61 |
std::map<absl::string_view, absl::string_view> key_map2( |
| 62 |
req2.key_map().begin(), req2.key_map().end()); |
| 63 |
if (key_map1 < key_map2) return true; |
| 64 |
if (req1.reason() < req2.reason()) return true; |
| 65 |
if (req1.stale_header_data() < req2.stale_header_data()) return true; |
| 66 |
return false; |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
struct ResponseData { |
| 71 |
grpc::lookup::v1::RouteLookupResponse response; |
| 72 |
grpc_core::Duration response_delay; |
| 73 |
}; |
| 74 |
|
| 75 |
ContextProcessingFunc context_proc_; |
| 76 |
grpc::internal::Mutex mu_; |
| 77 |
std::map<grpc::lookup::v1::RouteLookupRequest, ResponseData, |
| 78 |
RlsRequestLessThan> |
| 79 |
responses_ ABSL_GUARDED_BY(&mu_); |
| 80 |
std::vector<grpc::lookup::v1::RouteLookupRequest> unmatched_requests_ |
| 81 |
ABSL_GUARDED_BY(&mu_); |
| 82 |
}; |
| 83 |
|
| 84 |
grpc::lookup::v1::RouteLookupRequest BuildRlsRequest( |
| 85 |
std::map<std::string, std::string> key, |
| 86 |
grpc::lookup::v1::RouteLookupRequest::Reason reason = |
| 87 |
grpc::lookup::v1::RouteLookupRequest::REASON_MISS, |
| 88 |
const char* stale_header_data = ""); |
| 89 |
|
| 90 |
grpc::lookup::v1::RouteLookupResponse BuildRlsResponse( |
| 91 |
std::vector<std::string> targets, const char* header_data = ""); |
| 92 |
|
| 93 |
} // namespace testing |
| 94 |
} // namespace grpc |