Skip to content

Commit

Permalink
small code enhancements, graceful stop
Browse files Browse the repository at this point in the history
  • Loading branch information
gsoldevila committed May 26, 2022
1 parent c2a85f1 commit a772de9
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/core/server/elasticsearch/elasticsearch_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ jest.mock('./version_check/ensure_es_version', () => ({
pollEsNodesVersion: jest.fn(),
}));

// Mocking the module to disable caching for tests
jest.mock('../ui_settings/cache');

import { MockClusterClient, isScriptingEnabledMock } from './elasticsearch_service.test.mocks';

import type { NodesVersionCompatibility } from './version_check/ensure_es_version';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, firstValueFrom } from 'rxjs';
import { first } from 'rxjs/operators';
import { REPO_ROOT } from '@kbn/utils';
import { Env } from '../config';
Expand Down Expand Up @@ -197,13 +200,8 @@ describe('#setup', () => {

expect(mockedClient.nodes.info).toHaveBeenCalledTimes(0);

return new Promise<void>((done) => {
const subscription = setupContract.esNodesCompatibility$.subscribe(() => {
expect(mockedClient.nodes.info).toHaveBeenCalledTimes(1);
subscription.unsubscribe();
done();
});
});
await firstValueFrom(setupContract.esNodesCompatibility$);
expect(mockedClient.nodes.info).toHaveBeenCalledTimes(1);
});

it('esNodeVersionCompatibility$ stops polling when unsubscribed from', async () => {
Expand All @@ -216,18 +214,15 @@ describe('#setup', () => {

expect(mockedClient.nodes.info).toHaveBeenCalledTimes(0);

return new Promise<void>((done) => {
const subscription = setupContract.esNodesCompatibility$.subscribe(async () => {
subscription.unsubscribe();
await delay(100);
expect(mockedClient.nodes.info).toHaveBeenCalledTimes(1);
done();
});
});
await firstValueFrom(setupContract.esNodesCompatibility$);
await delay(100);
expect(mockedClient.nodes.info).toHaveBeenCalledTimes(1);
});
});

describe('#start', () => {
afterEach(async () => await elasticsearchService?.stop());

it('throws if called before `setup`', async () => {
await expect(() => elasticsearchService.start()).rejects.toMatchInlineSnapshot(
`[Error: ElasticsearchService needs to be setup before calling start]`
Expand Down

0 comments on commit a772de9

Please sign in to comment.