Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [TSVB] Fix per-request caching of index patterns (#97043) #97080

Merged
merged 1 commit into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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