Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
- (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);
}
}];
}
- (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