[chance] ensure random words are unique (#35302)

Fixes https://github.com/elastic/kibana/issues/35293

In very rare cases, `chance.word()` can return the same word for two invocations, which is likely what caused the failures described in the issue. In order to prevent this we keep a local cache of the words generated and regenerate when the word has already been seen.
This commit is contained in:
Spencer 2019-04-18 10:59:43 -07:00 committed by GitHub
parent 68b67e5a80
commit 460a5e158a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,6 +33,18 @@ import { PUBLIC_PATH_PLACEHOLDER } from '../../public_path_placeholder';
const chance = new Chance();
const outputFixture = resolve(__dirname, './fixtures/output');
const randomWordsCache = new Set();
const uniqueRandomWord = () => {
const word = chance.word();
if (randomWordsCache.has(word)) {
return uniqueRandomWord();
}
randomWordsCache.add(word);
return word;
};
function replaceAll(source, replace, replaceWith) {
return source.split(replace).join(replaceWith);
}
@ -172,7 +184,7 @@ describe('optimizer/bundle route', () => {
describe('js file with placeholder', () => {
it('responds with no content-length and modified file data', async () => {
const basePublicPath = `/${chance.word()}`;
const basePublicPath = `/${uniqueRandomWord()}`;
const server = createServer({ basePublicPath });
const response = await server.inject({
@ -209,7 +221,7 @@ describe('optimizer/bundle route', () => {
describe('css file with placeholder', () => {
it('responds with no content-length and modified file data', async () => {
const basePublicPath = `/${chance.word()}`;
const basePublicPath = `/${uniqueRandomWord()}`;
const server = createServer({ basePublicPath });
const response = await server.inject({
@ -306,8 +318,8 @@ describe('optimizer/bundle route', () => {
});
it('is unique per basePublicPath although content is the same', async () => {
const basePublicPath1 = `/${chance.word()}`;
const basePublicPath2 = `/${chance.word()}`;
const basePublicPath1 = `/${uniqueRandomWord()}`;
const basePublicPath2 = `/${uniqueRandomWord()}`;
const [resp1, resp2] = await Promise.all([
createServer({ basePublicPath: basePublicPath1 }).inject({