Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Don't fetch value if opted out, add tests
  • Loading branch information
Kim-Adeline Miguel
Kim-Adeline Miguel committed Aug 21, 2020
commit 62e7bd493cf07a3a029b726c7b29c8f217db8ec8
8 changes: 3 additions & 5 deletions src/client/common/experiments/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ export class ExperimentService implements IExperimentService {
return this.experimentationService.isCachedFlightEnabled(experiment);
}

public async getExperimentValue<T extends boolean | number | string>(
experimentName: string
): Promise<T | undefined> {
if (!this.experimentationService) {
public async getExperimentValue<T extends boolean | number | string>(experiment: string): Promise<T | undefined> {
if (!this.experimentationService || this._optOutFrom.includes('All') || this._optOutFrom.includes(experiment)) {
return;
}

return this.experimentationService.getTreatmentVariableAsync('vscode', experimentName);
return this.experimentationService.getTreatmentVariableAsync('vscode', experiment);
}

private logExperiments() {
Expand Down
75 changes: 75 additions & 0 deletions src/test/common/experiments/service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,79 @@ suite('Experimentation service', () => {
sinon.assert.notCalled(isCachedFlightEnabledStub);
});
});

suite('Experiment value retrieval', () => {
const experiment = 'Test Experiment - experiment';
let getTreatmentVariableAsyncStub: sinon.SinonStub;

setup(() => {
getTreatmentVariableAsyncStub = sinon.stub().returns(Promise.resolve('value'));
sinon.stub(tasClient, 'getExperimentationService').returns({
getTreatmentVariableAsync: getTreatmentVariableAsyncStub
// tslint:disable-next-line: no-any
} as any);

configureApplicationEnvironment('stable', extensionVersion);
});

test('If the service is enabled and the opt-out array is empty,return the value from the experimentation framework for a given experiment', async () => {
configureSettings(true, [], []);

const experimentService = new ExperimentService(
instance(configurationService),
instance(appEnvironment),
globalMemento,
outputChannel
);
const result = await experimentService.getExperimentValue(experiment);

assert.equal(result, 'value');
sinon.assert.calledOnce(getTreatmentVariableAsyncStub);
});

test('If the experiment setting is disabled, getExperimentValue should return undefined', async () => {
configureSettings(false, [], []);

const experimentService = new ExperimentService(
instance(configurationService),
instance(appEnvironment),
globalMemento,
outputChannel
);
const result = await experimentService.getExperimentValue(experiment);

assert.isUndefined(result);
sinon.assert.notCalled(getTreatmentVariableAsyncStub);
});

test('If the opt-out setting contains "All", getExperimentValue should return undefined', async () => {
configureSettings(true, [], ['All']);

const experimentService = new ExperimentService(
instance(configurationService),
instance(appEnvironment),
globalMemento,
outputChannel
);
const result = await experimentService.getExperimentValue(experiment);

assert.isUndefined(result);
sinon.assert.notCalled(getTreatmentVariableAsyncStub);
});

test('If the opt-out setting contains the experiment name, igetExperimentValue should return undefined', async () => {
configureSettings(true, [], [experiment]);

const experimentService = new ExperimentService(
instance(configurationService),
instance(appEnvironment),
globalMemento,
outputChannel
);
const result = await experimentService.getExperimentValue(experiment);

assert.isUndefined(result);
sinon.assert.notCalled(getTreatmentVariableAsyncStub);
});
});
});