Skip to content

Commit

Permalink
RSC15a (#509)
Browse files Browse the repository at this point in the history
* Update RSC15a to new requirements

* Increased the number of custom hosts to five in order to keep the randomized order.

* Added more test fallback hosts

* Enhance testCustomFallbackHosts
  • Loading branch information
ricardopereira authored Oct 11, 2016
1 parent 14f51c9 commit d3a5049
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Source/ARTClientOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ART_ASSUME_NONNULL_BEGIN
/**
Optionally allows one or more fallback hosts to be used instead of the default fallback hosts.
*/
@property (art_nullable, nonatomic, copy) NSArray<NSString *> *fallbackHosts;
@property (art_nullable, nonatomic, copy) __GENERIC(NSArray, NSString *) *fallbackHosts;

- (BOOL)isBasicAuth;
- (NSURL *)restUrl;
Expand Down
5 changes: 4 additions & 1 deletion Source/ARTFallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ extern int (^ARTFallback_getRandomHostIndex)(int count);

}

- (id)initWithFallbackHosts:(art_nullable NSArray<NSString*> *)fallbackHosts;
/**
Init with fallback hosts array.
*/
- (instancetype)initWithFallbackHosts:(art_nullable __GENERIC(NSArray, NSString *) *)fallbackHosts;

/**
returns a random fallback host, returns null when all hosts have been popped.
Expand Down
10 changes: 5 additions & 5 deletions Source/ARTFallback.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ @interface ARTFallback ()

@implementation ARTFallback

- (id)initWithFallbackHosts:(art_nullable NSArray<NSString*> *)fallbackHosts {
- (instancetype)initWithFallbackHosts:(art_nullable __GENERIC(NSArray, NSString *) *)fallbackHosts {
self = [super init];
if(self) {
if (self) {
if (fallbackHosts != nil && fallbackHosts.count == 0) {
return nil;
}

self.hosts = [NSMutableArray array];
NSMutableArray * hostArray =[[NSMutableArray alloc] initWithArray: fallbackHosts ? fallbackHosts : [ARTDefault fallbackHosts]];
NSMutableArray * hostArray = [[NSMutableArray alloc] initWithArray: fallbackHosts ? fallbackHosts : [ARTDefault fallbackHosts]];
size_t count = [hostArray count];
for (int i=0; i <count; i++) {
int randomIndex = ARTFallback_getRandomHostIndex((int)[hostArray count]);
Expand All @@ -44,7 +44,7 @@ - (id)initWithFallbackHosts:(art_nullable NSArray<NSString*> *)fallbackHosts {
return self;
}

- (id)init {
- (instancetype)init {
return [self initWithFallbackHosts:nil];
}

Expand Down
31 changes: 31 additions & 0 deletions Spec/RestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,37 @@ class RestClient: QuickSpec {

expect(resultFallbackHosts).to(equal(expectedFallbackHosts))
}

it("use custom fallback hosts if set") {
let options = ARTClientOptions(key: "xxxx:xxxx")
options.httpMaxRetryCount = 10
let customFallbackHosts = ["j.ably-realtime.com",
"i.ably-realtime.com",
"h.ably-realtime.com",
"g.ably-realtime.com",
"f.ably-realtime.com"]
options.fallbackHosts = customFallbackHosts
let client = ARTRest(options: options)
client.httpExecutor = testHTTPExecutor
testHTTPExecutor.http = MockHTTP(network: .HostUnreachable)
let channel = client.channels.get("test")

waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "nil") { _ in
done()
}
}

expect(testHTTPExecutor.requests).to(haveCount(customFallbackHosts.count + 1))

let extractHostname = { (request: NSMutableURLRequest) in
NSRegularExpression.extract(request.URL!.absoluteString, pattern: "[f-j].ably-realtime.com")
}
let resultFallbackHosts = testHTTPExecutor.requests.flatMap(extractHostname)
let expectedFallbackHosts = expectedHostOrder.map { customFallbackHosts[$0] }

expect(resultFallbackHosts).to(equal(expectedFallbackHosts))
}

it("until all fallback hosts have been tried") {
let options = ARTClientOptions(key: "xxxx:xxxx")
Expand Down
36 changes: 36 additions & 0 deletions Tests/ARTFallbackTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,40 @@ - (void)testAllHostsIncludedOnce {
}
}

- (void)testCustomFallbackHosts {
__weak int (^originalARTFallback_getRandomHostIndex)(int) = ARTFallback_getRandomHostIndex;
@try {
ARTFallback_getRandomHostIndex = ^() {
__block NSArray *hostIndexes = @[@1, @2, @0, @1, @0, @0];
__block int i = 0;
return ^int(int count) {
NSNumber *hostIndex = hostIndexes[i];
i++;
return hostIndex.intValue;
};
}();

NSArray *customHosts = @[@"testA.ably.com",
@"testB.ably.com",
@"testC.ably.com",
@"testD.ably.com",
@"testE.ably.com",
@"testF.ably.com"];

ARTFallback *f = [[ARTFallback alloc] initWithFallbackHosts:customHosts];

XCTAssertEqualObjects([f popFallbackHost], @"testF.ably.com");
XCTAssertEqualObjects([f popFallbackHost], @"testC.ably.com");
XCTAssertEqualObjects([f popFallbackHost], @"testE.ably.com");
XCTAssertEqualObjects([f popFallbackHost], @"testA.ably.com");
XCTAssertEqualObjects([f popFallbackHost], @"testD.ably.com");
XCTAssertEqualObjects([f popFallbackHost], @"testB.ably.com");

XCTAssertEqual([f popFallbackHost], nil);
}
@finally {
ARTFallback_getRandomHostIndex = originalARTFallback_getRandomHostIndex;
}
}

@end

0 comments on commit d3a5049

Please sign in to comment.