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