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