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
15 #include "src/core/lib/resource_quota/periodic_update.h"
16
17 #include <stddef.h>
18
19 #include <memory>
20 #include <thread>
21 #include <vector>
22
23 #include "gtest/gtest.h"
24
25 #include <grpc/support/log.h>
26 #include <grpc/support/time.h>
27
28 #include "src/core/lib/iomgr/exec_ctx.h"
29
30 namespace grpc_core {
31 namespace testing {
32
TEST(PeriodicUpdateTest,SimpleTest)33 TEST(PeriodicUpdateTest, SimpleTest) {
34 std::unique_ptr<PeriodicUpdate> upd;
35 Timestamp start;
36 Timestamp reset_start;
37 // Create a periodic update that updates every second.
38 {
39 ExecCtx exec_ctx;
40 upd = std::make_unique<PeriodicUpdate>(Duration::Seconds(1));
41 start = Timestamp::Now();
42 }
43 // Wait until the first period has elapsed.
44 bool done = false;
45 while (!done) {
46 ExecCtx exec_ctx;
47 upd->Tick([&](Duration elapsed) {
48 reset_start = Timestamp::Now();
49 EXPECT_GE(elapsed, Duration::Seconds(1));
50 done = true;
51 });
52 }
53 // Ensure that took at least 1 second.
54 {
55 ExecCtx exec_ctx;
56 EXPECT_GE(Timestamp::Now() - start, Duration::Seconds(1));
57 start = reset_start;
58 }
59 // Do ten more update cycles
60 for (int i = 0; i < 10; i++) {
61 done = false;
62 while (!done) {
63 ExecCtx exec_ctx;
64 upd->Tick([&](Duration) {
65 reset_start = Timestamp::Now();
66 EXPECT_GE(Timestamp::Now() - start, Duration::Seconds(1));
67 done = true;
68 });
69 }
70 // Ensure the time taken was between 1 and 3 seconds - we make a little
71 // allowance for the presumed inaccuracy of this type.
72 {
73 ExecCtx exec_ctx;
74 EXPECT_GE(Timestamp::Now() - start, Duration::Seconds(1));
75 EXPECT_LE(Timestamp::Now() - start, Duration::Seconds(3));
76 start = reset_start;
77 }
78 }
79 }
80
TEST(PeriodicUpdate,ThreadTest)81 TEST(PeriodicUpdate, ThreadTest) {
82 std::unique_ptr<PeriodicUpdate> upd;
83 std::atomic<int> count(0);
84 Timestamp start;
85 // Create a periodic update that updates every second.
86 {
87 ExecCtx exec_ctx;
88 upd = std::make_unique<PeriodicUpdate>(Duration::Seconds(1));
89 start = Timestamp::Now();
90 }
91 // Run ten threads all updating the counter continuously, for a total of ten
92 // update cycles.
93 // This allows TSAN to catch threading issues.
94 std::vector<std::thread> threads;
95 for (size_t i = 0; i < 10; i++) {
96 threads.push_back(std::thread([&]() {
97 while (count.load() < 10) {
98 ExecCtx exec_ctx;
99 upd->Tick([&](Duration d) {
100 EXPECT_GE(d, Duration::Seconds(1));
101 count.fetch_add(1);
102 });
103 }
104 }));
105 }
106
107 // Finish all threads.
108 for (auto& th : threads) {
109 th.join();
110 }
111 // Ensure our ten cycles took at least 10 seconds, and no more than 30.
112 {
113 ExecCtx exec_ctx;
114 EXPECT_GE(Timestamp::Now() - start, Duration::Seconds(10));
115 EXPECT_LE(Timestamp::Now() - start, Duration::Seconds(30));
116 }
117 }
118
119 } // namespace testing
120 } // namespace grpc_core
121
122 // Hook needed to run ExecCtx outside of iomgr.
grpc_set_default_iomgr_platform()123 void grpc_set_default_iomgr_platform() {}
124
main(int argc,char ** argv)125 int main(int argc, char** argv) {
126 ::testing::InitGoogleTest(&argc, argv);
127 gpr_log_verbosity_init();
128 gpr_time_init();
129 return RUN_ALL_TESTS();
130 }
131