Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fixes Issue #14615, delaying promocode and referral fetches on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Jul 12, 2018
1 parent c42cf87 commit 0c8e79f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
51 changes: 35 additions & 16 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let runTimeoutId
let promotionTimeoutId
let togglePromotionTimeoutId
let verifiedTimeoutId = false
let promoRefFetchTimeoutId = false

// Database
let v2RulesetDB
Expand Down Expand Up @@ -2342,21 +2343,8 @@ const initialize = (state, paymentsEnabled) => {
}
}

if (updateState.getUpdateProp(state, 'referralDownloadId') == null) {
promoCodeFirstRunStorage
.readFirstRunPromoCode()
.then((code) => {
onReferralCodeRead(code)
})
.catch(error => {
if (clientOptions.verboseP) {
console.error('read error: ' + error.toString())
}
fetchReferralHeaders()
})
} else {
fetchReferralHeaders()
}
// Schedule promo run and referral header fetch
schedulePromoRefFetch(state)

// Get referral headers every day
setInterval(() => fetchReferralHeaders, (24 * ledgerUtil.milliseconds.hour))
Expand Down Expand Up @@ -2419,6 +2407,34 @@ const initialize = (state, paymentsEnabled) => {
}
}

const schedulePromoRefFetch = (state) => {
if (promoRefFetchTimeoutId) {
clearTimeout(promoRefFetchTimeoutId)
}

promoRefFetchTimeoutId = setTimeout(() => {
if (updateState.getUpdateProp(state, 'referralDownloadId') == null) {
module.exports.firstRunPromoCode()
} else {
module.exports.fetchReferralHeaders()
}
}, random.randomInt({min: 50 * ledgerUtil.milliseconds.second, max: 70 * ledgerUtil.milliseconds.second}))
}

const firstRunPromoCode = () => {
promoCodeFirstRunStorage
.readFirstRunPromoCode()
.then((code) => {
onReferralCodeRead(code)
})
.catch(error => {
if (clientOptions.verboseP) {
console.error('read error: ' + error.toString())
}
fetchReferralHeaders()
})
}

const getContributionAmount = (state) => {
return ledgerState.getContributionAmount(state)
}
Expand Down Expand Up @@ -3355,7 +3371,10 @@ const getMethods = () => {
cacheRuleSet,
disablePayments,
runPromotionCheck,
onRunPromotionCheck
onRunPromotionCheck,
firstRunPromoCode,
fetchReferralHeaders,
schedulePromoRefFetch
}

let privateMethods = {}
Expand Down
57 changes: 57 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,63 @@ describe('ledger api unit tests', function () {
})
})

describe('schedulePromoRefFetch', function () {
const fortySeconds = 40000
const seventySeconds = 70000
let fakeClock, firstRunPromoCodeSpy, fetchReferralHeadersSpy

before(function () {
fakeClock = sinon.useFakeTimers()
firstRunPromoCodeSpy = sinon.stub(ledgerApi, 'firstRunPromoCode')
fetchReferralHeadersSpy = sinon.stub(ledgerApi, 'fetchReferralHeaders')
})

after(function () {
fakeClock.restore()
firstRunPromoCodeSpy.restore()
fetchReferralHeadersSpy.restore()
})

afterEach(function () {
firstRunPromoCodeSpy.reset()
fetchReferralHeadersSpy.reset()
})

it('delays call for firstRunPromoCode between 50 - 70 seonds when download id is null', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], null)
ledgerApi.schedulePromoRefFetch(state)
fakeClock.tick(seventySeconds)
assert(firstRunPromoCodeSpy.calledOnce)
})

it('does not call firstRunPromoCode after 40 seconds when download id is null', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], null)
ledgerApi.schedulePromoRefFetch(state)
fakeClock.tick(fortySeconds)
assert(firstRunPromoCodeSpy.notCalled)
})

it('does not call firstRunPromoCode and delays fetchReferralHeaders by 50 - 70 seconds when download id exists', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], 9999)
ledgerApi.schedulePromoRefFetch(state)
fakeClock.tick(seventySeconds)
assert(firstRunPromoCodeSpy.notCalled)
assert(fetchReferralHeadersSpy.calledOnce)
})

it('does not call firstRunPromoCode or fetchReferralHeaders after 40 seconds when download id exists', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], 9999)
ledgerApi.schedulePromoRefFetch(state)
fakeClock.tick(fortySeconds)
assert(firstRunPromoCodeSpy.notCalled)
assert(fetchReferralHeadersSpy.notCalled)
})
})

describe('checkReferralActivity', function () {
let checkForUpdateSpy, roundtripSpy, fakeClock

Expand Down

0 comments on commit 0c8e79f

Please sign in to comment.