Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / test / cpp / common / timer_test.cc
1 /*
2  *
3  * Copyright 2019 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/support/log.h>
21 #include <gtest/gtest.h>
22
23 #include "src/core/lib/iomgr/closure.h"
24 #include "src/core/lib/iomgr/error.h"
25 #include "src/core/lib/iomgr/exec_ctx.h"
26 #include "src/core/lib/iomgr/timer.h"
27 #include "src/core/lib/iomgr/timer_manager.h"
28 #include "test/core/util/test_config.h"
29
30 #ifdef GRPC_POSIX_SOCKET_EV
31 #include "src/core/lib/iomgr/ev_posix.h"
32 #endif
33
34 // MAYBE_SKIP_TEST is a macro to determine if this particular test configuration
35 // should be skipped based on a decision made at SetUp time.
36 #define MAYBE_SKIP_TEST \
37   do {                  \
38     if (do_not_test_) { \
39       return;           \
40     }                   \
41   } while (0)
42
43 class TimerTest : public ::testing::Test {
44  protected:
45   void SetUp() override {
46     grpc_init();
47     // Skip test if slowdown factor > 1, or we are
48     // using event manager.
49 #ifdef GRPC_POSIX_SOCKET_EV
50     if (grpc_test_slowdown_factor() != 1 ||
51         grpc_event_engine_run_in_background()) {
52 #else
53     if (grpc_test_slowdown_factor() != 1) {
54 #endif
55       do_not_test_ = true;
56     }
57   }
58
59   void TearDown() override { grpc_shutdown(); }
60
61   bool do_not_test_{false};
62 };
63
64 #ifndef GPR_WINDOWS
65 // the test fails with too many wakeups on windows opt build
66 // the mechanism by which that happens is described in
67 // https://github.com/grpc/grpc/issues/20436
68 TEST_F(TimerTest, NoTimers) {
69   MAYBE_SKIP_TEST;
70   grpc_core::ExecCtx exec_ctx;
71   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
72
73   // We expect to get 1 wakeup per second. Sometimes we also get a wakeup
74   // during initialization, so in 1.5 seconds we expect to get 1 or 2 wakeups.
75   int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
76   GPR_ASSERT(wakeups == 1 || wakeups == 2);
77 }
78 #endif
79
80 TEST_F(TimerTest, OneTimerExpires) {
81   MAYBE_SKIP_TEST;
82   grpc_core::ExecCtx exec_ctx;
83   grpc_timer timer;
84   int timer_fired = 0;
85   grpc_timer_init(&timer, grpc_core::ExecCtx::Get()->Now() + 500,
86                   GRPC_CLOSURE_CREATE(
87                       [](void* arg, grpc_error*) {
88                         int* timer_fired = static_cast<int*>(arg);
89                         ++*timer_fired;
90                       },
91                       &timer_fired, grpc_schedule_on_exec_ctx));
92   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
93   GPR_ASSERT(1 == timer_fired);
94
95   // We expect to get 1 wakeup/second + 1 wakeup for the expired timer + maybe 1
96   // wakeup during initialization. i.e. in 1.5 seconds we expect 2 or 3 wakeups.
97   // Actual number of wakeups is more due to bug
98   // https://github.com/grpc/grpc/issues/19947
99   int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
100   gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
101 }
102
103 TEST_F(TimerTest, MultipleTimersExpire) {
104   MAYBE_SKIP_TEST;
105   grpc_core::ExecCtx exec_ctx;
106   const int kNumTimers = 10;
107   grpc_timer timers[kNumTimers];
108   int timer_fired = 0;
109   for (int i = 0; i < kNumTimers; ++i) {
110     grpc_timer_init(&timers[i], grpc_core::ExecCtx::Get()->Now() + 500 + i,
111                     GRPC_CLOSURE_CREATE(
112                         [](void* arg, grpc_error*) {
113                           int* timer_fired = static_cast<int*>(arg);
114                           ++*timer_fired;
115                         },
116                         &timer_fired, grpc_schedule_on_exec_ctx));
117   }
118
119   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
120   GPR_ASSERT(kNumTimers == timer_fired);
121
122   // We expect to get 1 wakeup/second + 1 wakeup for per timer fired + maybe 1
123   // wakeup during initialization. i.e. in 1.5 seconds we expect 11 or 12
124   // wakeups. Actual number of wakeups is more due to bug
125   // https://github.com/grpc/grpc/issues/19947
126   int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
127   gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
128 }
129
130 TEST_F(TimerTest, CancelSomeTimers) {
131   MAYBE_SKIP_TEST;
132   grpc_core::ExecCtx exec_ctx;
133   const int kNumTimers = 10;
134   grpc_timer timers[kNumTimers];
135   int timer_fired = 0;
136   for (int i = 0; i < kNumTimers; ++i) {
137     grpc_timer_init(&timers[i], grpc_core::ExecCtx::Get()->Now() + 500 + i,
138                     GRPC_CLOSURE_CREATE(
139                         [](void* arg, grpc_error* error) {
140                           if (error == GRPC_ERROR_CANCELLED) {
141                             return;
142                           }
143                           int* timer_fired = static_cast<int*>(arg);
144                           ++*timer_fired;
145                         },
146                         &timer_fired, grpc_schedule_on_exec_ctx));
147   }
148   for (int i = 0; i < kNumTimers / 2; ++i) {
149     grpc_timer_cancel(&timers[i]);
150   }
151
152   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
153   GPR_ASSERT(kNumTimers / 2 == timer_fired);
154
155   // We expect to get 1 wakeup/second + 1 wakeup per timer fired + maybe 1
156   // wakeup during initialization. i.e. in 1.5 seconds we expect 6 or 7 wakeups.
157   // Actual number of wakeups is more due to bug
158   // https://github.com/grpc/grpc/issues/19947
159   int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
160   gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
161 }
162
163 // Enable the following test after
164 // https://github.com/grpc/grpc/issues/20049 has been fixed.
165 TEST_F(TimerTest, DISABLED_TimerNotCanceled) {
166   grpc_core::ExecCtx exec_ctx;
167   grpc_timer timer;
168   grpc_timer_init(&timer, grpc_core::ExecCtx::Get()->Now() + 10000,
169                   GRPC_CLOSURE_CREATE([](void*, grpc_error*) {}, nullptr,
170                                       grpc_schedule_on_exec_ctx));
171 }
172
173 // Enable the following test after
174 // https://github.com/grpc/grpc/issues/20064 has been fixed.
175 TEST_F(TimerTest, DISABLED_CancelRace) {
176   MAYBE_SKIP_TEST;
177   grpc_core::ExecCtx exec_ctx;
178   const int kNumTimers = 10;
179   grpc_timer timers[kNumTimers];
180   for (int i = 0; i < kNumTimers; ++i) {
181     grpc_timer* arg = (i != 0) ? &timers[i - 1] : nullptr;
182     grpc_timer_init(&timers[i], grpc_core::ExecCtx::Get()->Now() + 100,
183                     GRPC_CLOSURE_CREATE(
184                         [](void* arg, grpc_error* /*error*/) {
185                           grpc_timer* timer = static_cast<grpc_timer*>(arg);
186                           if (timer) {
187                             grpc_timer_cancel(timer);
188                           }
189                         },
190                         arg, grpc_schedule_on_exec_ctx));
191   }
192   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
193 }
194
195 // Enable the following test after
196 // https://github.com/grpc/grpc/issues/20066 has been fixed.
197 TEST_F(TimerTest, DISABLED_CancelNextTimer) {
198   MAYBE_SKIP_TEST;
199   grpc_core::ExecCtx exec_ctx;
200   const int kNumTimers = 10;
201   grpc_timer timers[kNumTimers];
202
203   for (int i = 0; i < kNumTimers; ++i) {
204     grpc_timer_init_unset(&timers[i]);
205   }
206
207   for (int i = 0; i < kNumTimers; ++i) {
208     grpc_timer* arg = nullptr;
209     if (i < kNumTimers - 1) {
210       arg = &timers[i + 1];
211     }
212     grpc_timer_init(&timers[i], grpc_core::ExecCtx::Get()->Now() + 100,
213                     GRPC_CLOSURE_CREATE(
214                         [](void* arg, grpc_error* /*error*/) {
215                           grpc_timer* timer = static_cast<grpc_timer*>(arg);
216                           if (timer) {
217                             grpc_timer_cancel(timer);
218                           }
219                         },
220                         arg, grpc_schedule_on_exec_ctx));
221   }
222   grpc_timer_cancel(&timers[0]);
223   gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
224 }
225
226 int main(int argc, char** argv) {
227   grpc::testing::TestEnvironment env(argc, argv);
228   ::testing::InitGoogleTest(&argc, argv);
229   return RUN_ALL_TESTS();
230 }