Skip to content

Commit

Permalink
Fixed CBLIS DefaultConflictHandler test failures
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pasin committed Sep 2, 2015
1 parent 9593d6e commit e75d407
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions Unit-Tests/IncrementalStore_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -144,6 +145,8 @@ @implementation IncrementalStore_Tests
CBLIncrementalStore *store;
}

@synthesize counter=_counter;

- (void) setUp {
[super setUp];

Expand All @@ -153,6 +156,9 @@ - (void) setUp {
[self reCreateCoreDataContext];

AssertEq(store.database, db);

// Reset counter:
self.counter = 0;
}

- (void) tearDown {
Expand Down Expand Up @@ -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:
Expand All @@ -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);
}];

Expand Down

0 comments on commit e75d407

Please sign in to comment.