Skip to content

Commit

Permalink
(pouchdb#5145) - Update checkpoint even when no changes
Browse files Browse the repository at this point in the history
* (pouchdb#5145) - Update checkpoint even when no changes
* (pouchdb#5145) - Fix eslint
* (pouchdb#5146) - Test requested parameter, couchdb 2 compatibility
* (pouchdb#5145) - Tidy up test to be more promisey
  • Loading branch information
garethbowen authored and daleharvey committed May 13, 2016
1 parent 7894c9e commit f1989ce
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/replicate/replicate.js
Expand Up @@ -121,11 +121,7 @@ function replicate(src, target, opts, returnValue, result) {
}
currentBatch = undefined;
getChanges();
}).catch(function (err) {
writingCheckpoint = false;
abortReplication('writeCheckpoint completed with error', err);
throw err;
});
}).catch(onCheckpointError);
}

function getDiffs() {
Expand Down Expand Up @@ -294,15 +290,33 @@ function replicate(src, target, opts, returnValue, result) {
if (changes.results.length > 0) {
changesOpts.since = changes.last_seq;
getChanges();
processPendingBatch(true);
} else {
if (continuous) {
changesOpts.live = true;
getChanges();

var complete = function () {
if (continuous) {
changesOpts.live = true;
getChanges();
} else {
changesCompleted = true;
}
processPendingBatch(true);
};

// update the checkpoint so we start from the right seq next time
if (!currentBatch && changes.last_seq > last_seq) {
writingCheckpoint = true;
checkpointer.writeCheckpoint(changes.last_seq,
session).then(function () {
writingCheckpoint = false;
result.last_seq = last_seq = changes.last_seq;
complete();
})
.catch(onCheckpointError);
} else {
changesCompleted = true;
complete();
}
}
processPendingBatch(true);
}


Expand Down
49 changes: 49 additions & 0 deletions tests/integration/test.replication.js
Expand Up @@ -1548,6 +1548,55 @@ adapters.forEach(function (adapters) {
});
});

it('Empty replication updates checkpoint (#5145)', function () {
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
var changes = remote.changes;
remote.changes = function (params) {
changesSince.push(params.since);
return changes.apply(this, arguments);
};
var changesSince = [];
var replicationOpts = {
filter: function () {
return false;
}
};
return remote.bulkDocs({ docs: docs }).then(function () {
return db.replicate.from(remote, replicationOpts);
}).then(function (result) {
result.ok.should.equal(true);
result.docs_written.should.equal(0);
result.docs_read.should.equal(0);
changesSince.length.should.equal(2);
// the returned last_seq should match the 'since'
// requested from remote
result.last_seq.should.equal(changesSince[1]);
// the 'since' parameter should be different on the
// next request
changesSince[0].should.not.equal(changesSince[1]);
// kick off a second replication
return db.replicate.from(remote, replicationOpts);
}).then(function (result) {
result.ok.should.equal(true);
result.docs_written.should.equal(0);
result.docs_read.should.equal(0);
changesSince.length.should.equal(3);
// the returned last_seq should match the 'since'
// requested from remote
result.last_seq.should.equal(changesSince[2]);
// nothing has changed on the remote so 'since'
// should be the same
changesSince[1].should.equal(changesSince[2]);
}).then(function () {
// Restore remote.changes to original
remote.changes = changes;
}).catch(function (err) {
remote.changes = changes;
throw err;
});
});

it('Replication with deleted doc', function (done) {
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
Expand Down

0 comments on commit f1989ce

Please sign in to comment.