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

[ML] Fix license subscription race condition. #70074

Merged
merged 5 commits into from
Jun 30, 2020
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
6 changes: 3 additions & 3 deletions x-pack/plugins/ml/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ export const renderApp = (

deps.kibanaLegacy.loadFontAwesome();

const mlLicense = setLicenseCache(deps.licensing);

appMountParams.onAppLeave((actions) => actions.default());

ReactDOM.render(<App coreStart={coreStart} deps={deps} />, appMountParams.element);
const mlLicense = setLicenseCache(deps.licensing, [
() => ReactDOM.render(<App coreStart={coreStart} deps={deps} />, appMountParams.element),
]);

return () => {
mlLicense.unsubscribe();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { LicensingPluginSetup } from '../../../../licensing/public';
import { MlLicense } from '../../../common/license';
import { MlClientLicense } from './ml_client_license';

let mlLicense: MlClientLicense | null = null;
Expand All @@ -16,9 +17,12 @@ let mlLicense: MlClientLicense | null = null;
* @param {LicensingPluginSetup} licensingSetup
* @returns {MlClientLicense}
*/
export function setLicenseCache(licensingSetup: LicensingPluginSetup) {
export function setLicenseCache(
licensingSetup: LicensingPluginSetup,
postInitFunctions?: Array<(lic: MlLicense) => void>
) {
mlLicense = new MlClientLicense();
mlLicense.setup(licensingSetup.license$);
mlLicense.setup(licensingSetup.license$, postInitFunctions);
return mlLicense;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Observable, Subject } from 'rxjs';
import { ILicense } from '../../../../licensing/common/types';

import { MlClientLicense } from './ml_client_license';

describe('MlClientLicense', () => {
test('should miss the license update when initialized without postInitFunction', () => {
const mlLicense = new MlClientLicense();

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);

const license$ = new Subject();

mlLicense.setup(license$ as Observable<ILicense>);

// if the observable wasn't triggered the full license is still not set
expect(mlLicense.isFullLicense()).toBe(false);

license$.next({
check: () => ({ state: 'valid' }),
getFeature: () => ({ isEnabled: true }),
status: 'valid',
});

// once the observable triggered the license should be set
expect(mlLicense.isFullLicense()).toBe(true);
});

test('should not miss the license update when initialized with postInitFunction', (done) => {
const mlLicense = new MlClientLicense();

// upon instantiation the full license doesn't get set
expect(mlLicense.isFullLicense()).toBe(false);

const license$ = new Subject();

mlLicense.setup(license$ as Observable<ILicense>, [
(license) => {
// when passed in via postInitFunction callback, the license should be valid
// even if the license$ observable gets triggered after this setup.
expect(license.isFullLicense()).toBe(true);
done();
},
]);

license$.next({
check: () => ({ state: 'valid' }),
getFeature: () => ({ isEnabled: true }),
status: 'valid',
});
});
});