From 37715966565a2d3b06caa9e8b68e0a4dac429381 Mon Sep 17 00:00:00 2001 From: Punam Dahiya Date: Wed, 11 Dec 2019 16:25:19 +0000 Subject: [PATCH] Bug 1600779 - Caching in ASRouterTargeting for attachedFxAOAuthClients r=k88hudson Differential Revision: https://phabricator.services.mozilla.com/D56251 --HG-- extra : moz-landing-system : lando --- .../newtab/lib/ASRouterTargeting.jsm | 39 +++++++++++---- .../unit/asrouter/ASRouterTargeting.test.js | 50 +++++++++++++++++++ 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/browser/components/newtab/lib/ASRouterTargeting.jsm b/browser/components/newtab/lib/ASRouterTargeting.jsm index 7c30ce634421..f92978141f3a 100644 --- a/browser/components/newtab/lib/ASRouterTargeting.jsm +++ b/browser/components/newtab/lib/ASRouterTargeting.jsm @@ -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() { diff --git a/browser/components/newtab/test/unit/asrouter/ASRouterTargeting.test.js b/browser/components/newtab/test/unit/asrouter/ASRouterTargeting.test.js index 2e88c3793fb5..c5cbde806609 100644 --- a/browser/components/newtab/test/unit/asrouter/ASRouterTargeting.test.js +++ b/browser/components/newtab/test/unit/asrouter/ASRouterTargeting.test.js @@ -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;