Files
waf-platform/HttpDNSSDK/sdk/ios/NewHttpDNSTests/HighLevelTest/EnableReuseExpiredIpTest.m

102 lines
3.4 KiB
Objective-C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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