diff --git a/src/vs/workbench/api/browser/mainThreadTesting.ts b/src/vs/workbench/api/browser/mainThreadTesting.ts index b33a86eae8e..0bfb150cb02 100644 --- a/src/vs/workbench/api/browser/mainThreadTesting.ts +++ b/src/vs/workbench/api/browser/mainThreadTesting.ts @@ -30,6 +30,7 @@ const reviveDiff = (diff: TestsDiff) => { export class MainThreadTesting extends Disposable implements MainThreadTestingShape { private readonly proxy: ExtHostTestingShape; private readonly testSubscriptions = new Map(); + private readonly testProviderRegistrations = new Map(); constructor( extHostContext: IExtHostContext, @@ -101,17 +102,20 @@ export class MainThreadTesting extends Disposable implements MainThreadTestingSh * @inheritdoc */ public $registerTestProvider(id: string) { - this.testService.registerTestController(id, { + const disposable = this.testService.registerTestController(id, { runTests: (req, token) => this.proxy.$runTestsForProvider(req, token), lookupTest: test => this.proxy.$lookupTest(test), }); + + this.testProviderRegistrations.set(id, disposable); } /** * @inheritdoc */ public $unregisterTestProvider(id: string) { - this.testService.unregisterTestController(id); + this.testProviderRegistrations.get(id)?.dispose(); + this.testProviderRegistrations.delete(id); } /** @@ -147,6 +151,7 @@ export class MainThreadTesting extends Disposable implements MainThreadTestingSh } public dispose() { + super.dispose(); this.testService.updateRootProviderCount(-1); for (const subscription of this.testSubscriptions.values()) { subscription.dispose(); diff --git a/src/vs/workbench/contrib/testing/common/testService.ts b/src/vs/workbench/contrib/testing/common/testService.ts index 7b562938108..ce0ce27ce1c 100644 --- a/src/vs/workbench/contrib/testing/common/testService.ts +++ b/src/vs/workbench/contrib/testing/common/testService.ts @@ -5,7 +5,7 @@ import { CancellationToken } from 'vs/base/common/cancellation'; import { Event } from 'vs/base/common/event'; -import { DisposableStore, IReference } from 'vs/base/common/lifecycle'; +import { DisposableStore, IDisposable, IReference } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { ExtHostTestingResource } from 'vs/workbench/api/common/extHost.protocol'; @@ -118,8 +118,7 @@ export interface ITestService { */ clearExcludedTests(): void; - registerTestController(id: string, controller: MainTestController): void; - unregisterTestController(id: string): void; + registerTestController(id: string, controller: MainTestController): IDisposable; runTests(req: RunTestsRequest, token?: CancellationToken): Promise; cancelTestRun(req: RunTestsRequest): void; publishDiff(resource: ExtHostTestingResource, uri: URI, diff: TestsDiff): void; diff --git a/src/vs/workbench/contrib/testing/common/testServiceImpl.ts b/src/vs/workbench/contrib/testing/common/testServiceImpl.ts index e5f2d942039..279eb3fc359 100644 --- a/src/vs/workbench/contrib/testing/common/testServiceImpl.ts +++ b/src/vs/workbench/contrib/testing/common/testServiceImpl.ts @@ -7,7 +7,7 @@ import { groupBy } from 'vs/base/common/arrays'; import { disposableTimeout } from 'vs/base/common/async'; import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation'; import { Emitter } from 'vs/base/common/event'; -import { Disposable, IDisposable, IReference } from 'vs/base/common/lifecycle'; +import { Disposable, IDisposable, IReference, toDisposable } from 'vs/base/common/lifecycle'; import { URI, UriComponents } from 'vs/base/common/uri'; import { localize } from 'vs/nls'; import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; @@ -284,19 +284,17 @@ export class TestService extends Disposable implements ITestService { /** * @inheritdoc */ - public registerTestController(id: string, controller: MainTestController): void { + public registerTestController(id: string, controller: MainTestController): IDisposable { this.testControllers.set(id, controller); this.providerCount.set(this.testControllers.size); this.changeProvidersEmitter.fire({ delta: 1 }); - } - /** - * @inheritdoc - */ - public unregisterTestController(id: string): void { - this.testControllers.delete(id); - this.providerCount.set(this.testControllers.size); - this.changeProvidersEmitter.fire({ delta: -1 }); + return toDisposable(() => { + if (this.testControllers.delete(id)) { + this.providerCount.set(this.testControllers.size); + this.changeProvidersEmitter.fire({ delta: -1 }); + } + }); } private findTest(predicate: (t: InternalTestItem) => boolean): InternalTestItem | undefined {