|
- (void)testThatAuthOperationBlocksGeneralOperations { |
|
// given |
|
XCTestExpectation *expectation = [self expectationWithDescription:@"Last operation fired"]; |
|
NSMutableArray *operationNames = [NSMutableArray array]; |
|
|
|
NSString *const kAuthOperationName = @"AuthOperation"; |
|
NSString *const kInitialOperationName = @"InitialOperation"; |
|
NSString *const kGeneralOperationName = @"GeneralOperation"; |
|
NSUInteger const kGeneralOperationsCount = 5; |
|
__block NSNumber *operationCounter = @0; |
|
|
|
NSBlockOperation *authOperation = [NSBlockOperation blockOperationWithBlock:^{ |
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
|
@synchronized(operationNames) { |
|
[operationNames addObject:kAuthOperationName]; |
|
} |
|
[NSThread sleepForTimeInterval:0.05]; |
|
}); |
|
}]; |
|
|
|
|
|
NSBlockOperation *initialOperation = [NSBlockOperation blockOperationWithBlock:^{ |
|
@synchronized(operationNames) { |
|
[operationNames addObject:kInitialOperationName]; |
|
} |
|
[self.scheduler addAuthOperation:authOperation]; |
|
}]; |
|
|
|
// when |
|
[self.scheduler addGeneralOperation:initialOperation]; |
|
for (NSUInteger i = 0; i < kGeneralOperationsCount; i++) { |
|
NSBlockOperation *generalOperation = [NSBlockOperation blockOperationWithBlock:^{ |
|
@synchronized(operationNames) { |
|
[operationNames addObject:kGeneralOperationName]; |
|
} |
|
|
|
@synchronized(operationCounter) { |
|
operationCounter = @([operationCounter integerValue] + 1); |
|
if ([operationCounter integerValue] == kGeneralOperationsCount) { |
|
dispatch_async(dispatch_get_main_queue(), ^{ |
|
[expectation fulfill]; |
|
}); |
|
} |
|
} |
|
}]; |
|
[self.scheduler addGeneralOperation:generalOperation]; |
|
} |
|
|
|
// then |
|
[self waitForExpectationsWithTimeout:kTestExpectationTimeout handler:^(NSError *error) { |
|
XCTAssertEqualObjects(operationNames[0], kInitialOperationName); |
|
XCTAssertEqualObjects(operationNames[1], kAuthOperationName); |
|
for (NSUInteger i = 2; i < kGeneralOperationsCount; i++) { |
|
XCTAssertEqualObjects(operationNames[i], kGeneralOperationName); |
|
} |
|
}]; |
|
} |