blob: b421720492413730c2bdc74771bed7e2cca8263e [file] [log] [blame]
Craig Tillere91ef682016-03-11 08:59:17 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillere91ef682016-03-11 08:59:17 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tillere91ef682016-03-11 08:59:17 -080034#include <string.h>
35
36#include <grpc/grpc.h>
37#include <grpc/support/alloc.h>
38
Mark D. Roth2137cd82016-09-14 09:04:00 -070039#include "src/core/ext/client_channel/resolver_registry.h"
Mark D. Rothb367c1b2016-10-24 12:59:07 -070040#include "src/core/lib/channel/channel_args.h"
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/resolve_address.h"
42#include "src/core/lib/iomgr/timer.h"
Craig Tillere91ef682016-03-11 08:59:17 -080043#include "test/core/util/test_config.h"
44
Craig Tillere91ef682016-03-11 08:59:17 -080045static gpr_mu g_mu;
46static bool g_fail_resolution = true;
47
Craig Tillerf707d622016-05-06 14:26:12 -070048static grpc_error *my_resolve_address(const char *name, const char *addr,
49 grpc_resolved_addresses **addrs) {
Craig Tillere91ef682016-03-11 08:59:17 -080050 gpr_mu_lock(&g_mu);
51 GPR_ASSERT(0 == strcmp("test", name));
52 if (g_fail_resolution) {
53 g_fail_resolution = false;
54 gpr_mu_unlock(&g_mu);
Craig Tillerf707d622016-05-06 14:26:12 -070055 return GRPC_ERROR_CREATE("Forced Failure");
Craig Tillere91ef682016-03-11 08:59:17 -080056 } else {
57 gpr_mu_unlock(&g_mu);
David Garcia Quintase77e9b32016-05-10 15:16:24 -070058 *addrs = gpr_malloc(sizeof(**addrs));
Craig Tillerf707d622016-05-06 14:26:12 -070059 (*addrs)->naddrs = 1;
60 (*addrs)->addrs = gpr_malloc(sizeof(*(*addrs)->addrs));
61 (*addrs)->addrs[0].len = 123;
62 return GRPC_ERROR_NONE;
Craig Tillere91ef682016-03-11 08:59:17 -080063 }
64}
65
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -080066static grpc_resolver *create_resolver(grpc_exec_ctx *exec_ctx,
67 const char *name) {
Craig Tiller65938df2016-03-31 13:08:49 -070068 grpc_resolver_factory *factory = grpc_resolver_factory_lookup("dns");
Craig Tillere91ef682016-03-11 08:59:17 -080069 grpc_uri *uri = grpc_uri_parse(name, 0);
70 GPR_ASSERT(uri);
71 grpc_resolver_args args;
72 memset(&args, 0, sizeof(args));
73 args.uri = uri;
Craig Tillere91ef682016-03-11 08:59:17 -080074 grpc_resolver *resolver =
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -080075 grpc_resolver_factory_create_resolver(exec_ctx, factory, &args);
Craig Tillere91ef682016-03-11 08:59:17 -080076 grpc_resolver_factory_unref(factory);
77 grpc_uri_destroy(uri);
78 return resolver;
79}
80
Craig Tillerf707d622016-05-06 14:26:12 -070081static void on_done(grpc_exec_ctx *exec_ctx, void *ev, grpc_error *error) {
Craig Tillere91ef682016-03-11 08:59:17 -080082 gpr_event_set(ev, (void *)1);
83}
84
Craig Tiller1e55bd42016-03-11 09:47:43 -080085// interleave waiting for an event with a timer check
86static bool wait_loop(int deadline_seconds, gpr_event *ev) {
87 while (deadline_seconds) {
88 gpr_log(GPR_DEBUG, "Test: waiting for %d more seconds", deadline_seconds);
89 if (gpr_event_wait(ev, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1))) return true;
90 deadline_seconds--;
91
92 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
93 grpc_timer_check(&exec_ctx, gpr_now(GPR_CLOCK_MONOTONIC), NULL);
94 grpc_exec_ctx_finish(&exec_ctx);
95 }
96 return false;
97}
98
Craig Tillere91ef682016-03-11 08:59:17 -080099int main(int argc, char **argv) {
100 grpc_test_init(argc, argv);
101
102 grpc_init();
103 gpr_mu_init(&g_mu);
104 grpc_blocking_resolve_address = my_resolve_address;
Mark D. Rothb367c1b2016-10-24 12:59:07 -0700105 grpc_channel_args *result = (grpc_channel_args *)1;
Craig Tillere91ef682016-03-11 08:59:17 -0800106
107 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Yuchen Zeng63e3e3b2016-12-15 12:06:33 -0800108 grpc_resolver *resolver = create_resolver(&exec_ctx, "dns:test");
Craig Tillere91ef682016-03-11 08:59:17 -0800109 gpr_event ev1;
110 gpr_event_init(&ev1);
Mark D. Roth1edf6522016-08-23 12:35:40 -0700111 grpc_resolver_next(&exec_ctx, resolver, &result,
Craig Tillere91ef682016-03-11 08:59:17 -0800112 grpc_closure_create(on_done, &ev1));
113 grpc_exec_ctx_flush(&exec_ctx);
Craig Tiller1e55bd42016-03-11 09:47:43 -0800114 GPR_ASSERT(wait_loop(5, &ev1));
Mark D. Roth1edf6522016-08-23 12:35:40 -0700115 GPR_ASSERT(result == NULL);
Craig Tillere91ef682016-03-11 08:59:17 -0800116
117 gpr_event ev2;
118 gpr_event_init(&ev2);
Mark D. Roth1edf6522016-08-23 12:35:40 -0700119 grpc_resolver_next(&exec_ctx, resolver, &result,
Craig Tillere91ef682016-03-11 08:59:17 -0800120 grpc_closure_create(on_done, &ev2));
121 grpc_exec_ctx_flush(&exec_ctx);
Craig Tiller1e55bd42016-03-11 09:47:43 -0800122 GPR_ASSERT(wait_loop(30, &ev2));
Mark D. Roth1edf6522016-08-23 12:35:40 -0700123 GPR_ASSERT(result != NULL);
Craig Tillere91ef682016-03-11 08:59:17 -0800124
Mark D. Rothb367c1b2016-10-24 12:59:07 -0700125 grpc_channel_args_destroy(result);
Craig Tillere91ef682016-03-11 08:59:17 -0800126 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test");
127 grpc_exec_ctx_finish(&exec_ctx);
128
129 grpc_shutdown();
130 gpr_mu_destroy(&g_mu);
131}