1 // Copyright 2023 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/ext/transport/chttp2/transport/ping_rate_policy.h"
16
17 #include <chrono>
18 #include <thread>
19
20 #include "gtest/gtest.h"
21
22 namespace grpc_core {
23 namespace {
24
SendGranted()25 Chttp2PingRatePolicy::RequestSendPingResult SendGranted() {
26 return Chttp2PingRatePolicy::SendGranted{};
27 }
28
TooManyRecentPings()29 Chttp2PingRatePolicy::RequestSendPingResult TooManyRecentPings() {
30 return Chttp2PingRatePolicy::TooManyRecentPings{};
31 }
32
TEST(PingRatePolicy,NoOpClient)33 TEST(PingRatePolicy, NoOpClient) {
34 Chttp2PingRatePolicy policy{ChannelArgs(), true};
35 EXPECT_EQ(policy.TestOnlyMaxPingsWithoutData(), 2);
36 }
37
TEST(PingRatePolicy,NoOpServer)38 TEST(PingRatePolicy, NoOpServer) {
39 Chttp2PingRatePolicy policy{ChannelArgs(), false};
40 EXPECT_EQ(policy.TestOnlyMaxPingsWithoutData(), 0);
41 }
42
TEST(PingRatePolicy,ServerCanSendAtStart)43 TEST(PingRatePolicy, ServerCanSendAtStart) {
44 Chttp2PingRatePolicy policy{ChannelArgs(), false};
45 EXPECT_EQ(policy.RequestSendPing(Duration::Milliseconds(100), 0),
46 SendGranted());
47 }
48
TEST(PingRatePolicy,ClientBlockedUntilDataSent)49 TEST(PingRatePolicy, ClientBlockedUntilDataSent) {
50 Chttp2PingRatePolicy policy{ChannelArgs(), true};
51 EXPECT_EQ(policy.RequestSendPing(Duration::Milliseconds(10), 0),
52 TooManyRecentPings());
53 policy.ResetPingsBeforeDataRequired();
54 EXPECT_EQ(policy.RequestSendPing(Duration::Milliseconds(10), 0),
55 SendGranted());
56 policy.SentPing();
57 EXPECT_EQ(policy.RequestSendPing(Duration::Zero(), 0), SendGranted());
58 policy.SentPing();
59 EXPECT_EQ(policy.RequestSendPing(Duration::Zero(), 0), TooManyRecentPings());
60 }
61
TEST(PingRatePolicy,RateThrottlingWorks)62 TEST(PingRatePolicy, RateThrottlingWorks) {
63 Chttp2PingRatePolicy policy{ChannelArgs(), false};
64 // Observe that we can fail if we send in a tight loop
65 while (policy.RequestSendPing(Duration::Milliseconds(10), 0) ==
66 SendGranted()) {
67 policy.SentPing();
68 }
69 // Observe that we can succeed if we wait a bit between pings
70 for (int i = 0; i < 100; i++) {
71 std::this_thread::sleep_for(std::chrono::milliseconds(20));
72 EXPECT_EQ(policy.RequestSendPing(Duration::Milliseconds(10), 0),
73 SendGranted());
74 policy.SentPing();
75 }
76 }
77
TEST(PingRatePolicy,TooManyPingsInflightBlocksSendingPings)78 TEST(PingRatePolicy, TooManyPingsInflightBlocksSendingPings) {
79 Chttp2PingRatePolicy policy{ChannelArgs(), false};
80 EXPECT_EQ(policy.RequestSendPing(Duration::Milliseconds(1), 100000000),
81 TooManyRecentPings());
82 }
83
84 } // namespace
85 } // namespace grpc_core
86
main(int argc,char ** argv)87 int main(int argc, char** argv) {
88 ::testing::InitGoogleTest(&argc, argv);
89 return RUN_ALL_TESTS();
90 }
91