• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpc/grpc.h>
18 
19 #include <atomic>
20 #include <memory>
21 #include <random>
22 
23 #include "absl/functional/any_invocable.h"
24 #include "absl/log/log.h"
25 #include "absl/time/clock.h"
26 #include "absl/time/time.h"
27 #include "gtest/gtest.h"
28 #include "src/core/lib/event_engine/common_closures.h"
29 #include "src/core/lib/event_engine/posix_engine/timer.h"
30 #include "src/core/lib/event_engine/thread_pool/thread_pool.h"
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "test/core/test_util/test_config.h"
33 
34 namespace grpc_event_engine {
35 namespace experimental {
36 
TEST(TimerManagerTest,StressTest)37 TEST(TimerManagerTest, StressTest) {
38   grpc_core::ExecCtx exec_ctx;
39   auto now = grpc_core::Timestamp::Now();
40   auto test_deadline = now + grpc_core::Duration::Seconds(15);
41   std::vector<Timer> timers;
42   constexpr int kTimerCount = 500;
43   timers.resize(kTimerCount);
44   std::atomic_int called{0};
45   std::random_device rd;
46   std::mt19937 gen(rd());
47   std::uniform_real_distribution<> dis_millis(100, 3000);
48   auto pool = MakeThreadPool(8);
49   {
50     TimerManager manager(pool);
51     for (auto& timer : timers) {
52       exec_ctx.InvalidateNow();
53       manager.TimerInit(
54           &timer, now + grpc_core::Duration::Milliseconds(dis_millis(gen)),
55           experimental::SelfDeletingClosure::Create([&called]() {
56             absl::SleepFor(absl::Milliseconds(50));
57             ++called;
58           }));
59     }
60     // Wait for all callbacks to have been called
61     while (called.load(std::memory_order_relaxed) < kTimerCount) {
62       exec_ctx.InvalidateNow();
63       if (grpc_core::Timestamp::Now() > test_deadline) {
64         FAIL() << "Deadline exceeded. "
65                << called.load(std::memory_order_relaxed) << "/" << kTimerCount
66                << " callbacks executed";
67       }
68       VLOG(2) << "Processed " << called.load(std::memory_order_relaxed) << "/"
69               << kTimerCount << " callbacks";
70       absl::SleepFor(absl::Milliseconds(333));
71     }
72   }
73   pool->Quiesce();
74 }
75 
TEST(TimerManagerTest,ShutDownBeforeAllCallbacksAreExecuted)76 TEST(TimerManagerTest, ShutDownBeforeAllCallbacksAreExecuted) {
77   // Should the internal timer_list complain in this scenario?
78   grpc_core::ExecCtx exec_ctx;
79   std::vector<Timer> timers;
80   constexpr int kTimerCount = 100;
81   timers.resize(kTimerCount);
82   std::atomic_int called{0};
83   experimental::AnyInvocableClosure closure([&called] { ++called; });
84   auto pool = MakeThreadPool(8);
85   {
86     TimerManager manager(pool);
87     for (auto& timer : timers) {
88       manager.TimerInit(&timer, grpc_core::Timestamp::InfFuture(), &closure);
89     }
90   }
91   ASSERT_EQ(called.load(), 0);
92   pool->Quiesce();
93 }
94 
95 }  // namespace experimental
96 }  // namespace grpc_event_engine
97 
main(int argc,char ** argv)98 int main(int argc, char** argv) {
99   grpc::testing::TestEnvironment env(&argc, argv);
100   ::testing::InitGoogleTest(&argc, argv);
101   grpc_init();
102   int ret = RUN_ALL_TESTS();
103   grpc_shutdown();
104   return ret;
105 }
106