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