Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'release/1.0'

  • Loading branch information...
commit 01facd26222da6e8ef9fb711210aa98ffd6f2926 2 parents 1ac6a73 + 090a129
@robrix authored
Showing with 92 additions and 27 deletions.
  1. +15 −4 RXFutures/RXFuture.m
  2. +77 −23 RXFuturesTests/RXFutureTests.m
View
19 RXFutures/RXFuture.m
@@ -29,15 +29,26 @@ -(void)dealloc {
}
+-(void)dispatchBlock:(void(^)())block {
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 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];
});
}
@@ -47,7 +58,7 @@ -(void)cancel {
if(!cancelled && !completed) {
self.cancelled = YES;
for(void (^block)() in cancellationHandlers) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
+ [self dispatchBlock:block];
}
}
});
@@ -58,7 +69,7 @@ -(void)complete {
if(!cancelled && !completed) {
self.completed = YES;
for(void (^block)() in completionHandlers) {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
+ [self dispatchBlock:block];
}
}
});
View
100 RXFuturesTests/RXFutureTests.m
@@ -9,7 +9,7 @@
@interface RXFutureTests : SenTestCase
@property (nonatomic, strong) RXFuture *future;
-@property (nonatomic, strong) NSMutableSet *actions;
+@property (nonatomic, assign) NSUInteger result;
@end
@@ -18,19 +18,19 @@ @implementation RXFutureTests {
dispatch_queue_t queue;
}
-@synthesize future, actions;
+@synthesize future, result;
-(void)setUp {
self.future = [RXFuture new];
- self.actions = [NSMutableSet new];
+ result = 0;
queue = dispatch_queue_create("RXFutureTests", 0);
}
-(void)tearDown {
self.future = nil;
- self.actions = nil;
dispatch_release(queue);
+ queue = NULL;
}
@@ -61,7 +61,6 @@ -(void)testCancellationPrecludesCompletion {
[future cancel];
[future complete];
});
-
RXAssert(future.isCancelled);
RXAssertFalse(future.isCompleted);
}
@@ -73,58 +72,113 @@ -(void)testCompletionPrecludesCancellation {
[future complete];
[future cancel];
});
-
RXAssertFalse(future.isCancelled);
RXAssert(future.isCompleted);
}
--(void)addAction:(NSString *)action {
- dispatch_async(queue, ^{
- [actions addObject:action];
+-(void)setResult:(NSUInteger)_result {
+ dispatch_sync(queue, ^{
+ result = _result;
});
}
-(void)testCallsCancellationHandlersOnCancellation {
- [future onCancel:^{ [self addAction:@"a"]; }];
- [future onCancel:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
- [future onCancel:done];
+ [future onCancel:^{
+ self.result = 1;
+ done();
+ }];
[future cancel];
});
- RXAssertEquals(actions, ([NSSet setWithObjects:@"a", @"b", nil]));
+ RXAssertEquals(result, 1);
+}
+
+-(void)testCallsCancellationHandlersAddedAfterCancellation {
+ RXSynchronously(^(RXSynchronousCompletionBlock done) {
+ [future cancel];
+ [future onCancel:^{
+ self.result = 2;
+ done();
+ }];
+ });
+ RXAssertEquals(result, 2);
}
-(void)testDoesNotCallCancellationHandlersOnCompletion {
- [future onCancel:^{ [self addAction:@"a"]; }];
- [future onCancel:^{ [self addAction:@"b"]; }];
+ [future onCancel:^{ self.result = 3; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
[future onComplete:done];
[future complete];
});
- RXAssertEquals(actions, [NSSet set]);
+ RXAssertEquals(result, 0);
+}
+
+-(void)testCallsMultipleCancellationHandlersOnCancellation {
+ RXSynchronously(^(RXSynchronousCompletionBlock doneA) {
+ RXSynchronously(^(RXSynchronousCompletionBlock doneB) {
+ [future onCancel:^{
+ self.result += 2;
+ doneA();
+ }];
+ [future onCancel:^{
+ self.result += 2;
+ doneB();
+ }];
+ [future cancel];
+ });
+ });
+ RXAssertEquals(result, 4);
}
-(void)testCallsCompletionHandlersOnCompletion {
- [future onComplete:^{ [self addAction:@"a"]; }];
- [future onComplete:^{ [self addAction:@"b"]; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
- [future onComplete:done];
+ [future onComplete:^{
+ self.result = 5;
+ done();
+ }];
[future complete];
});
- RXAssertEquals(actions, ([NSSet setWithObjects:@"a", @"b", nil]));
+ RXAssertEquals(result, 5);
+}
+
+-(void)testCallsCompletionHandlersAddedAfterCompletion {
+ RXSynchronously(^(RXSynchronousCompletionBlock done) {
+ [future complete];
+ [future onComplete:^{
+ self.result = 6;
+ done();
+ }];
+ });
+ RXAssertEquals(result, 6);
}
-(void)testDoesNotCallCompletionHandlersOnCancellation {
- [future onComplete:^{ [self addAction:@"a"]; }];
- [future onComplete:^{ [self addAction:@"b"]; }];
+ [future onComplete:^{ self.result = 7; }];
RXSynchronously(^(RXSynchronousCompletionBlock done) {
[future onCancel:done];
[future cancel];
});
- RXAssertEquals(actions, [NSSet set]);
+ RXAssertEquals(result, 0);
+}
+
+-(void)testCallsMultipleCompletionHandlersOnCompletion {
+ RXSynchronously(^(RXSynchronousCompletionBlock doneA) {
+ RXSynchronously(^(RXSynchronousCompletionBlock doneB) {
+ [future onComplete:^{
+ self.result += 4;
+ doneA();
+ }];
+ [future onComplete:^{
+ self.result += 4;
+ doneB();
+ }];
+ [future complete];
+ });
+ });
+ RXAssertEquals(result, 8);
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.