带阿里标识的版本

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_StateMachineTests.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.
//
// - (Q)
// 17 Q:17
// - <EFBFBD><EFBFBD>?(Q) <EFBFBD><EFBFBD>?
// <EFBFBD><EFBFBD>?7 Q:17<EFBFBD><EFBFBD>?
//
#import "HttpdnsNWHTTPClientTestBase.h"
@@ -17,18 +17,18 @@
@implementation HttpdnsNWHTTPClient_StateMachineTests
#pragma mark - Q.
#pragma mark - Q. <EFBFBD><EFBFBD>?
// Q1.1 LRU
- (void)testStateMachine_PoolOverflowLRU_RemovesOldestByLastUsedDate {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 5
// <EFBFBD><EFBFBD>?
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 5
// 5<EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 5; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Request %ld", (long)i]];
[expectations addObject:expectation];
@@ -42,15 +42,15 @@
error:&error];
[expectation fulfill];
});
[NSThread sleepForTimeInterval:0.05]; //
[NSThread sleepForTimeInterval:0.05]; // <EFBFBD><EFBFBD>?
}
[self waitForExpectations:expectations timeout:20.0];
//
// <EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:1.0];
// 4LRU
// <EFBFBD><EFBFBD>?4LRU<EFBFBD><EFBFBD>?
NSUInteger poolCount = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolCount, 4,
@"Pool should enforce max 4 connections (LRU)");
@@ -65,7 +65,7 @@
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 10
// <EFBFBD><EFBFBD>?0
for (NSInteger i = 0; i < 10; i++) {
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
@@ -78,12 +78,12 @@
//
[NSThread sleepForTimeInterval:1.0];
// 1
// <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
NSUInteger poolCount = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolCount, 1,
@"Pool should have at most 1 connection (rapid sequential reuse)");
// 1
// 1<EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 1,
@"Should create only 1 connection for sequential requests");
}
@@ -94,7 +94,7 @@
NSString *poolKey11080 = @"127.0.0.1:11080:tcp";
NSString *poolKey11443 = @"127.0.0.1:11443:tls";
// 11080
// <EFBFBD><EFBFBD>?1080
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"Port11080"
@@ -102,7 +102,7 @@
error:&error1];
XCTAssertNotNil(response1);
// 11443
// <EFBFBD><EFBFBD>?1443
NSError *error2 = nil;
HttpdnsNWHTTPClientResponse *response2 = [self.client performRequestWithURLString:@"https://127.0.0.1:11443/get"
userAgent:@"Port11443"
@@ -113,7 +113,7 @@
//
[NSThread sleepForTimeInterval:0.5];
// 1
// <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey11080], 1,
@"Port 11080 pool should have 1 connection");
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey11443], 1,
@@ -128,7 +128,7 @@
- (void)testInvariant_PoolSize_NeverExceedsLimit {
[self.client resetPoolStatistics];
// 20
// <EFBFBD><EFBFBD>?0
for (NSInteger i = 0; i < 20; i++) {
NSError *error = nil;
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
@@ -137,7 +137,7 @@
error:&error];
}
//
// <EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:1.5];
// 4
@@ -149,7 +149,7 @@
key, (unsigned long)poolCount);
}
// 4
// 4<EFBFBD><EFBFBD>?
XCTAssertLessThanOrEqual([self.client totalConnectionCount], 4,
@"Total connections should not exceed 4");
}
@@ -162,7 +162,7 @@
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 15
// 15<EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 15; i++) {
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Request %ld", (long)i]];
[expectations addObject:expectation];
@@ -182,17 +182,17 @@
//
[NSThread sleepForTimeInterval:1.0];
// 4
// <EFBFBD><EFBFBD>?4<EFBFBD><EFBFBD>?
NSUInteger poolCount = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolCount, 4,
@"Pool should not have duplicates (max 4 connections)");
// 15
// <EFBFBD><EFBFBD>?5<EFBFBD><EFBFBD>?
XCTAssertLessThanOrEqual(self.client.connectionCreationCount, 15,
@"Should not create excessive connections");
}
// Q4.1 30
// Q4.1 <EFBFBD><EFBFBD>?0
- (void)testBoundary_Exactly30Seconds_ConnectionExpired {
if (getenv("SKIP_SLOW_TESTS")) {
return;
@@ -200,7 +200,7 @@
[self.client resetPoolStatistics];
//
// <EFBFBD><EFBFBD>?
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"InitialRequest"
@@ -211,7 +211,7 @@
// 30.530
[NSThread sleepForTimeInterval:30.5];
//
// <EFBFBD><EFBFBD>?
NSError *error2 = nil;
HttpdnsNWHTTPClientResponse *response2 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"AfterExpiry"
@@ -219,18 +219,18 @@
error:&error2];
XCTAssertNotNil(response2);
// 2
// 2<EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 2,
@"Should create 2 connections (first expired after 30s)");
XCTAssertEqual(self.client.connectionReuseCount, 0,
@"Should not reuse expired connection");
}
// Q4.2 29
// Q4.2 <EFBFBD><EFBFBD>?9<EFBFBD><EFBFBD>?
- (void)testBoundary_Under30Seconds_ConnectionNotExpired {
[self.client resetPoolStatistics];
//
// <EFBFBD><EFBFBD>?
NSError *error1 = nil;
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"InitialRequest"
@@ -249,19 +249,19 @@
error:&error2];
XCTAssertNotNil(response2);
// 1
// <EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 1,
@"Should create only 1 connection (reused within 30s)");
XCTAssertEqual(self.client.connectionReuseCount, 1,
@"Should reuse connection within 30s");
}
// Q4.3 4
// Q4.3 <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
- (void)testBoundary_ExactlyFourConnections_AllKept {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 4使4
// 4使<EFBFBD><EFBFBD>?
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
@@ -272,7 +272,7 @@
dispatch_async(queue, ^{
NSError *error = nil;
// 使 /delay/2
// 使 /delay/2 <EFBFBD><EFBFBD>?
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/delay/2"
userAgent:[NSString stringWithFormat:@"Request%ld", (long)i]
timeout:15.0
@@ -280,7 +280,7 @@
[expectation fulfill];
});
[NSThread sleepForTimeInterval:0.05]; //
[NSThread sleepForTimeInterval:0.05]; // <EFBFBD><EFBFBD>?
}
[self waitForExpectations:expectations timeout:20.0];
@@ -288,22 +288,22 @@
//
[NSThread sleepForTimeInterval:1.0];
// 4
// <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
NSUInteger poolCount = [self.client connectionPoolCountForKey:poolKey];
XCTAssertEqual(poolCount, 4,
@"Pool should keep all 4 connections (not exceeding limit)");
// 4
// <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionCreationCount, 4,
@"Should create exactly 4 connections");
}
// Q1.2
// Q1.2 <EFBFBD><EFBFBD>?
- (void)testStateMachine_NormalSequence_StateTransitionsCorrect {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 1使 (CREATING IN_USE IDLE)
// <EFBFBD><EFBFBD>?使<EFBFBD><EFBFBD>?(CREATING <EFBFBD><EFBFBD>?IN_USE <EFBFBD><EFBFBD>?IDLE)
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"StateTest"
timeout:15.0
@@ -312,18 +312,18 @@
[NSThread sleepForTimeInterval:1.0]; //
// 1
// 1<EFBFBD><EFBFBD>?
XCTAssertEqual([self.client connectionPoolCountForKey:poolKey], 1,
@"Connection should be in pool");
// 2 (IDLE IN_USE IDLE)
// <EFBFBD><EFBFBD>? (IDLE <EFBFBD><EFBFBD>?IN_USE <EFBFBD><EFBFBD>?IDLE)
HttpdnsNWHTTPClientResponse *response2 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"StateTest"
timeout:15.0
error:nil];
XCTAssertNotNil(response2, @"Second request should reuse connection");
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual(self.client.connectionReuseCount, 1,
@"Should have reused connection once");
}
@@ -333,7 +333,7 @@
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
//
// <EFBFBD><EFBFBD>?
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"InUseTest"
timeout:15.0
@@ -364,20 +364,20 @@
[NSThread sleepForTimeInterval:1.0];
// lastUsedDate nil
// <EFBFBD><EFBFBD>?lastUsedDate <EFBFBD><EFBFBD>?nil
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
XCTAssertEqual(connections.count, 1, @"Should have connection");
HttpdnsNWReusableConnection *conn = connections.firstObject;
[conn debugSetLastUsedDate:nil];
// prune使 distantPast nil
// <EFBFBD><EFBFBD>?prune使 distantPast nil<EFBFBD><EFBFBD>?
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"NilDateTest"
timeout:15.0
error:nil];
//
// <EFBFBD><EFBFBD>?
XCTAssertNotNil(response, @"Should handle nil lastUsedDate gracefully");
}
@@ -386,7 +386,7 @@
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
//
// <EFBFBD><EFBFBD>?
for (NSInteger i = 0; i < 3; i++) {
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"InvariantTest"
@@ -394,7 +394,7 @@
error:nil];
}
// 1
// 1<EFBFBD><EFBFBD>?
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/delay/10"
userAgent:@"TimeoutTest"
timeout:0.5
@@ -402,7 +402,7 @@
[NSThread sleepForTimeInterval:2.0];
//
// <EFBFBD><EFBFBD>?
NSArray<HttpdnsNWReusableConnection *> *connections = [self.client connectionsInPoolForKey:poolKey];
//
@@ -411,12 +411,12 @@
}
}
// Q3.4 lastUsedDate
// Q3.4 lastUsedDate <EFBFBD><EFBFBD>?
- (void)testInvariant_LastUsedDate_Monotonic {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 1使
// <EFBFBD><EFBFBD>?使<EFBFBD><EFBFBD>?
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"MonotonicTest"
timeout:15.0
@@ -429,10 +429,10 @@
NSDate *date1 = connections1.firstObject.lastUsedDate;
XCTAssertNotNil(date1, @"lastUsedDate should be set");
// 1
// 1<EFBFBD><EFBFBD>?
[NSThread sleepForTimeInterval:1.0];
// 2使
// <EFBFBD><EFBFBD>?使
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"MonotonicTest"
timeout:15.0
@@ -449,12 +449,12 @@
@"lastUsedDate should increase after reuse");
}
// Q5.1 +
// Q5.1 +<EFBFBD><EFBFBD>?
- (void)testCompound_TimeoutDuringPoolOverflow_Handled {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
// 4
// <EFBFBD><EFBFBD>?
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
@@ -477,9 +477,9 @@
[NSThread sleepForTimeInterval:1.0];
NSUInteger poolCountBefore = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolCountBefore, 4, @"Pool should have ≤4 connections");
XCTAssertLessThanOrEqual(poolCountBefore, 4, @"Pool should have <EFBFBD><EFBFBD>? connections");
// 5
// <EFBFBD><EFBFBD>?<EFBFBD><EFBFBD>?
NSError *error = nil;
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/delay/10"
userAgent:@"TimeoutRequest"
@@ -491,7 +491,7 @@
[NSThread sleepForTimeInterval:1.0];
//
// <EFBFBD><EFBFBD>?
NSUInteger poolCountAfter = [self.client connectionPoolCountForKey:poolKey];
XCTAssertLessThanOrEqual(poolCountAfter, 4, @"Timed-out connection should not be added to pool");
}
@@ -507,15 +507,15 @@
timeout:2.0
error:&error];
//
// <EFBFBD><EFBFBD>?
XCTAssertNil(response, @"Should fail to connect to invalid port");
//
// <EFBFBD><EFBFBD>?
XCTAssertEqual([self.client totalConnectionCount], 0,
@"Failed connection should not be added to pool");
}
// Q2.5 invalidate
// Q2.5 invalidate <EFBFBD><EFBFBD>?
- (void)testAbnormal_MultipleInvalidate_Idempotent {
[self.client resetPoolStatistics];
NSString *poolKey = @"127.0.0.1:11080:tcp";
@@ -542,11 +542,11 @@
XCTAssertTrue(conn.isInvalidated, @"Connection should be invalidated");
}
// Q5.2 dequeue
// Q5.2 dequeue <EFBFBD><EFBFBD>?
- (void)testCompound_ConcurrentDequeueDuringPrune_Safe {
[self.client resetPoolStatistics];
//
// <EFBFBD><EFBFBD>?
[self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
userAgent:@"RaceTest"
timeout:15.0
@@ -562,7 +562,7 @@
// 30
[NSThread sleepForTimeInterval:30.5];
// dequeue prune
// <EFBFBD><EFBFBD>?dequeue prune<EFBFBD><EFBFBD>?
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);