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

109 lines
3.5 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.

//
// CustomTTLAndCleanCacheTest.m
// TrustHttpDNS
//
// Created by xuyecan on 2024/6/17.
// Copyright © 2024 trustapp.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <stdatomic.h>
#import <mach/mach.h>
#import "HttpdnsService.h"
#import "HttpdnsRemoteResolver.h"
#import "TestBase.h"
static int TEST_CUSTOM_TTL_SECOND = 3;
@interface CustomTTLAndCleanCacheTest : TestBase<HttpdnsTTLDelegate>
@end
@implementation CustomTTLAndCleanCacheTest
- (void)setUp {
[super setUp];
self.httpdns = [[HttpDnsService alloc] initWithAccountID:100000];
[self.httpdns setLogEnabled:YES];
[self.httpdns setTtlDelegate:self];
[self.httpdns setLogHandler:self];
self.currentTimeStamp = [[NSDate date] timeIntervalSince1970];
}
- (void)tearDown {
[super tearDown];
}
- (int64_t)httpdnsHost:(NSString *)host ipType:(TrustHttpDNS_IPType)ipType ttl:(int64_t)ttl {
// 为了在并å<C2B6>æµè¯•中域å<C5B8><C3A5>快速过期,将ttl设置ä¸?ç§?
NSString *testHost = hostNameIpPrefixMap.allKeys.firstObject;
if ([host isEqual:testHost]) {
return TEST_CUSTOM_TTL_SECOND;
}
return ttl;
}
- (void)testCustomTTL {
[self presetNetworkEnvAsIpv4];
[self.httpdns cleanAllHostCache];
NSString *testHost = hostNameIpPrefixMap.allKeys.firstObject;
NSString *expectedIpPrefix = hostNameIpPrefixMap[testHost];
HttpdnsRemoteResolver *resolver = [HttpdnsRemoteResolver new];
id mockResolver = OCMPartialMock(resolver);
__block int invokeCount = 0;
OCMStub([mockResolver resolve:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]])
.andDo(^(NSInvocation *invocation) {
invokeCount++;
})
.andForwardToRealObject();
id mockResolverClass = OCMClassMock([HttpdnsRemoteResolver class]);
OCMStub([mockResolverClass new]).andReturn(mockResolver);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
HttpdnsResult *result = [self.httpdns resolveHostSync:testHost byIpType:HttpdnsQueryIPTypeAuto];
XCTAssertNotNil(result);
XCTAssertEqual(result.ttl, TEST_CUSTOM_TTL_SECOND);
XCTAssertTrue([result.firstIpv4Address hasPrefix:expectedIpPrefix]);
XCTAssertEqual(invokeCount, 1);
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
HttpdnsResult *result = [self.httpdns resolveHostSync:testHost byIpType:HttpdnsQueryIPTypeAuto];
XCTAssertNotNil(result);
XCTAssertEqual(result.ttl, TEST_CUSTOM_TTL_SECOND);
XCTAssertTrue([result.firstIpv4Address hasPrefix:expectedIpPrefix]);
XCTAssertEqual(invokeCount, 1);
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
[NSThread sleepForTimeInterval:TEST_CUSTOM_TTL_SECOND + 1];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
HttpdnsResult *result = [self.httpdns resolveHostSync:testHost byIpType:HttpdnsQueryIPTypeAuto];
XCTAssertNotNil(result);
XCTAssertEqual(result.ttl, TEST_CUSTOM_TTL_SECOND);
XCTAssertTrue([result.firstIpv4Address hasPrefix:expectedIpPrefix]);
XCTAssertEqual(invokeCount, 2);
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
@end