Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 4 | * 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 Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/grpc.h> |
| 37 | #include <grpc/support/alloc.h> |
| 38 | |
Mark D. Roth | 2137cd8 | 2016-09-14 09:04:00 -0700 | [diff] [blame] | 39 | #include "src/core/ext/client_channel/resolver_registry.h" |
Mark D. Roth | b367c1b | 2016-10-24 12:59:07 -0700 | [diff] [blame] | 40 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 41 | #include "src/core/lib/iomgr/resolve_address.h" |
| 42 | #include "src/core/lib/iomgr/timer.h" |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 43 | #include "test/core/util/test_config.h" |
| 44 | |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 45 | static gpr_mu g_mu; |
| 46 | static bool g_fail_resolution = true; |
| 47 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 48 | static grpc_error *my_resolve_address(const char *name, const char *addr, |
| 49 | grpc_resolved_addresses **addrs) { |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 50 | 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 Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 55 | return GRPC_ERROR_CREATE("Forced Failure"); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 56 | } else { |
| 57 | gpr_mu_unlock(&g_mu); |
David Garcia Quintas | e77e9b3 | 2016-05-10 15:16:24 -0700 | [diff] [blame] | 58 | *addrs = gpr_malloc(sizeof(**addrs)); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 59 | (*addrs)->naddrs = 1; |
| 60 | (*addrs)->addrs = gpr_malloc(sizeof(*(*addrs)->addrs)); |
| 61 | (*addrs)->addrs[0].len = 123; |
| 62 | return GRPC_ERROR_NONE; |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame^] | 66 | static grpc_resolver *create_resolver(grpc_exec_ctx *exec_ctx, |
| 67 | const char *name) { |
Craig Tiller | 65938df | 2016-03-31 13:08:49 -0700 | [diff] [blame] | 68 | grpc_resolver_factory *factory = grpc_resolver_factory_lookup("dns"); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 69 | 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 Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 74 | grpc_resolver *resolver = |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame^] | 75 | grpc_resolver_factory_create_resolver(exec_ctx, factory, &args); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 76 | grpc_resolver_factory_unref(factory); |
| 77 | grpc_uri_destroy(uri); |
| 78 | return resolver; |
| 79 | } |
| 80 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 81 | static void on_done(grpc_exec_ctx *exec_ctx, void *ev, grpc_error *error) { |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 82 | gpr_event_set(ev, (void *)1); |
| 83 | } |
| 84 | |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 85 | // interleave waiting for an event with a timer check |
| 86 | static 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 Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 99 | int 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. Roth | b367c1b | 2016-10-24 12:59:07 -0700 | [diff] [blame] | 105 | grpc_channel_args *result = (grpc_channel_args *)1; |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 106 | |
| 107 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Yuchen Zeng | 63e3e3b | 2016-12-15 12:06:33 -0800 | [diff] [blame^] | 108 | grpc_resolver *resolver = create_resolver(&exec_ctx, "dns:test"); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 109 | gpr_event ev1; |
| 110 | gpr_event_init(&ev1); |
Mark D. Roth | 1edf652 | 2016-08-23 12:35:40 -0700 | [diff] [blame] | 111 | grpc_resolver_next(&exec_ctx, resolver, &result, |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 112 | grpc_closure_create(on_done, &ev1)); |
| 113 | grpc_exec_ctx_flush(&exec_ctx); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 114 | GPR_ASSERT(wait_loop(5, &ev1)); |
Mark D. Roth | 1edf652 | 2016-08-23 12:35:40 -0700 | [diff] [blame] | 115 | GPR_ASSERT(result == NULL); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 116 | |
| 117 | gpr_event ev2; |
| 118 | gpr_event_init(&ev2); |
Mark D. Roth | 1edf652 | 2016-08-23 12:35:40 -0700 | [diff] [blame] | 119 | grpc_resolver_next(&exec_ctx, resolver, &result, |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 120 | grpc_closure_create(on_done, &ev2)); |
| 121 | grpc_exec_ctx_flush(&exec_ctx); |
Craig Tiller | 1e55bd4 | 2016-03-11 09:47:43 -0800 | [diff] [blame] | 122 | GPR_ASSERT(wait_loop(30, &ev2)); |
Mark D. Roth | 1edf652 | 2016-08-23 12:35:40 -0700 | [diff] [blame] | 123 | GPR_ASSERT(result != NULL); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 124 | |
Mark D. Roth | b367c1b | 2016-10-24 12:59:07 -0700 | [diff] [blame] | 125 | grpc_channel_args_destroy(result); |
Craig Tiller | e91ef68 | 2016-03-11 08:59:17 -0800 | [diff] [blame] | 126 | 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 | } |