Last active
September 18, 2016 21:04
-
-
Save etolstoy/dd9552a247df0b327abc898ddb8be5ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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); | |
} | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)testThatAuthOperationBlocksGeneralOperations { | |
// given | |
NSUInteger const kGeneralOperationsCount = 5; | |
TestBlockingByAuthOperationEnvironment *environment = [TestBlockingByAuthOperationEnvironment new]; | |
[environment setupEnvironmentWithTestCase:self | |
generalOperationsCount:kGeneralOperationsCount | |
initialOperationBlock:^{ | |
[self.scheduler addAuthOperation:environment.authOperation]; | |
for (NSOperation *operation in environment.generalOperations) { | |
[self.scheduler addGeneralOperation:operation]; | |
} | |
}]; | |
// when | |
[self.scheduler addGeneralOperation:environment.initialOperation]; | |
// then | |
[self waitForExpectationsWithTimeout:kTestExpectationTimeout handler:^(NSError *error) { | |
XCTAssertEqualObjects(environment.firedOperationNames[0], kInitialOperationName); | |
XCTAssertEqualObjects(environment.firedOperationNames[1], kAuthOperationName); | |
for (NSUInteger i = 2; i < kGeneralOperationsCount; i++) { | |
XCTAssertEqualObjects(environment.firedOperationNames[i], kGeneralOperationName); | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment