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