grpc
test
core
transport
chttp2
flow_control_test.cc
Go to the documentation of this file.
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 <gtest/gtest.h>
18
19
#include "
src/core/lib/iomgr/exec_ctx.h
"
20
#include "
src/core/lib/resource_quota/resource_quota.h
"
21
22
namespace
grpc_core
{
23
namespace
chttp2 {
24
25
namespace
{
26
auto
* g_memory_owner =
new
MemoryOwner(
27
ResourceQuota::Default
()->memory_quota()->CreateMemoryOwner(
"test"
));
28
}
29
30
TEST
(FlowControl,
NoOp
) {
31
ExecCtx
exec_ctx
;
32
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
33
StreamFlowControl
sfc(&tfc);
34
// Check initial values are per http2 spec
35
EXPECT_EQ
(tfc.
acked_init_window
(), 65535);
36
EXPECT_EQ
(tfc.
remote_window
(), 65535);
37
EXPECT_EQ
(tfc.
target_frame_size
(), 16384);
38
EXPECT_EQ
(sfc.
remote_window_delta
(), 0);
39
EXPECT_EQ
(sfc.
min_progress_size
(), 0);
40
EXPECT_EQ
(sfc.
announced_window_delta
(), 0);
41
}
42
43
TEST
(FlowControl, SendData) {
44
ExecCtx
exec_ctx
;
45
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
46
StreamFlowControl
sfc(&tfc);
47
{
48
StreamFlowControl::OutgoingUpdateContext
sfc_upd(&sfc);
49
sfc_upd.
SentData
(1024);
50
}
51
EXPECT_EQ
(sfc.
remote_window_delta
(), -1024);
52
EXPECT_EQ
(tfc.
remote_window
(), 65535 - 1024);
53
}
54
55
TEST
(FlowControl, InitialTransportUpdate) {
56
ExecCtx
exec_ctx
;
57
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
58
EXPECT_EQ
(
TransportFlowControl::IncomingUpdateContext
(&tfc).
MakeAction
(),
59
FlowControlAction
());
60
}
61
62
TEST
(FlowControl, InitialStreamUpdate) {
63
ExecCtx
exec_ctx
;
64
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
65
StreamFlowControl
sfc(&tfc);
66
EXPECT_EQ
(
StreamFlowControl::IncomingUpdateContext
(&sfc).
MakeAction
(),
67
FlowControlAction
());
68
}
69
70
TEST
(FlowControl, RecvData) {
71
ExecCtx
exec_ctx
;
72
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
73
StreamFlowControl
sfc(&tfc);
74
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
75
EXPECT_EQ
(
absl::OkStatus
(), sfc_upd.
RecvData
(1024));
76
sfc_upd.
MakeAction
();
77
EXPECT_EQ
(tfc.
announced_window
(), 65535 - 1024);
78
EXPECT_EQ
(sfc.
announced_window_delta
(), -1024);
79
}
80
81
TEST
(FlowControl, TrackMinProgressSize) {
82
ExecCtx
exec_ctx
;
83
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
84
StreamFlowControl
sfc(&tfc);
85
{
86
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
87
sfc_upd.
SetMinProgressSize
(5);
88
sfc_upd.
MakeAction
();
89
}
90
EXPECT_EQ
(sfc.
min_progress_size
(), 5);
91
{
92
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
93
sfc_upd.
SetMinProgressSize
(10);
94
sfc_upd.
MakeAction
();
95
}
96
EXPECT_EQ
(sfc.
min_progress_size
(), 10);
97
{
98
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
99
EXPECT_EQ
(
absl::OkStatus
(), sfc_upd.
RecvData
(5));
100
sfc_upd.
MakeAction
();
101
}
102
EXPECT_EQ
(sfc.
min_progress_size
(), 5);
103
{
104
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
105
EXPECT_EQ
(
absl::OkStatus
(), sfc_upd.
RecvData
(5));
106
sfc_upd.
MakeAction
();
107
}
108
EXPECT_EQ
(sfc.
min_progress_size
(), 0);
109
{
110
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
111
EXPECT_EQ
(
absl::OkStatus
(), sfc_upd.
RecvData
(5));
112
sfc_upd.
MakeAction
();
113
}
114
EXPECT_EQ
(sfc.
min_progress_size
(), 0);
115
}
116
117
TEST
(FlowControl, NoUpdateWithoutReader) {
118
ExecCtx
exec_ctx
;
119
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
120
StreamFlowControl
sfc(&tfc);
121
for
(
int
i
= 0;
i
< 65535;
i
++) {
122
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
123
EXPECT_EQ
(sfc_upd.
RecvData
(1),
absl::OkStatus
());
124
EXPECT_EQ
(sfc_upd.
MakeAction
().
send_stream_update
(),
125
FlowControlAction::Urgency::NO_ACTION_NEEDED
);
126
}
127
// Empty window needing 1 byte to progress should trigger an immediate read.
128
{
129
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
130
sfc_upd.
SetMinProgressSize
(1);
131
EXPECT_EQ
(sfc.
min_progress_size
(), 1);
132
EXPECT_EQ
(sfc_upd.
MakeAction
().
send_stream_update
(),
133
FlowControlAction::Urgency::UPDATE_IMMEDIATELY
);
134
}
135
EXPECT_GT
(tfc.
MaybeSendUpdate
(
false
), 0);
136
EXPECT_GT
(sfc.
MaybeSendUpdate
(), 0);
137
}
138
139
TEST
(FlowControl, GradualReadsUpdate) {
140
ExecCtx
exec_ctx
;
141
TransportFlowControl
tfc(
"test"
,
true
, g_memory_owner);
142
StreamFlowControl
sfc(&tfc);
143
int
immediate_updates = 0;
144
int
queued_updates = 0;
145
for
(
int
i
= 0;
i
< 65535;
i
++) {
146
StreamFlowControl::IncomingUpdateContext
sfc_upd(&sfc);
147
EXPECT_EQ
(sfc_upd.
RecvData
(1),
absl::OkStatus
());
148
sfc_upd.
SetPendingSize
(0);
149
switch
(sfc_upd.
MakeAction
().
send_stream_update
()) {
150
case
FlowControlAction::Urgency::UPDATE_IMMEDIATELY
:
151
immediate_updates++;
152
break
;
153
case
FlowControlAction::Urgency::QUEUE_UPDATE
:
154
queued_updates++;
155
break
;
156
case
FlowControlAction::Urgency::NO_ACTION_NEEDED
:
157
break
;
158
}
159
}
160
EXPECT_GT
(immediate_updates, 0);
161
EXPECT_GT
(queued_updates, 0);
162
EXPECT_EQ
(immediate_updates + queued_updates, 65535);
163
}
164
165
}
// namespace chttp2
166
}
// namespace grpc_core
167
168
int
main
(
int
argc,
char
** argv) {
169
::testing::InitGoogleTest
(&argc, argv);
170
return
RUN_ALL_TESTS
();
171
}
grpc_core::chttp2::StreamFlowControl::min_progress_size
uint32_t min_progress_size() const
Definition:
flow_control.h:353
grpc_core::chttp2::FlowControlAction::Urgency::UPDATE_IMMEDIATELY
@ UPDATE_IMMEDIATELY
grpc_core::chttp2::TEST
TEST(FlowControl, NoOp)
Definition:
flow_control_test.cc:30
grpc_core::chttp2::FlowControlAction::Urgency::QUEUE_UPDATE
@ QUEUE_UPDATE
grpc_core::chttp2::StreamFlowControl::IncomingUpdateContext::SetPendingSize
void SetPendingSize(int64_t pending_size)
Definition:
flow_control.cc:307
NoOp
Definition:
bm_call_create.cc:477
grpc_core
Definition:
call_metric_recorder.h:31
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition:
bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
absl::OkStatus
Status OkStatus()
Definition:
third_party/abseil-cpp/absl/status/status.h:882
grpc_core::chttp2::TransportFlowControl::announced_window
int64_t announced_window() const
Definition:
flow_control.h:240
testing::MakeAction
Action< F > MakeAction(ActionInterface< F > *impl)
Definition:
bloaty/third_party/googletest/googlemock/include/gmock/gmock-actions.h:460
grpc_core::chttp2::FlowControlAction::Urgency::NO_ACTION_NEEDED
@ NO_ACTION_NEEDED
grpc_core::chttp2::StreamFlowControl::IncomingUpdateContext::MakeAction
FlowControlAction MakeAction()
Definition:
flow_control.h:308
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition:
iomgr/time_averaged_stats_test.cc:27
grpc_core::chttp2::StreamFlowControl::OutgoingUpdateContext::SentData
void SentData(int64_t outgoing_frame_size)
Definition:
flow_control.h:337
grpc_core::chttp2::TransportFlowControl
Definition:
flow_control.h:145
grpc_core::chttp2::StreamFlowControl::IncomingUpdateContext
Definition:
flow_control.h:303
grpc_core::chttp2::TransportFlowControl::target_frame_size
int64_t target_frame_size() const
Definition:
flow_control.h:230
grpc_core::chttp2::StreamFlowControl::IncomingUpdateContext::SetMinProgressSize
void SetMinProgressSize(uint32_t min_progress_size)
Definition:
flow_control.h:316
main
int main(int argc, char **argv)
Definition:
flow_control_test.cc:168
grpc_core::chttp2::TransportFlowControl::remote_window
int64_t remote_window() const
Definition:
flow_control.h:239
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition:
resource_quota.cc:27
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition:
bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::chttp2::FlowControlAction
Definition:
flow_control.h:71
grpc_core::chttp2::TransportFlowControl::MaybeSendUpdate
uint32_t MaybeSendUpdate(bool writing_anyway)
Definition:
flow_control.cc:117
resource_quota.h
grpc_core::chttp2::StreamFlowControl::announced_window_delta
int64_t announced_window_delta() const
Definition:
flow_control.h:352
grpc_core::ExecCtx
Definition:
exec_ctx.h:97
grpc_core::chttp2::StreamFlowControl
Definition:
flow_control.h:293
grpc_core::chttp2::StreamFlowControl::IncomingUpdateContext::RecvData
absl::Status RecvData(int64_t incoming_frame_size)
Definition:
flow_control.cc:133
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition:
bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::chttp2::TransportFlowControl::IncomingUpdateContext
Definition:
flow_control.h:162
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition:
end2end_binder_transport_test.cc:75
flow_control.h
grpc_core::chttp2::StreamFlowControl::remote_window_delta
int64_t remote_window_delta() const
Definition:
flow_control.h:351
exec_ctx.h
grpc_core::chttp2::FlowControlAction::send_stream_update
Urgency send_stream_update() const
Definition:
flow_control.h:83
grpc_core::chttp2::StreamFlowControl::OutgoingUpdateContext
Definition:
flow_control.h:329
grpc_core::chttp2::StreamFlowControl::MaybeSendUpdate
uint32_t MaybeSendUpdate()
Definition:
flow_control.cc:266
grpc_core::chttp2::TransportFlowControl::acked_init_window
uint32_t acked_init_window() const
Definition:
flow_control.h:234
i
uint64_t i
Definition:
abseil-cpp/absl/container/btree_benchmark.cc:230
grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:24