1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
content / browser / webtransport / web_transport_throttle_context_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "web_transport_throttle_context.h"
#include "base/check_op.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
using ThrottleResult = WebTransportThrottleContext::ThrottleResult;
using Tracker = WebTransportThrottleContext::Tracker;
class WebTransportThrottleContextTest : public testing::Test {
public:
WebTransportThrottleContext& context() { return context_; }
void FastForwardBy(base::TimeDelta delta) {
task_environment_.FastForwardBy(delta);
}
void CreatePending(int count) {
for (int i = 0; i < count; ++i) {
base::RunLoop run_loop;
auto result = context().PerformThrottle(base::BindLambdaForTesting(
[&run_loop, this](std::unique_ptr<Tracker> tracker) {
trackers_.push(std::move(tracker));
run_loop.Quit();
}));
EXPECT_EQ(ThrottleResult::kOk, result);
run_loop.Run();
}
}
// Causes the first `count` pending handshakes to be signalled established.
void EstablishPending(int count) {
DCHECK_LE(count, static_cast<int>(trackers_.size()));
for (int i = 0; i < count; ++i) {
trackers_.front()->OnHandshakeEstablished();
trackers_.pop();
}
}
// Causes the first `count` pending handshakes to be signalled failed.
void FailPending(int count) {
DCHECK_LE(count, static_cast<int>(trackers_.size()));
for (int i = 0; i < count; ++i) {
trackers_.front()->OnHandshakeFailed();
trackers_.pop();
}
}
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
WebTransportThrottleContext context_;
base::queue<std::unique_ptr<Tracker>> trackers_;
};
class CallTrackingCallback {
public:
WebTransportThrottleContext::ThrottleDoneCallback Callback() {
// This use of base::Unretained is safe because the
// WebTransportThrottleContext is always destroyed at the end of the test
// before it gets a change to call any callbacks.
return base::BindOnce(&CallTrackingCallback::OnCall,
base::Unretained(this));
}
bool called() const { return called_; }
private:
void OnCall(std::unique_ptr<Tracker> tracker) { called_ = true; }
bool called_ = false;
};
TEST_F(WebTransportThrottleContextTest, PerformThrottleSyncWithNonePending) {
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, PerformThrottleNotSyncWithOnePending) {
CreatePending(1);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EXPECT_FALSE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, Max64Connections) {
CreatePending(64);
CallTrackingCallback callback;
EXPECT_EQ(ThrottleResult::kTooManyPendingSessions,
context().PerformThrottle(callback.Callback()));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, DelayWithOnePending) {
CreatePending(1);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
// The delay should be at least 5ms.
FastForwardBy(base::Milliseconds(4));
EXPECT_FALSE(callback.called());
// The delay should be less than 16ms.
FastForwardBy(base::Milliseconds(12));
EXPECT_TRUE(callback.called());
}
// The reason for testing with 3 pending connections is that the possible range
// of delays doesn't overlap with 1 pending connection.
TEST_F(WebTransportThrottleContextTest, DelayWithThreePending) {
CreatePending(3);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
// The delay should be at least 20ms.
FastForwardBy(base::Milliseconds(19));
EXPECT_FALSE(callback.called());
// The delay should be less than 61ms.
FastForwardBy(base::Milliseconds(42));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, DelayIsTruncated) {
CreatePending(63);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
// The delay should be no less than 30s.
FastForwardBy(base::Seconds(29));
EXPECT_FALSE(callback.called());
// The delay should be less than 91s.
FastForwardBy(base::Seconds(62));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, EstablishedRemainsPendingFor10ms) {
CreatePending(1);
EstablishPending(1);
// The delay should be more than 9ms.
FastForwardBy(base::Milliseconds(9));
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EXPECT_FALSE(callback.called());
// The delay should be less than 11ms.
FastForwardBy(base::Milliseconds(2));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, FailedRemainsPendingFor3m) {
CreatePending(1);
FailPending(1);
// The delay should be more than 299 seconds.
FastForwardBy(base::Seconds(299));
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EXPECT_FALSE(callback.called());
// The delay should be less than 301 seconds.
FastForwardBy(base::Seconds(301));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, HandshakeCompletionTriggersPending) {
CreatePending(3);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EstablishPending(3);
// After 10ms the handshakes should no longer be pending and the pending
// throttle should fire.
FastForwardBy(base::Milliseconds(10));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest, HandshakeCompletionResetsTimer) {
CreatePending(5);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
EstablishPending(2);
// After 10ms the handshakes should no longer be pending and the timer for the
// pending throttle should be reset.
FastForwardBy(base::Milliseconds(10));
// The 10ms should be counted towards the throttling time.
// There should be more than 9ms remaining.
FastForwardBy(base::Milliseconds(9));
EXPECT_FALSE(callback.called());
// There should be less than 51 ms remaining.
FastForwardBy(base::Milliseconds(42));
EXPECT_TRUE(callback.called());
}
TEST_F(WebTransportThrottleContextTest,
ManyHandshakeCompletionsTriggerPending) {
CreatePending(63);
CallTrackingCallback callback;
const auto result = context().PerformThrottle(callback.Callback());
EXPECT_EQ(result, ThrottleResult::kOk);
// Move time forward so that the maximum delay for a handshake with one
// pending has passed.
FastForwardBy(base::Milliseconds(15));
// Leave one pending or the pending handshake will be triggered without
// recalculating the delay.
EstablishPending(62);
// After 10ms the handshakes should no longer be pending and the pending
// connection throttle timer should have fired.
FastForwardBy(base::Milliseconds(10));
EXPECT_TRUE(callback.called());
}
} // namespace
} // namespace content