阿里sdk

This commit is contained in:
Robin
2026-02-20 17:56:24 +08:00
parent 39524692e5
commit f3af234308
524 changed files with 58345 additions and 0 deletions

View File

@@ -0,0 +1,133 @@
//
// ScheduleCenterV4Test.m
// AlicloudHttpDNSTests
//
// Created by xuyecan on 2024/6/16.
// Copyright © 2024 alibaba-inc.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <OCMock/OCMock.h>
#import "TestBase.h"
#import "HttpdnsHostObject.h"
#import "HttpdnsScheduleCenter.h"
#import "HttpdnsService.h"
#import "HttpdnsService_Internal.h"
#import "HttpdnsRequest_Internal.h"
#import "HttpdnsScheduleExecutor.h"
#import "HttpdnsRemoteResolver.h"
/**
* 使OCMockMock(使stopMocking)
* case
*/
@interface ScheduleCenterV4Test : TestBase
@end
@implementation ScheduleCenterV4Test
+ (void)setUp {
[super setUp];
}
+ (void)tearDown {
[super tearDown];
}
- (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:NO];
[self.httpdns setLogHandler:self];
self.currentTimeStamp = [[NSDate date] timeIntervalSince1970];
}
- (void)tearDown {
[super tearDown];
}
- (void)testUpdateFailureWillMoveToNextUpdateServer {
[self presetNetworkEnvAsIpv4];
HttpdnsScheduleExecutor *realRequest = [HttpdnsScheduleExecutor new];
id mockRequest = OCMPartialMock(realRequest);
OCMStub([mockRequest fetchRegionConfigFromServer:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]])
.andReturn(nil);
id mockRequestClass = OCMClassMock([HttpdnsScheduleExecutor class]);
OCMStub([mockRequestClass new]).andReturn(mockRequest);
HttpdnsScheduleCenter *scheduleCenter = [[HttpdnsScheduleCenter alloc] initWithAccountId:100000];
NSArray<NSString *> *updateServerHostList = [scheduleCenter currentUpdateServerV4HostList];
int updateServerCount = (int)[updateServerHostList count];
XCTAssertGreaterThan(updateServerCount, 0);
int startIndex = [scheduleCenter currentActiveUpdateServerHostIndex];
// 2
[scheduleCenter asyncUpdateRegionScheduleConfigAtRetry:2];
[NSThread sleepForTimeInterval:0.1];
OCMVerify([mockRequest fetchRegionConfigFromServer:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]]);
int currentIndex = [scheduleCenter currentActiveUpdateServerHostIndex];
XCTAssertEqual((startIndex + 1) % updateServerCount, currentIndex);
for (int i = 0; i < updateServerCount; i++) {
[scheduleCenter asyncUpdateRegionScheduleConfigAtRetry:2];
[NSThread sleepForTimeInterval:0.1];
}
int finalIndex = [scheduleCenter currentActiveUpdateServerHostIndex];
XCTAssertEqual(currentIndex, finalIndex % updateServerCount);
[NSThread sleepForTimeInterval:3];
}
- (void)testResolveFailureWillMoveToNextServiceServer {
[self presetNetworkEnvAsIpv4];
id mockResolver = OCMPartialMock([HttpdnsRemoteResolver new]);
OCMStub([mockResolver resolve:[OCMArg any] error:(NSError * __autoreleasing *)[OCMArg anyPointer]])
.andDo(^(NSInvocation *invocation) {
NSError *mockError = [NSError errorWithDomain:@"com.example.error" code:123 userInfo:@{NSLocalizedDescriptionKey: @"Mock error"}];
NSError *__autoreleasing *errorPtr = nil;
[invocation getArgument:&errorPtr atIndex:3];
if (errorPtr) {
*errorPtr = mockError;
}
});
id mockResolverClass = OCMClassMock([HttpdnsRemoteResolver class]);
OCMStub([mockResolverClass new]).andReturn(mockResolver);
HttpdnsScheduleCenter *scheduleCenter = [[HttpdnsScheduleCenter alloc] initWithAccountId:100000];
int startIndex = [scheduleCenter currentActiveServiceServerHostIndex];
int serviceServerCount = (int)[scheduleCenter currentServiceServerV4HostList].count;
HttpdnsRequest *request = [[HttpdnsRequest alloc] initWithHost:@"mock" queryIpType:HttpdnsQueryIPTypeAuto];
[request becomeBlockingRequest];
HttpdnsRequestManager *requestManager = self.httpdns.requestManager;
[requestManager executeRequest:request retryCount:1];
int secondIndex = [scheduleCenter currentActiveServiceServerHostIndex];
XCTAssertEqual((startIndex + 1) % serviceServerCount, secondIndex);
}
@end