Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds race task #229

Merged
merged 4 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Bolts/Common/BFTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ typedef __nullable id(^BFContinuationBlock)(BFTask<ResultType> *task);
*/
+ (instancetype)taskForCompletionOfAllTasksWithResults:(nullable NSArray<BFTask *> *)tasks;

/*!
Returns a task that will be completed once there is at least one successful task.
The first task to successuly complete will set the result, all other tasks results are
ignored.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expand on behavior of errors/exceptions/cancellation, as without documentation it's an implementation detail at this point.

@param tasks An `NSArray` of the tasks to use as an input.
*/
+ (instancetype)taskForCompletionOfAnyTask:(nullable NSArray<BFTask *> *)tasks;

/*!
Returns a task that will be completed a certain amount of time in the future.
@param millis The approximate number of milliseconds to wait before the
Expand Down
68 changes: 68 additions & 0 deletions Bolts/Common/BFTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,74 @@ + (instancetype)taskForCompletionOfAllTasksWithResults:(nullable NSArray<BFTask
}];
}

+ (instancetype)taskForCompletionOfAnyTask:(nullable NSArray<BFTask *> *)tasks
{
__block int32_t total = (int32_t)tasks.count;
if (total == 0) {
return [self taskWithResult:nil];
}

__block int succeeded = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name suggestion: completed, since it doesn't necessarily mark success.

__block int32_t cancelled = 0;

NSObject *lock = [NSObject new];
NSMutableArray *errors = [NSMutableArray new];
NSMutableArray *exceptions = [NSMutableArray new];

BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];
for (BFTask *task in tasks) {
[task continueWithBlock:^id(BFTask *task) {
if (task.exception != nil) {
@synchronized(lock) {
[exceptions addObject:task.exception];
}
} else if (task.error != nil) {
@synchronized(lock) {
[errors addObject:task.error];
}
} else if (task.cancelled) {
OSAtomicIncrement32Barrier(&cancelled);
}

if (task.completed && !task.faulted && !task.cancelled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like you can unify this as else statement on the above condition. Also, there is no way this task is not going to be completed here, since continueWithBlock: is called only after it's completed.

if(OSAtomicCompareAndSwap32Barrier(0, 1, &succeeded)) {
[source setResult:task.result];
}
}

if (OSAtomicDecrement32Barrier(&total) == 0 &&
OSAtomicCompareAndSwap32Barrier(0, 1, &succeeded)) {
if (cancelled > 0) {
[source cancel];
} else if (exceptions.count > 0) {
if (exceptions.count == 1) {
source.exception = [exceptions firstObject];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use dot-syntax, since firstObject is a property.

} else {
NSException *exception =
[NSException exceptionWithName:BFTaskMultipleExceptionsException
reason:@"There were multiple exceptions."
userInfo:@{ @"exceptions": exceptions }];
source.exception = exception;
}
} else if (errors.count > 0) {
if (errors.count == 1) {
source.error = [errors firstObject];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Use dot-syntax, since firstObject is a property.

} else {
NSError *error = [NSError errorWithDomain:BFTaskErrorDomain
code:kBFMultipleErrorsError
userInfo:@{ @"errors": errors }];
source.error = error;
}
}
}
// Abort execution of per tasks continuations
return nil;
}];
}
return source.task;
}


+ (instancetype)taskWithDelay:(int)millis {
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, millis * NSEC_PER_MSEC);
Expand Down
78 changes: 78 additions & 0 deletions BoltsTests/TaskTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ - (void)testTaskForCompletionOfAllTasksSuccessError {
XCTAssertTrue(allTasks.faulted, @"Task should be faulted");
}


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

Expand All @@ -628,6 +629,83 @@ - (void)testTaskForCompletionOfAllTasksWithResultsNoTasksImmediateCompletion {
XCTAssertTrue(task.result != nil);
}

- (void)testTasksForTaskForCompletionOfAnyTasksWithSuccess {
BFTask * task = [BFTask taskForCompletionOfAnyTask:@[[BFTask taskWithDelay:20], [BFTask taskWithResult:@"success"]]];
[task waitUntilFinished];

XCTAssertEqualObjects(@"success", task.result);
}

- (void)testTasksForTaskForCompletionOfAnyTasksWithRacing {
BFTask *first = [[BFTask taskWithDelay:2] continueWithBlock:^id _Nullable(BFTask * _Nonnull task) {
return [BFTask taskWithResult:@"first"];
}];
BFTask *second = [[BFTask taskWithDelay:3] continueWithBlock:^id _Nullable(BFTask * _Nonnull task) {
return [BFTask taskWithResult:@"second"];
}];


BFTask * task = [BFTask taskForCompletionOfAnyTask:@[first, second]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove space before task.

[task waitUntilFinished];

XCTAssertEqualObjects(@"first", task.result);
}



- (void)testTasksForTaskForCompletionOfAnyTasksWithErrorAndSuccess {
NSError *error = [NSError errorWithDomain:@"BoltsTests"
code:35
userInfo:nil];

BFTask * task = [BFTask taskForCompletionOfAnyTask:@[[BFTask taskWithError:error], [BFTask taskWithResult:@"success"]]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove space before task.

[task waitUntilFinished];

XCTAssertEqualObjects(@"success", task.result);
XCTAssertNil(task.error);
}

- (void)testTasksForTaskForCompletionOfAnyTasksWithError {
NSError *error = [NSError errorWithDomain:@"BoltsTests"
code:35
userInfo:nil];

BFTask * task = [BFTask taskForCompletionOfAnyTask:@[[BFTask taskWithError:error]]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove space before task.

[task waitUntilFinished];

XCTAssertEqualObjects(error, task.error);
XCTAssertNotNil(task.error);
}

- (void)testTasksForTaskForCompletionOfAnyTasksWithNilArray {
NSError *error = [NSError errorWithDomain:@"BoltsTests"
code:35
userInfo:nil];

BFTask * task = [BFTask taskForCompletionOfAnyTask:nil];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove space before task.

[task waitUntilFinished];

XCTAssertNil(task.result);
XCTAssertNil(task.error);
XCTAssertNil(task.exception);
}

- (void)testTasksForTaskForCompletionOfAnyTasksAllErrors {
NSError *error = [NSError errorWithDomain:@"BoltsTests"
code:35
userInfo:nil];

BFTask * task = [BFTask taskForCompletionOfAnyTask:@[[BFTask taskWithError:error], [BFTask taskWithError:error]]];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove space before task.

[task waitUntilFinished];

XCTAssertNil(task.result);
XCTAssertNotNil(task.error);
XCTAssertNotNil(task.error.userInfo);
XCTAssertEqualObjects(@"bolts", task.error.domain);
XCTAssertTrue([task.error.userInfo[@"errors"] isKindOfClass:[NSArray class]]);
XCTAssertEqual(2, [task.error.userInfo[@"errors"] count]);
}

- (void)testWaitUntilFinished {
BFTask *task = [[BFTask taskWithDelay:50] continueWithBlock:^id(BFTask *task) {
return @"foo";
Expand Down
2 changes: 1 addition & 1 deletion Vendor/xctoolchain