Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
fix: trigger poll if last sync was in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
Flydiverny committed Nov 4, 2019
1 parent 8839f5d commit 9608ddb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/poller.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,14 @@ class Poller {
return this._setNextPoll(0)
}

const now = Date.now()
const lastPollTime = Date.parse(lastSync) || 0
const elapsedTime = Date.now() - lastPollTime

if (lastPollTime > now) {
return this._setNextPoll(0)
}

const elapsedTime = now - lastPollTime
const nextPollIn = Math.max(this._intervalMilliseconds - elapsedTime, 0)

return this._setNextPoll(nextPollIn)
Expand Down
8 changes: 3 additions & 5 deletions lib/poller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,14 @@ describe('Poller', () => {
expect(poller._setNextPoll.calledWith(poller._intervalMilliseconds - elapsedTime)).to.equal(true)
})

it('with last sync in the future - queues poll', async () => {
it('with last sync in the future - triggers poll', async () => {
fakeDate.setFullYear(fakeDate.getFullYear() + 1)

const elapsedTime = 0
clock.tick(elapsedTime)
fakeStatus.body.status.lastSync = fakeDate.toISOString()

await poller._scheduleNextPoll()

expect(externalSecretsApiMock.status.get.calledWith()).to.equal(true)
expect(poller._setNextPoll.calledWith(poller._intervalMilliseconds)).to.equal(true)
expect(poller._setNextPoll.calledWith(0)).to.equal(true)
})

it('with old last sync - triggers poll', async () => {
Expand Down

0 comments on commit 9608ddb

Please sign in to comment.