3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include <grpc/grpc.h>
22 #include <grpc/support/log.h>
24 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
25 #include "src/core/ext/filters/client_channel/resolver_registry.h"
26 #include "src/core/ext/filters/client_channel/server_address.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/gprpp/memory.h"
29 #include "src/core/lib/iomgr/combiner.h"
30 #include "src/core/lib/iomgr/sockaddr_utils.h"
31 #include "test/core/util/test_config.h"
33 constexpr int kMinResolutionPeriodMs = 1000;
34 // Provide some slack when checking intervals, to allow for test timing issues.
35 constexpr int kMinResolutionPeriodForCheckMs = 900;
37 extern grpc_address_resolver_vtable* grpc_resolve_address_impl;
38 static grpc_address_resolver_vtable* default_resolve_address;
40 static grpc_core::Combiner* g_combiner;
42 static grpc_ares_request* (*g_default_dns_lookup_ares_locked)(
43 const char* dns_server, const char* name, const char* default_port,
44 grpc_pollset_set* interested_parties, grpc_closure* on_done,
45 std::unique_ptr<grpc_core::ServerAddressList>* addresses,
46 std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
47 char** service_config_json, int query_timeout_ms,
48 grpc_core::Combiner* combiner);
50 // Counter incremented by test_resolve_address_impl indicating the number of
51 // times a system-level resolution has happened.
52 static int g_resolution_count;
54 static struct iomgr_args {
58 grpc_pollset* pollset;
59 grpc_pollset_set* pollset_set;
62 // Wrapper around default resolve_address in order to count the number of
63 // times we incur in a system-level name resolution.
64 static void test_resolve_address_impl(const char* name,
65 const char* default_port,
66 grpc_pollset_set* /*interested_parties*/,
67 grpc_closure* on_done,
68 grpc_resolved_addresses** addrs) {
69 default_resolve_address->resolve_address(
70 name, default_port, g_iomgr_args.pollset_set, on_done, addrs);
72 static grpc_millis last_resolution_time = 0;
73 if (last_resolution_time == 0) {
74 last_resolution_time =
75 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
78 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
79 GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
80 last_resolution_time = now;
84 static grpc_error* test_blocking_resolve_address_impl(
85 const char* name, const char* default_port,
86 grpc_resolved_addresses** addresses) {
87 return default_resolve_address->blocking_resolve_address(name, default_port,
91 static grpc_address_resolver_vtable test_resolver = {
92 test_resolve_address_impl, test_blocking_resolve_address_impl};
94 static grpc_ares_request* test_dns_lookup_ares_locked(
95 const char* dns_server, const char* name, const char* default_port,
96 grpc_pollset_set* /*interested_parties*/, grpc_closure* on_done,
97 std::unique_ptr<grpc_core::ServerAddressList>* addresses,
98 std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
99 char** service_config_json, int query_timeout_ms,
100 grpc_core::Combiner* combiner) {
101 grpc_ares_request* result = g_default_dns_lookup_ares_locked(
102 dns_server, name, default_port, g_iomgr_args.pollset_set, on_done,
103 addresses, balancer_addresses, service_config_json, query_timeout_ms,
105 ++g_resolution_count;
106 static grpc_millis last_resolution_time = 0;
108 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
110 "last_resolution_time:%" PRId64 " now:%" PRId64
111 " min_time_between:%d",
112 last_resolution_time, now, kMinResolutionPeriodForCheckMs);
113 if (last_resolution_time == 0) {
114 last_resolution_time =
115 grpc_timespec_to_millis_round_up(gpr_now(GPR_CLOCK_MONOTONIC));
117 GPR_ASSERT(now - last_resolution_time >= kMinResolutionPeriodForCheckMs);
118 last_resolution_time = now;
123 static gpr_timespec test_deadline(void) {
124 return grpc_timeout_seconds_to_deadline(100);
127 static void do_nothing(void* /*arg*/, grpc_error* /*error*/) {}
129 static void iomgr_args_init(iomgr_args* args) {
130 gpr_event_init(&args->ev);
131 args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
132 grpc_pollset_init(args->pollset, &args->mu);
133 args->pollset_set = grpc_pollset_set_create();
134 grpc_pollset_set_add_pollset(args->pollset_set, args->pollset);
135 gpr_atm_rel_store(&args->done_atm, 0);
138 static void iomgr_args_finish(iomgr_args* args) {
139 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline()));
140 grpc_pollset_set_del_pollset(args->pollset_set, args->pollset);
141 grpc_pollset_set_destroy(args->pollset_set);
142 grpc_closure do_nothing_cb;
143 GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr,
144 grpc_schedule_on_exec_ctx);
145 gpr_mu_lock(args->mu);
146 grpc_pollset_shutdown(args->pollset, &do_nothing_cb);
147 gpr_mu_unlock(args->mu);
148 // exec_ctx needs to be flushed before calling grpc_pollset_destroy()
149 grpc_core::ExecCtx::Get()->Flush();
150 grpc_pollset_destroy(args->pollset);
151 gpr_free(args->pollset);
154 static grpc_millis n_sec_deadline(int seconds) {
155 return grpc_timespec_to_millis_round_up(
156 grpc_timeout_seconds_to_deadline(seconds));
159 static void poll_pollset_until_request_done(iomgr_args* args) {
160 grpc_core::ExecCtx exec_ctx;
161 grpc_millis deadline = n_sec_deadline(10);
163 bool done = gpr_atm_acq_load(&args->done_atm) != 0;
167 grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now();
168 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left);
169 GPR_ASSERT(time_left >= 0);
170 grpc_pollset_worker* worker = nullptr;
171 gpr_mu_lock(args->mu);
172 GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker,
174 gpr_mu_unlock(args->mu);
175 grpc_core::ExecCtx::Get()->Flush();
177 gpr_event_set(&args->ev, (void*)1);
180 struct OnResolutionCallbackArg;
182 class ResultHandler : public grpc_core::Resolver::ResultHandler {
184 using ResultCallback = void (*)(OnResolutionCallbackArg* state);
186 void SetCallback(ResultCallback result_cb, OnResolutionCallbackArg* state) {
187 GPR_ASSERT(result_cb_ == nullptr);
188 result_cb_ = result_cb;
189 GPR_ASSERT(state_ == nullptr);
193 void ReturnResult(grpc_core::Resolver::Result /*result*/) override {
194 GPR_ASSERT(result_cb_ != nullptr);
195 GPR_ASSERT(state_ != nullptr);
196 ResultCallback cb = result_cb_;
197 OnResolutionCallbackArg* state = state_;
198 result_cb_ = nullptr;
203 void ReturnError(grpc_error* error) override {
204 gpr_log(GPR_ERROR, "resolver returned error: %s", grpc_error_string(error));
209 ResultCallback result_cb_ = nullptr;
210 OnResolutionCallbackArg* state_ = nullptr;
213 struct OnResolutionCallbackArg {
214 const char* uri_str = nullptr;
215 grpc_core::OrphanablePtr<grpc_core::Resolver> resolver;
216 ResultHandler* result_handler;
219 // Set to true by the last callback in the resolution chain.
220 static bool g_all_callbacks_invoked;
222 // It's interesting to run a few rounds of this test because as
223 // we run more rounds, the base starting time
224 // (i.e. ExecCtx g_start_time) gets further and further away
225 // from "Now()". Thus the more rounds ran, the more highlighted the
226 // difference is between absolute and relative times values.
227 static void on_fourth_resolution(OnResolutionCallbackArg* cb_arg) {
228 gpr_log(GPR_INFO, "4th: g_resolution_count: %d", g_resolution_count);
229 GPR_ASSERT(g_resolution_count == 4);
230 cb_arg->resolver.reset();
231 gpr_atm_rel_store(&g_iomgr_args.done_atm, 1);
232 gpr_mu_lock(g_iomgr_args.mu);
233 GRPC_LOG_IF_ERROR("pollset_kick",
234 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
235 gpr_mu_unlock(g_iomgr_args.mu);
237 g_all_callbacks_invoked = true;
240 static void on_third_resolution(OnResolutionCallbackArg* cb_arg) {
241 gpr_log(GPR_INFO, "3rd: g_resolution_count: %d", g_resolution_count);
242 GPR_ASSERT(g_resolution_count == 3);
243 cb_arg->result_handler->SetCallback(on_fourth_resolution, cb_arg);
244 cb_arg->resolver->RequestReresolutionLocked();
245 gpr_mu_lock(g_iomgr_args.mu);
246 GRPC_LOG_IF_ERROR("pollset_kick",
247 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
248 gpr_mu_unlock(g_iomgr_args.mu);
251 static void on_second_resolution(OnResolutionCallbackArg* cb_arg) {
252 gpr_log(GPR_INFO, "2nd: g_resolution_count: %d", g_resolution_count);
253 // The resolution callback was not invoked until new data was
254 // available, which was delayed until after the cooldown period.
255 GPR_ASSERT(g_resolution_count == 2);
256 cb_arg->result_handler->SetCallback(on_third_resolution, cb_arg);
257 cb_arg->resolver->RequestReresolutionLocked();
258 gpr_mu_lock(g_iomgr_args.mu);
259 GRPC_LOG_IF_ERROR("pollset_kick",
260 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
261 gpr_mu_unlock(g_iomgr_args.mu);
264 static void on_first_resolution(OnResolutionCallbackArg* cb_arg) {
265 gpr_log(GPR_INFO, "1st: g_resolution_count: %d", g_resolution_count);
266 // There's one initial system-level resolution and one invocation of a
267 // notification callback (the current function).
268 GPR_ASSERT(g_resolution_count == 1);
269 cb_arg->result_handler->SetCallback(on_second_resolution, cb_arg);
270 cb_arg->resolver->RequestReresolutionLocked();
271 gpr_mu_lock(g_iomgr_args.mu);
272 GRPC_LOG_IF_ERROR("pollset_kick",
273 grpc_pollset_kick(g_iomgr_args.pollset, nullptr));
274 gpr_mu_unlock(g_iomgr_args.mu);
277 static void start_test_under_combiner(void* arg, grpc_error* /*error*/) {
278 OnResolutionCallbackArg* res_cb_arg =
279 static_cast<OnResolutionCallbackArg*>(arg);
280 res_cb_arg->result_handler = new ResultHandler();
281 grpc_core::ResolverFactory* factory =
282 grpc_core::ResolverRegistry::LookupResolverFactory("dns");
283 grpc_uri* uri = grpc_uri_parse(res_cb_arg->uri_str, 0);
284 gpr_log(GPR_DEBUG, "test: '%s' should be valid for '%s'", res_cb_arg->uri_str,
286 GPR_ASSERT(uri != nullptr);
287 grpc_core::ResolverArgs args;
289 args.combiner = g_combiner;
290 args.result_handler = std::unique_ptr<grpc_core::Resolver::ResultHandler>(
291 res_cb_arg->result_handler);
292 g_resolution_count = 0;
294 grpc_arg cooldown_arg = grpc_channel_arg_integer_create(
295 const_cast<char*>(GRPC_ARG_DNS_MIN_TIME_BETWEEN_RESOLUTIONS_MS),
296 kMinResolutionPeriodMs);
297 grpc_channel_args cooldown_args = {1, &cooldown_arg};
298 args.args = &cooldown_args;
299 res_cb_arg->resolver = factory->CreateResolver(std::move(args));
300 GPR_ASSERT(res_cb_arg->resolver != nullptr);
301 grpc_uri_destroy(uri);
302 // First resolution, would incur in system-level resolution.
303 res_cb_arg->result_handler->SetCallback(on_first_resolution, res_cb_arg);
304 res_cb_arg->resolver->StartLocked();
307 static void test_cooldown() {
308 grpc_core::ExecCtx exec_ctx;
309 iomgr_args_init(&g_iomgr_args);
310 OnResolutionCallbackArg* res_cb_arg = new OnResolutionCallbackArg();
311 res_cb_arg->uri_str = "dns:127.0.0.1";
314 GRPC_CLOSURE_CREATE(start_test_under_combiner, res_cb_arg, nullptr),
316 grpc_core::ExecCtx::Get()->Flush();
317 poll_pollset_until_request_done(&g_iomgr_args);
318 iomgr_args_finish(&g_iomgr_args);
321 int main(int argc, char** argv) {
322 grpc::testing::TestEnvironment env(argc, argv);
325 g_combiner = grpc_combiner_create();
327 g_default_dns_lookup_ares_locked = grpc_dns_lookup_ares_locked;
328 grpc_dns_lookup_ares_locked = test_dns_lookup_ares_locked;
329 default_resolve_address = grpc_resolve_address_impl;
330 grpc_set_resolver_impl(&test_resolver);
334 grpc_core::ExecCtx exec_ctx;
335 GRPC_COMBINER_UNREF(g_combiner, "test");
337 grpc_shutdown_blocking();
338 GPR_ASSERT(g_all_callbacks_invoked);