iomgr/timer_list_test.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 <string.h>
20 
21 #include <cstdint>
22 #include <limits>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
26 
34 
35 #define MAX_CB 30
36 
39 
40 static int cb_called[MAX_CB][2];
41 static const int64_t kHoursIn25Days = 25 * 24;
44 
45 static void cb(void* arg, grpc_error_handle error) {
46  cb_called[reinterpret_cast<intptr_t>(arg)][GRPC_ERROR_IS_NONE(error)]++;
47 }
48 
49 static void add_test(void) {
50  int i;
51  grpc_timer timers[20];
53 
54  gpr_log(GPR_INFO, "add_test");
55 
59  memset(cb_called, 0, sizeof(cb_called));
60 
62 
63  /* 10 ms timers. will expire in the current epoch */
64  for (i = 0; i < 10; i++) {
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++) {
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. */
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 
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 */
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 
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 
115 }
116 
117 /* Cleaning up a list with pending timers. */
118 void destruction_test(void) {
119  grpc_timer timers[5];
121 
122  gpr_log(GPR_INFO, "destruction_test");
123 
129  memset(cb_called, 0, sizeof(cb_called));
130 
133  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)0, grpc_schedule_on_exec_ctx));
136  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)1, grpc_schedule_on_exec_ctx));
139  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)2, grpc_schedule_on_exec_ctx));
142  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)3, grpc_schedule_on_exec_ctx));
145  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)4, grpc_schedule_on_exec_ctx));
150  GPR_ASSERT(1 == cb_called[4][1]);
151  grpc_timer_cancel(&timers[0]);
152  grpc_timer_cancel(&timers[3]);
154  GPR_ASSERT(1 == cb_called[0][0]);
155  GPR_ASSERT(1 == cb_called[3][0]);
156 
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 */
176  grpc_timer timers[4];
178 
179  gpr_log(GPR_INFO, "long_running_service_cleanup_test");
180 
182  GPR_ASSERT(now.milliseconds_after_process_epoch() >= k25Days.millis());
186  memset(cb_called, 0, sizeof(cb_called));
187 
189  &timers[0], now + k25Days,
190  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)0, grpc_schedule_on_exec_ctx));
192  &timers[1], now + grpc_core::Duration::Milliseconds(3),
193  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)1, grpc_schedule_on_exec_ctx));
195  &timers[2],
198  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)2, grpc_schedule_on_exec_ctx));
199 
200  gpr_timespec deadline_spec =
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 */
206  &timers[3], grpc_core::Timestamp::FromTimespecRoundUp(deadline_spec),
207  GRPC_CLOSURE_CREATE(cb, (void*)(intptr_t)3, grpc_schedule_on_exec_ctx));
208 
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 
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);
241  add_test();
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. */
261  add_test();
264  }
265 
266  return 0;
267 }
trace.h
GPR_TIMESPAN
@ GPR_TIMESPAN
Definition: gpr_types.h:45
grpc_core::Duration::Hours
static constexpr Duration Hours(int64_t hours)
Definition: src/core/lib/gprpp/time.h:143
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
now
static double now(void)
Definition: test/core/fling/client.cc:130
log.h
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
iomgr_internal.h
grpc_core::TestOnlySetProcessEpoch
void TestOnlySetProcessEpoch(gpr_timespec epoch)
Definition: src/core/lib/gprpp/time.cc:201
grpc_timer_trace
grpc_core::TraceFlag grpc_timer_trace
cb
static void cb(void *arg, grpc_error_handle error)
Definition: iomgr/timer_list_test.cc:45
string.h
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
error
grpc_error_handle error
Definition: retry_filter.cc:499
tracer_util.h
grpc_timer_check
grpc_timer_check_result grpc_timer_check(grpc_core::Timestamp *next)
Definition: iomgr/timer.cc:38
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
MAX_CB
#define MAX_CB
Definition: iomgr/timer_list_test.cc:35
kHoursIn25Days
static const int64_t kHoursIn25Days
Definition: iomgr/timer_list_test.cc:41
grpc_timer
Definition: iomgr/timer.h:33
GPR_LOG_SEVERITY_DEBUG
@ GPR_LOG_SEVERITY_DEBUG
Definition: include/grpc/impl/codegen/log.h:46
grpc_set_default_iomgr_platform
void grpc_set_default_iomgr_platform()
Definition: memory_quota_test.cc:167
add_test
static void add_test(void)
Definition: iomgr/timer_list_test.cc:49
GRPC_TIMERS_FIRED
@ GRPC_TIMERS_FIRED
Definition: iomgr/timer.h:58
start
static uint64_t start
Definition: benchmark-pound.c:74
grpc_timer_list_init
void grpc_timer_list_init()
Definition: iomgr/timer.cc:42
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
gpr_time_sub
GPRAPI gpr_timespec gpr_time_sub(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:168
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc_iomgr_platform_init
void grpc_iomgr_platform_init()
Definition: iomgr_internal.cc:35
grpc_timer_check_trace
grpc_core::TraceFlag grpc_timer_check_trace
arg
Definition: cmdline.cc:40
time.h
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
GRPC_TIMERS_CHECKED_AND_EMPTY
@ GRPC_TIMERS_CHECKED_AND_EMPTY
Definition: iomgr/timer.h:57
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch
static constexpr Timestamp FromMillisecondsAfterProcessEpoch(int64_t millis)
Definition: src/core/lib/gprpp/time.h:73
grpc_core::TraceFlag
Definition: debug/trace.h:63
test_config.h
grpc_core::ExecCtx::TestOnlySetNow
void TestOnlySetNow(Timestamp new_val)
Definition: exec_ctx.h:199
grpc_timer_cancel
void grpc_timer_cancel(grpc_timer *timer)
Definition: iomgr/timer.cc:36
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
grpc_core::testing::grpc_tracer_enable_flag
void grpc_tracer_enable_flag(TraceFlag *flag)
Definition: tracer_util.cc:25
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition: src/core/lib/gprpp/time.h:208
gpr_time_add
GPRAPI gpr_timespec gpr_time_add(gpr_timespec a, gpr_timespec b)
Definition: src/core/lib/gpr/time.cc:135
cb_called
static int cb_called[MAX_CB][2]
Definition: iomgr/timer_list_test.cc:40
port.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
main
int main(int argc, char **argv)
Definition: iomgr/timer_list_test.cc:231
gpr_time_from_hours
GPRAPI gpr_timespec gpr_time_from_hours(int64_t h, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:131
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_iomgr_platform_shutdown
void grpc_iomgr_platform_shutdown()
Definition: iomgr_internal.cc:39
arg
struct arg arg
grpc_timer_init
void grpc_timer_init(grpc_timer *timer, grpc_core::Timestamp deadline, grpc_closure *closure)
Definition: iomgr/timer.cc:31
k25Days
static const grpc_core::Duration k25Days
Definition: iomgr/timer_list_test.cc:42
timer.h
grpc_timer_list_shutdown
void grpc_timer_list_shutdown()
Definition: iomgr/timer.cc:44
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
gpr_timespec
Definition: gpr_types.h:50
grpc_error
Definition: error_internal.h:42
long_running_service_cleanup_test
void long_running_service_cleanup_test(void)
Definition: iomgr/timer_list_test.cc:175
grpc_core::Timestamp::FromTimespecRoundUp
static Timestamp FromTimespecRoundUp(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:136
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
gpr_time_init
GPRAPI void gpr_time_init(void)
gpr_set_log_verbosity
GPRAPI void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)
Definition: log.cc:96
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
destruction_test
void destruction_test(void)
Definition: iomgr/timer_list_test.cc:118
gpr_time_from_seconds
GPRAPI gpr_timespec gpr_time_from_seconds(int64_t s, gpr_clock_type clock_type)
Definition: src/core/lib/gpr/time.cc:123
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:38