带阿里标识的版本
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
//
|
||||
// HttpdnsNWHTTPClient_ConcurrencyTests.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.
|
||||
//
|
||||
// 并发测试 - 包含并发请求 (H)、竞态条件 (I)、并发多端口 (N) 测试组
|
||||
// 测试总数:13 个(H:5 + I:5 + N:3)
|
||||
// 并发测试 - 包含并发请求 (H)、竞态条<EFBFBD><EFBFBD>?(I)、并发多端口 (N) 测试<EFBFBD><EFBFBD>?
|
||||
// 测试总数<EFBFBD><EFBFBD>?3 个(H:5 + I:5 + N:3<EFBFBD><EFBFBD>?
|
||||
//
|
||||
|
||||
#import "HttpdnsNWHTTPClientTestBase.h"
|
||||
@@ -137,7 +137,7 @@
|
||||
[self waitForExpectations:expectations timeout:40.0];
|
||||
}
|
||||
|
||||
// H.4 高负载压力测试
|
||||
// H.4 高负载压力测<EFBFBD><EFBFBD>?
|
||||
- (void)testConcurrency_HighLoad50Concurrent_NoDeadlock {
|
||||
NSInteger concurrentCount = 50;
|
||||
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
|
||||
@@ -169,7 +169,7 @@
|
||||
|
||||
[self waitForExpectations:expectations timeout:60.0];
|
||||
|
||||
// 至少大部分请求应该成功(允许部分失败,因为高负载)
|
||||
// 至少大部分请求应该成功(允许部分失败,因为高负载<EFBFBD><EFBFBD>?
|
||||
XCTAssertGreaterThan(successCount, concurrentCount * 0.8, @"At least 80%% should succeed");
|
||||
}
|
||||
|
||||
@@ -230,14 +230,14 @@
|
||||
[self waitForExpectations:@[serialExpectation, parallel1, parallel2, parallel3] timeout:60.0];
|
||||
}
|
||||
|
||||
#pragma mark - I. 竞态条件测试
|
||||
#pragma mark - I. 竞态条件测<EFBFBD><EFBFBD>?
|
||||
|
||||
// I.1 连接池容量测试
|
||||
// I.1 连接池容量测<EFBFBD><EFBFBD>?
|
||||
- (void)testRaceCondition_ExceedPoolCapacity_MaxFourConnections {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"Pool capacity test"];
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
// 快速连续发起 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"
|
||||
@@ -251,7 +251,7 @@
|
||||
[NSThread sleepForTimeInterval:1.0];
|
||||
|
||||
// 注意:无法直接检查池大小(内部实现),只能通过行为验证
|
||||
// 如果实现正确,池应自动限制为最多 4 个空闲连接
|
||||
// 如果实现正确,池应自动限制为最<EFBFBD><EFBFBD>?4 个空闲连<EFBFBD><EFBFBD>?
|
||||
|
||||
[expectation fulfill];
|
||||
});
|
||||
@@ -276,7 +276,7 @@
|
||||
timeout:15.0
|
||||
error:&error];
|
||||
XCTAssertTrue(response != nil || error != nil);
|
||||
// 连接在这里自动归还
|
||||
// 连接在这里自动归<EFBFBD><EFBFBD>?
|
||||
|
||||
[expectation fulfill];
|
||||
});
|
||||
@@ -284,15 +284,15 @@
|
||||
|
||||
[self waitForExpectations:expectations timeout:30.0];
|
||||
|
||||
// 如果没有崩溃或断言失败,说明并发归还处理正确
|
||||
// 如果没有崩溃或断言失败,说明并发归还处理正<EFBFBD><EFBFBD>?
|
||||
}
|
||||
|
||||
// I.3 获取-归还-再获取竞态
|
||||
// I.3 获取-归还-再获取竞<EFBFBD><EFBFBD>?
|
||||
- (void)testRaceCondition_AcquireReturnReacquire_CorrectState {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"Acquire-Return-Reacquire"];
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
// 第一个请求
|
||||
// 第一个请<EFBFBD><EFBFBD>?
|
||||
NSError *error1 = nil;
|
||||
HttpdnsNWHTTPClientResponse *response1 = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/get"
|
||||
userAgent:@"First"
|
||||
@@ -300,7 +300,7 @@
|
||||
error:&error1];
|
||||
XCTAssertTrue(response1 != nil || error1 != nil);
|
||||
|
||||
// 极短暂等待确保连接归还
|
||||
// 极短暂等待确保连接归<EFBFBD><EFBFBD>?
|
||||
[NSThread sleepForTimeInterval:0.1];
|
||||
|
||||
// 第二个请求应该能复用连接
|
||||
@@ -317,7 +317,7 @@
|
||||
[self waitForExpectations:@[expectation] timeout:35.0];
|
||||
}
|
||||
|
||||
// I.4 超时与活跃连接冲突(需要31秒,标记为慢测试)
|
||||
// I.4 超时与活跃连接冲突(需<EFBFBD><EFBFBD>?1秒,标记为慢测试<EFBFBD><EFBFBD>?
|
||||
- (void)testRaceCondition_ExpiredConnectionPruning_CreatesNewConnection {
|
||||
// 跳过此测试如果环境变量设置了 SKIP_SLOW_TESTS
|
||||
if (getenv("SKIP_SLOW_TESTS")) {
|
||||
@@ -335,7 +335,7 @@
|
||||
error:&error1];
|
||||
XCTAssertTrue(response1 != nil || error1 != nil);
|
||||
|
||||
// 等待超过30秒超时
|
||||
// 等待超过30秒超<EFBFBD><EFBFBD>?
|
||||
[NSThread sleepForTimeInterval:31.0];
|
||||
|
||||
// 新请求应该创建新连接(旧连接已过期)
|
||||
@@ -352,19 +352,19 @@
|
||||
[self waitForExpectations:@[expectation] timeout:70.0];
|
||||
}
|
||||
|
||||
// I.5 错误恢复竞态
|
||||
// I.5 错误恢复竞<EFBFBD><EFBFBD>?
|
||||
- (void)testRaceCondition_ErrorRecovery_PoolRemainsHealthy {
|
||||
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
|
||||
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
||||
|
||||
// 发起一些会失败的请求
|
||||
// 发起一些会失败的请<EFBFBD><EFBFBD>?
|
||||
for (NSInteger i = 0; i < 3; i++) {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Error %ld", (long)i]];
|
||||
[expectations addObject:expectation];
|
||||
|
||||
dispatch_async(queue, ^{
|
||||
NSError *error = nil;
|
||||
// 使用短超时导致失败
|
||||
// 使用短超时导致失<EFBFBD><EFBFBD>?
|
||||
HttpdnsNWHTTPClientResponse *response = [self.client performRequestWithURLString:@"http://127.0.0.1:11080/delay/5"
|
||||
userAgent:@"ErrorTest"
|
||||
timeout:1.0
|
||||
@@ -391,9 +391,9 @@
|
||||
[self waitForExpectations:@[recoveryExpectation] timeout:20.0];
|
||||
}
|
||||
|
||||
#pragma mark - N. 并发多端口测试
|
||||
#pragma mark - N. 并发多端口测<EFBFBD><EFBFBD>?
|
||||
|
||||
// N.1 并发保持连接(慢测试)
|
||||
// N.1 并发保持连接(慢测试<EFBFBD><EFBFBD>?
|
||||
- (void)testConcurrentMultiPort_ParallelKeepAlive_IndependentConnections {
|
||||
if (getenv("SKIP_SLOW_TESTS")) {
|
||||
return;
|
||||
@@ -402,7 +402,7 @@
|
||||
XCTestExpectation *expectation11443 = [self expectationWithDescription:@"Port 11443 keep-alive"];
|
||||
XCTestExpectation *expectation11444 = [self expectationWithDescription:@"Port 11444 keep-alive"];
|
||||
|
||||
// 线程 1:向端口 11443 发起 10 个请求,间隔 1 秒
|
||||
// 线程 1:向端口 11443 发起 10 个请求,间隔 1 <EFBFBD><EFBFBD>?
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
for (NSInteger i = 0; i < 10; i++) {
|
||||
if (i > 0) {
|
||||
@@ -417,7 +417,7 @@
|
||||
[expectation11443 fulfill];
|
||||
});
|
||||
|
||||
// 线程 2:同时向端口 11444 发起 10 个请求,间隔 1 秒
|
||||
// 线程 2:同时向端口 11444 发起 10 个请求,间隔 1 <EFBFBD><EFBFBD>?
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
for (NSInteger i = 0; i < 10; i++) {
|
||||
if (i > 0) {
|
||||
@@ -449,7 +449,7 @@
|
||||
portRequestCounts[port] = @0;
|
||||
}
|
||||
|
||||
// 以轮询方式向 4 个端口分发 100 个请求
|
||||
// 以轮询方式向 4 个端口分<EFBFBD><EFBFBD>?100 个请<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];
|
||||
@@ -466,7 +466,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 验证每个端口大约获得 25 个请求
|
||||
// 验证每个端口大约获得 25 个请<EFBFBD><EFBFBD>?
|
||||
for (NSNumber *port in ports) {
|
||||
NSInteger count = [portRequestCounts[port] integerValue];
|
||||
XCTAssertEqual(count, 25, @"Port %@ should receive 25 requests", port);
|
||||
@@ -478,12 +478,12 @@
|
||||
[self waitForExpectations:@[expectation] timeout:180.0];
|
||||
}
|
||||
|
||||
// N.3 混合负载多端口场景
|
||||
// N.3 混合负载多端口场<EFBFBD><EFBFBD>?
|
||||
- (void)testConcurrentMultiPort_MixedLoadPattern_RobustHandling {
|
||||
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray array];
|
||||
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
||||
|
||||
// 端口 11443:高负载(20 个请求)
|
||||
// 端口 11443:高负载<EFBFBD><EFBFBD>?0 个请求)
|
||||
for (NSInteger i = 0; i < 20; i++) {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Heavy11443 %ld", (long)i]];
|
||||
[expectations addObject:expectation];
|
||||
@@ -498,7 +498,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 端口 11444:中负载(10 个请求)
|
||||
// 端口 11444:中负载<EFBFBD><EFBFBD>?0 个请求)
|
||||
for (NSInteger i = 0; i < 10; i++) {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Medium11444 %ld", (long)i]];
|
||||
[expectations addObject:expectation];
|
||||
@@ -513,7 +513,7 @@
|
||||
});
|
||||
}
|
||||
|
||||
// 端口 11445:低负载(5 个请求)
|
||||
// 端口 11445:低负载<EFBFBD><EFBFBD>? 个请求)
|
||||
for (NSInteger i = 0; i < 5; i++) {
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:[NSString stringWithFormat:@"Light11445 %ld", (long)i]];
|
||||
[expectations addObject:expectation];
|
||||
|
||||
Reference in New Issue
Block a user