Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Completion handlers and cancellation handlers are dispatched immediat…

…ely if the future has already been completed/cancelled.

Also corrects the tests, which had some race conditions due to the nondeterminism of the order of completion/cancellation handler execution and the fact that adding something to the actions set was done asynchronously.

Closes #1.
  • Loading branch information...
commit 3d979248c93360617ef2ce015f64eb68605f6624 1 parent a2592fa
Rob Rix authored
Showing with 41 additions and 13 deletions.
  1. +8 −2 RXFutures/RXFuture.m
  2. +33 −11 RXFuturesTests/RXFutureTests.m
10 RXFutures/RXFuture.m
View
@@ -36,13 +36,19 @@ -(void)dispatchBlock:(void(^)())block {
-(void)onComplete:(void(^)())block {
dispatch_async(queue, ^{
- [completionHandlers addObject:block];
+ if(completed)
+ [self dispatchBlock:block];
+ else
+ [completionHandlers addObject:block];
});
}
-(void)onCancel:(void(^)())block {
dispatch_async(queue, ^{
- [cancellationHandlers addObject:block];
+ if(cancelled)
+ [self dispatchBlock:block];
+ else
+ [cancellationHandlers addObject:block];
});
}
44 RXFuturesTests/RXFutureTests.m
View
@@ -80,25 +80,36 @@ -(void)testCompletionPrecludesCancellation {
-(void)addAction:(NSString *)action {
- dispatch_async(queue, ^{
+ dispatch_sync(queue, ^{
[actions addObject:action];
});
}
-(void)testCallsCancellationHandlersOnCancellation {
- [future onCancel:^{ [self addAction:@"a"]; }];
- [future onCancel:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
- [future onCancel:done];
+ [future onCancel:^{
+ [self addAction:@"a"];
+ done();
+ }];
[future cancel];
});
- RXAssertEquals(actions, ([NSSet setWithObjects:@"a", @"b", nil]));
+ RXAssertEquals(actions, [NSSet setWithObject:@"a"]);
+}
+
+-(void)testCallsCancellationHandlersAddedAfterCancellation {
+ RXSynchronously(^(RXSynchronousCompletionBlock done) {
+ [future cancel];
+ [future onCancel:^{
+ [self addAction:@"a"];
+ done();
+ }];
+ });
+ RXAssertEquals(actions, [NSSet setWithObject:@"a"]);
}
-(void)testDoesNotCallCancellationHandlersOnCompletion {
[future onCancel:^{ [self addAction:@"a"]; }];
- [future onCancel:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
[future onComplete:done];
[future complete];
@@ -108,18 +119,29 @@ -(void)testDoesNotCallCancellationHandlersOnCompletion {
-(void)testCallsCompletionHandlersOnCompletion {
- [future onComplete:^{ [self addAction:@"a"]; }];
- [future onComplete:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
- [future onComplete:done];
+ [future onComplete:^{
+ [self addAction:@"a"];
+ done();
+ }];
[future complete];
});
- RXAssertEquals(actions, ([NSSet setWithObjects:@"a", @"b", nil]));
+ RXAssertEquals(actions, [NSSet setWithObject:@"a"]);
+}
+
+-(void)testCallsCompletionHandlersAddedAfterCompletion {
+ RXSynchronously(^(RXSynchronousCompletionBlock done) {
+ [future complete];
+ [future onComplete:^{
+ [self addAction:@"a"];
+ done();
+ }];
+ });
+ RXAssertEquals(actions, [NSSet setWithObject:@"a"]);
}
-(void)testDoesNotCallCompletionHandlersOnCancellation {
[future onComplete:^{ [self addAction:@"a"]; }];
- [future onComplete:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
[future onCancel:done];
[future cancel];
Please sign in to comment.
Something went wrong with that request. Please try again.