| 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 <string.h> |
| 20 |
|
| 21 |
#include <cstdint> |
| 22 |
#include <limits> |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/support/log.h> |
| 26 |
|
| 27 |
#include "src/core/lib/debug/trace.h" |
| 28 |
#include "src/core/lib/gprpp/time.h" |
| 29 |
#include "src/core/lib/iomgr/iomgr_internal.h" |
| 30 |
#include "src/core/lib/iomgr/port.h" |
| 31 |
#include "src/core/lib/iomgr/timer.h" |
| 32 |
#include "test/core/util/test_config.h" |
| 33 |
#include "test/core/util/tracer_util.h" |
| 34 |
|
| 35 |
#define MAX_CB 30 |
| 36 |
|
| 37 |
extern grpc_core::TraceFlag grpc_timer_trace; |
| 38 |
extern grpc_core::TraceFlag grpc_timer_check_trace; |
| 39 |
|
| 40 |
static int cb_called[MAX_CB][2]; |
| 41 |
static const int64_t kHoursIn25Days = 25 * 24; |
| 42 |
static const grpc_core::Duration k25Days = |
| 43 |
grpc_core::Duration::Hours(kHoursIn25Days); |
| 44 |
|
| 45 |
static void cb(void* arg, grpc_error_handle error) { |
| 46 |
cb_called[reinterpret_cast<intptr_t>(arg)][error.ok()]++; |
| 47 |
} |
| 48 |
|
| 49 |
static void add_test(void) { |
| 50 |
int i; |
| 51 |
grpc_timer timers[20]; |
| 52 |
grpc_core::ExecCtx exec_ctx; |
| 53 |
|
| 54 |
gpr_log(GPR_INFO, "add_test"); |
| 55 |
|
| 56 |
grpc_timer_list_init(); |
| 57 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_trace); |
| 58 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_check_trace); |
| 59 |
memset(cb_called, 0, sizeof(cb_called)); |
| 60 |
|
| 61 |
grpc_core::Timestamp start = grpc_core::Timestamp::Now(); |
| 62 |
|
| 63 |
/* 10 ms timers. will expire in the current epoch */ |
| 64 |
for (i = 0; i < 10; i++) { |
| 65 |
grpc_timer_init( |
| 66 |
&timers[i], start + grpc_core::Duration::Milliseconds(10), |
| 67 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)i, grpc_schedule_on_exec_ctx)); |
| 68 |
} |
| 69 |
|
| 70 |
/* 1010 ms timers. will expire in the next epoch */ |
| 71 |
for (i = 10; i < 20; i++) { |
| 72 |
grpc_timer_init( |
| 73 |
&timers[i], start + grpc_core::Duration::Milliseconds(1010), |
| 74 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)i, grpc_schedule_on_exec_ctx)); |
| 75 |
} |
| 76 |
|
| 77 |
/* collect timers. Only the first batch should be ready. */ |
| 78 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 79 |
start + grpc_core::Duration::Milliseconds(500)); |
| 80 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED); |
| 81 |
grpc_core::ExecCtx::Get()->Flush(); |
| 82 |
for (i = 0; i < 20; i++) { |
| 83 |
GPR_ASSERT(cb_called[i][1] == (i < 10)); |
| 84 |
GPR_ASSERT(cb_called[i][0] == 0); |
| 85 |
} |
| 86 |
|
| 87 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 88 |
start + grpc_core::Duration::Milliseconds(600)); |
| 89 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_CHECKED_AND_EMPTY); |
| 90 |
grpc_core::ExecCtx::Get()->Flush(); |
| 91 |
for (i = 0; i < 30; i++) { |
| 92 |
GPR_ASSERT(cb_called[i][1] == (i < 10)); |
| 93 |
GPR_ASSERT(cb_called[i][0] == 0); |
| 94 |
} |
| 95 |
|
| 96 |
/* collect the rest of the timers */ |
| 97 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 98 |
start + grpc_core::Duration::Milliseconds(1500)); |
| 99 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED); |
| 100 |
grpc_core::ExecCtx::Get()->Flush(); |
| 101 |
for (i = 0; i < 30; i++) { |
| 102 |
GPR_ASSERT(cb_called[i][1] == (i < 20)); |
| 103 |
GPR_ASSERT(cb_called[i][0] == 0); |
| 104 |
} |
| 105 |
|
| 106 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 107 |
start + grpc_core::Duration::Milliseconds(1600)); |
| 108 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_CHECKED_AND_EMPTY); |
| 109 |
for (i = 0; i < 30; i++) { |
| 110 |
GPR_ASSERT(cb_called[i][1] == (i < 20)); |
| 111 |
GPR_ASSERT(cb_called[i][0] == 0); |
| 112 |
} |
| 113 |
|
| 114 |
grpc_timer_list_shutdown(); |
| 115 |
} |
| 116 |
|
| 117 |
/* Cleaning up a list with pending timers. */ |
| 118 |
void destruction_test(void) { |
| 119 |
grpc_timer timers[5]; |
| 120 |
grpc_core::ExecCtx exec_ctx; |
| 121 |
|
| 122 |
gpr_log(GPR_INFO, "destruction_test"); |
| 123 |
|
| 124 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 125 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(0)); |
| 126 |
grpc_timer_list_init(); |
| 127 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_trace); |
| 128 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_check_trace); |
| 129 |
memset(cb_called, 0, sizeof(cb_called)); |
| 130 |
|
| 131 |
grpc_timer_init( |
| 132 |
&timers[0], grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(100), |
| 133 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)0, grpc_schedule_on_exec_ctx)); |
| 134 |
grpc_timer_init( |
| 135 |
&timers[1], grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(3), |
| 136 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)1, grpc_schedule_on_exec_ctx)); |
| 137 |
grpc_timer_init( |
| 138 |
&timers[2], grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(100), |
| 139 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)2, grpc_schedule_on_exec_ctx)); |
| 140 |
grpc_timer_init( |
| 141 |
&timers[3], grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(3), |
| 142 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)3, grpc_schedule_on_exec_ctx)); |
| 143 |
grpc_timer_init( |
| 144 |
&timers[4], grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(1), |
| 145 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)4, grpc_schedule_on_exec_ctx)); |
| 146 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 147 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch(2)); |
| 148 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED); |
| 149 |
grpc_core::ExecCtx::Get()->Flush(); |
| 150 |
GPR_ASSERT(1 == cb_called[4][1]); |
| 151 |
grpc_timer_cancel(&timers[0]); |
| 152 |
grpc_timer_cancel(&timers[3]); |
| 153 |
grpc_core::ExecCtx::Get()->Flush(); |
| 154 |
GPR_ASSERT(1 == cb_called[0][0]); |
| 155 |
GPR_ASSERT(1 == cb_called[3][0]); |
| 156 |
|
| 157 |
grpc_timer_list_shutdown(); |
| 158 |
grpc_core::ExecCtx::Get()->Flush(); |
| 159 |
GPR_ASSERT(1 == cb_called[1][0]); |
| 160 |
GPR_ASSERT(1 == cb_called[2][0]); |
| 161 |
} |
| 162 |
|
| 163 |
/* Cleans up a list with pending timers that simulate long-running-services. |
| 164 |
This test does the following: |
| 165 |
1) Simulates grpc server start time to 25 days in the past (completed in |
| 166 |
`main` using TestOnlyGlobalInit()) |
| 167 |
2) Creates 4 timers - one with a deadline 25 days in the future, one just |
| 168 |
3 milliseconds in future, one way out in the future, and one using the |
| 169 |
grpc_timespec_to_millis_round_up function to compute a deadline of 25 |
| 170 |
days in the future |
| 171 |
3) Simulates 4 milliseconds of elapsed time by changing `now` (cached at |
| 172 |
step 1) to `now+4` |
| 173 |
4) Shuts down the timer list |
| 174 |
https://github.com/grpc/grpc/issues/15904 */ |
| 175 |
void long_running_service_cleanup_test(void) { |
| 176 |
grpc_timer timers[4]; |
| 177 |
grpc_core::ExecCtx exec_ctx; |
| 178 |
|
| 179 |
gpr_log(GPR_INFO, "long_running_service_cleanup_test"); |
| 180 |
|
| 181 |
grpc_core::Timestamp now = grpc_core::Timestamp::Now(); |
| 182 |
GPR_ASSERT(now.milliseconds_after_process_epoch() >= k25Days.millis()); |
| 183 |
grpc_timer_list_init(); |
| 184 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_trace); |
| 185 |
grpc_core::testing::grpc_tracer_enable_flag(&grpc_timer_check_trace); |
| 186 |
memset(cb_called, 0, sizeof(cb_called)); |
| 187 |
|
| 188 |
grpc_timer_init( |
| 189 |
&timers[0], now + k25Days, |
| 190 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)0, grpc_schedule_on_exec_ctx)); |
| 191 |
grpc_timer_init( |
| 192 |
&timers[1], now + grpc_core::Duration::Milliseconds(3), |
| 193 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)1, grpc_schedule_on_exec_ctx)); |
| 194 |
grpc_timer_init( |
| 195 |
&timers[2], |
| 196 |
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 197 |
std::numeric_limits<int64_t>::max() - 1), |
| 198 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)2, grpc_schedule_on_exec_ctx)); |
| 199 |
|
| 200 |
gpr_timespec deadline_spec = |
| 201 |
(now + k25Days).as_timespec(gpr_clock_type::GPR_CLOCK_MONOTONIC); |
| 202 |
|
| 203 |
/* grpc_timespec_to_millis_round_up is how users usually compute a millisecond |
| 204 |
input value into grpc_timer_init, so we mimic that behavior here */ |
| 205 |
grpc_timer_init( |
| 206 |
&timers[3], grpc_core::Timestamp::FromTimespecRoundUp(deadline_spec), |
| 207 |
GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)3, grpc_schedule_on_exec_ctx)); |
| 208 |
|
| 209 |
grpc_core::ExecCtx::Get()->TestOnlySetNow( |
| 210 |
now + grpc_core::Duration::Milliseconds(4)); |
| 211 |
GPR_ASSERT(grpc_timer_check(nullptr) == GRPC_TIMERS_FIRED); |
| 212 |
grpc_core::ExecCtx::Get()->Flush(); |
| 213 |
GPR_ASSERT(0 == cb_called[0][0]); // Timer 0 not called |
| 214 |
GPR_ASSERT(0 == cb_called[0][1]); |
| 215 |
GPR_ASSERT(0 == cb_called[1][0]); |
| 216 |
GPR_ASSERT(1 == cb_called[1][1]); // Timer 1 fired |
| 217 |
GPR_ASSERT(0 == cb_called[2][0]); // Timer 2 not called |
| 218 |
GPR_ASSERT(0 == cb_called[2][1]); |
| 219 |
GPR_ASSERT(0 == cb_called[3][0]); // Timer 3 not called |
| 220 |
GPR_ASSERT(0 == cb_called[3][1]); |
| 221 |
|
| 222 |
grpc_timer_list_shutdown(); |
| 223 |
grpc_core::ExecCtx::Get()->Flush(); |
| 224 |
/* Timers 0, 2, and 3 were fired with an error during cleanup */ |
| 225 |
GPR_ASSERT(1 == cb_called[0][0]); |
| 226 |
GPR_ASSERT(0 == cb_called[1][0]); |
| 227 |
GPR_ASSERT(1 == cb_called[2][0]); |
| 228 |
GPR_ASSERT(1 == cb_called[3][0]); |
| 229 |
} |
| 230 |
|
| 231 |
int main(int argc, char** argv) { |
| 232 |
gpr_time_init(); |
| 233 |
|
| 234 |
/* Tests with default g_start_time */ |
| 235 |
{ |
| 236 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 237 |
grpc_core::ExecCtx exec_ctx; |
| 238 |
grpc_set_default_iomgr_platform(); |
| 239 |
grpc_iomgr_platform_init(); |
| 240 |
gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG); |
| 241 |
add_test(); |
| 242 |
destruction_test(); |
| 243 |
grpc_iomgr_platform_shutdown(); |
| 244 |
} |
| 245 |
|
| 246 |
/* Begin long running service tests */ |
| 247 |
{ |
| 248 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 249 |
/* Set g_start_time back 25 days. */ |
| 250 |
/* We set g_start_time here in case there are any initialization |
| 251 |
dependencies that use g_start_time. */ |
| 252 |
grpc_core::TestOnlySetProcessEpoch(gpr_time_sub( |
| 253 |
gpr_now(gpr_clock_type::GPR_CLOCK_MONOTONIC), |
| 254 |
gpr_time_add(gpr_time_from_hours(kHoursIn25Days, GPR_TIMESPAN), |
| 255 |
gpr_time_from_seconds(10, GPR_TIMESPAN)))); |
| 256 |
grpc_core::ExecCtx exec_ctx; |
| 257 |
grpc_set_default_iomgr_platform(); |
| 258 |
grpc_iomgr_platform_init(); |
| 259 |
gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG); |
| 260 |
long_running_service_cleanup_test(); |
| 261 |
add_test(); |
| 262 |
destruction_test(); |
| 263 |
grpc_iomgr_platform_shutdown(); |
| 264 |
} |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |