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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_stream_pool_ip_endpoint_state_tracker.h"
#include <memory>
#include <optional>
#include <string_view>
#include <vector>
#include "net/base/network_anonymization_key.h"
#include "net/dns/host_resolver.h"
#include "net/http/http_stream_pool.h"
#include "net/http/http_stream_pool_test_util.h"
#include "net/log/net_log_with_source.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
using ::testing::Optional;
namespace net {
using IPEndPointStateTracker = HttpStreamPool::IPEndPointStateTracker;
using IPEndPointState = IPEndPointStateTracker::IPEndPointState;
namespace {
static const url::SchemeHostPort kDestination("https", "www.example.org", 443);
IPEndPoint MakeIPEndPoint(std::string_view addr, uint16_t port = 80) {
return IPEndPoint(*IPAddress::FromIPLiteral(addr), port);
}
class FakeDelegate : public IPEndPointStateTracker::Delegate {
public:
explicit FakeDelegate(std::unique_ptr<HostResolver::ServiceEndpointRequest>
service_endpoint_request)
: service_endpoint_request_(std::move(service_endpoint_request)) {}
~FakeDelegate() override = default;
FakeDelegate(const FakeDelegate&) = delete;
FakeDelegate& operator=(const FakeDelegate&) = delete;
// HttpStreamPool::IPEndPointStateTracker::Delegate:
HostResolver::ServiceEndpointRequest* GetServiceEndpointRequest() override {
return service_endpoint_request_.get();
}
bool IsSvcbOptional() override { return false; }
bool IsEndpointUsableForTcpBasedAttempt(const ServiceEndpoint& endpoint,
bool svcb_optional) override {
return true;
}
bool HasEnoughTcpBasedAttemptsForSlowIPEndPoint(
const IPEndPoint& ip_endpoint) override {
return false;
}
private:
std::unique_ptr<HostResolver::ServiceEndpointRequest>
service_endpoint_request_;
};
} // namespace
class HttpStreamPoolIPEndPointStateTrackerTest
: public TestWithTaskEnvironment {
public:
HttpStreamPoolIPEndPointStateTrackerTest()
: TestWithTaskEnvironment(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
~HttpStreamPoolIPEndPointStateTrackerTest() override = default;
protected:
std::unique_ptr<FakeDelegate> CreateDelegate() {
std::unique_ptr<HostResolver::ServiceEndpointRequest>
service_endpoint_request = resolver_.CreateServiceEndpointRequest(
HostResolver::Host(kDestination), NetworkAnonymizationKey(),
NetLogWithSource(), HostResolver::ResolveHostParameters());
auto delegate =
std::make_unique<FakeDelegate>(std::move(service_endpoint_request));
return delegate;
}
FakeServiceEndpointResolver& resolver() { return resolver_; }
private:
FakeServiceEndpointResolver resolver_;
};
TEST_F(HttpStreamPoolIPEndPointStateTrackerTest, GetIPEndPointToAttempt) {
struct TestCase {
std::string_view description = "";
std::vector<std::pair<IPEndPoint, std::optional<IPEndPointState>>>
endpoint_states;
std::optional<IPEndPoint> exclude_ip_endpoint = std::nullopt;
std::optional<IPEndPoint> expected;
} kTestCases[] = {
{
.description = "Prefer fast or non-attempted endpoint",
.endpoint_states =
{
{MakeIPEndPoint("2001:db8::1"),
IPEndPointState::kSlowAttempting},
{MakeIPEndPoint("2001:db8::2"), std::nullopt},
{MakeIPEndPoint("192.0.2.1"),
IPEndPointState::kSlowSucceeded},
},
.expected = MakeIPEndPoint("2001:db8::2"),
},
{
.description = "Prefer slow-succeeded to slow-attempting",
.endpoint_states =
{
{MakeIPEndPoint("2001:db8::1"),
IPEndPointState::kSlowAttempting},
{MakeIPEndPoint("192.0.2.1"),
IPEndPointState::kSlowSucceeded},
{MakeIPEndPoint("192.0.2.2"),
IPEndPointState::kSlowSucceeded},
},
.expected = MakeIPEndPoint("192.0.2.1"),
},
{
.description = "Slow-attempting and failed only",
.endpoint_states =
{
{MakeIPEndPoint("2001:db8::1"),
IPEndPointState::kSlowAttempting},
{MakeIPEndPoint("2001:db8::2"),
IPEndPointState::kSlowAttempting},
{MakeIPEndPoint("192.0.2.1"), IPEndPointState::kFailed},
},
.expected = MakeIPEndPoint("2001:db8::1"),
},
{
.description = "Failed endpoint only",
.endpoint_states = {{
{MakeIPEndPoint("2001:db8::1"), IPEndPointState::kFailed},
{MakeIPEndPoint("192.0.2.1"), IPEndPointState::kFailed},
}},
.expected = std::nullopt,
},
{
.description = "Exclude an endpoint that is the only endpoint",
.endpoint_states = {{
{MakeIPEndPoint("2001:db8::1"), IPEndPointState::kSlowAttempting},
}},
.exclude_ip_endpoint = MakeIPEndPoint("2001:db8::1"),
.expected = std::nullopt,
},
};
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.description);
// Setup ServiceEndpointRequest.
ServiceEndpointBuilder service_endpoint_builder;
for (const auto& [ip_endpoint, state] : test_case.endpoint_states) {
service_endpoint_builder.add_ip_endpoint(ip_endpoint);
}
resolver()
.ConfigureDefaultResolution()
.add_endpoint(service_endpoint_builder.endpoint())
.CompleteStartSynchronously(OK);
std::unique_ptr<FakeDelegate> delegate = CreateDelegate();
IPEndPointStateTracker tracker(delegate.get());
// Setup IPEndPoint states.
for (const auto& [ip_endpoint, state] : test_case.endpoint_states) {
if (!state.has_value()) {
continue;
}
switch (*state) {
case IPEndPointState::kFailed:
tracker.OnEndpointFailed(ip_endpoint);
break;
case IPEndPointState::kSlowAttempting:
tracker.OnEndpointSlow(ip_endpoint);
break;
case IPEndPointState::kSlowSucceeded:
// Mark slow first then mark slow succeeded.
tracker.OnEndpointSlow(ip_endpoint);
tracker.OnEndpointSlowSucceeded(ip_endpoint);
break;
default:
NOTREACHED();
}
}
std::optional<IPEndPoint> actual =
tracker.GetIPEndPointToAttemptTcpBased(test_case.exclude_ip_endpoint);
EXPECT_THAT(actual, test_case.expected);
}
}
TEST_F(HttpStreamPoolIPEndPointStateTrackerTest,
IPEndPointSlowSuccessSlowFail) {
const IPEndPoint ip_endpoint = MakeIPEndPoint("2001:db8::1");
resolver()
.ConfigureDefaultResolution()
.add_endpoint(
ServiceEndpointBuilder().add_ip_endpoint(ip_endpoint).endpoint())
.CompleteStartSynchronously(OK);
std::unique_ptr<FakeDelegate> delegate = CreateDelegate();
IPEndPointStateTracker tracker(delegate.get());
// The first attempt is slow and succeeds.
tracker.OnEndpointSlow(ip_endpoint);
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kSlowAttempting));
tracker.OnEndpointSlowSucceeded(ip_endpoint);
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kSlowSucceeded));
// The second attempt is slow and succeeds.
tracker.OnEndpointSlow(ip_endpoint);
// The state should not be updated.
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kSlowSucceeded));
tracker.OnEndpointSlowSucceeded(ip_endpoint);
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kSlowSucceeded));
// The third attempt is slow and fails.
tracker.OnEndpointSlow(ip_endpoint);
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kSlowSucceeded));
tracker.OnEndpointFailed(ip_endpoint);
EXPECT_THAT(tracker.GetState(ip_endpoint),
Optional(IPEndPointState::kFailed));
}
TEST_F(HttpStreamPoolIPEndPointStateTrackerTest, PreferNonSlowIPEndPoint) {
const IPEndPoint ip_endpoint_v6 = MakeIPEndPoint("2001:db8::1");
const IPEndPoint ip_endpoint_v4 = MakeIPEndPoint("192.0.2.1");
resolver()
.ConfigureDefaultResolution()
.add_endpoint(ServiceEndpointBuilder()
.add_ip_endpoint(ip_endpoint_v6)
.add_ip_endpoint(ip_endpoint_v4)
.endpoint())
.CompleteStartSynchronously(OK);
std::unique_ptr<FakeDelegate> delegate = CreateDelegate();
IPEndPointStateTracker tracker(delegate.get());
// The first attempt gets the IPv6 endpoint. Mark the endpoint as slow and
// successful.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_v6));
tracker.OnEndpointSlow(ip_endpoint_v6);
tracker.OnEndpointSlowSucceeded(ip_endpoint_v6);
EXPECT_THAT(tracker.GetState(ip_endpoint_v6),
Optional(IPEndPointState::kSlowSucceeded));
// Subsequent attempts get the IPv4 endpoint because it is considered as
// non-slow.
// TODO(crbug.com/383606724): Note that there is no callback for non-slow
// success attempts. We would want to add non-slow success callback to
// improve the logic to select IPEndPoints.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_v4));
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_v4));
EXPECT_FALSE(tracker.GetState(ip_endpoint_v4).has_value());
}
// Tests that a slow-attempting endpoint is used as a last resort option.
TEST_F(HttpStreamPoolIPEndPointStateTrackerTest, UseSlowAttemptingIPEndPoint) {
// An endpoint that is slow.
const IPEndPoint ip_endpoint_slow = MakeIPEndPoint("2001:db8::1");
// An endpoint that fails.
const IPEndPoint ip_endpoint_failure = MakeIPEndPoint("192.0.2.1");
resolver()
.ConfigureDefaultResolution()
.add_endpoint(ServiceEndpointBuilder()
.add_ip_endpoint(ip_endpoint_slow)
.add_ip_endpoint(ip_endpoint_failure)
.endpoint())
.CompleteStartSynchronously(OK);
std::unique_ptr<FakeDelegate> delegate = CreateDelegate();
IPEndPointStateTracker tracker(delegate.get());
// The first attempt gets `ip_endpoint_slow` because the endpoint isn't
// attempted yet and considered as non-slow. Mark the endpoint slow
// attempting.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_slow));
tracker.OnEndpointSlow(ip_endpoint_slow);
EXPECT_THAT(tracker.GetState(ip_endpoint_slow),
Optional(IPEndPointState::kSlowAttempting));
// The second attempt gets `ip_endpoint_failure` because the endpoint isn't
// attempted yet. Mark `ip_endpoint_failure` failed.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_failure));
tracker.OnEndpointFailed(ip_endpoint_failure);
EXPECT_THAT(tracker.GetState(ip_endpoint_failure),
Optional(IPEndPointState::kFailed));
// The third attempt gets `ip_endpoint_slow` as a last resort option.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_slow));
}
TEST_F(HttpStreamPoolIPEndPointStateTrackerTest,
PreferSlowSucceededToSlowAttempting) {
// An endpoint that is slow but succeeds.
const IPEndPoint ip_endpoint_slow_success = MakeIPEndPoint("2001:db8::1");
// An endpoint that is slow.
const IPEndPoint ip_endpoint_slow = MakeIPEndPoint("192.0.2.1");
resolver()
.ConfigureDefaultResolution()
.add_endpoint(ServiceEndpointBuilder()
.add_ip_endpoint(ip_endpoint_slow_success)
.add_ip_endpoint(ip_endpoint_slow)
.endpoint())
.CompleteStartSynchronously(OK);
std::unique_ptr<FakeDelegate> delegate = CreateDelegate();
IPEndPointStateTracker tracker(delegate.get());
// The first attempt gets `ip_endpoint_slow_success`. Mark the endpoint slow
// succeeded.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_slow_success));
tracker.OnEndpointSlow(ip_endpoint_slow_success);
tracker.OnEndpointSlowSucceeded(ip_endpoint_slow_success);
EXPECT_THAT(tracker.GetState(ip_endpoint_slow_success),
Optional(IPEndPointState::kSlowSucceeded));
// The second attempt gets `ip_endpoint_slow` because the endpoint isn't
// attempted yet. Mark `ip_endpoint_slow` slow.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_slow));
tracker.OnEndpointSlow(ip_endpoint_slow);
EXPECT_THAT(tracker.GetState(ip_endpoint_slow),
Optional(IPEndPointState::kSlowAttempting));
// The third attempt gets `ip_endpoint_slow_success` because we prefer
// succeeded endpoints than slow attempting endpoints.
EXPECT_THAT(tracker.GetIPEndPointToAttemptTcpBased(),
Optional(ip_endpoint_slow_success));
}
} // namespace net
|