Skip to content

Commit

Permalink
Merge pull request #642 from couchbase/feature/issue_639_replicator_stop
Browse files Browse the repository at this point in the history
Fix stop idle replicator not getting change notification
  • Loading branch information
snej committed Apr 3, 2015
2 parents 3f8655c + 5438f47 commit 38dd697
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
5 changes: 0 additions & 5 deletions Source/API/CBLReplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,8 @@ - (void) stop {
[self tellReplicatorAndWait:^id(CBL_Replicator * bgReplicator) {
// This runs on the server thread:
[bgReplicator stop];
[[NSNotificationCenter defaultCenter] removeObserver: self name: nil
object: _bg_replicator];
return @(YES);
}];

_started = NO;
[_database forgetReplication: self];
}


Expand Down
4 changes: 3 additions & 1 deletion Source/CBL_Replicator.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,10 @@ - (void) stop {
[self stopRemoteRequests];
[NSObject cancelPreviousPerformRequestsWithTarget: self
selector: @selector(retryIfReady) object: nil];
if (_running && _asyncTaskCount == 0)
if (_running && _asyncTaskCount == 0) {
[self stopped];
[self postProgressChanged];
}
}


Expand Down
86 changes: 86 additions & 0 deletions Unit-Tests/Replication_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,90 @@ - (void) test11_ReplicationWithReplacedDatabase {
Assert([importDb deleteDatabase:&error], @"Couldn't delete db: %@", error);
}

- (void) test12_StopIdlePush {
NSURL* remoteDbURL = [self remoteTestDBURL: kPushThenPullDBName];
if (!remoteDbURL)
return;
[self eraseRemoteDB: remoteDbURL];

// Create a continuous push replicator:
CBLReplication* pusher = [db createPushReplication: remoteDbURL];
pusher.continuous = YES;

// Run the replicator:
[self runReplication:pusher expectedChangesCount: 0];

// Make sure the replication is now idle:
AssertEq(pusher.status, kCBLReplicationIdle);

// Setup replication change notification observver:
__block BOOL stopped = NO;
id observer =
[[NSNotificationCenter defaultCenter] addObserverForName: kCBLReplicationChangeNotification
object: pusher
queue: nil
usingBlock: ^(NSNotification *note) {
if (pusher.status == kCBLReplicationStopped)
stopped = YES;
}];

// Stop the replicator:
[pusher stop];

// Wait to get a notification after the replication is stopped:
NSDate* timeout = [NSDate dateWithTimeIntervalSinceNow: 2.0];
while (!stopped && timeout.timeIntervalSinceNow > 0.0) {
if (![[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]])
break;
}
[[NSNotificationCenter defaultCenter] removeObserver: observer];

// Check result:
Assert(stopped);
}

- (void) test13_StopIdlePullReplication {
NSURL* remoteDbURL = [self remoteTestDBURL: kPushThenPullDBName];
if (!remoteDbURL)
return;
[self eraseRemoteDB: remoteDbURL];

// Create a continuous push replicator:
CBLReplication* puller = [db createPullReplication: remoteDbURL];
puller.continuous = YES;

// Run the replicator:
[self runReplication:puller expectedChangesCount: 0];

// Make sure the replication is now idle:
AssertEq(puller.status, kCBLReplicationIdle);

// Setup replication change notification observver:
__block BOOL stopped = NO;
id observer =
[[NSNotificationCenter defaultCenter] addObserverForName: kCBLReplicationChangeNotification
object: puller
queue: nil
usingBlock: ^(NSNotification *note) {
if (puller.status == kCBLReplicationStopped)
stopped = YES;
}];

// Stop the replicator:
[puller stop];

// Wait to get a notification after the replication is stopped:
NSDate* timeout = [NSDate dateWithTimeIntervalSinceNow: 2.0];
while (!stopped && timeout.timeIntervalSinceNow > 0.0) {
if (![[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]])
break;
}
[[NSNotificationCenter defaultCenter] removeObserver: observer];

// Check result:
Assert(stopped);
}

@end

0 comments on commit 38dd697

Please sign in to comment.