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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
// 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/socket/socket_pool_additional_capacity.h"
#include "base/notimplemented.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "net/base/features.h"
#include "net/socket/client_socket_pool.h"
#include "net/socket/connect_job_factory.h"
#include "net/socket/socket_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/fuzztest/src/fuzztest/fuzztest.h"
namespace net {
namespace {
TEST(SocketPoolAdditionalCapacityTest, CreateWithDisabledFeature) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndDisableFeature(
features::kTcpSocketPoolLimitRandomization);
EXPECT_EQ(SocketPoolAdditionalCapacity::Create(),
SocketPoolAdditionalCapacity::CreateEmpty());
}
TEST(SocketPoolAdditionalCapacityTest, CreateWithEnabledFeature) {
base::test::ScopedFeatureList scoped_feature_list;
scoped_feature_list.InitAndEnableFeatureWithParameters(
features::kTcpSocketPoolLimitRandomization,
{
{
"TcpSocketPoolLimitRandomizationBase",
"0.1",
},
{
"TcpSocketPoolLimitRandomizationCapacity",
"2",
},
{
"TcpSocketPoolLimitRandomizationMinimum",
"0.3",
},
{
"TcpSocketPoolLimitRandomizationNoise",
"0.4",
},
});
EXPECT_EQ(SocketPoolAdditionalCapacity::Create(),
SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 0.3, 0.4));
}
TEST(SocketPoolAdditionalCapacityTest, CreateForTest) {
EXPECT_EQ(std::string(
SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 0.3, 0.4)),
"SocketPoolAdditionalCapacity(base:1.000000e-01,capacity:2,minimum:"
"3.000000e-01,noise:4.000000e-01)");
}
TEST(SocketPoolAdditionalCapacityTest, InvalidCreation) {
const SocketPoolAdditionalCapacity empty_pool =
SocketPoolAdditionalCapacity::CreateEmpty();
// base range
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(-0.1, 2, 0.3, 0.4),
empty_pool);
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(1.1, 2, 0.3, 0.4),
empty_pool);
EXPECT_EQ(
SocketPoolAdditionalCapacity::CreateForTest(std::nan(""), 2, 0.3, 0.4),
empty_pool);
// capacity range
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(0.1, 2000, 0.3, 0.4),
empty_pool);
// minimum range
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, -0.3, 0.4),
empty_pool);
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 1.3, 0.4),
empty_pool);
EXPECT_EQ(
SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, std::nan(""), 0.4),
empty_pool);
// noise range
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 0.3, -0.4),
empty_pool);
EXPECT_EQ(SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 0.3, 1.4),
empty_pool);
EXPECT_EQ(
SocketPoolAdditionalCapacity::CreateForTest(0.1, 2, 0.3, std::nan("")),
empty_pool);
}
TEST(SocketPoolAdditionalCapacityTest, NextStateBeforeAllocation) {
// We use a base and noise of 0.0 with a minimum of 0.5 to ensure every roll
// is a 50/50 shot so that we don't need to run the test millions of times
// for flakes to be noticeable. The capacity of 2 is needed to test the logic.
SocketPoolAdditionalCapacity pool_capacity =
SocketPoolAdditionalCapacity::CreateForTest(0.0, 2, 0.5, 0.0);
// Test out of bounds cases
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kUncapped, 5, 2));
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 5, 2));
// Below soft cap we are always uncapped
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kUncapped, 0, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 0, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kUncapped, 1, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 1, 2));
// At hard cap we are always capped
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 4, 2));
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kUncapped, 4, 2));
// If capped at or above soft cap we always stay that way
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 2, 2));
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kCapped, 3, 2));
// When uncapped between soft and hard cap, we should be able to see some
// distribution of each option. The probability inputs here should make it
// a coin toss, but to prevent this from being flakey we run it 1000 times.
bool did_see_uncapped = false;
bool did_see_capped = false;
for (size_t i = 0; i < 1000; ++i) {
if (SocketPoolState::kUncapped == pool_capacity.NextStateBeforeAllocation(
SocketPoolState::kUncapped, 3, 2)) {
did_see_uncapped = true;
} else {
did_see_capped = true;
}
}
EXPECT_TRUE(did_see_uncapped);
EXPECT_TRUE(did_see_capped);
}
TEST(SocketPoolAdditionalCapacityTest, NextStateAfterRelease) {
// We use a base and noise of 0.0 with a minimum of 0.5 to ensure every roll
// is a 50/50 shot so that we don't need to run the test millions of times
// for flakes to be noticeable. The capacity of 2 is needed to test the logic.
SocketPoolAdditionalCapacity pool_capacity =
SocketPoolAdditionalCapacity::CreateForTest(0.0, 2, 0.5, 0.0);
// Test out of bounds cases
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 5, 2));
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kCapped, 5, 2));
// Below soft cap we are always uncapped
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 0, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kCapped, 0, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 1, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kCapped, 1, 2));
// At hard cap we are always capped
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kCapped, 4, 2));
EXPECT_EQ(SocketPoolState::kCapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 4, 2));
// If uncapped at or above soft cap we always stay that way
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 2, 2));
EXPECT_EQ(SocketPoolState::kUncapped, pool_capacity.NextStateAfterRelease(
SocketPoolState::kUncapped, 3, 2));
// When capped between soft and hard cap, we should be able to see some
// distribution of each option. The probability inputs here should make it
// a coin toss, but to prevent this from being flakey we run it 1000 times.
bool did_see_uncapped = false;
bool did_see_capped = false;
for (size_t i = 0; i < 1000; ++i) {
if (SocketPoolState::kUncapped ==
pool_capacity.NextStateAfterRelease(SocketPoolState::kCapped, 3, 2)) {
did_see_uncapped = true;
} else {
did_see_capped = true;
}
}
EXPECT_TRUE(did_see_uncapped);
EXPECT_TRUE(did_see_capped);
}
TEST(SocketPoolAdditionalCapacityTest, EmptyPool) {
const SocketPoolAdditionalCapacity empty_pool =
SocketPoolAdditionalCapacity::CreateEmpty();
// No sockets in use
EXPECT_EQ(
SocketPoolState::kUncapped,
empty_pool.NextStateBeforeAllocation(SocketPoolState::kUncapped, 0, 256));
EXPECT_EQ(
SocketPoolState::kUncapped,
empty_pool.NextStateAfterRelease(SocketPoolState::kUncapped, 0, 256));
EXPECT_EQ(SocketPoolState::kUncapped, empty_pool.NextStateBeforeAllocation(
SocketPoolState::kCapped, 0, 256));
EXPECT_EQ(SocketPoolState::kUncapped,
empty_pool.NextStateAfterRelease(SocketPoolState::kCapped, 0, 256));
// 50% of soft cap in use
EXPECT_EQ(SocketPoolState::kUncapped,
empty_pool.NextStateBeforeAllocation(SocketPoolState::kUncapped,
128, 256));
EXPECT_EQ(
SocketPoolState::kUncapped,
empty_pool.NextStateAfterRelease(SocketPoolState::kUncapped, 128, 256));
EXPECT_EQ(
SocketPoolState::kUncapped,
empty_pool.NextStateBeforeAllocation(SocketPoolState::kCapped, 128, 256));
EXPECT_EQ(
SocketPoolState::kUncapped,
empty_pool.NextStateAfterRelease(SocketPoolState::kCapped, 128, 256));
// 100% of soft cap in use
EXPECT_EQ(SocketPoolState::kCapped,
empty_pool.NextStateBeforeAllocation(SocketPoolState::kUncapped,
256, 256));
EXPECT_EQ(
SocketPoolState::kCapped,
empty_pool.NextStateAfterRelease(SocketPoolState::kUncapped, 256, 256));
EXPECT_EQ(SocketPoolState::kCapped, empty_pool.NextStateBeforeAllocation(
SocketPoolState::kCapped, 256, 256));
EXPECT_EQ(SocketPoolState::kCapped, empty_pool.NextStateAfterRelease(
SocketPoolState::kCapped, 256, 256));
}
TEST(SocketPoolAdditionalCapacityTest,
TestDefaultDistributionForFieldTrialConfig) {
// In order to do that we need an easy way to measure distributions.
// Since we are applying noise, we run a ten thousand variants.
auto percentage_transition_for_allocation_and_release =
[&](size_t sockets_in_use) -> std::tuple<double, double> {
size_t transition_allocation_count = 0;
size_t transition_release_count = 0;
for (size_t i = 0; i < 10000; ++i) {
if (SocketPoolState::kCapped ==
kFieldTrialPool.NextStateBeforeAllocation(SocketPoolState::kUncapped,
sockets_in_use, 256)) {
++transition_allocation_count;
}
if (SocketPoolState::kUncapped ==
kFieldTrialPool.NextStateAfterRelease(SocketPoolState::kCapped,
sockets_in_use, 256)) {
++transition_release_count;
}
}
return {transition_allocation_count / 10000.0,
transition_release_count / 10000.0};
};
// We want to validate the distribution at three points: 5%, 50%, and 95%.
auto fifth_percentile = percentage_transition_for_allocation_and_release(268);
auto fiftieth_percentile =
percentage_transition_for_allocation_and_release(384);
auto ninetyfifth_percentile =
percentage_transition_for_allocation_and_release(500);
// When allocating sockets and uncapped:
// We expect a ~1% transition rate if 5% of additional sockets are in use.
EXPECT_GT(std::get<0>(fifth_percentile), 0.00);
EXPECT_LT(std::get<0>(fifth_percentile), 0.025);
// We expect a ~1% transition rate if 50% of additional sockets are in use.
EXPECT_GT(std::get<0>(fiftieth_percentile), 0.00);
EXPECT_LT(std::get<0>(fiftieth_percentile), 0.025);
// We expect a ~50% transition rate if 95% of additional sockets are in use.
EXPECT_GT(std::get<0>(ninetyfifth_percentile), 0.35);
EXPECT_LT(std::get<0>(ninetyfifth_percentile), 0.65);
// When releasing sockets and capped:
// We expect a ~50% transition rate if 5% of additional sockets are in use.
EXPECT_GT(std::get<1>(fifth_percentile), 0.35);
EXPECT_LT(std::get<1>(fifth_percentile), 0.65);
// We expect a ~1% transition rate if 50% of additional sockets are in use.
EXPECT_GT(std::get<1>(fiftieth_percentile), 0.00);
EXPECT_LT(std::get<1>(fiftieth_percentile), 0.025);
// We expect a ~1% transition rate if 95% of additional sockets are in use.
EXPECT_GT(std::get<1>(ninetyfifth_percentile), 0.00);
EXPECT_LT(std::get<1>(ninetyfifth_percentile), 0.025);
}
void ValidateRandomizedInputs(double base,
size_t capacity,
double minimum,
double noise,
bool capped,
size_t sockets_in_use,
size_t socket_soft_cap) {
SocketPoolAdditionalCapacity pool =
SocketPoolAdditionalCapacity::CreateForTest(base, capacity, minimum,
noise);
// Because there's some randomization here, we want to run these a few times.
for (size_t i = 0; i < 1000; ++i) {
pool.NextStateBeforeAllocation(
capped ? SocketPoolState::kCapped : SocketPoolState::kUncapped,
sockets_in_use, socket_soft_cap);
pool.NextStateAfterRelease(
capped ? SocketPoolState::kCapped : SocketPoolState::kUncapped,
sockets_in_use, socket_soft_cap);
}
}
FUZZ_TEST(SocketPoolAdditionalCapacityTest, ValidateRandomizedInputs)
.WithDomains(fuzztest::Arbitrary<double>(),
fuzztest::Arbitrary<size_t>(),
fuzztest::Arbitrary<double>(),
fuzztest::Arbitrary<double>(),
fuzztest::Arbitrary<bool>(),
fuzztest::Arbitrary<size_t>(),
fuzztest::Arbitrary<size_t>())
.WithSeeds({
{std::nan(""), 0, std::nan(""), std::nan(""), false, 0, 0},
{0.0, 0, 0.0, 0.0, false, 0, 0},
{0.3, 64, 0.1, 0.1, false, 96, 64},
{0.6, 128, 0.2, 0.2, true, 192, 128},
{1.0, 256, 1.0, 1.0, true, 320, 256},
{1.0, 256, 1.0, 1.0, true, std::numeric_limits<uint32_t>::max(),
std::numeric_limits<uint32_t>::max()},
});
// This is mocked up so that we can model the sort of function usage we expect
// in the additions to ClientSocketPool. We won't actually be implementing or
// using the normal public interface functions of a ClientSocketPool.
class MockClientSocketPool : public ClientSocketPool {
public:
MockClientSocketPool()
: ClientSocketPool(/*socket_soft_cap=*/256,
kFieldTrialPool,
ProxyChain::Direct(),
/*is_for_websockets=*/false,
/*common_connect_job_params*/ nullptr,
/*connect_job_factory*/ nullptr) {}
SocketPoolState RequestSocket() {
UpdateStateBeforeAllocation();
if (State() == SocketPoolState::kUncapped) {
++sockets_in_use_;
}
CHECK_LE(sockets_in_use_, 512);
return State();
}
SocketPoolState ReleaseSocket() {
--sockets_in_use_;
UpdateStateAfterRelease();
CHECK_GE(sockets_in_use_, 0);
return State();
}
size_t SocketsInUse() const override { return sockets_in_use_; }
private:
int RequestSocket(
const GroupId& group_id,
scoped_refptr<SocketParams> params,
const std::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag,
RequestPriority priority,
const SocketTag& socket_tag,
RespectLimits respect_limits,
ClientSocketHandle* handle,
CompletionOnceCallback callback,
const ProxyAuthCallback& proxy_auth_callback,
bool fail_if_alias_requires_proxy_override,
const NetLogWithSource& net_log) override {
NOTIMPLEMENTED();
return ERR_IO_PENDING;
}
int RequestSockets(
const GroupId& group_id,
scoped_refptr<SocketParams> params,
const std::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag,
size_t num_sockets,
bool fail_if_alias_requires_proxy_override,
CompletionOnceCallback callback,
const NetLogWithSource& net_log) override {
NOTIMPLEMENTED();
return ERR_IO_PENDING;
}
void SetPriority(const GroupId& group_id,
ClientSocketHandle* handle,
RequestPriority priority) override {
NOTIMPLEMENTED();
}
void CancelRequest(const GroupId& group_id,
ClientSocketHandle* handle,
bool cancel_connect_job) override {
NOTIMPLEMENTED();
}
void ReleaseSocket(const GroupId& group_id,
std::unique_ptr<StreamSocket> socket,
int64_t generation) override {
NOTIMPLEMENTED();
}
void FlushWithError(int error, const char* net_log_reason_utf8) override {
NOTIMPLEMENTED();
}
void CloseIdleSockets(const char* net_log_reason_utf8) override {
NOTIMPLEMENTED();
}
void CloseIdleSocketsInGroup(const GroupId& group_id,
const char* net_log_reason_utf8) override {
NOTIMPLEMENTED();
}
size_t IdleSocketCount() const override {
NOTIMPLEMENTED();
return 0;
}
size_t IdleSocketCountInGroup(const GroupId& group_id) const override {
NOTIMPLEMENTED();
return 0;
}
LoadState GetLoadState(const GroupId& group_id,
const ClientSocketHandle* handle) const override {
NOTIMPLEMENTED();
return LOAD_STATE_IDLE;
}
base::Value GetInfoAsValue(const std::string& name,
const std::string& type) const override {
NOTIMPLEMENTED();
return base::Value();
}
bool HasActiveSocket(const GroupId& group_id) const override {
NOTIMPLEMENTED();
return false;
}
bool IsStalled() const override {
NOTIMPLEMENTED();
return false;
}
void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override {
NOTIMPLEMENTED();
}
void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override {
NOTIMPLEMENTED();
}
private:
size_t sockets_in_use_{0};
};
TEST(SocketPoolAdditionalCapacityTest,
ValidateAdditionalCapacityForMockClientSocketPool) {
MockClientSocketPool pool;
ValidateAdditionalCapacityForSocketPool(
base::BindLambdaForTesting([&]() { return pool.RequestSocket(); }),
base::DoNothing(),
base::BindLambdaForTesting([&]() { return pool.ReleaseSocket(); }),
base::BindLambdaForTesting([&]() { return pool.SocketsInUse(); }));
}
} // namespace
} // namespace net
|