From 7ea5bab2065d65d761ed025ffdca1fb55bd9d25f Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sat, 13 Aug 2016 03:33:32 -0400 Subject: [PATCH] Fix delaying if remote library version changes during downloads --- .../content/zotero/xpcom/sync/syncEngine.js | 6 +- test/tests/syncEngineTest.js | 62 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/sync/syncEngine.js b/chrome/content/zotero/xpcom/sync/syncEngine.js index 3e16ff1637..a2ff49c494 100644 --- a/chrome/content/zotero/xpcom/sync/syncEngine.js +++ b/chrome/content/zotero/xpcom/sync/syncEngine.js @@ -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"); } diff --git a/test/tests/syncEngineTest.js b/test/tests/syncEngineTest.js index 790a2a17e0..d8aad6522a 100644 --- a/test/tests/syncEngineTest.js +++ b/test/tests/syncEngineTest.js @@ -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(); + }); });