| 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 <stddef.h> |
| 20 |
|
| 21 |
#include <string> |
| 22 |
|
| 23 |
#include "gtest/gtest.h" |
| 24 |
|
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/grpc_security.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/time.h> |
| 30 |
|
| 31 |
#include "src/core/lib/channel/channel_args.h" |
| 32 |
#include "src/core/lib/gprpp/host_port.h" |
| 33 |
#include "src/core/lib/iomgr/error.h" |
| 34 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 35 |
#include "src/core/lib/iomgr/load_file.h" |
| 36 |
#include "test/core/util/port.h" |
| 37 |
#include "test/core/util/test_config.h" |
| 38 |
|
| 39 |
#define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem" |
| 40 |
|
| 41 |
typedef struct test_fixture { |
| 42 |
const char* name; |
| 43 |
grpc_channel* (*create_channel)(const char* addr); |
| 44 |
} test_fixture; |
| 45 |
|
| 46 |
static size_t next_tag = 1; |
| 47 |
|
| 48 |
static void channel_idle_start_watch(grpc_channel* channel, |
| 49 |
grpc_completion_queue* cq) { |
| 50 |
gpr_timespec connect_deadline = grpc_timeout_milliseconds_to_deadline(1); |
| 51 |
ASSERT_EQ(grpc_channel_check_connectivity_state(channel, 0), |
| 52 |
GRPC_CHANNEL_IDLE); |
| 53 |
|
| 54 |
grpc_channel_watch_connectivity_state(channel, GRPC_CHANNEL_IDLE, |
| 55 |
connect_deadline, cq, |
| 56 |
reinterpret_cast<void*>(next_tag++)); |
| 57 |
gpr_log(GPR_DEBUG, "number of active connect watchers: %d", |
| 58 |
grpc_channel_num_external_connectivity_watchers(channel)); |
| 59 |
} |
| 60 |
|
| 61 |
static void channel_idle_poll_for_timeout(grpc_channel* channel, |
| 62 |
grpc_completion_queue* cq) { |
| 63 |
grpc_event ev = grpc_completion_queue_next( |
| 64 |
cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 65 |
|
| 66 |
/* expect watch_connectivity_state to end with a timeout */ |
| 67 |
ASSERT_EQ(ev.type, GRPC_OP_COMPLETE); |
| 68 |
ASSERT_EQ(ev.success, false); |
| 69 |
ASSERT_EQ(grpc_channel_check_connectivity_state(channel, 0), |
| 70 |
GRPC_CHANNEL_IDLE); |
| 71 |
} |
| 72 |
|
| 73 |
/* Test and use the "num_external_watchers" call to make sure |
| 74 |
* that "connectivity watcher" structs are free'd just after, if |
| 75 |
* their corresponding timeouts occur. */ |
| 76 |
static void run_timeouts_test(const test_fixture* fixture) { |
| 77 |
gpr_log(GPR_INFO, "TEST: %s", fixture->name); |
| 78 |
|
| 79 |
grpc_init(); |
| 80 |
std::string addr = |
| 81 |
grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die()); |
| 82 |
|
| 83 |
grpc_channel* channel = fixture->create_channel(addr.c_str()); |
| 84 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 85 |
|
| 86 |
/* start 1 watcher and then let it time out */ |
| 87 |
channel_idle_start_watch(channel, cq); |
| 88 |
channel_idle_poll_for_timeout(channel, cq); |
| 89 |
ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(channel), 0); |
| 90 |
|
| 91 |
/* start 3 watchers and then let them all time out */ |
| 92 |
for (size_t i = 1; i <= 3; i++) { |
| 93 |
channel_idle_start_watch(channel, cq); |
| 94 |
} |
| 95 |
for (size_t i = 1; i <= 3; i++) { |
| 96 |
channel_idle_poll_for_timeout(channel, cq); |
| 97 |
} |
| 98 |
ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(channel), 0); |
| 99 |
|
| 100 |
/* start 3 watchers, see one time out, start another 3, and then see them all |
| 101 |
* time out */ |
| 102 |
for (size_t i = 1; i <= 3; i++) { |
| 103 |
channel_idle_start_watch(channel, cq); |
| 104 |
} |
| 105 |
channel_idle_poll_for_timeout(channel, cq); |
| 106 |
for (size_t i = 3; i <= 5; i++) { |
| 107 |
channel_idle_start_watch(channel, cq); |
| 108 |
} |
| 109 |
for (size_t i = 1; i <= 5; i++) { |
| 110 |
channel_idle_poll_for_timeout(channel, cq); |
| 111 |
} |
| 112 |
ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(channel), 0); |
| 113 |
|
| 114 |
grpc_channel_destroy(channel); |
| 115 |
grpc_completion_queue_shutdown(cq); |
| 116 |
ASSERT_EQ(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 117 |
nullptr) |
| 118 |
.type, |
| 119 |
GRPC_QUEUE_SHUTDOWN); |
| 120 |
grpc_completion_queue_destroy(cq); |
| 121 |
|
| 122 |
grpc_shutdown(); |
| 123 |
} |
| 124 |
|
| 125 |
/* An edge scenario; sets channel state to explicitly, and outside |
| 126 |
* of a polling call. */ |
| 127 |
static void run_channel_shutdown_before_timeout_test( |
| 128 |
const test_fixture* fixture) { |
| 129 |
gpr_log(GPR_INFO, "TEST: %s", fixture->name); |
| 130 |
|
| 131 |
grpc_init(); |
| 132 |
std::string addr = |
| 133 |
grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die()); |
| 134 |
|
| 135 |
grpc_channel* channel = fixture->create_channel(addr.c_str()); |
| 136 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 137 |
|
| 138 |
/* start 1 watcher and then shut down the channel before the timer goes off */ |
| 139 |
ASSERT_EQ(grpc_channel_num_external_connectivity_watchers(channel), 0); |
| 140 |
|
| 141 |
/* expecting a 30 second timeout to go off much later than the shutdown. */ |
| 142 |
gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline(30); |
| 143 |
ASSERT_EQ(grpc_channel_check_connectivity_state(channel, 0), |
| 144 |
GRPC_CHANNEL_IDLE); |
| 145 |
|
| 146 |
grpc_channel_watch_connectivity_state(channel, GRPC_CHANNEL_IDLE, |
| 147 |
connect_deadline, cq, |
| 148 |
reinterpret_cast<void*>(1)); |
| 149 |
grpc_channel_destroy(channel); |
| 150 |
|
| 151 |
grpc_event ev = grpc_completion_queue_next( |
| 152 |
cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 153 |
ASSERT_EQ(ev.type, GRPC_OP_COMPLETE); |
| 154 |
/* expect success with a state transition to CHANNEL_SHUTDOWN */ |
| 155 |
ASSERT_EQ(ev.success, true); |
| 156 |
|
| 157 |
grpc_completion_queue_shutdown(cq); |
| 158 |
ASSERT_EQ(grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 159 |
nullptr) |
| 160 |
.type, |
| 161 |
GRPC_QUEUE_SHUTDOWN); |
| 162 |
grpc_completion_queue_destroy(cq); |
| 163 |
|
| 164 |
grpc_shutdown(); |
| 165 |
} |
| 166 |
|
| 167 |
static grpc_channel* insecure_test_create_channel(const char* addr) { |
| 168 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 169 |
grpc_channel* channel = grpc_channel_create(addr, creds, nullptr); |
| 170 |
grpc_channel_credentials_release(creds); |
| 171 |
return channel; |
| 172 |
} |
| 173 |
|
| 174 |
static const test_fixture insecure_test = { |
| 175 |
"insecure", |
| 176 |
insecure_test_create_channel, |
| 177 |
}; |
| 178 |
|
| 179 |
static grpc_channel* secure_test_create_channel(const char* addr) { |
| 180 |
grpc_slice ca_slice; |
| 181 |
EXPECT_TRUE(GRPC_LOG_IF_ERROR("load_file", |
| 182 |
grpc_load_file(CA_CERT_PATH, 1, &ca_slice))); |
| 183 |
const char* test_root_cert = |
| 184 |
reinterpret_cast<const char*> GRPC_SLICE_START_PTR(ca_slice); |
| 185 |
grpc_channel_credentials* ssl_creds = |
| 186 |
grpc_ssl_credentials_create(test_root_cert, nullptr, nullptr, nullptr); |
| 187 |
grpc_slice_unref(ca_slice); |
| 188 |
grpc_arg ssl_name_override = { |
| 189 |
GRPC_ARG_STRING, |
| 190 |
const_cast<char*>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG), |
| 191 |
{const_cast<char*>("foo.test.google.fr")}}; |
| 192 |
grpc_channel_args* new_client_args = |
| 193 |
grpc_channel_args_copy_and_add(nullptr, &ssl_name_override, 1); |
| 194 |
grpc_channel* channel = grpc_channel_create(addr, ssl_creds, new_client_args); |
| 195 |
{ |
| 196 |
grpc_core::ExecCtx exec_ctx; |
| 197 |
grpc_channel_args_destroy(new_client_args); |
| 198 |
} |
| 199 |
grpc_channel_credentials_release(ssl_creds); |
| 200 |
return channel; |
| 201 |
} |
| 202 |
|
| 203 |
static const test_fixture secure_test = { |
| 204 |
"secure", |
| 205 |
secure_test_create_channel, |
| 206 |
}; |
| 207 |
|
| 208 |
TEST(NumExternalConnectivityWatchersTest, MainTest) { |
| 209 |
run_timeouts_test(&insecure_test); |
| 210 |
run_timeouts_test(&secure_test); |
| 211 |
|
| 212 |
run_channel_shutdown_before_timeout_test(&insecure_test); |
| 213 |
run_channel_shutdown_before_timeout_test(&secure_test); |
| 214 |
} |
| 215 |
|
| 216 |
int main(int argc, char** argv) { |
| 217 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 218 |
::testing::InitGoogleTest(&argc, argv); |
| 219 |
return RUN_ALL_TESTS(); |
| 220 |
} |