-
Notifications
You must be signed in to change notification settings - Fork 576
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
Adds race task #229
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name suggestion: |
||
__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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like you can unify this as |
||
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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Use dot-syntax, since |
||
} 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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Use dot-syntax, since |
||
} 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -618,6 +618,7 @@ - (void)testTaskForCompletionOfAllTasksSuccessError { | |
XCTAssertTrue(allTasks.faulted, @"Task should be faulted"); | ||
} | ||
|
||
|
||
- (void)testTaskForCompletionOfAllTasksWithResultsNoTasksImmediateCompletion { | ||
NSMutableArray *tasks = [NSMutableArray array]; | ||
|
||
|
@@ -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]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove space before |
||
[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"]]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove space before |
||
[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]]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove space before |
||
[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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove space before |
||
[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]]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Remove space before |
||
[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"; | ||
|
There was a problem hiding this comment.
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.