From e75d407983e88678a5747a8ef8e0355a0331278d Mon Sep 17 00:00:00 2001 From: Pasin Suriyentrakorn Date: Thu, 20 Aug 2015 10:27:10 -0700 Subject: [PATCH] Fixed CBLIS DefaultConflictHandler test failures As the conflict handler will be call on the main thread but in a synchronously manner, there is a race condition when checking for the expectation to fullfil. Add counter property to the test case so that we can use KVO expection to check if the conflict handler get called correctly or not. #874 # Conflicts: # Unit-Tests/IncrementalStore_Tests.m --- Unit-Tests/IncrementalStore_Tests.m | 31 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/Unit-Tests/IncrementalStore_Tests.m b/Unit-Tests/IncrementalStore_Tests.m index 01a2ea191..74ead4805 100644 --- a/Unit-Tests/IncrementalStore_Tests.m +++ b/Unit-Tests/IncrementalStore_Tests.m @@ -17,6 +17,7 @@ @interface IncrementalStore_Tests : CBLTestCaseWithDB +@property NSUInteger counter; // General purpose counter that can be used with XCTest Async KVO expectation check @end @@ -144,6 +145,8 @@ @implementation IncrementalStore_Tests CBLIncrementalStore *store; } +@synthesize counter=_counter; + - (void) setUp { [super setUp]; @@ -153,6 +156,9 @@ - (void) setUp { [self reCreateCoreDataContext]; AssertEq(store.database, db); + + // Reset counter: + self.counter = 0; } - (void) tearDown { @@ -1812,26 +1818,23 @@ - (void)test_ConflictHandler { } - (void)test_DefaultConflictHandler { - XCTestExpectation *expectation = [self expectationWithDescription:@"CBLIS Conflict Handler"]; - + [self keyValueObservingExpectationForObject: self keyPath: @"counter" expectedValue: @(1)]; CBLISConflictHandler defaultHandler = [store.conflictHandler copy]; + __weak IncrementalStore_Tests* weakSelf = self; store.conflictHandler = ^(NSArray* conflictingRevisions) { defaultHandler(conflictingRevisions); - [expectation fulfill]; + weakSelf.counter++; }; - NSError *error; - Entry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"Entry" - inManagedObjectContext:context]; + NSError* error; + Entry* entry = [NSEntityDescription insertNewObjectForEntityForName: @"Entry" + inManagedObjectContext: context]; entry.text = @"test"; - - BOOL success = [context save:&error]; + BOOL success = [context save: &error]; Assert(success, @"Could not save context: %@", error); - CBLDocument *doc = [store.database documentWithID: [entry.objectID couchbaseLiteIDRepresentation]]; - AssertEqual(entry.text, [doc propertyForKey:@"text"]); - + AssertEqual(entry.text, [doc propertyForKey: @"text"]); CBLSavedRevision* rev1 = doc.currentRevision; // Create rev2a: @@ -1842,14 +1845,14 @@ - (void)test_DefaultConflictHandler { // Create rev2b: properties = rev1.properties.mutableCopy; - NSString *date = [CBLJSON JSONObjectWithDate:[NSDate date]]; + NSString* date = [CBLJSON JSONObjectWithDate: [NSDate date]]; properties[@"created_at"] = date; CBLUnsavedRevision* newRev = [rev1 createRevision]; newRev.properties = properties; CBLSavedRevision* rev2b = [newRev saveAllowingConflict: &error]; Assert(rev2b, @"Failed to create a conflict revision: %@", error); - - [self waitForExpectationsWithTimeout:5.0 handler:^(NSError *error) { + + [self waitForExpectationsWithTimeout: 5.0 handler: ^(NSError *error) { Assert(error == nil, "Timeout error: %@", error); }];