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

Have secrets adopt ensureNoDisposablesAreLeakedInTestSuite #192258

Merged
merged 1 commit into from
Sep 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/vs/platform/secrets/common/secrets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import { IStorageService, InMemoryStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
import { Emitter, Event } from 'vs/base/common/event';
import { ILogService } from 'vs/platform/log/common/log';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { Lazy } from 'vs/base/common/lazy';

export const ISecretStorageService = createDecorator<ISecretStorageService>('secretStorageService');
Expand All @@ -26,26 +26,28 @@ export interface ISecretStorageService extends ISecretStorageProvider {
onDidChangeSecret: Event<string>;
}

export class BaseSecretStorageService implements ISecretStorageService {
export class BaseSecretStorageService extends Disposable implements ISecretStorageService {
declare readonly _serviceBrand: undefined;

private readonly _storagePrefix = 'secret://';

protected readonly onDidChangeSecretEmitter = new Emitter<string>();
protected readonly onDidChangeSecretEmitter = this._register(new Emitter<string>());
onDidChangeSecret: Event<string> = this.onDidChangeSecretEmitter.event;

protected readonly _sequencer = new SequencerByKey<string>();

private _type: 'in-memory' | 'persisted' | 'unknown' = 'unknown';

private readonly _onDidChangeValueDisposable = new DisposableStore();
private readonly _onDidChangeValueDisposable = this._register(new DisposableStore());

constructor(
private readonly _useInMemoryStorage: boolean,
@IStorageService private _storageService: IStorageService,
@IEncryptionService protected _encryptionService: IEncryptionService,
@ILogService protected readonly _logService: ILogService,
) { }
) {
super();
}

/**
* @Note initialize must be called first so that this can be resolved properly
Expand Down Expand Up @@ -134,7 +136,7 @@ export class BaseSecretStorageService implements ISecretStorageService {
}
this._logService.trace('[SecretStorageService] Encryption is not available, falling back to in-memory storage');
this._type = 'in-memory';
storageService = new InMemoryStorageService();
storageService = this._register(new InMemoryStorageService());
}

this._onDidChangeValueDisposable.clear();
Expand Down
32 changes: 25 additions & 7 deletions src/vs/platform/secrets/test/common/secrets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as assert from 'assert';
import * as sinon from 'sinon';
import { ensureNoDisposablesAreLeakedInTestSuite } from 'vs/base/test/common/utils';
import { IEncryptionService, KnownStorageProvider } from 'vs/platform/encryption/common/encryptionService';
import { NullLogService } from 'vs/platform/log/common/log';
import { BaseSecretStorageService } from 'vs/platform/secrets/common/secrets';
Expand Down Expand Up @@ -50,6 +51,8 @@ class TestNoEncryptionService implements IEncryptionService {
}

suite('secrets', () => {
const store = ensureNoDisposablesAreLeakedInTestSuite();

suite('BaseSecretStorageService useInMemoryStorage=true', () => {
let service: BaseSecretStorageService;
let spyEncryptionService: sinon.SinonSpiedInstance<TestEncryptionService>;
Expand All @@ -58,7 +61,12 @@ suite('secrets', () => {
setup(() => {
sandbox = sinon.createSandbox();
spyEncryptionService = sandbox.spy(new TestEncryptionService());
service = new BaseSecretStorageService(true, new InMemoryStorageService(), spyEncryptionService, new NullLogService());
service = store.add(new BaseSecretStorageService(
true,
store.add(new InMemoryStorageService()),
spyEncryptionService,
store.add(new NullLogService())
));
});

teardown(() => {
Expand Down Expand Up @@ -98,10 +106,10 @@ suite('secrets', () => {
const key = 'my-secret';
const value = 'my-secret-value';
let eventFired = false;
service.onDidChangeSecret((changedKey) => {
store.add(service.onDidChangeSecret((changedKey) => {
assert.strictEqual(changedKey, key);
eventFired = true;
});
}));
await service.set(key, value);
assert.strictEqual(eventFired, true);
});
Expand All @@ -115,7 +123,12 @@ suite('secrets', () => {
setup(() => {
sandbox = sinon.createSandbox();
spyEncryptionService = sandbox.spy(new TestEncryptionService());
service = new BaseSecretStorageService(false, new InMemoryStorageService(), spyEncryptionService, new NullLogService());
service = store.add(new BaseSecretStorageService(
false,
store.add(new InMemoryStorageService()),
spyEncryptionService,
store.add(new NullLogService()))
);
});

teardown(() => {
Expand Down Expand Up @@ -155,10 +168,10 @@ suite('secrets', () => {
const key = 'my-secret';
const value = 'my-secret-value';
let eventFired = false;
service.onDidChangeSecret((changedKey) => {
store.add(service.onDidChangeSecret((changedKey) => {
assert.strictEqual(changedKey, key);
eventFired = true;
});
}));
await service.set(key, value);
assert.strictEqual(eventFired, true);
});
Expand All @@ -172,7 +185,12 @@ suite('secrets', () => {
setup(() => {
sandbox = sinon.createSandbox();
spyNoEncryptionService = sandbox.spy(new TestNoEncryptionService());
service = new BaseSecretStorageService(false, new InMemoryStorageService(), spyNoEncryptionService, new NullLogService());
service = store.add(new BaseSecretStorageService(
false,
store.add(new InMemoryStorageService()),
spyNoEncryptionService,
store.add(new NullLogService()))
);
});

teardown(() => {
Expand Down