OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 #include "test/core/end2end/end2end_tests.h" |
| 35 |
| 36 #include <grpc/support/log.h> |
| 37 #include <grpc/support/sync.h> |
| 38 #include <grpc/support/thd.h> |
| 39 #include <grpc/support/time.h> |
| 40 |
| 41 #include "test/core/end2end/cq_verifier.h" |
| 42 |
| 43 static void *tag(intptr_t t) { return (void *)t; } |
| 44 |
| 45 typedef struct { |
| 46 gpr_event started; |
| 47 grpc_channel *channel; |
| 48 grpc_completion_queue *cq; |
| 49 } child_events; |
| 50 |
| 51 static void child_thread(void *arg) { |
| 52 child_events *ce = arg; |
| 53 grpc_event ev; |
| 54 gpr_event_set(&ce->started, (void *)1); |
| 55 gpr_log(GPR_DEBUG, "verifying"); |
| 56 ev = grpc_completion_queue_next(ce->cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), |
| 57 NULL); |
| 58 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 59 GPR_ASSERT(ev.tag == tag(1)); |
| 60 GPR_ASSERT(ev.success == 0); |
| 61 } |
| 62 |
| 63 static void test_connectivity(grpc_end2end_test_config config) { |
| 64 grpc_end2end_test_fixture f = config.create_fixture(NULL, NULL); |
| 65 grpc_connectivity_state state; |
| 66 cq_verifier *cqv = cq_verifier_create(f.cq); |
| 67 child_events ce; |
| 68 gpr_thd_options thdopt = gpr_thd_options_default(); |
| 69 gpr_thd_id thdid; |
| 70 |
| 71 config.init_client(&f, NULL); |
| 72 |
| 73 ce.channel = f.client; |
| 74 ce.cq = f.cq; |
| 75 gpr_event_init(&ce.started); |
| 76 gpr_thd_options_set_joinable(&thdopt); |
| 77 GPR_ASSERT(gpr_thd_new(&thdid, child_thread, &ce, &thdopt)); |
| 78 |
| 79 gpr_event_wait(&ce.started, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 80 |
| 81 /* channels should start life in IDLE, and stay there */ |
| 82 GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == |
| 83 GRPC_CHANNEL_IDLE); |
| 84 gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100)); |
| 85 GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == |
| 86 GRPC_CHANNEL_IDLE); |
| 87 |
| 88 /* start watching for a change */ |
| 89 gpr_log(GPR_DEBUG, "watching"); |
| 90 grpc_channel_watch_connectivity_state( |
| 91 f.client, GRPC_CHANNEL_IDLE, gpr_now(GPR_CLOCK_MONOTONIC), f.cq, tag(1)); |
| 92 |
| 93 /* eventually the child thread completion should trigger */ |
| 94 gpr_thd_join(thdid); |
| 95 |
| 96 /* check that we're still in idle, and start connecting */ |
| 97 GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) == |
| 98 GRPC_CHANNEL_IDLE); |
| 99 /* start watching for a change */ |
| 100 grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE, |
| 101 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), |
| 102 f.cq, tag(2)); |
| 103 |
| 104 /* and now the watch should trigger */ |
| 105 cq_expect_completion(cqv, tag(2), 1); |
| 106 cq_verify(cqv); |
| 107 state = grpc_channel_check_connectivity_state(f.client, 0); |
| 108 GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 109 state == GRPC_CHANNEL_CONNECTING); |
| 110 |
| 111 /* quickly followed by a transition to TRANSIENT_FAILURE */ |
| 112 grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_CONNECTING, |
| 113 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), |
| 114 f.cq, tag(3)); |
| 115 cq_expect_completion(cqv, tag(3), 1); |
| 116 cq_verify(cqv); |
| 117 state = grpc_channel_check_connectivity_state(f.client, 0); |
| 118 GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 119 state == GRPC_CHANNEL_CONNECTING); |
| 120 |
| 121 gpr_log(GPR_DEBUG, "*** STARTING SERVER ***"); |
| 122 |
| 123 /* now let's bring up a server to connect to */ |
| 124 config.init_server(&f, NULL); |
| 125 |
| 126 gpr_log(GPR_DEBUG, "*** STARTED SERVER ***"); |
| 127 |
| 128 /* we'll go through some set of transitions (some might be missed), until |
| 129 READY is reached */ |
| 130 while (state != GRPC_CHANNEL_READY) { |
| 131 grpc_channel_watch_connectivity_state( |
| 132 f.client, state, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), f.cq, tag(4)); |
| 133 cq_expect_completion(cqv, tag(4), 1); |
| 134 cq_verify(cqv); |
| 135 state = grpc_channel_check_connectivity_state(f.client, 0); |
| 136 GPR_ASSERT(state == GRPC_CHANNEL_READY || |
| 137 state == GRPC_CHANNEL_CONNECTING || |
| 138 state == GRPC_CHANNEL_TRANSIENT_FAILURE); |
| 139 } |
| 140 |
| 141 /* bring down the server again */ |
| 142 /* we should go immediately to TRANSIENT_FAILURE */ |
| 143 gpr_log(GPR_DEBUG, "*** SHUTTING DOWN SERVER ***"); |
| 144 |
| 145 grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_READY, |
| 146 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(3), |
| 147 f.cq, tag(5)); |
| 148 |
| 149 grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 150 |
| 151 cq_expect_completion(cqv, tag(5), 1); |
| 152 cq_expect_completion(cqv, tag(0xdead), 1); |
| 153 cq_verify(cqv); |
| 154 state = grpc_channel_check_connectivity_state(f.client, 0); |
| 155 GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 156 state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_IDLE); |
| 157 |
| 158 /* cleanup server */ |
| 159 grpc_server_destroy(f.server); |
| 160 |
| 161 gpr_log(GPR_DEBUG, "*** SHUTDOWN SERVER ***"); |
| 162 |
| 163 grpc_channel_destroy(f.client); |
| 164 grpc_completion_queue_shutdown(f.cq); |
| 165 grpc_completion_queue_destroy(f.cq); |
| 166 config.tear_down_data(&f); |
| 167 |
| 168 cq_verifier_destroy(cqv); |
| 169 } |
| 170 |
| 171 void connectivity(grpc_end2end_test_config config) { |
| 172 GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 173 test_connectivity(config); |
| 174 } |
OLD | NEW |