xref: /aosp_15_r20/external/grpc-grpc/test/core/event_engine/posix/timer_manager_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/core/lib/event_engine/posix_engine/timer_manager.h"
16 
17 #include <atomic>
18 #include <memory>
19 #include <random>
20 
21 #include "absl/functional/any_invocable.h"
22 #include "absl/time/clock.h"
23 #include "absl/time/time.h"
24 #include "gtest/gtest.h"
25 
26 #include <grpc/grpc.h>
27 #include <grpc/support/log.h>
28 
29 #include "src/core/lib/event_engine/common_closures.h"
30 #include "src/core/lib/event_engine/posix_engine/timer.h"
31 #include "src/core/lib/event_engine/thread_pool/thread_pool.h"
32 #include "src/core/lib/iomgr/exec_ctx.h"
33 #include "test/core/util/test_config.h"
34 
35 namespace grpc_event_engine {
36 namespace experimental {
37 
TEST(TimerManagerTest,StressTest)38 TEST(TimerManagerTest, StressTest) {
39   grpc_core::ExecCtx exec_ctx;
40   auto now = grpc_core::Timestamp::Now();
41   auto test_deadline = now + grpc_core::Duration::Seconds(15);
42   std::vector<Timer> timers;
43   constexpr int kTimerCount = 500;
44   timers.resize(kTimerCount);
45   std::atomic_int called{0};
46   std::random_device rd;
47   std::mt19937 gen(rd());
48   std::uniform_real_distribution<> dis_millis(100, 3000);
49   auto pool = MakeThreadPool(8);
50   {
51     TimerManager manager(pool);
52     for (auto& timer : timers) {
53       exec_ctx.InvalidateNow();
54       manager.TimerInit(
55           &timer, now + grpc_core::Duration::Milliseconds(dis_millis(gen)),
56           experimental::SelfDeletingClosure::Create([&called]() {
57             absl::SleepFor(absl::Milliseconds(50));
58             ++called;
59           }));
60     }
61     // Wait for all callbacks to have been called
62     while (called.load(std::memory_order_relaxed) < kTimerCount) {
63       exec_ctx.InvalidateNow();
64       if (grpc_core::Timestamp::Now() > test_deadline) {
65         FAIL() << "Deadline exceeded. "
66                << called.load(std::memory_order_relaxed) << "/" << kTimerCount
67                << " callbacks executed";
68       }
69       gpr_log(GPR_DEBUG, "Processed %d/%d callbacks",
70               called.load(std::memory_order_relaxed), kTimerCount);
71       absl::SleepFor(absl::Milliseconds(333));
72     }
73   }
74   pool->Quiesce();
75 }
76 
TEST(TimerManagerTest,ShutDownBeforeAllCallbacksAreExecuted)77 TEST(TimerManagerTest, ShutDownBeforeAllCallbacksAreExecuted) {
78   // Should the internal timer_list complain in this scenario?
79   grpc_core::ExecCtx exec_ctx;
80   std::vector<Timer> timers;
81   constexpr int kTimerCount = 100;
82   timers.resize(kTimerCount);
83   std::atomic_int called{0};
84   experimental::AnyInvocableClosure closure([&called] { ++called; });
85   auto pool = MakeThreadPool(8);
86   {
87     TimerManager manager(pool);
88     for (auto& timer : timers) {
89       manager.TimerInit(&timer, grpc_core::Timestamp::InfFuture(), &closure);
90     }
91   }
92   ASSERT_EQ(called.load(), 0);
93   pool->Quiesce();
94 }
95 
96 }  // namespace experimental
97 }  // namespace grpc_event_engine
98 
main(int argc,char ** argv)99 int main(int argc, char** argv) {
100   grpc::testing::TestEnvironment env(&argc, argv);
101   ::testing::InitGoogleTest(&argc, argv);
102   grpc_init();
103   int ret = RUN_ALL_TESTS();
104   grpc_shutdown();
105   return ret;
106 }
107