| 1 |
// Copyright 2022 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/flow_control.h" |
| 16 |
|
| 17 |
#include <memory> |
| 18 |
|
| 19 |
#include "gtest/gtest.h" |
| 20 |
|
| 21 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 22 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 23 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 24 |
|
| 25 |
namespace grpc_core { |
| 26 |
namespace chttp2 { |
| 27 |
|
| 28 |
namespace { |
| 29 |
auto* g_memory_owner = new MemoryOwner( |
| 30 |
ResourceQuota::Default()->memory_quota()->CreateMemoryOwner("test")); |
| 31 |
} |
| 32 |
|
| 33 |
TEST(FlowControl, NoOp) { |
| 34 |
ExecCtx exec_ctx; |
| 35 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 36 |
StreamFlowControl sfc(&tfc); |
| 37 |
// Check initial values are per http2 spec |
| 38 |
EXPECT_EQ(tfc.acked_init_window(), 65535); |
| 39 |
EXPECT_EQ(tfc.remote_window(), 65535); |
| 40 |
EXPECT_EQ(tfc.target_frame_size(), 16384); |
| 41 |
EXPECT_EQ(sfc.remote_window_delta(), 0); |
| 42 |
EXPECT_EQ(sfc.min_progress_size(), 0); |
| 43 |
EXPECT_EQ(sfc.announced_window_delta(), 0); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(FlowControl, SendData) { |
| 47 |
ExecCtx exec_ctx; |
| 48 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 49 |
StreamFlowControl sfc(&tfc); |
| 50 |
{ |
| 51 |
StreamFlowControl::OutgoingUpdateContext sfc_upd(&sfc); |
| 52 |
sfc_upd.SentData(1024); |
| 53 |
} |
| 54 |
EXPECT_EQ(sfc.remote_window_delta(), -1024); |
| 55 |
EXPECT_EQ(tfc.remote_window(), 65535 - 1024); |
| 56 |
} |
| 57 |
|
| 58 |
TEST(FlowControl, InitialTransportUpdate) { |
| 59 |
ExecCtx exec_ctx; |
| 60 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 61 |
EXPECT_EQ(TransportFlowControl::IncomingUpdateContext(&tfc).MakeAction(), |
| 62 |
FlowControlAction()); |
| 63 |
} |
| 64 |
|
| 65 |
TEST(FlowControl, InitialStreamUpdate) { |
| 66 |
ExecCtx exec_ctx; |
| 67 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 68 |
StreamFlowControl sfc(&tfc); |
| 69 |
EXPECT_EQ(StreamFlowControl::IncomingUpdateContext(&sfc).MakeAction(), |
| 70 |
FlowControlAction()); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(FlowControl, RecvData) { |
| 74 |
ExecCtx exec_ctx; |
| 75 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 76 |
StreamFlowControl sfc(&tfc); |
| 77 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 78 |
EXPECT_EQ(absl::OkStatus(), sfc_upd.RecvData(1024)); |
| 79 |
sfc_upd.MakeAction(); |
| 80 |
EXPECT_EQ(tfc.announced_window(), 65535 - 1024); |
| 81 |
EXPECT_EQ(sfc.announced_window_delta(), -1024); |
| 82 |
} |
| 83 |
|
| 84 |
TEST(FlowControl, TrackMinProgressSize) { |
| 85 |
ExecCtx exec_ctx; |
| 86 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 87 |
StreamFlowControl sfc(&tfc); |
| 88 |
{ |
| 89 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 90 |
sfc_upd.SetMinProgressSize(5); |
| 91 |
sfc_upd.MakeAction(); |
| 92 |
} |
| 93 |
EXPECT_EQ(sfc.min_progress_size(), 5); |
| 94 |
{ |
| 95 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 96 |
sfc_upd.SetMinProgressSize(10); |
| 97 |
sfc_upd.MakeAction(); |
| 98 |
} |
| 99 |
EXPECT_EQ(sfc.min_progress_size(), 10); |
| 100 |
{ |
| 101 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 102 |
EXPECT_EQ(absl::OkStatus(), sfc_upd.RecvData(5)); |
| 103 |
sfc_upd.MakeAction(); |
| 104 |
} |
| 105 |
EXPECT_EQ(sfc.min_progress_size(), 5); |
| 106 |
{ |
| 107 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 108 |
EXPECT_EQ(absl::OkStatus(), sfc_upd.RecvData(5)); |
| 109 |
sfc_upd.MakeAction(); |
| 110 |
} |
| 111 |
EXPECT_EQ(sfc.min_progress_size(), 0); |
| 112 |
{ |
| 113 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 114 |
EXPECT_EQ(absl::OkStatus(), sfc_upd.RecvData(5)); |
| 115 |
sfc_upd.MakeAction(); |
| 116 |
} |
| 117 |
EXPECT_EQ(sfc.min_progress_size(), 0); |
| 118 |
} |
| 119 |
|
| 120 |
TEST(FlowControl, NoUpdateWithoutReader) { |
| 121 |
ExecCtx exec_ctx; |
| 122 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 123 |
StreamFlowControl sfc(&tfc); |
| 124 |
for (int i = 0; i < 65535; i++) { |
| 125 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 126 |
EXPECT_EQ(sfc_upd.RecvData(1), absl::OkStatus()); |
| 127 |
EXPECT_EQ(sfc_upd.MakeAction().send_stream_update(), |
| 128 |
FlowControlAction::Urgency::NO_ACTION_NEEDED); |
| 129 |
} |
| 130 |
// Empty window needing 1 byte to progress should trigger an immediate read. |
| 131 |
{ |
| 132 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 133 |
sfc_upd.SetMinProgressSize(1); |
| 134 |
EXPECT_EQ(sfc.min_progress_size(), 1); |
| 135 |
EXPECT_EQ(sfc_upd.MakeAction().send_stream_update(), |
| 136 |
FlowControlAction::Urgency::UPDATE_IMMEDIATELY); |
| 137 |
} |
| 138 |
EXPECT_GT(tfc.MaybeSendUpdate(false), 0); |
| 139 |
EXPECT_GT(sfc.MaybeSendUpdate(), 0); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(FlowControl, GradualReadsUpdate) { |
| 143 |
ExecCtx exec_ctx; |
| 144 |
TransportFlowControl tfc("test", true, g_memory_owner); |
| 145 |
StreamFlowControl sfc(&tfc); |
| 146 |
int immediate_updates = 0; |
| 147 |
int queued_updates = 0; |
| 148 |
for (int i = 0; i < 65535; i++) { |
| 149 |
StreamFlowControl::IncomingUpdateContext sfc_upd(&sfc); |
| 150 |
EXPECT_EQ(sfc_upd.RecvData(1), absl::OkStatus()); |
| 151 |
sfc_upd.SetPendingSize(0); |
| 152 |
switch (sfc_upd.MakeAction().send_stream_update()) { |
| 153 |
case FlowControlAction::Urgency::UPDATE_IMMEDIATELY: |
| 154 |
immediate_updates++; |
| 155 |
break; |
| 156 |
case FlowControlAction::Urgency::QUEUE_UPDATE: |
| 157 |
queued_updates++; |
| 158 |
break; |
| 159 |
case FlowControlAction::Urgency::NO_ACTION_NEEDED: |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
EXPECT_GE(immediate_updates, 0); |
| 164 |
EXPECT_GT(queued_updates, 0); |
| 165 |
EXPECT_EQ(immediate_updates + queued_updates, 65535); |
| 166 |
} |
| 167 |
|
| 168 |
} // namespace chttp2 |
| 169 |
} // namespace grpc_core |
| 170 |
|
| 171 |
int main(int argc, char** argv) { |
| 172 |
::testing::InitGoogleTest(&argc, argv); |
| 173 |
return RUN_ALL_TESTS(); |
| 174 |
} |