Bug 1545312 - Make L10nRegistry handle removed sources. r=mossop

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2019-04-24 20:36:00 +00:00
Родитель 74534d3cbc
Коммит bf768d3bb3
3 изменённых файлов: 71 добавлений и 5 удалений

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

@ -312,7 +312,8 @@ async function* generateResourceSetsForLocale(locale, sourcesOrder, resourceIds,
// loop, but if it's somewhere in the middle, we can
// safely bail from the whole branch.
for (let [idx, sourceName] of order.entries()) {
if (L10nRegistry.sources.get(sourceName).hasFile(locale, resourceIds[idx]) === false) {
const source = L10nRegistry.sources.get(sourceName);
if (!source || source.hasFile(locale, resourceIds[idx]) === false) {
if (idx === order.length - 1) {
continue;
} else {
@ -371,7 +372,8 @@ function* generateResourceSetsForLocaleSync(locale, sourcesOrder, resourceIds, r
// loop, but if it's somewhere in the middle, we can
// safely bail from the whole branch.
for (let [idx, sourceName] of order.entries()) {
if (L10nRegistry.sources.get(sourceName).hasFile(locale, resourceIds[idx]) === false) {
const source = L10nRegistry.sources.get(sourceName);
if (!source || source.hasFile(locale, resourceIds[idx]) === false) {
if (idx === order.length - 1) {
continue;
} else {
@ -526,9 +528,13 @@ const PSEUDO_STRATEGIES = {
* @param {Array} resourceIds
* @returns {Promise<FluentBundle>}
*/
async function generateResourceSet(locale, sourcesOrder, resourceIds) {
function generateResourceSet(locale, sourcesOrder, resourceIds) {
return Promise.all(resourceIds.map((resourceId, i) => {
return L10nRegistry.sources.get(sourcesOrder[i]).fetchFile(locale, resourceId);
const source = L10nRegistry.sources.get(sourcesOrder[i]);
if (!source) {
return false;
}
return source.fetchFile(locale, resourceId);
}));
}
@ -544,7 +550,11 @@ async function generateResourceSet(locale, sourcesOrder, resourceIds) {
*/
function generateResourceSetSync(locale, sourcesOrder, resourceIds) {
return resourceIds.map((resourceId, i) => {
return L10nRegistry.sources.get(sourcesOrder[i]).fetchFile(locale, resourceId, {sync: true});
const source = L10nRegistry.sources.get(sourcesOrder[i]);
if (!source) {
return false;
}
return source.fetchFile(locale, resourceId, {sync: true});
});
}

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

@ -459,3 +459,31 @@ add_task(async function test_hasSource() {
equal(L10nRegistry.hasSource("app"), true, "hasSource returns true after registering a source");
L10nRegistry.sources.clear();
});
/**
* This test verifies that we handle correctly a scenario where a source
* is being removed while the iterator operates.
*/
add_task(async function test_remove_source_mid_iter_cycle() {
let oneSource = new FileSource("platform", ["en-US"], "./platform/data/locales/{locale}/");
L10nRegistry.registerSource(oneSource);
let secondSource = new FileSource("app", ["pl"], "./app/data/locales/{locale}/");
L10nRegistry.registerSource(secondSource);
fs = {
"./platform/data/locales/en-US/test.ftl": "key = platform value",
"./app/data/locales/pl/test.ftl": "key = app value",
};
let bundles = L10nRegistry.generateBundles(["en-US", "pl"], ["test.ftl"]);
let bundle0 = await bundles.next();
L10nRegistry.removeSource("app");
equal((await bundles.next()).done, true);
// cleanup
L10nRegistry.sources.clear();
});

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

@ -451,3 +451,31 @@ add_task(function test_parallel_io() {
L10nRegistry.sources.clear();
L10nRegistry.load = originalLoad;
});
/**
* This test verifies that we handle correctly a scenario where a source
* is being removed while the iterator operates.
*/
add_task(function test_remove_source_mid_iter_cycle() {
let oneSource = new FileSource("platform", ["en-US"], "./platform/data/locales/{locale}/");
L10nRegistry.registerSource(oneSource);
let secondSource = new FileSource("app", ["pl"], "./app/data/locales/{locale}/");
L10nRegistry.registerSource(secondSource);
fs = {
"./platform/data/locales/en-US/test.ftl": "key = platform value",
"./app/data/locales/pl/test.ftl": "key = app value",
};
let bundles = L10nRegistry.generateBundlesSync(["en-US", "pl"], ["test.ftl"]);
let bundle0 = bundles.next();
L10nRegistry.removeSource("app");
equal((bundles.next()).done, true);
// cleanup
L10nRegistry.sources.clear();
});