mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 17:59:23 -04:00
[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:
parent
68b67e5a80
commit
460a5e158a
1 changed files with 16 additions and 4 deletions
|
@ -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({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue