From 873232d5606af86299a3ae278d9320bd29d85c57 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Tue, 16 Dec 2014 18:43:08 +0100 Subject: [PATCH 1/2] FIXED : a CPOperation marked as cancelled was never started (CPOperation -start) when managed in a queue. FIXED : After an operation started, either from a queue or explicitely, its finished property was never set to true. This was causing dependant operation to never be executed. After this commit, cancelled operations are started (but not executed) and correctly marked as finished. Tests: CPOperationQueueTest and CPOperationTest for an operation managed by a queue or run explicitely. --- Foundation/CPOperation.j | 7 ++-- Foundation/CPOperationQueue.j | 4 +- Tests/Foundation/CPOperationQueueTest.j | 50 +++++++++++++++++++++++++ Tests/Foundation/CPOperationTest.j | 17 +++++++++ 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/Foundation/CPOperation.j b/Foundation/CPOperation.j index 4fbdd5612..b0b8a6154 100644 --- a/Foundation/CPOperation.j +++ b/Foundation/CPOperation.j @@ -117,10 +117,11 @@ CPOperationQueuePriorityVeryHigh = 8; [self willChangeValueForKey:@"isExecuting"]; _executing = NO; [self didChangeValueForKey:@"isExecuting"]; - [self willChangeValueForKey:@"isFinished"]; - _finished = YES; - [self didChangeValueForKey:@"isFinished"]; } + + [self willChangeValueForKey:@"isFinished"]; + _finished = YES; + [self didChangeValueForKey:@"isFinished"]; } /*! diff --git a/Foundation/CPOperationQueue.j b/Foundation/CPOperationQueue.j index 1e018df15..de9322ce6 100644 --- a/Foundation/CPOperationQueue.j +++ b/Foundation/CPOperationQueue.j @@ -70,7 +70,7 @@ var cpOperationMainQueue = nil; for (; i < count; i++) { var op = [_operations objectAtIndex:i]; - if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting]) + if ([op isReady] && ![op isFinished] && ![op isExecuting]) { [op start]; } @@ -260,7 +260,7 @@ var cpOperationMainQueue = nil; for (; i < count; i++) { var op = [ops objectAtIndex:i]; - if ([op isReady] && ![op isCancelled] && ![op isFinished] && ![op isExecuting]) + if ([op isReady] && ![op isFinished] && ![op isExecuting]) { [op start]; } diff --git a/Tests/Foundation/CPOperationQueueTest.j b/Tests/Foundation/CPOperationQueueTest.j index 0cc5de666..23bea619e 100644 --- a/Tests/Foundation/CPOperationQueueTest.j +++ b/Tests/Foundation/CPOperationQueueTest.j @@ -16,6 +16,33 @@ globalResults = []; @end +@implementation TestOperation2 : CPOperation +{ + BOOL _started @accessors(getter=didStart); + BOOL _mained @accessors(getter=didMain); +} + +- (id)init +{ + self = [super init]; + _started = NO; + _mained = NO; + return self; +} + +- (void)main +{ + _mained = YES; +} + +- (void)start +{ + [super start]; + _started = YES; +} + +@end + @implementation TestObserver : CPObject { CPArray changedKeyPaths @accessors; @@ -167,4 +194,27 @@ globalResults = []; [self assert:@"name" equals:[[obs changedKeyPaths] objectAtIndex:4]]; } +- (void)testCancelledOperationDoesStart +{ + var op = [[TestOperation2 alloc] init], + queue = [[CPOperationQueue alloc] init]; + + [self assertFalse:[op isCancelled]]; + [self assertFalse:[op isFinished]]; + [self assertFalse:[op didMain]]; + [self assertFalse:[op didStart]]; + + [op cancel]; + + [self assertTrue:[op isCancelled]]; + [self assertFalse:[op isFinished]]; + + [queue addOperations:[op] waitUntilFinished:YES]; + + [self assertFalse:[op didMain]]; + [self assertTrue:[op didStart]]; + [self assertTrue:[op isCancelled]]; + [self assertTrue:[op isFinished]]; +} + @end \ No newline at end of file diff --git a/Tests/Foundation/CPOperationTest.j b/Tests/Foundation/CPOperationTest.j index 1c1beef56..ea424ef56 100644 --- a/Tests/Foundation/CPOperationTest.j +++ b/Tests/Foundation/CPOperationTest.j @@ -180,4 +180,21 @@ [self assert:@"isCancelled" equals:[[obs changedKeyPaths] objectAtIndex:9]]; } +- (void)testCancelledOperationIsFinished +{ + var results = @[], + funcOp = [CPFunctionOperation functionOperationWithFunction:function() {[results addObject:"funcOp"];}]; + + [funcOp cancel]; + [self assertTrue:[funcOp isCancelled]]; + [self assertFalse:[funcOp isFinished]]; + + [funcOp start]; + + [self assertTrue:[funcOp isCancelled]]; + [self assertTrue:[funcOp isFinished]]; + + [self assertTrue:([results count] == 0)]; +} + @end From e407eeea4069d92cb7b1d59d7a42b5b58a15835f Mon Sep 17 00:00:00 2001 From: cacaodev Date: Tue, 16 Dec 2014 21:43:19 +0100 Subject: [PATCH 2/2] FIXED : travis build --- Tests/Foundation/CPOperationQueueTest.j | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Foundation/CPOperationQueueTest.j b/Tests/Foundation/CPOperationQueueTest.j index 23bea619e..5005a1ae7 100644 --- a/Tests/Foundation/CPOperationQueueTest.j +++ b/Tests/Foundation/CPOperationQueueTest.j @@ -16,7 +16,7 @@ globalResults = []; @end -@implementation TestOperation2 : CPOperation +@implementation TestCancelOperation : CPOperation { BOOL _started @accessors(getter=didStart); BOOL _mained @accessors(getter=didMain); @@ -196,7 +196,7 @@ globalResults = []; - (void)testCancelledOperationDoesStart { - var op = [[TestOperation2 alloc] init], + var op = [[TestCancelOperation alloc] init], queue = [[CPOperationQueue alloc] init]; [self assertFalse:[op isCancelled]];