Skip to content
This repository has been archived by the owner on Feb 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1481 from sarracini/recommendation_cleanup
Browse files Browse the repository at this point in the history
fix(tests): Fixes #1197 Intermittent RecommendationProvider failure
  • Loading branch information
sarracini committed Oct 7, 2016
2 parents da37c98 + eb6597c commit f3448b9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
28 changes: 19 additions & 9 deletions addon/ActivityStreams.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const DEFAULT_OPTIONS = {
recommendationTTL: 3600000, // every hour, get a new recommendation
shareProvider: null,
pageScraper: null,
searchProvider: null
searchProvider: null,
recommendationProvider: null
};

const PLACES_CHANGES_EVENTS = [
Expand Down Expand Up @@ -129,14 +130,7 @@ function ActivityStreams(metadataStore, options = {}) {

this._asyncBuildPlacesCache();

// Only create RecommendationProvider if they are in the experiment
if (this._experimentProvider.data.recommendedHighlight) {
this._recommendationProvider = new RecommendationProvider(this._previewProvider, this._tabTracker);
if (simplePrefs.prefs.recommendations) {
this._recommendationProvider.asyncSetRecommendedContent();
}
this._refreshRecommendations(this.options.recommendationTTL);
}
this._initializeRecommendationProvider();

if (this.options.shareProvider) {
this._shareProvider = this.options.shareProvider;
Expand Down Expand Up @@ -300,6 +294,22 @@ ActivityStreams.prototype = {
}
},

_initializeRecommendationProvider() {
// Only create RecommendationProvider if they are in the experiment
if (this._experimentProvider.data.recommendedHighlight) {
if (!this.options.recommendationProvider) {
this._recommendationProvider = new RecommendationProvider(this._previewProvider, this._tabTracker);
} else {
this._recommendationProvider = this.options.recommendationProvider;
}
this._recommendationProvider.init();
if (simplePrefs.prefs.recommendations) {
this._recommendationProvider.asyncSetRecommendedContent();
}
this._refreshRecommendations(this.options.recommendationTTL);
}
},

/**
* Instantiate the recommender that scores the highlights items.
* Called when weightedHighlights prefs is toggled to true.
Expand Down
15 changes: 10 additions & 5 deletions addon/RecommendationProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ Cu.importGlobalProperties(["fetch"]);

function RecommendationProvider(previewProvider, tabTracker, options = {}) {
this.options = Object.assign({}, DEFAULT_TIMEOUTS, options);
this._recommendedContent = [];
this._blockedRecommendedContent = new Set();
this._currentRecommendation = null;
this._pocketEndpoint = simplePrefs.prefs[POCKET_API_URL];
this._previewProvider = previewProvider;
this._tabTracker = tabTracker;
this._asyncUpdateRecommendations(this.options.pocketTimeout);
}

RecommendationProvider.prototype = {
Expand Down Expand Up @@ -135,6 +130,16 @@ RecommendationProvider.prototype = {
}
},

init() {
this._recommendedContent = [];
this._blockedRecommendedContent = new Set();
this._currentRecommendation = null;
this._pocketEndpoint = simplePrefs.prefs[POCKET_API_URL];
if (this.options.pocketTimeout) {
this._asyncUpdateRecommendations(this.options.pocketTimeout);
}
},

/**
* Uninitialize the recommendation provider
*/
Expand Down
11 changes: 11 additions & 0 deletions test/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function doDump(object, trailer) {
dump(JSON.stringify(object, null, 1) + trailer); // eslint-disable-line no-undef
}

function getTestRecommendationProvider() {
return {
init() {},
asyncSetRecommendedContent() {},
setBlockedRecommendation() {},
getRecommendation() {},
uninit() {}
};
}

function getTestSearchProvider() {
return {
init() {},
Expand Down Expand Up @@ -105,6 +115,7 @@ function getTestActivityStream(options = {}) {

options.pageScraper = mockPageScraper;
options.searchProvider = getTestSearchProvider();
options.recommendationProvider = getTestRecommendationProvider();
let mockApp = new ActivityStreams(mockMetadataStore, options);
return mockApp;
}
Expand Down
3 changes: 2 additions & 1 deletion test/test-RecommendationProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ before(exports, () => {
}
};
const mockTabTracker = {handleUserEvent() {}, generateEvent() {}, handlePerformanceEvent() {}};
gRecommendationProvider = new RecommendationProvider(mockPreviewProvider, mockTabTracker);
gRecommendationProvider = new RecommendationProvider(mockPreviewProvider, mockTabTracker, {pocketTimeout: null});
gRecommendationProvider.init();
});

after(exports, () => {
Expand Down

0 comments on commit f3448b9

Please sign in to comment.