Bug 1600779 - Caching in ASRouterTargeting for attachedFxAOAuthClients r=k88hudson

Differential Revision: https://phabricator.services.mozilla.com/D56251

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Punam Dahiya 2019-12-11 16:25:19 +00:00
Родитель 2218f47b13
Коммит 3771596656
2 изменённых файлов: 79 добавлений и 10 удалений

Просмотреть файл

@ -108,6 +108,7 @@ const MOZ_JEXL_FILEPATH = "mozjexl";
const { activityStreamProvider: asProvider } = NewTabUtils;
const FXA_ATTACHED_CLIENTS_UPDATE_INTERVAL = 2 * 60 * 60 * 1000; // Two hours
const FRECENT_SITES_UPDATE_INTERVAL = 6 * 60 * 60 * 1000; // Six hours
const FRECENT_SITES_IGNORE_BLOCKED = false;
const FRECENT_SITES_NUM_ITEMS = 25;
@ -146,6 +147,32 @@ function CachedTargetingGetter(
};
}
function CacheListAttachedOAuthClients() {
return {
_lastUpdated: 0,
_value: null,
expire() {
this._lastUpdated = 0;
this._value = null;
},
get() {
const now = Date.now();
if (now - this._lastUpdated >= FXA_ATTACHED_CLIENTS_UPDATE_INTERVAL) {
this._value = new Promise(resolve => {
fxAccounts
.listAttachedOAuthClients()
.then(clients => {
resolve(clients);
})
.catch(() => resolve([]));
});
this._lastUpdated = now;
}
return this._value;
},
};
}
function CheckBrowserNeedsUpdate(
updateInterval = FRECENT_SITES_UPDATE_INTERVAL
) {
@ -210,6 +237,7 @@ const QueryCache = {
TotalBookmarksCount: new CachedTargetingGetter("getTotalBookmarksCount"),
CheckBrowserNeedsUpdate: new CheckBrowserNeedsUpdate(),
RecentBookmarks: new CachedTargetingGetter("getRecentBookmarks"),
ListAttachedOAuthClients: new CacheListAttachedOAuthClients(),
},
};
@ -518,17 +546,8 @@ const TargetingGetters = {
);
},
get attachedFxAOAuthClients() {
// Explicitly catch error objects e.g. NO_ACCOUNT triggered when
// setting FXA_USERNAME_PREF from tests
return this.usesFirefoxSync
? new Promise(resolve => {
fxAccounts
.listAttachedOAuthClients()
.then(clients => {
resolve(clients);
})
.catch(() => resolve([]));
})
? QueryCache.queries.ListAttachedOAuthClients.get()
: [];
},
get platformName() {

Просмотреть файл

@ -2,6 +2,7 @@ import {
ASRouterTargeting,
CachedTargetingGetter,
getSortedMessages,
QueryCache,
} from "lib/ASRouterTargeting.jsm";
import { OnboardingMessageProvider } from "lib/OnboardingMessageProvider.jsm";
import { ASRouterPreferences } from "lib/ASRouterPreferences.jsm";
@ -221,6 +222,55 @@ describe("#CachedTargetingGetter", () => {
});
});
});
describe("#CacheListAttachedOAuthClients", () => {
const twoHours = 2 * 60 * 60 * 1000;
let sandbox;
let clock;
let fakeFxAccount;
let authClientsCache;
let globals;
beforeEach(() => {
globals = new GlobalOverrider();
sandbox = sinon.createSandbox();
clock = sinon.useFakeTimers();
fakeFxAccount = {
listAttachedOAuthClients: () => {},
};
globals.set("fxAccounts", fakeFxAccount);
authClientsCache = QueryCache.queries.ListAttachedOAuthClients;
sandbox
.stub(fxAccounts, "listAttachedOAuthClients")
.returns(Promise.resolve({}));
});
afterEach(() => {
authClientsCache.expire();
sandbox.restore();
clock.restore();
});
it("should only make additional request every 2 hours", async () => {
clock.tick(twoHours);
await authClientsCache.get();
assert.calledOnce(fxAccounts.listAttachedOAuthClients);
clock.tick(twoHours);
await authClientsCache.get();
assert.calledTwice(fxAccounts.listAttachedOAuthClients);
});
it("should not make additional request before 2 hours", async () => {
clock.tick(twoHours);
await authClientsCache.get();
assert.calledOnce(fxAccounts.listAttachedOAuthClients);
await authClientsCache.get();
assert.calledOnce(fxAccounts.listAttachedOAuthClients);
});
});
describe("ASRouterTargeting", () => {
let evalStub;
let sandbox;