num_external_connectivity_watchers_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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/grpc.h>
20 #include <grpc/grpc_security.h>
21 #include <grpc/support/alloc.h>
22 #include <grpc/support/log.h>
23 
27 #include "src/core/lib/gprpp/thd.h"
30 #include "test/core/util/port.h"
32 
33 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
34 
35 typedef struct test_fixture {
36  const char* name;
37  grpc_channel* (*create_channel)(const char* addr);
38 } test_fixture;
39 
40 static size_t next_tag = 1;
41 
47 
49  connect_deadline, cq,
50  reinterpret_cast<void*>(next_tag++));
51  gpr_log(GPR_DEBUG, "number of active connect watchers: %d",
53 }
54 
59 
60  /* expect watch_connectivity_state to end with a timeout */
62  GPR_ASSERT(ev.success == false);
65 }
66 
67 /* Test and use the "num_external_watchers" call to make sure
68  * that "connectivity watcher" structs are free'd just after, if
69  * their corresponding timeouts occur. */
70 static void run_timeouts_test(const test_fixture* fixture) {
71  gpr_log(GPR_INFO, "TEST: %s", fixture->name);
72 
73  grpc_init();
76 
77  grpc_channel* channel = fixture->create_channel(addr.c_str());
79 
80  /* start 1 watcher and then let it time out */
84 
85  /* start 3 watchers and then let them all time out */
86  for (size_t i = 1; i <= 3; i++) {
88  }
89  for (size_t i = 1; i <= 3; i++) {
91  }
93 
94  /* start 3 watchers, see one time out, start another 3, and then see them all
95  * time out */
96  for (size_t i = 1; i <= 3; i++) {
98  }
100  for (size_t i = 3; i <= 5; i++) {
102  }
103  for (size_t i = 1; i <= 5; i++) {
105  }
107 
111  nullptr)
114 
115  grpc_shutdown();
116 }
117 
118 /* An edge scenario; sets channel state to explicitly, and outside
119  * of a polling call. */
121  const test_fixture* fixture) {
122  gpr_log(GPR_INFO, "TEST: %s", fixture->name);
123 
124  grpc_init();
125  std::string addr =
127 
128  grpc_channel* channel = fixture->create_channel(addr.c_str());
130 
131  /* start 1 watcher and then shut down the channel before the timer goes off */
133 
134  /* expecting a 30 second timeout to go off much later than the shutdown. */
135  gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30);
138 
140  connect_deadline, cq,
141  reinterpret_cast<void*>(1));
143 
147  /* expect success with a state transition to CHANNEL_SHUTDOWN */
148  GPR_ASSERT(ev.success == true);
149 
152  nullptr)
155 
156  grpc_shutdown();
157 }
158 
161  grpc_channel* channel = grpc_channel_create(addr, creds, nullptr);
163  return channel;
164 }
165 
166 static const test_fixture insecure_test = {
167  "insecure",
169 };
170 
172  grpc_slice ca_slice;
173  GPR_ASSERT(GRPC_LOG_IF_ERROR("load_file",
174  grpc_load_file(CA_CERT_PATH, 1, &ca_slice)));
175  const char* test_root_cert =
176  reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice);
177  grpc_channel_credentials* ssl_creds =
178  grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr);
179  grpc_slice_unref(ca_slice);
180  grpc_arg ssl_name_override = {
182  const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
183  {const_cast<char*>("foo.test.google.fr")}};
184  grpc_channel_args* new_client_args =
185  grpc_channel_args_copy_and_add(nullptr, &ssl_name_override, 1);
186  grpc_channel* channel = grpc_channel_create(addr, ssl_creds, new_client_args);
187  {
189  grpc_channel_args_destroy(new_client_args);
190  }
192  return channel;
193 }
194 
195 static const test_fixture secure_test = {
196  "secure",
198 };
199 
200 int main(int argc, char** argv) {
201  grpc::testing::TestEnvironment env(&argc, argv);
202 
205 
208 }
grpc_arg
Definition: grpc_types.h:103
run_timeouts_test
static void run_timeouts_test(const test_fixture *fixture)
Definition: num_external_connectivity_watchers_test.cc:70
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
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
port.h
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
generate.env
env
Definition: generate.py:37
load_file.h
GRPC_ARG_STRING
@ GRPC_ARG_STRING
Definition: grpc_types.h:80
test_fixture::name
const char * name
Definition: num_external_connectivity_watchers_test.cc:36
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
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Definition: grpc_types.h:554
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
channel_idle_poll_for_timeout
static void channel_idle_poll_for_timeout(grpc_channel *channel, grpc_completion_queue *cq)
Definition: num_external_connectivity_watchers_test.cc:55
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
next_tag
static size_t next_tag
Definition: num_external_connectivity_watchers_test.cc:40
grpc_security.h
insecure_test_create_channel
static grpc_channel * insecure_test_create_channel(const char *addr)
Definition: num_external_connectivity_watchers_test.cc:159
grpc_channel_args
Definition: grpc_types.h:132
grpc_ssl_credentials_create
GRPCAPI grpc_channel_credentials * grpc_ssl_credentials_create(const char *pem_root_certs, grpc_ssl_pem_key_cert_pair *pem_key_cert_pair, const verify_peer_options *verify_options, void *reserved)
Definition: ssl_credentials.cc:132
main
int main(int argc, char **argv)
Definition: num_external_connectivity_watchers_test.cc:200
memory.h
test_fixture
Definition: num_external_connectivity_watchers_test.cc:35
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_channel_num_external_connectivity_watchers
GRPCAPI int grpc_channel_num_external_connectivity_watchers(grpc_channel *channel)
Definition: channel_connectivity.cc:79
GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
#define GRPC_SSL_TARGET_NAME_OVERRIDE_ARG
Definition: grpc_types.h:278
secure_test
static const test_fixture secure_test
Definition: num_external_connectivity_watchers_test.cc:195
fixture
static const char fixture[]
Definition: test-fs-copyfile.c:36
insecure_test
static const test_fixture insecure_test
Definition: num_external_connectivity_watchers_test.cc:166
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
test_fixture
struct test_fixture test_fixture
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
CA_CERT_PATH
#define CA_CERT_PATH
Definition: num_external_connectivity_watchers_test.cc:33
grpc.h
grpc_core::JoinHostPort
std::string JoinHostPort(absl::string_view host, int port)
Definition: host_port.cc:32
grpc_insecure_credentials_create
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
Definition: core/lib/security/credentials/insecure/insecure_credentials.cc:64
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
GRPC_CHANNEL_IDLE
@ GRPC_CHANNEL_IDLE
Definition: include/grpc/impl/codegen/connectivity_state.h:32
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
host_port.h
grpc_pick_unused_port_or_die
int grpc_pick_unused_port_or_die(void)
grpc_core::ExecCtx
Definition: exec_ctx.h:97
test_config.h
grpc_channel_credentials_release
GRPCAPI void grpc_channel_credentials_release(grpc_channel_credentials *creds)
Definition: credentials.cc:36
grpc_channel_create
GRPCAPI grpc_channel * grpc_channel_create(const char *target, grpc_channel_credentials *creds, const grpc_channel_args *args)
Definition: chttp2_connector.cc:366
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
thd.h
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
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
grpc_channel_destroy
GRPCAPI void grpc_channel_destroy(grpc_channel *channel)
Definition: channel.cc:437
exec_ctx.h
grpc_channel
struct grpc_channel grpc_channel
Definition: grpc_types.h:62
channel_args.h
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
channel_idle_start_watch
static void channel_idle_start_watch(grpc_channel *channel, grpc_completion_queue *cq)
Definition: num_external_connectivity_watchers_test.cc:42
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
test_root_cert
const char test_root_cert[]
run_channel_shutdown_before_timeout_test
static void run_channel_shutdown_before_timeout_test(const test_fixture *fixture)
Definition: num_external_connectivity_watchers_test.cc:120
grpc_completion_queue_create_for_next
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_next(void *reserved)
Definition: completion_queue_factory.cc:62
secure_test_create_channel
static grpc_channel * secure_test_create_channel(const char *addr)
Definition: num_external_connectivity_watchers_test.cc:171
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
gpr_timespec
Definition: gpr_types.h:50
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc_event::success
int success
Definition: grpc_types.h:572
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_channel_credentials
Definition: src/core/lib/security/credentials/credentials.h:96
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
grpc_channel_args_copy_and_add
grpc_channel_args * grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:224
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:33