periodic_update_test.cc
Go to the documentation of this file.
1 // Copyright 2021 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 
16 
17 #include <stddef.h>
18 
19 #include <algorithm>
20 #include <memory>
21 #include <thread>
22 #include <vector>
23 
24 #include "absl/memory/memory.h"
25 #include "gtest/gtest.h"
26 
27 #include <grpc/support/log.h>
28 #include <grpc/support/time.h>
29 
31 
32 namespace grpc_core {
33 namespace testing {
34 
35 TEST(PeriodicUpdateTest, SimpleTest) {
36  std::unique_ptr<PeriodicUpdate> upd;
38  // Create a periodic update that updates every second.
39  {
41  upd = absl::make_unique<PeriodicUpdate>(Duration::Seconds(1));
42  start = exec_ctx.Now();
43  }
44  // Wait until the first period has elapsed.
45  while (true) {
47  if (upd->Tick()) break;
48  }
49  // Ensure that took at least 1 second.
50  {
53  start = exec_ctx.Now();
54  }
55  // Do ten more update cycles
56  for (int i = 0; i < 10; i++) {
57  while (true) {
59  if (upd->Tick()) break;
60  }
61  // Ensure the time taken was between 1 and 1.5 seconds - we make a little
62  // allowance for the presumed inaccuracy of this type.
63  {
67  start = exec_ctx.Now();
68  }
69  }
70 }
71 
72 TEST(PeriodicUpdate, ThreadTest) {
73  std::unique_ptr<PeriodicUpdate> upd;
74  std::atomic<int> count(0);
76  // Create a periodic update that updates every second.
77  {
79  upd = absl::make_unique<PeriodicUpdate>(Duration::Seconds(1));
80  start = exec_ctx.Now();
81  }
82  // Run ten threads all updating the counter continuously, for a total of ten
83  // update cycles.
84  // This allows TSAN to catch threading issues.
85  std::vector<std::thread> threads;
86  for (size_t i = 0; i < 10; i++) {
87  threads.push_back(std::thread([&]() {
88  while (count.load() < 10) {
89  ExecCtx exec_ctx;
90  if (upd->Tick()) count.fetch_add(1);
91  }
92  }));
93  }
94 
95  // Finish all threads.
96  for (auto& th : threads) {
97  th.join();
98  }
99  // Ensure our ten cycles took at least 10 seconds, and no more than 15.
100  {
104  }
105 }
106 
107 } // namespace testing
108 } // namespace grpc_core
109 
110 // Hook needed to run ExecCtx outside of iomgr.
112 
113 int main(int argc, char** argv) {
114  ::testing::InitGoogleTest(&argc, argv);
116  gpr_time_init();
117  return RUN_ALL_TESTS();
118 }
testing
Definition: aws_request_signer_test.cc:25
grpc_set_default_iomgr_platform
void grpc_set_default_iomgr_platform()
Definition: periodic_update_test.cc:111
log.h
gpr_log_verbosity_init
GPRAPI void gpr_log_verbosity_init(void)
Definition: log.cc:115
grpc_core
Definition: call_metric_recorder.h:31
main
int main(int argc, char **argv)
Definition: periodic_update_test.cc:113
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
time.h
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
threads
static uv_thread_t * threads
Definition: threadpool.c:38
start
static uint64_t start
Definition: benchmark-pound.c:74
periodic_update.h
grpc_core::PeriodicUpdate
Definition: periodic_update.h:36
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
exec_ctx.h
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
gpr_time_init
GPRAPI void gpr_time_init(void)
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:42