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/resolver.h"
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/gprpp/ref_counted.h"
25 #include "src/core/lib/iomgr/error.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 result. If the
49   // resolver is not available yet, delays response setting until it is. This
50   // can be called at most once before the resolver is available.
51   void SetResponse(Resolver::Result result);
52 
53   // Sets the re-resolution response, which is returned by the fake resolver
54   // when re-resolution is requested (via \a RequestReresolutionLocked()).
55   // The new re-resolution response replaces any previous re-resolution
56   // response that may have been set by a previous call.
57   void SetReresolutionResponse(Resolver::Result result);
58 
59   // Unsets the re-resolution response.  After this, the fake resolver will
60   // not return anything when \a RequestReresolutionLocked() is called.
61   void UnsetReresolutionResponse();
62 
63   // Tells the resolver to return a transient failure.
64   void SetFailure();
65 
66   // Same as SetFailure(), but instead of returning the error
67   // immediately, waits for the next call to RequestReresolutionLocked().
68   void SetFailureOnReresolution();
69 
70   // Returns a channel arg containing \a generator.
71   static grpc_arg MakeChannelArg(FakeResolverResponseGenerator* generator);
72 
73   // Returns the response generator in \a args, or null if not found.
74   static FakeResolverResponseGenerator* GetFromArgs(
75       const grpc_channel_args* args);
76 
77  private:
78   friend class FakeResolver;
79 
80   static void SetResponseLocked(void* arg, grpc_error* error);
81   static void SetReresolutionResponseLocked(void* arg, grpc_error* error);
82   static void SetFailureLocked(void* arg, grpc_error* error);
83 
84   FakeResolver* resolver_ = nullptr;  // Do not own.
85   Resolver::Result result_;
86   bool has_result_ = false;
87 };
88 
89 }  // namespace grpc_core
90 
91 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_FAKE_FAKE_RESOLVER_H \
92         */
93