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

Delaying ref headers fetch until after startup #14620

Merged
merged 1 commit into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let promotionTimeoutId
let togglePromotionTimeoutId
let publisherInfoTimeoutId = false
let publisherInfoUpdateIntervalId
let promoRefFetchTimeoutId = false

// Database
let v2RulesetDB
Expand Down Expand Up @@ -133,6 +134,8 @@ let referralAPI = 'key'
if (clientOptions.environment === 'production') {
referralServer = 'https://laptop-updates.brave.com'
referralAPI = config.referralAPI || process.env.LEDGER_REFERRAL_API_KEY || ''
} else {
referralAPI = process.env.LEDGER_REFERRAL_API_KEY || referralAPI
}

const fileTypes = {
Expand Down Expand Up @@ -2366,22 +2369,6 @@ 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()
}

// Get referral headers every day
setInterval(() => fetchReferralHeaders, (24 * ledgerUtil.milliseconds.hour))

Expand Down Expand Up @@ -2453,6 +2440,39 @@ const initialize = (state, paymentsEnabled) => {
}
}

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

promoRefFetchTimeoutId = setTimeout(() => {
appActions.onPromoRefFetch()
}, random.randomInt({min: 50 * ledgerUtil.milliseconds.second, max: 70 * ledgerUtil.milliseconds.second}))
}

const onRunPromoRefFetch = (state) => {
if (updateState.getUpdateProp(state, 'referralDownloadId') == null) {
module.exports.firstRunPromoCode()
} else {
module.exports.fetchReferralHeaders()
}
return state
}

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 @@ -3405,7 +3425,11 @@ const getMethods = () => {
checkPublisherInfoUpdate,
updatePublishersInfo,
runPublishersUpdate,
checkBrowserActivityTime
checkBrowserActivityTime,
firstRunPromoCode,
fetchReferralHeaders,
schedulePromoRefFetch,
onRunPromoRefFetch
}

let privateMethods = {}
Expand Down
6 changes: 6 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,18 @@ const ledgerReducer = (state, action, immutableAction) => {
state = ledgerApi.checkBrowserActivityTime(state)
break
}
case appConstants.APP_ON_PROMO_REF_FETCH:
{
state = ledgerApi.onRunPromoRefFetch(state)
break
}
}
return state
}

process.on(messages.APP_INITIALIZED, () => {
ledgerApi.runPromotionCheck()
ledgerApi.schedulePromoRefFetch()
})

module.exports = ledgerReducer
6 changes: 6 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,12 @@ const appActions = {
dispatch({
actionType: appConstants.APP_ON_CHECK_BROWSER_ACTIVITY_TIME
})
},

onPromoRefFetch: function () {
dispatch({
actionType: appConstants.APP_ON_PROMO_REF_FETCH
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ const appConstants = {
APP_ON_PUBLISHERS_INFO_RECEIVED: _,
APP_ON_PUBLISHERS_INFO_WRITE: _,
APP_ON_PUBLISHERS_INFO_READ: _,
APP_ON_CHECK_BROWSER_ACTIVITY_TIME: _
APP_ON_CHECK_BROWSER_ACTIVITY_TIME: _,
APP_ON_PROMO_REF_FETCH: _
}

module.exports = mapValuesByKeys(appConstants)
69 changes: 69 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2715,6 +2715,75 @@ describe('ledger api unit tests', function () {
})
})

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

before(function () {
fakeClock = sinon.useFakeTimers()
onPromoRefFetchSpy = sinon.spy(appActions, 'onPromoRefFetch')
})

after(function () {
fakeClock.restore()
onPromoRefFetchSpy.restore()
})

afterEach(function () {
onPromoRefFetchSpy.reset()
})

it('dispatches onPromoRefFetch after 50 - 70 seconds', function () {
ledgerApi.schedulePromoRefFetch(defaultAppState)
fakeClock.tick(seventySeconds)
assert(onPromoRefFetchSpy.calledOnce)
})

it('does not dispatch onPromoRefFetch after 40 seconds', function () {
ledgerApi.schedulePromoRefFetch(defaultAppState)
fakeClock.tick(fortySeconds)
assert(onPromoRefFetchSpy.notCalled)
})
})

describe('onRunPromoRefFetch', function () {
let fakeRoundtrip, firstRunPromoCodeSpy, fetchReferralHeadersSpy

before(function () {
fakeRoundtrip = sinon.stub(ledgerApi, 'roundtrip')
firstRunPromoCodeSpy = sinon.spy(ledgerApi, 'firstRunPromoCode')
fetchReferralHeadersSpy = sinon.spy(ledgerApi, 'fetchReferralHeaders')
})

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

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

it('calls firstRunPromoCode when download id is null', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], null)
ledgerApi.onRunPromoRefFetch(state)
assert(firstRunPromoCodeSpy.calledOnce)
})

it('does not call firstRunPromoCode and delays fetchReferralHeaders when download id exists', function () {
const state = defaultAppState
.setIn(['updates', 'referralDownloadId'], 9999)
ledgerApi.onRunPromoRefFetch(state)
assert(firstRunPromoCodeSpy.notCalled)
assert(fetchReferralHeadersSpy.calledOnce)
})
})

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

Expand Down