Skip to content

Commit

Permalink
Merge pull request #93 from JosephEarl/issue/87
Browse files Browse the repository at this point in the history
Fix State Priority Bug #87
  • Loading branch information
nlutsenko committed Apr 15, 2015
2 parents 0912767 + 1bc65e4 commit 11c6c90
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Bolts/Common/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ + (instancetype)taskForCompletionOfAllTasks:(NSArray *)tasks {
if (total == 0) {
return [self taskWithResult:nil];
}

__block int32_t cancelled = 0;
NSObject *lock = [[NSObject alloc] init];
NSMutableArray *errors = [NSMutableArray array];
Expand All @@ -91,22 +91,20 @@ + (instancetype)taskForCompletionOfAllTasks:(NSArray *)tasks {
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
for (BFTask *task in tasks) {
[task continueWithBlock:^id(BFTask *task) {
if (task.cancelled) {
OSAtomicIncrement32(&cancelled);
} else if (task.exception) {
if (task.exception) {
@synchronized (lock) {
[exceptions addObject:task.exception];
}
} else if (task.error) {
@synchronized (lock) {
[errors addObject:task.error];
}
} else if (task.cancelled) {
OSAtomicIncrement32(&cancelled);
}

if (OSAtomicDecrement32(&total) == 0) {
if (cancelled > 0) {
[tcs cancel];
} else if (exceptions.count > 0) {
if (exceptions.count > 0) {
if (exceptions.count == 1) {
tcs.exception = [exceptions firstObject];
} else {
Expand All @@ -125,6 +123,8 @@ + (instancetype)taskForCompletionOfAllTasks:(NSArray *)tasks {
userInfo:@{ @"errors": errors }];
tcs.error = error;
}
} else if (cancelled > 0) {
[tcs cancel];
} else {
tcs.result = nil;
}
Expand Down
63 changes: 63 additions & 0 deletions BoltsTests/TaskTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,69 @@ - (void)testTaskForCompletionOfAllTasksWithResultsSuccess {
}] waitUntilFinished];
}

- (void)testTaskForCompletionOfAllTasksErrorCancelledSuccess {
BFTask *errorTask = [BFTask taskWithError:[NSError new]];
BFTask *cancelledTask = [BFTask cancelledTask];
BFTask *successfulTask = [BFTask taskWithResult:[NSNumber numberWithInt:2]];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[successfulTask, cancelledTask, errorTask]];

XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
}

- (void)testTaskForCompletionOfAllTasksExceptionCancelledSuccess {
NSException *exception = [NSException exceptionWithName:@"" reason:@"" userInfo:nil];
BFTask *exceptionTask = [BFTask taskWithException:exception];
BFTask *cancelledTask = [BFTask cancelledTask];
BFTask *successfulTask = [BFTask taskWithResult:[NSNumber numberWithInt:2]];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[successfulTask, cancelledTask, exceptionTask]];

XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
XCTAssertNil(allTasks.error, @"Task shoud not have error");
XCTAssertNotNil(allTasks.exception, @"Task should have exception");
}

- (void)testTaskForCompletionOfAllTasksExceptionErrorCancelledSuccess {
BFTask *errorTask = [BFTask taskWithError:[NSError new]];
BFTask *exceptionTask = [BFTask taskWithException:[NSException new]];
BFTask *cancelledTask = [BFTask cancelledTask];
BFTask *successfulTask = [BFTask taskWithResult:[NSNumber numberWithInt:2]];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[successfulTask, cancelledTask, exceptionTask, errorTask]];

XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
XCTAssertNotNil(allTasks.error, @"Task should have error");
XCTAssertNil(allTasks.exception, @"Task should not have exception");
}

- (void)testTaskForCompletionOfAllTasksErrorCancelled {
BFTask *errorTask = [BFTask taskWithError:[NSError new]];
BFTask *cancelledTask = [BFTask cancelledTask];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[cancelledTask, errorTask]];

XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
}

- (void)testTaskForCompletionOfAllTasksSuccessCancelled {
BFTask *cancelledTask = [BFTask cancelledTask];
BFTask *successfulTask = [BFTask taskWithResult:[NSNumber numberWithInt:2]];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[successfulTask, cancelledTask]];

XCTAssertTrue(allTasks.cancelled, @"Task should be cancelled");
}

- (void)testTaskForCompletionOfAllTasksSuccessError {
BFTask *errorTask = [BFTask taskWithError:[NSError new]];
BFTask *successfulTask = [BFTask taskWithResult:[NSNumber numberWithInt:2]];

BFTask *allTasks = [BFTask taskForCompletionOfAllTasks:@[successfulTask, errorTask]];

XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
}

- (void)testTaskForCompletionOfAllTasksWithResultsNoTasksImmediateCompletion {
NSMutableArray *tasks = [NSMutableArray array];

Expand Down

0 comments on commit 11c6c90

Please sign in to comment.