Fix flakiness in //test/cpp/common:timer_test (#30606)

pull/30612/head
Vignesh Babu 2 years ago committed by GitHub
parent 6154959138
commit f0a2b3d660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      test/cpp/common/timer_test.cc

@ -140,18 +140,24 @@ TEST_F(TimerTest, CancelSomeTimers) {
grpc_core::ExecCtx exec_ctx; grpc_core::ExecCtx exec_ctx;
const int kNumTimers = 10; const int kNumTimers = 10;
grpc_timer timers[kNumTimers]; grpc_timer timers[kNumTimers];
int timer_fired = 0; std::atomic<int> timer_fired{0};
grpc_core::ExecCtx::Get()->InvalidateNow();
for (int i = 0; i < kNumTimers; ++i) { for (int i = 0; i < kNumTimers; ++i) {
grpc_timer_init(&timers[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::ExecCtx::Get()->Now() + grpc_core::ExecCtx::Get()->Now() +
grpc_core::Duration::Milliseconds(500) + ((i < kNumTimers / 2) ? grpc_core ::Duration::Milliseconds(60000)
grpc_core::Duration::Milliseconds(i), : grpc_core ::Duration::Milliseconds(100) +
grpc_core::Duration::Milliseconds(i)),
GRPC_CLOSURE_CREATE( GRPC_CLOSURE_CREATE(
[](void* arg, grpc_error_handle error) { [](void* arg, grpc_error_handle error) {
if (error == GRPC_ERROR_CANCELLED) { if (error == GRPC_ERROR_CANCELLED) {
return; return;
} }
int* timer_fired = static_cast<int*>(arg); std::atomic<int>* timer_fired =
static_cast<std::atomic<int>*>(arg);
++*timer_fired; ++*timer_fired;
}, },
&timer_fired, grpc_schedule_on_exec_ctx)); &timer_fired, grpc_schedule_on_exec_ctx));

Loading…
Cancel
Save