带阿里标识的版本

This commit is contained in:
robin
2026-02-28 18:55:33 +08:00
parent 150799f41d
commit 5d0b7c7e91
477 changed files with 10813 additions and 4044 deletions

View File

@@ -1,12 +1,12 @@
//
// HttpdnsNWHTTPClient_PoolManagementTests.m
// AlicloudHttpDNSTests
// TrustHttpDNSTests
//
// @author Created by Claude Code on 2025-11-01
// Copyright © 2025 alibaba-inc.com. All rights reserved.
// Copyright © 2025 trustapp.com. All rights reserved.
//
// - (K) (L) (O) (S)
// 16 K:5 + L:3 + O:3 + S:5
// <EFBFBD><EFBFBD>?- <EFBFBD><EFBFBD>?(K) (L) (O)<EFBFBD><EFBFBD>?(S) <EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD>?6 K:5 + L:3 + O:3 + S:5<EFBFBD><EFBFBD>?
//
#import "HttpdnsNWHTTPClientTestBase.h"
@@ -17,9 +17,9 @@
@implementation HttpdnsNWHTTPClient_PoolManagementTests
#pragma mark - K.
#pragma mark - K. <EFBFBD><EFBFBD>?
// K.1 HTTPS 使
// K.1 HTTPS 使<EFBFBD><EFBFBD>?
- (void)testMultiPort_DifferentHTTPSPorts_SeparatePoolKeys {
XCTestExpectation *expectation = [self expectationWithDescription:@"Different ports use different pools"];
@@ -42,7 +42,7 @@
XCTAssertNotNil(response2, @"First request to port 11444 should succeed");
XCTAssertEqual(response2.statusCode, 200);
// 11443
// 11443<EFBFBD><EFBFBD>?
NSError *error3 = nil;
HttpdnsNWHTTPClientResponse *response3 = [self.client performRequestWithURLString:@"https://127.0.0.1:11443/get"
userAgent:@"Port11443Again"
@@ -57,7 +57,7 @@
[self waitForExpectations:@[expectation] timeout:50.0];
}
// K.2 HTTPS
// K.2 HTTPS <EFBFBD><EFBFBD>?
- (void)testMultiPort_ConcurrentThreePorts_AllSucceed {
NSInteger requestsPerPort = 10;
NSArray<NSNumber *> *ports = @[@11443, @11444, @11445];
@@ -97,17 +97,17 @@
XCTAssertEqual(successCount, ports.count * requestsPerPort, @"All 30 requests should succeed");
}
// K.3
// K.3 <EFBFBD><EFBFBD>?
- (void)testMultiPort_SequentialPortSwitching_ConnectionReusePerPort {
XCTestExpectation *expectation = [self expectationWithDescription:@"Sequential port switching"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray<NSString *> *urls = @[
@"https://127.0.0.1:11443/get", // 访 11443
@"https://127.0.0.1:11444/get", // 访 11444
@"https://127.0.0.1:11445/get", // 访 11445
@"https://127.0.0.1:11443/get", // 访 11443
@"https://127.0.0.1:11444/get", // 访 11444
@"https://127.0.0.1:11443/get", // 访<EFBFBD><EFBFBD>?11443
@"https://127.0.0.1:11444/get", // 访<EFBFBD><EFBFBD>?11444
@"https://127.0.0.1:11445/get", // 访<EFBFBD><EFBFBD>?11445
@"https://127.0.0.1:11443/get", // 访<EFBFBD><EFBFBD>?11443<EFBFBD><EFBFBD>?
@"https://127.0.0.1:11444/get", // 访<EFBFBD><EFBFBD>?11444<EFBFBD><EFBFBD>?
];
NSMutableArray<NSNumber *> *responseTimes = [NSMutableArray array];
@@ -140,7 +140,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Per-port pool limits"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 11443 10
// <EFBFBD><EFBFBD>?11443 <EFBFBD><EFBFBD>?10 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 10; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"https://127.0.0.1:11443/get"
@@ -150,7 +150,7 @@
XCTAssertTrue(response != nil || error != nil);
}
// 11444 10
// <EFBFBD><EFBFBD>?11444 <EFBFBD><EFBFBD>?10
for (NSInteger i = 0; i < 10; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"https://127.0.0.1:11444/get"
@@ -163,7 +163,7 @@
//
[NSThread sleepForTimeInterval:1.0];
//
// <EFBFBD><EFBFBD>?
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"https://127.0.0.1:11443/get"
userAgent:@"Verify11443"
@@ -193,7 +193,7 @@
NSInteger totalRequests = 20;
NSInteger successCount = 0;
// 访
// 访<EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < totalRequests; i++) {
NSNumber *port = ports[i % ports.count];
NSString *urlString = [NSString stringWithFormat:@"https://127.0.0.1:%@/get", port];
@@ -219,7 +219,7 @@
#pragma mark - L.
// L.1
// L.1 <EFBFBD><EFBFBD>?
- (void)testPoolExhaustion_FourPortsSimultaneous_AllSucceed {
NSInteger requestsPerPort = 10;
NSArray<NSNumber *> *ports = @[@11443, @11444, @11445, @11446];
@@ -229,7 +229,7 @@
NSLock *successCountLock = [[NSLock alloc] init];
__block NSInteger successCount = 0;
// 4 10 40
// <EFBFBD><EFBFBD>?4 10 <EFBFBD><EFBFBD>?40
for (NSNumber *port in ports) {
for (NSInteger i = 0; i < requestsPerPort; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Port %@ Request %ld", port, (long)i]];
@@ -256,11 +256,11 @@
[self waitForExpectations:expectations timeout:80.0];
// > 95%
// <EFBFBD><EFBFBD>?> 95%
XCTAssertGreaterThan(successCount, 38, @"At least 95%% of 40 requests should succeed");
}
// L.2
// L.2 <EFBFBD><EFBFBD>?
- (void)testPoolExhaustion_SinglePortExhausted_OthersUnaffected {
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
@@ -268,7 +268,7 @@
__block NSInteger port11444SuccessCount = 0;
NSLock *countLock = [[NSLock alloc] init];
// 11443 20
// <EFBFBD><EFBFBD>?11443 20 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 20; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Exhaust11443 %ld", (long)i]];
[expectations addObject:expectation];
@@ -283,7 +283,7 @@
});
}
// 11444 5 11443
// <EFBFBD><EFBFBD>?11444 5 11443 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 5; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Port11444 %ld", (long)i]];
[expectations addObject:expectation];
@@ -316,7 +316,7 @@
XCTAssertEqual(port11444SuccessCount, 5, @"All port 11444 requests should succeed despite 11443 load");
}
// L.3 使
// L.3 使<EFBFBD><EFBFBD>?
- (void)testPoolExhaustion_MultiPortCleanup_ExpiredConnectionsPruned {
if (getenv("SKIP_SLOW_TESTS")) {
return;
@@ -327,7 +327,7 @@
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray<NSNumber *> *ports = @[@11443, @11444, @11445];
//
// <EFBFBD><EFBFBD>?
for (NSNumber *port in ports) {
NSString *urlString = [NSString stringWithFormat:@"https://127.0.0.1:%@/get", port];
NSError *error = nil;
@@ -338,7 +338,7 @@
XCTAssertTrue(response != nil || error != nil);
}
// 30
// 30 <EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:31.0];
//
@@ -358,14 +358,14 @@
[self waitForExpectations:@[expectation] timeout:80.0];
}
#pragma mark - O. 使 API
#pragma mark - O. 使<EFBFBD><EFBFBD>?API<EFBFBD><EFBFBD>?
// O.1 -
// O.1 <EFBFBD><EFBFBD>?- <EFBFBD><EFBFBD>?
- (void)testPoolVerification_ComprehensiveCheck_AllAspectsVerified {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey], 0,
@"Pool should be empty initially");
XCTAssertEqual([self.client totalConnectionCount], 0,
@@ -375,7 +375,7 @@
XCTAssertEqual(self.client.connectionReuseCount, 0,
@"Reuse count should be 0 initially");
// 5
// <EFBFBD><EFBFBD>?5
for (NSInteger i = 0; i < 5; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
@@ -386,7 +386,7 @@
XCTAssertEqual(response.statusCode, 200, @"Request %ld should return 200", (long)i);
}
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey], 1,
@"Should have exactly 1 connection in pool for key: %@", poolKey);
XCTAssertEqual([self.client totalConnectionCount], 1,
@@ -398,7 +398,7 @@
XCTAssertEqual(self.client.connectionReuseCount, 4,
@"Should reuse connection 4 times");
//
// <EFBFBD><EFBFBD>?
CGFloat reuseRate = (CGFloat)self.client.connectionReuseCount /
(self.client.connectionCreationCount + self.client.connectionReuseCount);
XCTAssertGreaterThanOrEqual(reuseRate, 0.8,
@@ -417,11 +417,11 @@
NSString *key11443 = @"127.0.0.1:11443:tls";
NSString *key11444 = @"127.0.0.1:11444:tls";
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:key11443], 0);
XCTAssertEqual([self.client connectionPoolCountForKey:key11444], 0);
// 11443 3
// <EFBFBD><EFBFBD>?11443 <EFBFBD><EFBFBD>?3 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 3; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"https://127.0.0.1:11443/get"
@@ -431,13 +431,13 @@
XCTAssertNotNil(response);
}
// 11443
// 11443 <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:key11443], 1,
@"Port 11443 should have 1 connection");
XCTAssertEqual([self.client connectionPoolCountForKey:key11444], 0,
@"Port 11444 should still be empty");
// 11444 3
// <EFBFBD><EFBFBD>?11444 <EFBFBD><EFBFBD>?3 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 3; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"https://127.0.0.1:11444/get"
@@ -455,7 +455,7 @@
XCTAssertEqual([self.client totalConnectionCount], 2,
@"Total should be 2 connections (one per port)");
// 2 4
// 2 <EFBFBD><EFBFBD>?4 <EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 2,
@"Should create 2 connections (one per port)");
XCTAssertEqual(self.client.connectionReuseCount, 4,
@@ -468,12 +468,12 @@
XCTAssertTrue([allKeys containsObject:key11444], @"Should contain key for port 11444");
}
// O.3
// O.3 <EFBFBD><EFBFBD>?
- (void)testPoolVerification_PoolCapacity_MaxFourConnections {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 10
// <EFBFBD><EFBFBD>?10 <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 10; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
@@ -486,12 +486,12 @@
//
[NSThread sleepForTimeInterval:1.0];
// 4kHttpdnsNWHTTPClientMaxIdleConnectionsPerKey
// 4kHttpdnsNWHTTPClientMaxIdleConnectionsPerKey<EFBFBD><EFBFBD>?
NSUInteger poolSize = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolSize, 4,
@"Pool size should not exceed 4 (actual: %lu)", (unsigned long)poolSize);
// 1
// <EFBFBD><EFBFBD>?1 <EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 1,
@"Should create only 1 connection for sequential requests");
XCTAssertEqual(self.client.connectionReuseCount, 9,
@@ -500,12 +500,12 @@
#pragma mark - S.
// S.1 -
// S.1 <EFBFBD><EFBFBD>?- <EFBFBD><EFBFBD>?
- (void)testIdleTimeout_MixedExpiredValid_SelectivePruning {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
//
// <EFBFBD><EFBFBD>?
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"ConnectionA"
@@ -517,7 +517,7 @@
//
[NSThread sleepForTimeInterval:0.5];
// 使 DEBUG API A 35
// 使 DEBUG API A <EFBFBD><EFBFBD>?5 <EFBFBD><EFBFBD>?
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
XCTAssertEqual(connections.count, 1, @"Should have 1 connection in pool");
@@ -525,7 +525,7 @@
NSDate *expiredDate = [NSDate dateWithTimeIntervalSinceNow:-35.0];
[connectionA debugSetLastUsedDate:expiredDate];
//
// <EFBFBD><EFBFBD>?
NSError *error2 = nil;
HttpdnsNWHTTPClientResponse *response2 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"ConnectionB"
@@ -536,12 +536,12 @@
//
[NSThread sleepForTimeInterval:0.5];
// 1 connectionA connectionB
// 1 connectionA connectionB <EFBFBD><EFBFBD>?
connections = [self.client connectionsInPoolForKey:poolKey];
XCTAssertEqual(connections.count, 1,
@"Should have only 1 connection (expired A removed, valid B kept)");
// connectionB
// <EFBFBD><EFBFBD>?connectionB
[self.client resetPoolStatistics];
NSError *error3 = nil;
HttpdnsNWHTTPClientResponse *response3 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
@@ -550,7 +550,7 @@
error:&error3];
XCTAssertNotNil(response3);
// connectionB
// connectionB<EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 0,
@"Should not create new connection (reuse existing valid connection)");
XCTAssertEqual(self.client.connectionReuseCount, 1,
@@ -562,7 +562,7 @@
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
//
// <EFBFBD><EFBFBD>?
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"Initial"
@@ -572,17 +572,17 @@
[NSThread sleepForTimeInterval:0.5];
// inUse=YES
// <EFBFBD><EFBFBD>?inUse=YES
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
XCTAssertEqual(connections.count, 1);
HttpdnsNWReusableConnection *conn = connections.firstObject;
// 60 30
// <EFBFBD><EFBFBD>?60 <EFBFBD><EFBFBD>?30
NSDate *veryOldDate = [NSDate dateWithTimeIntervalSinceNow:-60.0];
[conn debugSetLastUsedDate:veryOldDate];
// 使
// 使<EFBFBD><EFBFBD>?
[conn debugSetInUse:YES];
//
@@ -593,7 +593,7 @@
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
connectionsBefore = [self.client totalConnectionCount];
// pruneConnectionPool
// pruneConnectionPool<EFBFBD><EFBFBD>?
NSError *error2 = nil;
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"TriggerPrune"
@@ -608,18 +608,18 @@
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC));
// inUse
// <EFBFBD><EFBFBD>?inUse <EFBFBD><EFBFBD>?
[conn debugSetInUse:NO];
// inUse=YES
// connectionsBefore = 1 (), connectionsAfter = 2 ( + )
// inUse=YES <EFBFBD><EFBFBD>?
// connectionsBefore = 1 (<EFBFBD><EFBFBD>?, connectionsAfter = 2 (<EFBFBD><EFBFBD>?+ <EFBFBD><EFBFBD>?
XCTAssertEqual(connectionsBefore, 1,
@"Should have 1 connection before (in-use protected)");
XCTAssertEqual(connectionsAfter, 2,
@"Should have 2 connections after (in-use connection NOT pruned, new connection added)");
}
// S.3 -
// S.3 <EFBFBD><EFBFBD>?-
- (void)testIdleTimeout_AllExpired_BulkPruning {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
@@ -645,21 +645,21 @@
[self waitForExpectations:expectations timeout:30.0];
//
// <EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:1.0];
//
// <EFBFBD><EFBFBD>?
NSUInteger poolSizeBefore = [self.client connectionPoolCountForKey:poolKey];
XCTAssertGreaterThan(poolSizeBefore, 0, @"Pool should have connections");
// 31
// <EFBFBD><EFBFBD>?1 <EFBFBD><EFBFBD>?
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
NSDate *expiredDate = [NSDate dateWithTimeIntervalSinceNow:-31.0];
for (HttpdnsNWReusableConnection *conn in connections) {
[conn debugSetLastUsedDate:expiredDate];
}
//
// <EFBFBD><EFBFBD>?
NSError *errorNew = nil;
HttpdnsNWHTTPClientResponse *responseNew = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"AfterBulkExpiry"
@@ -677,7 +677,7 @@
@"Pool should have only 1 connection (new one after bulk pruning)");
}
// S.4
// S.4 <EFBFBD><EFBFBD>?
- (void)testIdleTimeout_PoolStateAfterExpiry_DirectVerification {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
@@ -692,11 +692,11 @@
[NSThread sleepForTimeInterval:0.5];
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey], 1,
@"Pool should have 1 connection");
//
// <EFBFBD><EFBFBD>?
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
HttpdnsNWReusableConnection *conn = connections.firstObject;
[conn debugSetLastUsedDate:[NSDate dateWithTimeIntervalSinceNow:-31.0]];
@@ -711,7 +711,7 @@
[NSThread sleepForTimeInterval:0.5];
//
// <EFBFBD><EFBFBD>?
NSUInteger poolSizeAfter = [self.client connectionPoolCountForKey:poolKey];
XCTAssertEqual(poolSizeAfter, 1,
@"Pool should have 1 connection (expired removed, new added)");
@@ -721,7 +721,7 @@
@"Should have created at least 1 new connection");
}
// S.5 -
// S.5 <EFBFBD><EFBFBD>? <EFBFBD><EFBFBD>?
- (void)testIdleTimeout_FastExpiry_NoWaiting {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
@@ -740,7 +740,7 @@
[NSThread sleepForTimeInterval:0.5];
// 使 DEBUG 31
// 使 DEBUG 31 <EFBFBD><EFBFBD>?
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
XCTAssertEqual(connections.count, 1);
@@ -766,7 +766,7 @@
CFAbsoluteTime elapsed = CFAbsoluteTimeGetCurrent() - startTime;
// < 5 30+
// < 5 30+ <EFBFBD><EFBFBD>?
XCTAssertLessThan(elapsed, 5.0,
@"Fast expiry test should complete quickly (%.1fs) without 30s wait", elapsed);
}