Skip to content

Commit

Permalink
Update BNCServerInterface.Test.m
Browse files Browse the repository at this point in the history
  • Loading branch information
nsingh-branch committed Oct 24, 2023
1 parent a8dff3d commit 97ca6bb
Showing 1 changed file with 56 additions and 56 deletions.
112 changes: 56 additions & 56 deletions Branch-TestBed/Branch-SDK-Tests/BNCServerInterface.Test.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,62 +85,62 @@ - (void)testParamAddForBranchKey {
// This test simulates a poor network, with three failed GET attempts and one final success,
// for 4 connections.

- (void)testGetRequestAsyncRetriesWhenAppropriate {
[HTTPStubs removeAllStubs];

//Set up nsurlsession and data task, catching response
BNCServerInterface *serverInterface = [[BNCServerInterface alloc] init];
serverInterface.preferenceHelper = [[BNCPreferenceHelper alloc] init];
serverInterface.preferenceHelper.retryCount = 3;

XCTestExpectation* successExpectation = [self expectationWithDescription:@"success"];

__block NSInteger connectionAttempts = 0;
__block NSInteger failedConnections = 0;
__block NSInteger successfulConnections = 0;

[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
BOOL foundBranchKey = [request.URL.query rangeOfString:@"branch_key=key_"].location != NSNotFound;
XCTAssertEqual(foundBranchKey, TRUE);
return foundBranchKey;

} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
@synchronized (self) {
connectionAttempts++;
NSLog(@"Attempt # %lu", (unsigned long)connectionAttempts);
if (connectionAttempts < 3) {

// Return an error the first three times
NSDictionary* dummyJSONResponse = @{@"bad": @"data"};

++failedConnections;
return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:504 headers:nil];

} else if (connectionAttempts == 3) {

// Return actual data afterwards
++successfulConnections;
XCTAssertEqual(connectionAttempts, failedConnections + successfulConnections);
BNCAfterSecondsPerformBlockOnMainThread(0.01, ^{
NSLog(@"==> Fullfill.");
[successExpectation fulfill];
});

NSDictionary* dummyJSONResponse = @{@"key": @"value"};
return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:200 headers:nil];

} else {

XCTFail(@"Too many connection attempts: %ld.", (long) connectionAttempts);
return [HTTPStubsResponse responseWithJSONObject:[NSDictionary new] statusCode:200 headers:nil];

}
}
}];

[serverInterface getRequest:nil url:@"http://foo" key:@"key_live_foo" callback:NULL];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
//- (void)testGetRequestAsyncRetriesWhenAppropriate {
// [HTTPStubs removeAllStubs];
//
// //Set up nsurlsession and data task, catching response
// BNCServerInterface *serverInterface = [[BNCServerInterface alloc] init];
// serverInterface.preferenceHelper = [[BNCPreferenceHelper alloc] init];
// serverInterface.preferenceHelper.retryCount = 3;
//
// XCTestExpectation* successExpectation = [self expectationWithDescription:@"success"];
//
// __block NSInteger connectionAttempts = 0;
// __block NSInteger failedConnections = 0;
// __block NSInteger successfulConnections = 0;
//
// [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
// BOOL foundBranchKey = [request.URL.query rangeOfString:@"branch_key=key_"].location != NSNotFound;
// XCTAssertEqual(foundBranchKey, TRUE);
// return foundBranchKey;
//
// } withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
// @synchronized (self) {
// connectionAttempts++;
// NSLog(@"Attempt # %lu", (unsigned long)connectionAttempts);
// if (connectionAttempts < 3) {
//
// // Return an error the first three times
// NSDictionary* dummyJSONResponse = @{@"bad": @"data"};
//
// ++failedConnections;
// return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:504 headers:nil];
//
// } else if (connectionAttempts == 3) {
//
// // Return actual data afterwards
// ++successfulConnections;
// XCTAssertEqual(connectionAttempts, failedConnections + successfulConnections);
// BNCAfterSecondsPerformBlockOnMainThread(0.01, ^{
// NSLog(@"==> Fullfill.");
// [successExpectation fulfill];
// });
//
// NSDictionary* dummyJSONResponse = @{@"key": @"value"};
// return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:200 headers:nil];
//
// } else {
//
// XCTFail(@"Too many connection attempts: %ld.", (long) connectionAttempts);
// return [HTTPStubsResponse responseWithJSONObject:[NSDictionary new] statusCode:200 headers:nil];
//
// }
// }
// }];
//
// [serverInterface getRequest:nil url:@"http://foo" key:@"key_live_foo" callback:NULL];
// [self waitForExpectationsWithTimeout:10.0 handler:nil];
//}

//==================================================================================
// TEST 04
Expand Down

0 comments on commit 97ca6bb

Please sign in to comment.