Skip to content

Commit

Permalink
core/remote/watcher: Do not watch if stopped
Browse files Browse the repository at this point in the history
  The remote watcher can be stopped while it was waiting for the PouchDB
  lock to be free.
  In this case, we've already passed all checks on the running status of
  the watcher and the `watch()` call will be executed once the lock is
  free which we don't want (this can trigger unwanted requests to the
  remote Cozy when the watcher is stopped by Sync because of an
  unreachable Cozy for example).

  Adding another check right after the lock gate prevents this.
  • Loading branch information
taratatach committed Apr 22, 2022
1 parent 7024c93 commit 6a567f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/remote/watcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ class RemoteWatcher {
async watch() /*: Promise<?RemoteError> */ {
const release = await this.pouch.lock(this)
try {
if (!this.running) {
log.debug('Watcher stopped: skipping remote watch')
return
}

this.events.emit('buffering-start')

const seq = await this.pouch.getRemoteSeq()
Expand Down
2 changes: 2 additions & 0 deletions test/support/helpers/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class RemoteTestHelpers {
}

async pullChanges() {
this.side.watcher.running = true
await this.side.watcher.watch()
this.side.watcher.running = false
}

async createDirectory(
Expand Down
30 changes: 30 additions & 0 deletions test/unit/remote/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ describe('RemoteWatcher', function () {
sinon.spy(this.events, 'emit')

this.pouch.getRemoteSeq.resolves(lastLocalSeq)
this.watcher.running = true
this.watcher.processRemoteChanges.resolves([])
this.remoteCozy.changes.resolves(changes)
})
Expand All @@ -398,6 +399,7 @@ describe('RemoteWatcher', function () {
this.events.emit.restore()
this.remoteCozy.changes.restore()
this.watcher.processRemoteChanges.restore()
this.watcher.running = false
this.pouch.setRemoteSeq.restore()
this.pouch.getRemoteSeq.restore()
})
Expand Down Expand Up @@ -543,6 +545,34 @@ describe('RemoteWatcher', function () {
})
})
})

context('when watcher is not running', () => {
beforeEach(function () {
this.watcher.running = false
})

afterEach(function () {
this.watcher.running = true
})

it('returns without fetching changes', async function () {
await this.watcher.watch()

should(this.remoteCozy.changes).not.have.been.called()
})

it('still tries to get hold of the PouchDB lock', async function () {
sinon.spy(this.pouch, 'lock')

try {
await this.watcher.watch()

should(this.pouch.lock).have.been.calledOnce()
} finally {
this.pouch.lock.restore()
}
})
})
})

const validMetadata = (
Expand Down

0 comments on commit 6a567f3

Please sign in to comment.