connectivity.cc
Go to the documentation of this file.
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 <grpc/support/log.h>
20 #include <grpc/support/sync.h>
21 #include <grpc/support/time.h>
22 
23 #include "src/core/lib/gprpp/thd.h"
26 
27 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
28 
29 typedef struct {
33 } child_events;
34 
39  int success)) {
41  functor.inlineable = false;
43  }
44 };
45 
46 static void child_thread(void* arg) {
47  child_events* ce = static_cast<child_events*>(arg);
48  grpc_event ev;
49  gpr_event_set(&ce->started, reinterpret_cast<void*>(1));
50  gpr_log(GPR_DEBUG, "verifying");
52  nullptr);
54  GPR_ASSERT(ev.tag == tag(1));
55  GPR_ASSERT(ev.success == 0);
56 }
57 
59  grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
61  cq_verifier* cqv = cq_verifier_create(f.cq);
62  child_events ce;
63 
64  grpc_channel_args client_args;
65  grpc_arg arg_array[1];
66  arg_array[0].type = GRPC_ARG_INTEGER;
67  arg_array[0].key =
68  const_cast<char*>("grpc.testing.fixed_reconnect_backoff_ms");
69  arg_array[0].value.integer = 1000;
70  client_args.args = arg_array;
71  client_args.num_args = 1;
72 
73  config.init_client(&f, &client_args);
74 
75  ce.channel = f.client;
76  ce.cq = f.cq;
78  grpc_core::Thread thd("grpc_connectivity", child_thread, &ce);
79  thd.Start();
80 
82 
83  /* channels should start life in IDLE, and stay there */
89 
90  /* start watching for a change */
91  gpr_log(GPR_DEBUG, "watching");
94 
95  /* eventually the child thread completion should trigger */
96  thd.Join();
97 
98  /* check that we're still in idle, and start connecting */
101  /* start watching for a change */
104  f.cq, tag(2));
105 
106  /* and now the watch should trigger */
107  CQ_EXPECT_COMPLETION(cqv, tag(2), 1);
108  cq_verify(cqv);
112 
113  /* quickly followed by a transition to TRANSIENT_FAILURE */
116  f.cq, tag(3));
117  CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
118  cq_verify(cqv);
122 
123  gpr_log(GPR_DEBUG, "*** STARTING SERVER ***");
124 
125  /* now let's bring up a server to connect to */
126  config.init_server(&f, nullptr);
127 
128  gpr_log(GPR_DEBUG, "*** STARTED SERVER ***");
129 
130  /* we'll go through some set of transitions (some might be missed), until
131  READY is reached */
132  while (state != GRPC_CHANNEL_READY) {
134  f.client, state, grpc_timeout_seconds_to_deadline(3), f.cq, tag(4));
135  CQ_EXPECT_COMPLETION(cqv, tag(4), 1);
136  cq_verify(cqv);
141  }
142 
143  /* bring down the server again */
144  /* we should go immediately to TRANSIENT_FAILURE */
145  gpr_log(GPR_DEBUG, "*** SHUTTING DOWN SERVER ***");
146 
149  f.cq, tag(5));
150 
151  grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
152 
153  CQ_EXPECT_COMPLETION(cqv, tag(5), 1);
154  CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
155  cq_verify(cqv);
159 
160  /* cleanup server */
161  grpc_server_destroy(f.server);
162 
163  gpr_log(GPR_DEBUG, "*** SHUTDOWN SERVER ***");
164 
165  grpc_channel_destroy(f.client);
168 
169  config.tear_down_data(&f);
170 
171  cq_verifier_destroy(cqv);
172 }
173 
175  int success) {
176  CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor);
177 
178  gpr_log(GPR_DEBUG, "cb_watch_connectivity called, verifying");
179 
180  /* callback must not have errors */
181  GPR_ASSERT(success != 0);
182 
183  gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1));
184 }
185 
187  int /*success*/) {
188  CallbackContext* cb_ctx = reinterpret_cast<CallbackContext*>(functor);
189 
190  gpr_log(GPR_DEBUG, "cb_shutdown called, nothing to do");
191  gpr_event_set(&cb_ctx->finished, reinterpret_cast<void*>(1));
192 }
193 
197  CallbackContext cb_shutdown_ctx(cb_shutdown);
199  grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
200 
201  config.init_client(&f, nullptr);
202 
203  /* start connecting */
205 
206  /* create the cq callback */
208  nullptr);
209 
210  /* start watching for any change, cb is immediately called
211  * and no dead lock should be raised */
214  &cb_ctx.functor);
215 
216  /* we just check that the callback was executed once notifying a connection
217  * transition */
219  gpr_inf_future(GPR_CLOCK_MONOTONIC)) != nullptr);
220 
221  /* shutdown, since shutdown cb might be executed in a background thread
222  * we actively wait till is executed. */
224  gpr_event_wait(&cb_shutdown_ctx.finished,
226 
227  /* cleanup */
228  grpc_channel_destroy(f.client);
230 
231  /* cq is not used in this test */
233 
234  config.tear_down_data(&f);
235 }
236 
241 }
242 
grpc_arg
Definition: grpc_types.h:103
CallbackContext
Definition: connectivity.cc:35
GRPC_CHANNEL_READY
@ GRPC_CHANNEL_READY
Definition: include/grpc/impl/codegen/connectivity_state.h:36
child_thread
static void child_thread(void *arg)
Definition: connectivity.cc:46
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
grpc_completion_queue_create_for_callback
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_callback(grpc_completion_queue_functor *shutdown_callback, void *reserved)
Definition: completion_queue_factory.cc:76
GRPC_ARG_INTEGER
@ GRPC_ARG_INTEGER
Definition: grpc_types.h:81
grpc_arg::value
union grpc_arg::grpc_arg_value value
child_events::cq
grpc_completion_queue * cq
Definition: connectivity.cc:32
tag
static void * tag(intptr_t t)
Definition: connectivity.cc:27
grpc_channel_check_connectivity_state
GRPCAPI grpc_connectivity_state grpc_channel_check_connectivity_state(grpc_channel *channel, int try_to_connect)
Definition: channel_connectivity.cc:56
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
GRPC_CHANNEL_TRANSIENT_FAILURE
@ GRPC_CHANNEL_TRANSIENT_FAILURE
Definition: include/grpc/impl/codegen/connectivity_state.h:38
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
time.h
grpc_end2end_test_config
Definition: end2end_tests.h:53
grpc_channel_args
Definition: grpc_types.h:132
grpc_connectivity_state
grpc_connectivity_state
Definition: include/grpc/impl/codegen/connectivity_state.h:30
grpc_end2end_test_fixture
Definition: end2end_tests.h:46
FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION
#define FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION
Definition: end2end_tests.h:28
test_watch_connectivity_cq_callback
static void test_watch_connectivity_cq_callback(grpc_end2end_test_config config)
Definition: connectivity.cc:194
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_completion_queue_functor *, int)
Definition: grpc_types.h:778
connectivity
void connectivity(grpc_end2end_test_config config)
Definition: connectivity.cc:237
cb_watch_connectivity
static void cb_watch_connectivity(grpc_completion_queue_functor *functor, int success)
Definition: connectivity.cc:174
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
cq_verifier_destroy
void cq_verifier_destroy(cq_verifier *v)
Definition: cq_verifier.cc:92
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
GRPC_CHANNEL_IDLE
@ GRPC_CHANNEL_IDLE
Definition: include/grpc/impl/codegen/connectivity_state.h:32
grpc_channel_args::num_args
size_t num_args
Definition: grpc_types.h:133
arg
Definition: cmdline.cc:40
cq_verifier_create
cq_verifier * cq_verifier_create(grpc_completion_queue *cq)
Definition: cq_verifier.cc:86
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
cq_verifier
Definition: cq_verifier.cc:76
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
child_events::started
gpr_event started
Definition: connectivity.cc:30
CQ_EXPECT_COMPLETION
#define CQ_EXPECT_COMPLETION(v, tag, success)
Definition: cq_verifier.h:58
end2end_tests.h
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
cb_shutdown
static void cb_shutdown(grpc_completion_queue_functor *functor, int)
Definition: connectivity.cc:186
GRPC_CHANNEL_CONNECTING
@ GRPC_CHANNEL_CONNECTING
Definition: include/grpc/impl/codegen/connectivity_state.h:34
gpr_event
Definition: impl/codegen/sync_generic.h:31
cq_verifier.h
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
grpc_arg::key
char * key
Definition: grpc_types.h:105
thd.h
grpc_server_shutdown_and_notify
GRPCAPI void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Definition: src/core/lib/surface/server.cc:1503
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
cq_verify
void cq_verify(cq_verifier *v, int timeout_sec)
Definition: cq_verifier.cc:268
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
grpc_completion_queue_functor
Definition: grpc_types.h:773
arg
struct arg arg
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
grpc_core::Thread
Definition: thd.h:43
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
test_connectivity
static void test_connectivity(grpc_end2end_test_config config)
Definition: connectivity.cc:58
child_events
Definition: connectivity.cc:29
grpc_channel_watch_connectivity_state
GRPCAPI void grpc_channel_watch_connectivity_state(grpc_channel *channel, grpc_connectivity_state last_observed_state, gpr_timespec deadline, grpc_completion_queue *cq, void *tag)
Definition: channel_connectivity.cc:227
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
profile_analyzer.thd
thd
Definition: profile_analyzer.py:168
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
grpc_arg::grpc_arg_value::integer
int integer
Definition: grpc_types.h:108
grpc_arg::type
grpc_arg_type type
Definition: grpc_types.h:104
CallbackContext::finished
gpr_event finished
Definition: connectivity.cc:37
grpc_channel_args::args
grpc_arg * args
Definition: grpc_types.h:134
sync.h
grpc_event::success
int success
Definition: grpc_types.h:572
child_events::channel
grpc_channel * channel
Definition: connectivity.cc:31
CallbackContext::functor
grpc_completion_queue_functor functor
Definition: connectivity.cc:36
grpc_event::tag
void * tag
Definition: grpc_types.h:576
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
connectivity_pre_init
void connectivity_pre_init(void)
Definition: connectivity.cc:243
CallbackContext::CallbackContext
CallbackContext(void(*cb)(grpc_completion_queue_functor *functor, int success))
Definition: connectivity.cc:38
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87
grpc_completion_queue_functor::inlineable
int inlineable
Definition: grpc_types.h:782


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01