| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 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. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#include <grpc/grpc.h> |
| 22 |
#include <grpc/support/log.h> |
| 23 |
#include <grpc/support/sync.h> |
| 24 |
#include <grpc/support/time.h> |
| 25 |
|
| 26 |
#include "src/core/lib/channel/channel_args.h" |
| 27 |
#include "src/core/lib/gprpp/thd.h" |
| 28 |
#include "src/core/lib/gprpp/time.h" |
| 29 |
#include "test/core/end2end/cq_verifier.h" |
| 30 |
#include "test/core/end2end/end2end_tests.h" |
| 31 |
#include "test/core/util/test_config.h" |
| 32 |
|
| 33 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 34 |
|
| 35 |
typedef struct { |
| 36 |
gpr_event started; |
| 37 |
grpc_channel* channel; |
| 38 |
grpc_completion_queue* cq; |
| 39 |
} child_events; |
| 40 |
|
| 41 |
struct CallbackContext { |
| 42 |
grpc_completion_queue_functor functor; |
| 43 |
gpr_event finished; |
| 44 |
explicit CallbackContext(void (*cb)(grpc_completion_queue_functor* functor, |
| 45 |
int success)) { |
| 46 |
functor.functor_run = cb; |
| 47 |
functor.inlineable = false; |
| 48 |
gpr_event_init(&finished); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
static void child_thread(void* arg) { |
| 53 |
child_events* ce = static_cast<child_events*>(arg); |
| 54 |
grpc_event ev; |
| 55 |
gpr_event_set(&ce->started, reinterpret_cast<void*>(1)); |
| 56 |
gpr_log(GPR_DEBUG, "verifying"); |
| 57 |
ev = grpc_completion_queue_next(ce->cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), |
| 58 |
nullptr); |
| 59 |
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 60 |
GPR_ASSERT(ev.tag == tag(1)); |
| 61 |
GPR_ASSERT(ev.success == 0); |
| 62 |
} |
| 63 |
|
| 64 |
static void test_connectivity(grpc_end2end_test_config config) { |
| 65 |
grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr); |
| 66 |
grpc_connectivity_state state; |
| 67 |
grpc_core::CqVerifier cqv(f.cq); |
| 68 |
child_events ce; |
| 69 |
|
| 70 |
auto client_args = grpc_core::ChannelArgs() |
| 71 |
.Set(GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS, 1000) |
| 72 |
.Set(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, 1000) |
| 73 |
.Set(GRPC_ARG_MIN_RECONNECT_BACKOFF_MS, 5000) |
| 74 |
.ToC(); |
| 75 |
|
| 76 |
config.init_client(&f, client_args.get()); |
| 77 |
|
| 78 |
ce.channel = f.client; |
| 79 |
ce.cq = f.cq; |
| 80 |
gpr_event_init(&ce.started); |
| 81 |
grpc_core::Thread thd("grpc_connectivity", child_thread, &ce); |
| 82 |
thd.Start(); |
| 83 |
|
| 84 |
gpr_event_wait(&ce.started, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 85 |
|
| 86 |
/* channels should start life in IDLE, and stay there */ |
| 87 |
GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == |
| 88 |
GRPC_CHANNEL_IDLE); |
| 89 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100)); |
| 90 |
GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 0) == |
| 91 |
GRPC_CHANNEL_IDLE); |
| 92 |
|
| 93 |
/* start watching for a change */ |
| 94 |
gpr_log(GPR_DEBUG, "watching"); |
| 95 |
grpc_channel_watch_connectivity_state( |
| 96 |
f.client, GRPC_CHANNEL_IDLE, gpr_now(GPR_CLOCK_MONOTONIC), f.cq, tag(1)); |
| 97 |
|
| 98 |
/* eventually the child thread completion should trigger */ |
| 99 |
thd.Join(); |
| 100 |
|
| 101 |
/* check that we're still in idle, and start connecting */ |
| 102 |
GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) == |
| 103 |
GRPC_CHANNEL_IDLE); |
| 104 |
/* start watching for a change */ |
| 105 |
grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE, |
| 106 |
grpc_timeout_seconds_to_deadline(10), |
| 107 |
f.cq, tag(2)); |
| 108 |
|
| 109 |
/* and now the watch should trigger */ |
| 110 |
cqv.Expect(tag(2), true); |
| 111 |
cqv.Verify(); |
| 112 |
state = grpc_channel_check_connectivity_state(f.client, 0); |
| 113 |
GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 114 |
state == GRPC_CHANNEL_CONNECTING); |
| 115 |
|
| 116 |
/* quickly followed by a transition to TRANSIENT_FAILURE */ |
| 117 |
grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_CONNECTING, |
| 118 |
grpc_timeout_seconds_to_deadline(10), |
| 119 |
f.cq, tag(3)); |
| 120 |
cqv.Expect(tag(3), true); |
| 121 |
cqv.Verify(); |
| 122 |
state = grpc_channel_check_connectivity_state(f.client, 0); |
| 123 |
GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 124 |
state == GRPC_CHANNEL_CONNECTING); |
| 125 |
|
| 126 |
gpr_log(GPR_DEBUG, "*** STARTING SERVER ***"); |
| 127 |
|
| 128 |
/* now let's bring up a server to connect to */ |
| 129 |
config.init_server(&f, nullptr); |
| 130 |
|
| 131 |
gpr_log(GPR_DEBUG, "*** STARTED SERVER ***"); |
| 132 |
|
| 133 |
/* we'll go through some set of transitions (some might be missed), until |
| 134 |
READY is reached */ |
| 135 |
while (state != GRPC_CHANNEL_READY) { |
| 136 |
grpc_channel_watch_connectivity_state( |
| 137 |
f.client, state, grpc_timeout_seconds_to_deadline(10), f.cq, tag(4)); |
| 138 |
cqv.Expect(tag(4), true); |
| 139 |
cqv.Verify(grpc_core::Duration::Seconds(20)); |
| 140 |
state = grpc_channel_check_connectivity_state(f.client, 0); |
| 141 |
GPR_ASSERT(state == GRPC_CHANNEL_READY || |
| 142 |
state == GRPC_CHANNEL_CONNECTING || |
| 143 |
state == GRPC_CHANNEL_TRANSIENT_FAILURE); |
| 144 |
} |
| 145 |
|
| 146 |
/* bring down the server again */ |
| 147 |
/* we should go immediately to TRANSIENT_FAILURE */ |
| 148 |
gpr_log(GPR_DEBUG, "*** SHUTTING DOWN SERVER ***"); |
| 149 |
|
| 150 |
grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_READY, |
| 151 |
grpc_timeout_seconds_to_deadline(10), |
| 152 |
f.cq, tag(5)); |
| 153 |
|
| 154 |
grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead)); |
| 155 |
|
| 156 |
cqv.Expect(tag(5), true); |
| 157 |
cqv.Expect(tag(0xdead), true); |
| 158 |
cqv.Verify(); |
| 159 |
state = grpc_channel_check_connectivity_state(f.client, 0); |
| 160 |
GPR_ASSERT(state == GRPC_CHANNEL_TRANSIENT_FAILURE || |
| 161 |
state == GRPC_CHANNEL_CONNECTING || state == GRPC_CHANNEL_IDLE); |
| 162 |
|
| 163 |
/* cleanup server */ |
| 164 |
grpc_server_destroy(f.server); |
| 165 |
|
| 166 |
gpr_log(GPR_DEBUG, "*** SHUTDOWN SERVER ***"); |
| 167 |
|
| 168 |
grpc_channel_destroy(f.client); |
| 169 |
grpc_completion_queue_shutdown(f.cq); |
| 170 |
grpc_completion_queue_destroy(f.cq); |
| 171 |
|
| 172 |
config.tear_down_data(&f); |
| 173 |
} |
| 174 |
|
| 175 |
static void cb_watch_connectivity(grpc_completion_queue_functor* functor, |
| 176 |
int success) { |
| 177 |
CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor); |
| 178 |
|
| 179 |
gpr_log(GPR_DEBUG, "cb_watch_connectivity called, verifying"); |
| 180 |
|
| 181 |
/* callback must not have errors */ |
| 182 |
GPR_ASSERT(success != 0); |
| 183 |
|
| 184 |
gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1)); |
| 185 |
} |
| 186 |
|
| 187 |
static void cb_shutdown(grpc_completion_queue_functor* functor, |
| 188 |
int /*success*/) { |
| 189 |
CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor); |
| 190 |
|
| 191 |
gpr_log(GPR_DEBUG, "cb_shutdown called, nothing to do"); |
| 192 |
gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1)); |
| 193 |
} |
| 194 |
|
| 195 |
static void test_watch_connectivity_cq_callback( |
| 196 |
grpc_end2end_test_config config) { |
| 197 |
CallbackContext cb_ctx(cb_watch_connectivity); |
| 198 |
CallbackContext cb_shutdown_ctx(cb_shutdown); |
| 199 |
grpc_completion_queue* cq; |
| 200 |
grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr); |
| 201 |
|
| 202 |
config.init_client(&f, nullptr); |
| 203 |
|
| 204 |
/* start connecting */ |
| 205 |
grpc_channel_check_connectivity_state(f.client, 1); |
| 206 |
|
| 207 |
/* create the cq callback */ |
| 208 |
cq = grpc_completion_queue_create_for_callback(&cb_shutdown_ctx.functor, |
| 209 |
nullptr); |
| 210 |
|
| 211 |
/* start watching for any change, cb is immediately called |
| 212 |
* and no dead lock should be raised */ |
| 213 |
grpc_channel_watch_connectivity_state(f.client, GRPC_CHANNEL_IDLE, |
| 214 |
grpc_timeout_seconds_to_deadline(3), cq, |
| 215 |
&cb_ctx.functor); |
| 216 |
|
| 217 |
/* we just check that the callback was executed once notifying a connection |
| 218 |
* transition */ |
| 219 |
GPR_ASSERT(gpr_event_wait(&cb_ctx.finished, |
| 220 |
gpr_inf_future(GPR_CLOCK_MONOTONIC)) != nullptr); |
| 221 |
|
| 222 |
/* shutdown, since shutdown cb might be executed in a background thread |
| 223 |
* we actively wait till is executed. */ |
| 224 |
grpc_completion_queue_shutdown(cq); |
| 225 |
gpr_event_wait(&cb_shutdown_ctx.finished, |
| 226 |
gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 227 |
|
| 228 |
/* cleanup */ |
| 229 |
grpc_channel_destroy(f.client); |
| 230 |
grpc_completion_queue_destroy(cq); |
| 231 |
|
| 232 |
/* cq is not used in this test */ |
| 233 |
grpc_completion_queue_destroy(f.cq); |
| 234 |
|
| 235 |
config.tear_down_data(&f); |
| 236 |
} |
| 237 |
|
| 238 |
void connectivity(grpc_end2end_test_config config) { |
| 239 |
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION); |
| 240 |
test_connectivity(config); |
| 241 |
test_watch_connectivity_cq_callback(config); |
| 242 |
} |
| 243 |
|
| 244 |
void connectivity_pre_init(void) {} |