Skip to content

Commit

Permalink
expose isCompleted sync accessor for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
bgerstle committed Jun 22, 2016
1 parent a580cef commit 7214f9f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions SPAsync.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@
05B647D116B85AF90050002D /* Project object */ = {
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0730;
LastUpgradeCheck = 0630;
ORGANIZATIONNAME = ThirdCog;
TargetAttributes = {
Expand Down
7 changes: 6 additions & 1 deletion SPAsyncTests/SPTaskTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ - (void)testCallback
__block BOOL firstCallbackTriggered = NO;
__block BOOL secondCallbackTriggered = NO;

__weak __typeof(task) weakTask = task;
[task addCallback:^(id value) {
XCTAssertTrue(weakTask.isCompleted, @"Should be completed by the time the first callback fires.");
XCTAssertEqualObjects(value, @(1337), @"Unexpected value");
XCTAssertEqual(firstCallbackTriggered, NO, @"Callback should only trigger once");
firstCallbackTriggered = YES;
Expand All @@ -41,7 +43,7 @@ - (void)testCallback

XCTAssertEqual(firstCallbackTriggered, YES, @"First callback should have triggered");
XCTAssertEqual(secondCallbackTriggered, YES, @"Second callback should have triggered");

XCTAssertTrue(task.isCompleted, @"Completion state not altered by callbacks.");
}

- (void)testErrback
Expand All @@ -52,7 +54,9 @@ - (void)testErrback
__block BOOL firstErrbackTriggered = NO;
__block BOOL secondErrbackTriggered = NO;

__weak __typeof(task) weakTask = task;
[task addErrorCallback:^(NSError *error) {
XCTAssertTrue(weakTask.isCompleted, @"Should be completed by the time the first callback fires.");
XCTAssertEqual(error.code, (NSInteger)1337, @"Unexpected error code");
XCTAssertEqual(firstErrbackTriggered, NO, @"Errback should only trigger once");
firstErrbackTriggered = YES;
Expand All @@ -74,6 +78,7 @@ - (void)testErrback

XCTAssertEqual(firstErrbackTriggered, YES, @"First errback should have triggered");
XCTAssertEqual(secondErrbackTriggered, YES, @"Second errback should have triggered");
XCTAssertTrue(task.isCompleted, @"Completion state not altered by callbacks.");
}

- (void)testLateCallback
Expand Down
4 changes: 2 additions & 2 deletions Sources/SPTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ @interface SPA_NS(Task) ()
NSMutableArray *_errbacks;
NSMutableArray *_finallys;
NSMutableArray *_childTasks;
BOOL _isCompleted;
BOOL _isCancelled;
id _completedValue;
NSError *_completedError;
__weak SPA_NS(TaskCompletionSource) *_source;
}
@property(getter=isCancelled,readwrite) BOOL cancelled;
@property(getter=isCompleted,readwrite) BOOL completed;
@end

@interface SPA_NS(TaskCompletionSource) ()
Expand All @@ -29,6 +28,7 @@ - (void)cancel;

@implementation SPA_NS(Task)
@synthesize cancelled = _isCancelled;
@synthesize completed = _isCompleted;

- (instancetype)initFromSource:(SPA_NS(TaskCompletionSource)*)source;
{
Expand Down
11 changes: 11 additions & 0 deletions include/SPAsync/SPTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ typedef SPA_NS(Task)*(^SPTaskRecoverCallback)(NSError *error);
@end


@interface SPA_NS(Task) (Testing)

/** Check a task's completed state.
@warning This is intended for testing purposes only, and it is not recommended to use
this property as a means to do micro optimizations for synchronous code.
@return Whether or not the receiver has been completed with a value or error. */
@property(getter=isCompleted,readonly) BOOL completed;

@end

/** Convenience holder of a callback and the queue that the callback should be called on */
@interface SPA_NS(CallbackHolder) : NSObject
- (id)initWithCallback:(SPTaskCallback)callback onQueue:(dispatch_queue_t)callbackQueue;
Expand Down

0 comments on commit 7214f9f

Please sign in to comment.