Skip to content

Commit

Permalink
[TSVB] Fix per-request caching of index patterns (#97043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wylie Conlon authored and alexwizp committed Apr 14, 2021
1 parent 5e6b35b commit e873b2a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

const mock = jest.requireActual('../index_patterns_utils');

jest.spyOn(mock, 'fetchIndexPattern');

export const {
isStringTypeIndexPattern,
getIndexPatternKey,
extractIndexPatternValues,
fetchIndexPattern,
} = mock;
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
*/

import { IndexPattern, IndexPatternsService } from 'src/plugins/data/server';
import { fetchIndexPattern } from '../../../../common/index_patterns_utils';
import {
getCachedIndexPatternFetcher,
CachedIndexPatternFetcher,
} from './cached_index_pattern_fetcher';

jest.mock('../../../../common/index_patterns_utils');

describe('CachedIndexPatternFetcher', () => {
let mockedIndices: IndexPattern[] | [];
let cachedIndexPatternFetcher: CachedIndexPatternFetcher;
Expand All @@ -25,6 +28,8 @@ describe('CachedIndexPatternFetcher', () => {
find: jest.fn(() => Promise.resolve(mockedIndices || [])),
} as unknown) as IndexPatternsService;

(fetchIndexPattern as jest.Mock).mockClear();

cachedIndexPatternFetcher = getCachedIndexPatternFetcher(indexPatternsService);
});

Expand Down Expand Up @@ -52,6 +57,14 @@ describe('CachedIndexPatternFetcher', () => {
}
`);
});

test('should cache once', async () => {
await cachedIndexPatternFetcher('indexTitle');
await cachedIndexPatternFetcher('indexTitle');
await cachedIndexPatternFetcher('indexTitle');

expect(fetchIndexPattern as jest.Mock).toHaveBeenCalledTimes(1);
});
});

describe('object-based index', () => {
Expand Down Expand Up @@ -86,5 +99,20 @@ describe('CachedIndexPatternFetcher', () => {
}
`);
});

test('should cache once', async () => {
mockedIndices = [
{
id: 'indexId',
title: 'indexTitle',
},
] as IndexPattern[];

await cachedIndexPatternFetcher({ id: 'indexId' });
await cachedIndexPatternFetcher({ id: 'indexId' });
await cachedIndexPatternFetcher({ id: 'indexId' });

expect(fetchIndexPattern as jest.Mock).toHaveBeenCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getCachedIndexPatternFetcher = (indexPatternsService: IndexPatterns

const fetchedIndex = fetchIndexPattern(indexPatternValue, indexPatternsService);

cache.set(indexPatternValue, fetchedIndex);
cache.set(key, fetchedIndex);

return fetchedIndex;
};
Expand Down

0 comments on commit e873b2a

Please sign in to comment.