File: timer_test.cc

package info (click to toggle)
grpc 1.51.1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,328 kB
  • sloc: cpp: 361,873; python: 72,206; ansic: 37,787; objc: 12,434; ruby: 11,521; sh: 7,652; php: 7,615; makefile: 3,481; xml: 3,246; cs: 1,836; javascript: 1,614; java: 465; pascal: 227; awk: 132
file content (252 lines) | stat: -rw-r--r-- 8,637 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 *
 * Copyright 2019 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "src/core/lib/iomgr/timer.h"

#include <gtest/gtest.h>

#include <grpc/grpc.h>
#include <grpc/support/log.h>

#include "src/core/lib/gprpp/time.h"
#include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/error.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/timer_manager.h"
#include "test/core/util/test_config.h"

#ifdef GRPC_POSIX_SOCKET_EV
#include "src/core/lib/iomgr/ev_posix.h"
#endif

// MAYBE_SKIP_TEST is a macro to determine if this particular test configuration
// should be skipped based on a decision made at SetUp time.
#define MAYBE_SKIP_TEST \
  do {                  \
    if (do_not_test_) { \
      return;           \
    }                   \
  } while (0)

class TimerTest : public ::testing::Test {
 protected:
  void SetUp() override {
    grpc_init();
    // Skip test if slowdown factor > 1, or we are
    // using event manager.
#ifdef GRPC_POSIX_SOCKET_EV
    if (grpc_test_slowdown_factor() != 1 ||
        grpc_event_engine_run_in_background()) {
#else
    if (grpc_test_slowdown_factor() != 1) {
#endif
      do_not_test_ = true;
    }
  }

  void TearDown() override { grpc_shutdown(); }

  bool do_not_test_{false};
};

#ifndef GPR_WINDOWS
// the test fails with too many wakeups on windows opt build
// the mechanism by which that happens is described in
// https://github.com/grpc/grpc/issues/20436
TEST_F(TimerTest, NoTimers) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));

  // We expect to get 1 wakeup per second. Sometimes we also get a wakeup
  // during initialization, so in 1.5 seconds we expect to get 1 or 2 wakeups.
  int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  GPR_ASSERT(wakeups == 1 || wakeups == 2);
}
#endif

TEST_F(TimerTest, OneTimerExpires) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  grpc_timer timer;
  int timer_fired = 0;
  grpc_timer_init(
      &timer,
      grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(500),
      GRPC_CLOSURE_CREATE(
          [](void* arg, grpc_error_handle) {
            int* timer_fired = static_cast<int*>(arg);
            ++*timer_fired;
          },
          &timer_fired, grpc_schedule_on_exec_ctx));
  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  GPR_ASSERT(1 == timer_fired);

  // We expect to get 1 wakeup/second + 1 wakeup for the expired timer + maybe 1
  // wakeup during initialization. i.e. in 1.5 seconds we expect 2 or 3 wakeups.
  // Actual number of wakeups is more due to bug
  // https://github.com/grpc/grpc/issues/19947
  int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
}

TEST_F(TimerTest, MultipleTimersExpire) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  const int kNumTimers = 10;
  grpc_timer timers[kNumTimers];
  int timer_fired = 0;
  for (int i = 0; i < kNumTimers; ++i) {
    grpc_timer_init(&timers[i],
                    grpc_core::Timestamp::Now() +
                        grpc_core::Duration::Milliseconds(500) +
                        grpc_core::Duration::Milliseconds(i),
                    GRPC_CLOSURE_CREATE(
                        [](void* arg, grpc_error_handle) {
                          int* timer_fired = static_cast<int*>(arg);
                          ++*timer_fired;
                        },
                        &timer_fired, grpc_schedule_on_exec_ctx));
  }

  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  GPR_ASSERT(kNumTimers == timer_fired);

  // We expect to get 1 wakeup/second + 1 wakeup for per timer fired + maybe 1
  // wakeup during initialization. i.e. in 1.5 seconds we expect 11 or 12
  // wakeups. Actual number of wakeups is more due to bug
  // https://github.com/grpc/grpc/issues/19947
  int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
}

TEST_F(TimerTest, CancelSomeTimers) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  const int kNumTimers = 10;
  grpc_timer timers[kNumTimers];
  std::atomic<int> timer_fired{0};
  grpc_core::ExecCtx::Get()->InvalidateNow();
  for (int i = 0; i < kNumTimers; ++i) {
    // Set a large firing time for timers which are bound to be cancelled
    // and set a small firing time for timers which need to execute.
    grpc_timer_init(
        &timers[i],
        grpc_core::Timestamp::Now() +
            ((i < kNumTimers / 2) ? grpc_core ::Duration::Milliseconds(60000)
                                  : grpc_core ::Duration::Milliseconds(100) +
                                        grpc_core::Duration::Milliseconds(i)),
        GRPC_CLOSURE_CREATE(
            [](void* arg, grpc_error_handle error) {
              if (error == absl::CancelledError()) {
                return;
              }
              std::atomic<int>* timer_fired =
                  static_cast<std::atomic<int>*>(arg);
              ++*timer_fired;
            },
            &timer_fired, grpc_schedule_on_exec_ctx));
  }
  for (int i = 0; i < kNumTimers / 2; ++i) {
    grpc_timer_cancel(&timers[i]);
  }

  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(1500));
  GPR_ASSERT(kNumTimers / 2 == timer_fired);

  // We expect to get 1 wakeup/second + 1 wakeup per timer fired + maybe 1
  // wakeup during initialization. i.e. in 1.5 seconds we expect 6 or 7 wakeups.
  // Actual number of wakeups is more due to bug
  // https://github.com/grpc/grpc/issues/19947
  int64_t wakeups = grpc_timer_manager_get_wakeups_testonly();
  gpr_log(GPR_DEBUG, "wakeups: %" PRId64 "", wakeups);
}

// Enable the following test after
// https://github.com/grpc/grpc/issues/20049 has been fixed.
TEST_F(TimerTest, DISABLED_TimerNotCanceled) {
  grpc_core::ExecCtx exec_ctx;
  grpc_timer timer;
  grpc_timer_init(
      &timer, grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(10),
      GRPC_CLOSURE_CREATE([](void*, grpc_error_handle) {}, nullptr,
                          grpc_schedule_on_exec_ctx));
}

// Enable the following test after
// https://github.com/grpc/grpc/issues/20064 has been fixed.
TEST_F(TimerTest, DISABLED_CancelRace) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  const int kNumTimers = 10;
  grpc_timer timers[kNumTimers];
  for (int i = 0; i < kNumTimers; ++i) {
    grpc_timer* arg = (i != 0) ? &timers[i - 1] : nullptr;
    grpc_timer_init(
        &timers[i],
        grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(100),
        GRPC_CLOSURE_CREATE(
            [](void* arg, grpc_error_handle /*error*/) {
              grpc_timer* timer = static_cast<grpc_timer*>(arg);
              if (timer) {
                grpc_timer_cancel(timer);
              }
            },
            arg, grpc_schedule_on_exec_ctx));
  }
  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
}

// Enable the following test after
// https://github.com/grpc/grpc/issues/20066 has been fixed.
TEST_F(TimerTest, DISABLED_CancelNextTimer) {
  MAYBE_SKIP_TEST;
  grpc_core::ExecCtx exec_ctx;
  const int kNumTimers = 10;
  grpc_timer timers[kNumTimers];

  for (int i = 0; i < kNumTimers; ++i) {
    grpc_timer_init_unset(&timers[i]);
  }

  for (int i = 0; i < kNumTimers; ++i) {
    grpc_timer* arg = nullptr;
    if (i < kNumTimers - 1) {
      arg = &timers[i + 1];
    }
    grpc_timer_init(
        &timers[i],
        grpc_core::Timestamp::Now() + grpc_core::Duration::Milliseconds(100),
        GRPC_CLOSURE_CREATE(
            [](void* arg, grpc_error_handle /*error*/) {
              grpc_timer* timer = static_cast<grpc_timer*>(arg);
              if (timer) {
                grpc_timer_cancel(timer);
              }
            },
            arg, grpc_schedule_on_exec_ctx));
  }
  grpc_timer_cancel(&timers[0]);
  gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
}

int main(int argc, char** argv) {
  grpc::testing::TestEnvironment env(&argc, argv);
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}