阿里sdk
This commit is contained in:
59
EdgeHttpDNS/sdk/ios/AlicloudHttpDNSTests/Testbase/TestBase.h
Normal file
59
EdgeHttpDNS/sdk/ios/AlicloudHttpDNSTests/Testbase/TestBase.h
Normal file
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// TestBase.h
|
||||
// AlicloudHttpDNS
|
||||
//
|
||||
// Created by ElonChan(地风) on 2017/4/14.
|
||||
// Copyright © 2017年 alibaba-inc.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <XCTest/XCTest.h>
|
||||
#import <OCMock/OCMock.h>
|
||||
#import "XCTestCase+AsyncTesting.h"
|
||||
#import "HttpdnsRequest.h"
|
||||
#import "HttpdnsHostObject.h"
|
||||
#import "HttpdnsService.h"
|
||||
#import "HttpdnsService_Internal.h"
|
||||
|
||||
|
||||
#define NOTIFY [self notify:XCTAsyncTestCaseStatusSucceeded];
|
||||
#define WAIT [self waitForStatus:XCTAsyncTestCaseStatusSucceeded timeout:30];
|
||||
#define WAIT_60 [self waitForStatus:XCTAsyncTestCaseStatusSucceeded timeout:60];
|
||||
#define WAIT_120 [self waitForStatus:XCTAsyncTestCaseStatusSucceeded timeout:120];
|
||||
#define WAIT_10 [self waitForStatus:XCTAsyncTestCaseStatusSucceeded timeout:10.0];
|
||||
#define WAIT_FOREVER [self waitForStatus:XCTAsyncTestCaseStatusSucceeded timeout:DBL_MAX];
|
||||
|
||||
static NSString *ipv4OnlyHost = @"ipv4.only.com";
|
||||
static NSString *ipv6OnlyHost = @"ipv6.only.com";
|
||||
static NSString *ipv4AndIpv6Host = @"ipv4.and.ipv6.com";
|
||||
|
||||
static NSString *ipv41 = @"1.1.1.1";
|
||||
static NSString *ipv42 = @"2.2.2.2";
|
||||
static NSString *ipv61 = @"2001:4860:4860::8888";
|
||||
static NSString *ipv62 = @"2001:4860:4860::8844";
|
||||
|
||||
extern NSDictionary<NSString *, NSString *> *hostNameIpPrefixMap;
|
||||
|
||||
@interface TestBase : XCTestCase <HttpdnsLoggerProtocol>
|
||||
|
||||
@property (nonatomic, strong) HttpDnsService *httpdns;
|
||||
|
||||
@property (nonatomic, assign) NSTimeInterval currentTimeStamp;
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv4HostObject;
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv6HostObject;
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv4AndIpv6HostObject;
|
||||
|
||||
- (void)presetNetworkEnvAsIpv4;
|
||||
|
||||
- (void)presetNetworkEnvAsIpv6;
|
||||
|
||||
- (void)presetNetworkEnvAsIpv4AndIpv6;
|
||||
|
||||
- (void)shouldNotHaveCallNetworkRequestWhenResolving:(void (^)(void))resolvingBlock;
|
||||
|
||||
- (void)shouldHaveCalledRequestWhenResolving:(void (^)(void))resolvingBlock;
|
||||
|
||||
@end
|
||||
133
EdgeHttpDNS/sdk/ios/AlicloudHttpDNSTests/Testbase/TestBase.m
Normal file
133
EdgeHttpDNS/sdk/ios/AlicloudHttpDNSTests/Testbase/TestBase.m
Normal file
@@ -0,0 +1,133 @@
|
||||
//
|
||||
// TestBase.m
|
||||
// AlicloudHttpDNS
|
||||
//
|
||||
// Created by ElonChan(地风) on 2017/4/14.
|
||||
// Copyright © 2017年 alibaba-inc.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TestBase.h"
|
||||
#import <mach/mach.h>
|
||||
#import "HttpdnsIpStackDetector.h"
|
||||
|
||||
NSDictionary<NSString *, NSString *> *hostNameIpPrefixMap;
|
||||
|
||||
@implementation TestBase
|
||||
|
||||
+ (void)setUp {
|
||||
hostNameIpPrefixMap = @{
|
||||
@"v4host1.onlyforhttpdnstest.run.place": @"0.0.1",
|
||||
@"v4host2.onlyforhttpdnstest.run.place": @"0.0.2",
|
||||
@"v4host3.onlyforhttpdnstest.run.place": @"0.0.3",
|
||||
@"v4host4.onlyforhttpdnstest.run.place": @"0.0.4",
|
||||
@"v4host5.onlyforhttpdnstest.run.place": @"0.0.5"
|
||||
};
|
||||
}
|
||||
|
||||
- (void)setUp {
|
||||
[super setUp];
|
||||
}
|
||||
|
||||
- (void)tearDown {
|
||||
[super tearDown];
|
||||
}
|
||||
|
||||
- (void)log:(NSString *)logStr {
|
||||
mach_port_t threadID = mach_thread_self();
|
||||
NSString *threadIDString = [NSString stringWithFormat:@"%x", threadID];
|
||||
printf("%ld-%s %s\n", (long)[[NSDate date] timeIntervalSince1970], [threadIDString UTF8String], [logStr UTF8String]);
|
||||
}
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv4HostObject {
|
||||
HttpdnsHostObject *hostObject = [[HttpdnsHostObject alloc] init];
|
||||
hostObject.hostName = ipv4OnlyHost;
|
||||
hostObject.v4ttl = 60;
|
||||
HttpdnsIpObject *ip1 = [[HttpdnsIpObject alloc] init];
|
||||
[ip1 setIp:ipv41];
|
||||
HttpdnsIpObject *ip2 = [[HttpdnsIpObject alloc] init];
|
||||
[ip2 setIp:ipv42];
|
||||
hostObject.v4Ips = @[ip1, ip2];
|
||||
hostObject.lastIPv4LookupTime = self.currentTimeStamp;
|
||||
return hostObject;
|
||||
}
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv6HostObject {
|
||||
HttpdnsHostObject *hostObject = [[HttpdnsHostObject alloc] init];
|
||||
hostObject.hostName = ipv4OnlyHost;
|
||||
hostObject.v6ttl = 60;
|
||||
HttpdnsIpObject *ip1 = [[HttpdnsIpObject alloc] init];
|
||||
[ip1 setIp:@"2001:4860:4860::8888"];
|
||||
HttpdnsIpObject *ip2 = [[HttpdnsIpObject alloc] init];
|
||||
[ip2 setIp:@"2001:4860:4860::8844"];
|
||||
hostObject.v6Ips = @[ip1, ip2];
|
||||
hostObject.lastIPv6LookupTime = self.currentTimeStamp;
|
||||
return hostObject;
|
||||
}
|
||||
|
||||
- (HttpdnsHostObject *)constructSimpleIpv4AndIpv6HostObject {
|
||||
HttpdnsHostObject *hostObject = [[HttpdnsHostObject alloc] init];
|
||||
hostObject.hostName = ipv4AndIpv6Host;
|
||||
hostObject.v4ttl = 60;
|
||||
HttpdnsIpObject *ip1 = [[HttpdnsIpObject alloc] init];
|
||||
[ip1 setIp:ipv41];
|
||||
HttpdnsIpObject *ip2 = [[HttpdnsIpObject alloc] init];
|
||||
[ip2 setIp:ipv42];
|
||||
hostObject.v4Ips = @[ip1, ip2];
|
||||
hostObject.lastIPv4LookupTime = self.currentTimeStamp;
|
||||
|
||||
hostObject.v6ttl = 60;
|
||||
HttpdnsIpObject *ip3 = [[HttpdnsIpObject alloc] init];
|
||||
[ip3 setIp:ipv61];
|
||||
HttpdnsIpObject *ip4 = [[HttpdnsIpObject alloc] init];
|
||||
[ip4 setIp:ipv62];
|
||||
hostObject.v6Ips = @[ip3, ip4];
|
||||
hostObject.lastIPv6LookupTime = self.currentTimeStamp;
|
||||
return hostObject;
|
||||
}
|
||||
|
||||
- (void)presetNetworkEnvAsIpv4 {
|
||||
HttpdnsIpStackDetector *mockIpv6Adapter = OCMPartialMock([HttpdnsIpStackDetector sharedInstance]);
|
||||
OCMStub([mockIpv6Adapter currentIpStack]).andReturn(kHttpdnsIpv4Only);
|
||||
OCMStub([mockIpv6Adapter isIpv6OnlyNetwork]).andReturn(NO);
|
||||
|
||||
id mockAdapterClass = OCMClassMock([HttpdnsIpStackDetector class]);
|
||||
OCMStub([mockAdapterClass sharedInstance]).andReturn(mockIpv6Adapter);
|
||||
}
|
||||
|
||||
- (void)presetNetworkEnvAsIpv6 {
|
||||
HttpdnsIpStackDetector *mockIpv6Adapter = OCMPartialMock([HttpdnsIpStackDetector sharedInstance]);
|
||||
OCMStub([mockIpv6Adapter currentIpStack]).andReturn(kHttpdnsIpv6Only);
|
||||
OCMStub([mockIpv6Adapter isIpv6OnlyNetwork]).andReturn(YES);
|
||||
|
||||
id mockAdapterClass = OCMClassMock([HttpdnsIpStackDetector class]);
|
||||
OCMStub([mockAdapterClass sharedInstance]).andReturn(mockIpv6Adapter);
|
||||
}
|
||||
|
||||
- (void)presetNetworkEnvAsIpv4AndIpv6 {
|
||||
HttpdnsIpStackDetector *mockIpv6Adapter = OCMPartialMock([HttpdnsIpStackDetector sharedInstance]);
|
||||
OCMStub([mockIpv6Adapter currentIpStack]).andReturn(kHttpdnsIpDual);
|
||||
OCMStub([mockIpv6Adapter isIpv6OnlyNetwork]).andReturn(NO);
|
||||
|
||||
id mockAdapterClass = OCMClassMock([HttpdnsIpStackDetector class]);
|
||||
OCMStub([mockAdapterClass sharedInstance]).andReturn(mockIpv6Adapter);
|
||||
}
|
||||
|
||||
- (void)shouldNotHaveCallNetworkRequestWhenResolving:(void (^)(void))resolvingBlock {
|
||||
HttpDnsService *httpdns = [HttpDnsService sharedInstance];
|
||||
HttpdnsRequestManager *requestManager = httpdns.requestManager;
|
||||
HttpdnsRequestManager *mockScheduler = OCMPartialMock(requestManager);
|
||||
OCMReject([mockScheduler executeRequest:[OCMArg any] retryCount:0]);
|
||||
resolvingBlock();
|
||||
OCMVerifyAll(mockScheduler);
|
||||
}
|
||||
|
||||
- (void)shouldHaveCalledRequestWhenResolving:(void (^)(void))resolvingBlock {
|
||||
HttpDnsService *httpdns = [HttpDnsService sharedInstance];
|
||||
HttpdnsRequestManager *requestManager = httpdns.requestManager;
|
||||
HttpdnsRequestManager *mockScheduler = OCMPartialMock(requestManager);
|
||||
OCMExpect([mockScheduler executeRequest:[OCMArg any] retryCount:0]).andReturn(nil);
|
||||
resolvingBlock();
|
||||
OCMVerifyAll(mockScheduler);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// TestBase.h
|
||||
// AlicloudHttpDNS
|
||||
//
|
||||
// Created by ElonChan(地风) on 2017/4/14.
|
||||
// Copyright © 2017年 alibaba-inc.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
|
||||
enum {
|
||||
XCTAsyncTestCaseStatusUnknown = 0,
|
||||
XCTAsyncTestCaseStatusWaiting,
|
||||
XCTAsyncTestCaseStatusSucceeded,
|
||||
XCTAsyncTestCaseStatusFailed,
|
||||
XCTAsyncTestCaseStatusCancelled,
|
||||
};
|
||||
typedef NSUInteger XCTAsyncTestCaseStatus;
|
||||
|
||||
|
||||
@interface XCTestCase (AsyncTesting)
|
||||
|
||||
- (void)waitForStatus:(XCTAsyncTestCaseStatus)status timeout:(NSTimeInterval)timeout;
|
||||
- (void)waitForTimeout:(NSTimeInterval)timeout;
|
||||
- (void)notify:(XCTAsyncTestCaseStatus)status;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,113 @@
|
||||
//
|
||||
// TestBase.h
|
||||
// AlicloudHttpDNS
|
||||
//
|
||||
// Created by ElonChan(地风) on 2017/4/14.
|
||||
// Copyright © 2017年 alibaba-inc.com. All rights reserved.
|
||||
//
|
||||
|
||||
#import "XCTestCase+AsyncTesting.h"
|
||||
#import "objc/runtime.h"
|
||||
|
||||
static void *kLoopUntil_Key = "LoopUntil_Key";
|
||||
static void *kNotified_Key = "kNotified_Key";
|
||||
static void *kNotifiedStatus_Key = "kNotifiedStatus_Key";
|
||||
static void *kExpectedStatus_Key = "kExpectedStatus_Key";
|
||||
|
||||
@implementation XCTestCase (AsyncTesting)
|
||||
|
||||
#pragma mark - Public
|
||||
|
||||
|
||||
- (void)waitForStatus:(XCTAsyncTestCaseStatus)status timeout:(NSTimeInterval)timeout {
|
||||
self.notified = NO;
|
||||
self.expectedStatus = status;
|
||||
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
|
||||
|
||||
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
|
||||
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0) {
|
||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
|
||||
beforeDate:dt];
|
||||
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
|
||||
}
|
||||
|
||||
// Only assert when notified. Do not assert when timed out
|
||||
// Fail if not notified
|
||||
if (self.notified) {
|
||||
XCTAssertEqual(self.notifiedStatus, self.expectedStatus, @"Notified status does not match the expected status.");
|
||||
} else {
|
||||
XCTFail(@"Async test timed out.");
|
||||
}
|
||||
}
|
||||
|
||||
- (void)waitForTimeout:(NSTimeInterval)timeout {
|
||||
self.notified = NO;
|
||||
self.expectedStatus = XCTAsyncTestCaseStatusUnknown;
|
||||
self.loopUntil = [NSDate dateWithTimeIntervalSinceNow:timeout];
|
||||
|
||||
NSDate *dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
|
||||
while (!self.notified && [self.loopUntil timeIntervalSinceNow] > 0) {
|
||||
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
|
||||
beforeDate:dt];
|
||||
dt = [NSDate dateWithTimeIntervalSinceNow:0.1];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)notify:(XCTAsyncTestCaseStatus)status {
|
||||
self.notifiedStatus = status;
|
||||
// self.notified must be set at the last of this method
|
||||
self.notified = YES;
|
||||
}
|
||||
|
||||
#pragma nark - Object Association Helpers -
|
||||
|
||||
- (void)setAssociatedObject:(id)anObject key:(void*)key {
|
||||
objc_setAssociatedObject(self, key, anObject, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (id)getAssociatedObject:(void*)key {
|
||||
id anObject = objc_getAssociatedObject(self, key);
|
||||
return anObject;
|
||||
}
|
||||
|
||||
#pragma mark - Property Implementations -
|
||||
|
||||
- (NSDate*)loopUntil {
|
||||
return [self getAssociatedObject:kLoopUntil_Key];
|
||||
}
|
||||
|
||||
- (void)setLoopUntil:(NSDate*)value {
|
||||
[self setAssociatedObject:value key:kLoopUntil_Key];
|
||||
}
|
||||
|
||||
- (BOOL)notified {
|
||||
NSNumber *valueNumber = [self getAssociatedObject:kNotified_Key];
|
||||
return [valueNumber boolValue];
|
||||
}
|
||||
|
||||
- (void)setNotified:(BOOL)value {
|
||||
NSNumber *valueNumber = [NSNumber numberWithBool:value];
|
||||
[self setAssociatedObject:valueNumber key:kNotified_Key];
|
||||
}
|
||||
|
||||
- (XCTAsyncTestCaseStatus)notifiedStatus {
|
||||
NSNumber *valueNumber = [self getAssociatedObject:kNotifiedStatus_Key];
|
||||
return [valueNumber integerValue];
|
||||
}
|
||||
|
||||
- (void)setNotifiedStatus:(XCTAsyncTestCaseStatus)value {
|
||||
NSNumber *valueNumber = [NSNumber numberWithInt:(int)value];
|
||||
[self setAssociatedObject:valueNumber key:kNotifiedStatus_Key];
|
||||
}
|
||||
|
||||
- (XCTAsyncTestCaseStatus)expectedStatus {
|
||||
NSNumber *valueNumber = [self getAssociatedObject:kExpectedStatus_Key];
|
||||
return [valueNumber integerValue];
|
||||
}
|
||||
|
||||
- (void)setExpectedStatus:(XCTAsyncTestCaseStatus)value {
|
||||
NSNumber *valueNumber = [NSNumber numberWithInt:(int)value];
|
||||
[self setAssociatedObject:valueNumber key:kExpectedStatus_Key];
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user