Skip to content

Commit

Permalink
allow multiple registration of the same feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Aug 31, 2020
1 parent 647f397 commit f74f1b9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ describe('FeatureUsageService', () => {

describe('#setup', () => {
describe('#register', () => {
it('throws when registering the same feature twice', () => {
it('does not throw when registering the same feature twice with the same license', () => {
const setup = service.setup();
setup.register('foo', 'basic');
expect(() => {
setup.register('foo', 'basic');
}).toThrowErrorMatchingInlineSnapshot(`"Feature 'foo' has already been registered."`);
}).not.toThrow();
});

it('throws when registering the same feature again with a different license', () => {
const setup = service.setup();
setup.register('foo', 'basic');
expect(() => {
setup.register('foo', 'enterprise');
}).toThrowErrorMatchingInlineSnapshot(
`"Feature 'foo' has already been registered with another license type. (current: basic, new: enterprise)"`
);
});
});
});
Expand Down
20 changes: 13 additions & 7 deletions x-pack/plugins/licensing/server/services/feature_usage_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ export class FeatureUsageService {
public setup(): FeatureUsageServiceSetup {
return {
register: (featureName, licenseType) => {
if (this.lastUsages.has(featureName)) {
throw new Error(`Feature '${featureName}' has already been registered.`);
const registered = this.lastUsages.get(featureName);
if (registered) {
if (registered.licenseType !== licenseType) {
throw new Error(
`Feature '${featureName}' has already been registered with another license type. (current: ${registered.licenseType}, new: ${licenseType})`
);
}
} else {
this.lastUsages.set(featureName, {
name: featureName,
lastUsed: null,
licenseType,
});
}
this.lastUsages.set(featureName, {
name: featureName,
lastUsed: null,
licenseType,
});
},
};
}
Expand Down

0 comments on commit f74f1b9

Please sign in to comment.