102 lines
3.4 KiB
Objective-C
102 lines
3.4 KiB
Objective-C
//
|
||
// EnableReuseExpiredIpTest.m
|
||
// TrustHttpDNSTests
|
||
//
|
||
// Created by xuyecan on 2024/5/28.
|
||
// Copyright © 2024 trustapp.com. All rights reserved.
|
||
//
|
||
|
||
#import <Foundation/Foundation.h>
|
||
#import "TestBase.h"
|
||
|
||
@interface EnableReuseExpiredIpTest : TestBase <HttpdnsTTLDelegate>
|
||
|
||
@end
|
||
|
||
static int ttlForTest = 3;
|
||
|
||
@implementation EnableReuseExpiredIpTest
|
||
|
||
+ (void)setUp {
|
||
[super setUp];
|
||
}
|
||
|
||
- (void)setUp {
|
||
[super setUp];
|
||
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
self.httpdns = [[HttpDnsService alloc] initWithAccountID:100000];
|
||
});
|
||
|
||
[self.httpdns setLogEnabled:YES];
|
||
|
||
[self.httpdns setReuseExpiredIPEnabled:YES];
|
||
|
||
[self.httpdns setTtlDelegate:self];
|
||
[self.httpdns setLogHandler:self];
|
||
|
||
self.currentTimeStamp = [[NSDate date] timeIntervalSince1970];
|
||
}
|
||
|
||
- (int64_t)httpdnsHost:(NSString *)host ipType:(TrustHttpDNS_IPType)ipType ttl:(int64_t)ttl {
|
||
// 在测试ä¸åŸŸå<C5B8><C3A5>快速过æœ?
|
||
return ttlForTest;
|
||
}
|
||
|
||
- (void)testReuseExpiredIp {
|
||
NSString *host = hostNameIpPrefixMap.allKeys.firstObject;
|
||
NSString *ipPrefix = hostNameIpPrefixMap[host];
|
||
|
||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
||
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
// 清空缓å˜
|
||
[self.httpdns.requestManager cleanAllHostMemoryCache];
|
||
|
||
// 首次解æž<C3A6>
|
||
HttpdnsResult *result = [self.httpdns resolveHostSync:host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
XCTAssertNotNil(result);
|
||
XCTAssertTrue([result.host isEqualToString:host]);
|
||
XCTAssertGreaterThan(result.ttl, 0);
|
||
XCTAssertLessThanOrEqual(result.ttl, ttlForTest);
|
||
XCTAssertLessThan([[NSDate date] timeIntervalSince1970], result.lastUpdatedTimeInterval + result.ttl);
|
||
NSString *firstIp = [result firstIpv4Address];
|
||
XCTAssertTrue([firstIp hasPrefix:ipPrefix]);
|
||
|
||
// ç‰å¾…过期
|
||
[NSThread sleepForTimeInterval:ttlForTest + 1];
|
||
|
||
// é‡<C3A9>å¤<C3A5>è§£æž<C3A6>
|
||
HttpdnsResult *result2 = [self.httpdns resolveHostSync:host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
XCTAssertNotNil(result2);
|
||
XCTAssertTrue([result2.host isEqualToString:host]);
|
||
XCTAssertGreaterThan(result2.ttl, 0);
|
||
XCTAssertLessThanOrEqual(result2.ttl, ttlForTest);
|
||
// å› ä¸ºè¿<C3A8>行å¤<C3A5>用过期解æž<C3A6>ç»“æžœï¼Œå› æ¤è¿™é‡ŒèŽ·å¾—çš„ä¸€å®šæ˜¯å·²ç»<C3A7>过期çš?
|
||
XCTAssertGreaterThan([[NSDate date] timeIntervalSince1970], result2.lastUpdatedTimeInterval + result2.ttl);
|
||
NSString *firstIp2 = [result2 firstIpv4Address];
|
||
XCTAssertTrue([firstIp2 hasPrefix:ipPrefix]);
|
||
|
||
// ç‰å¾…第二次解æž<C3A6>触å<C2A6>‘的请求完æˆ<C3A6>
|
||
[NSThread sleepForTimeInterval:1];
|
||
|
||
// å†<C3A5>次使用nonblocking方法解æž<C3A6>ï¼Œæ¤æ—¶åº”该已ç»<C3A7>拿到有效结æž?
|
||
HttpdnsResult *result3 = [self.httpdns resolveHostSyncNonBlocking:host byIpType:HttpdnsQueryIPTypeIpv4];
|
||
XCTAssertNotNil(result3);
|
||
XCTAssertTrue([result3.host isEqualToString:host]);
|
||
XCTAssertGreaterThan(result3.ttl, 0);
|
||
XCTAssertLessThanOrEqual(result3.ttl, ttlForTest);
|
||
// 有效结果必定未过�
|
||
XCTAssertLessThan([[NSDate date] timeIntervalSince1970], result3.lastUpdatedTimeInterval + result3.ttl);
|
||
NSString *firstIp3 = [result3 firstIpv4Address];
|
||
XCTAssertTrue([firstIp3 hasPrefix:ipPrefix]);
|
||
|
||
dispatch_semaphore_signal(semaphore);
|
||
});
|
||
|
||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
||
}
|
||
|
||
@end
|