Fix delaying if remote library version changes during downloads

This commit is contained in:
Dan Stillman 2016-08-13 03:33:32 -04:00
parent 2bcd77f870
commit 7ea5bab206
2 changed files with 65 additions and 3 deletions

View file

@ -154,7 +154,7 @@ Zotero.Sync.Data.Engine.prototype.start = Zotero.Promise.coroutine(function* ()
if (!gen) {
var gen = Zotero.Utilities.Internal.delayGenerator(
Zotero.Sync.Data.delayIntervals, 60 * 1000
Zotero.Sync.Data.conflictDelayIntervals, 60 * 1000
);
}
// After the first upload version conflict (which is expected after remote changes),
@ -211,7 +211,7 @@ Zotero.Sync.Data.Engine.prototype._startDownload = Zotero.Promise.coroutine(func
var newLibraryVersion;
this.downloadDelayGenerator = Zotero.Utilities.Internal.delayGenerator(
Zotero.Sync.Data.delayIntervals, 60 * 60 * 1000
Zotero.Sync.Data.conflictDelayIntervals, 60 * 60 * 1000
);
loop:
@ -740,7 +740,7 @@ Zotero.Sync.Data.Engine.prototype._downloadDeletions = Zotero.Promise.coroutine(
*/
Zotero.Sync.Data.Engine.prototype._onLibraryVersionChange = Zotero.Promise.coroutine(function* (mode) {
Zotero.logError("Library version changed since last download -- restarting sync");
let keepGoing = yield this.downloadDelayGenerator.next();
let keepGoing = yield this.downloadDelayGenerator.next().value;
if (!keepGoing) {
throw new Error("Could not update " + this.library.name + " -- library in use");
}

View file

@ -1905,6 +1905,68 @@ describe("Zotero.Sync.Data.Engine", function () {
// Library version should not have advanced
assert.equal(library.libraryVersion, 5);
});
it("should restart if remote library version changes", function* () {
var library = Zotero.Libraries.userLibrary;
library.libraryVersion = 5;
yield library.saveTx();
({ engine, client, caller } = yield setup());
var lastLibraryVersion = 5;
var calls = 0;
var t;
server.respond(function (req) {
if (req.url.startsWith(baseURL + "users/1/settings")) {
calls++;
if (calls == 2) {
assert.isAbove(new Date() - t, 50);
}
t = new Date();
req.respond(
200,
{
"Last-Modified-Version": ++lastLibraryVersion
},
JSON.stringify({})
);
return;
}
else if (req.url.startsWith(baseURL + "users/1/searches")) {
if (calls == 1) {
t = new Date();
req.respond(
200,
{
// On the first pass, return a later library version to simulate data
// being updated by a concurrent upload
"Last-Modified-Version": lastLibraryVersion + 1
},
JSON.stringify([])
);
return;
}
}
else if (req.url.startsWith(baseURL + "users/1/items")) {
// Since /searches is called before /items and it should cause a reset,
// /items shouldn't be called until the second pass
if (calls < 1) {
throw new Error("/users/1/items called in first pass");
}
}
t = new Date();
req.respond(
200,
{
"Last-Modified-Version": lastLibraryVersion
},
JSON.stringify([])
);
});
Zotero.Sync.Data.conflictDelayIntervals = [50, 70000];
yield engine._startDownload();
});
});