1 // 2 // Copyright 2016 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H 18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "src/core/ext/filters/client_channel/lb_policy_factory.h" 23 #include "src/core/ext/filters/client_channel/uri_parser.h" 24 #include "src/core/lib/channel/channel_args.h" 25 #include "src/core/lib/gprpp/ref_counted.h" 26 27 #define GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR \ 28 "grpc.fake_resolver.response_generator" 29 30 namespace grpc_core { 31 32 class FakeResolver; 33 34 /// A mechanism for generating responses for the fake resolver. 35 /// An instance of this class is passed to the fake resolver via a channel 36 /// argument (see \a MakeChannelArg()) and used to inject and trigger custom 37 /// resolutions. 38 // TODO(roth): I would ideally like this to be InternallyRefCounted 39 // instead of RefCounted, but external refs are currently needed to 40 // encode this in channel args. Once channel_args are converted to C++, 41 // see if we can find a way to fix this. 42 class FakeResolverResponseGenerator 43 : public RefCounted<FakeResolverResponseGenerator> { 44 public: FakeResolverResponseGenerator()45 FakeResolverResponseGenerator() {} 46 47 // Instructs the fake resolver associated with the response generator 48 // instance to trigger a new resolution with the specified response. 49 void SetResponse(grpc_channel_args* next_response); 50 51 // Sets the re-resolution response, which is returned by the fake resolver 52 // when re-resolution is requested (via \a RequestReresolutionLocked()). 53 // The new re-resolution response replaces any previous re-resolution 54 // response that may have been set by a previous call. 55 // If the re-resolution response is set to NULL, then the fake 56 // resolver will not return anything when \a RequestReresolutionLocked() 57 // is called. 58 void SetReresolutionResponse(grpc_channel_args* response); 59 60 // Tells the resolver to return a transient failure (signalled by 61 // returning a null result with no error). 62 void SetFailure(); 63 64 // Returns a channel arg containing \a generator. 65 static grpc_arg MakeChannelArg(FakeResolverResponseGenerator* generator); 66 67 // Returns the response generator in \a args, or null if not found. 68 static FakeResolverResponseGenerator* GetFromArgs( 69 const grpc_channel_args* args); 70 71 private: 72 friend class FakeResolver; 73 74 static void SetResponseLocked(void* arg, grpc_error* error); 75 static void SetReresolutionResponseLocked(void* arg, grpc_error* error); 76 static void SetFailureLocked(void* arg, grpc_error* error); 77 78 FakeResolver* resolver_ = nullptr; // Do not own. 79 }; 80 81 } // namespace grpc_core 82 83 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H \ 84 */ 85