Skip to content

Commit ab01280

Browse files
committed
fix: don't set timer for refreshing connection status after deactivation
Closes #120. Fix introduces deactivated boolean variable to pass extension status to updateConnectionsPeriodically() function to avoid setting timer after extension deactivation when promise returned from function above resolves after deactivate() call. Assisted-by: <Cursor> Signed-off-by: Denis Golovin <dgolovin@redhat.com>
1 parent 05db9c2 commit ab01280

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/extension.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,48 @@ suite('kubernetes provider connection factory', () => {
399399
});
400400
});
401401

402+
describe('deactivation stops periodic connection updates', () => {
403+
function setupActivation(): podmanDesktopApi.Provider {
404+
const providerMock: podmanDesktopApi.Provider = {
405+
setKubernetesProviderConnectionFactory: vi.fn(),
406+
registerKubernetesProviderConnection: () => ({
407+
dispose: vi.fn(),
408+
}),
409+
} as any as podmanDesktopApi.Provider;
410+
vi.spyOn(podmanDesktopApi.provider, 'createProvider').mockReturnValue(providerMock);
411+
vi.spyOn(kubeconfig, 'createOrLoadFromFile').mockReturnValue(new KubeConfig());
412+
return providerMock;
413+
}
414+
415+
test('clearTimeout is called when deactivate is invoked', async () => {
416+
setupActivation();
417+
await extension.activate(context);
418+
419+
const clearTimeoutSpy = vi.spyOn(global, 'clearTimeout');
420+
extension.deactivate();
421+
422+
expect(clearTimeoutSpy).toHaveBeenCalled();
423+
});
424+
425+
test('updateConnections is not called again after deactivate', async () => {
426+
vi.useFakeTimers();
427+
setupActivation();
428+
await extension.activate(context);
429+
430+
await vi.advanceTimersByTimeAsync(0);
431+
432+
const createOrLoadSpy = vi.mocked(kubeconfig.createOrLoadFromFile);
433+
createOrLoadSpy.mockClear();
434+
435+
extension.deactivate();
436+
437+
await vi.advanceTimersByTimeAsync(10000);
438+
439+
expect(createOrLoadSpy).not.toHaveBeenCalled();
440+
vi.useRealTimers();
441+
});
442+
});
443+
402444
test('push image to sandbox does not change title after it is finished', async () => {
403445
vi.mocked(got).mockImplementation(
404446
// use vi.fn(), so there is no need to deal with types safety when mocking

src/extension.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,11 @@ export async function getDevSandboxSignUpStatus(idToken: string): Promise<SBSign
212212
return status;
213213
}
214214

215+
let deactivated = false; // flag to prevent setting timeout after deactivation
216+
215217
export async function activate(extensionContext: extensionApi.ExtensionContext): Promise<void> {
216218
console.log('starting extension redhat-developer-sandbox');
219+
deactivated = false; // reset for tests to work correctly
217220

218221
let status: extensionApi.ProviderStatus = 'ready';
219222
const icon = './icon.png';
@@ -331,12 +334,15 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
331334

332335
function updateConnectionsPeriodically(): void {
333336
updateConnections().then(() => {
334-
updateConnectionTimeout = setTimeout(updateConnectionsPeriodically, 2000);
337+
if (!deactivated) {
338+
updateConnectionTimeout = setTimeout(updateConnectionsPeriodically, 2000);
339+
}
335340
});
336341
}
337342

338343
export function deactivate(): void {
339344
console.log('deactivating redhat-developer-sandbox extension');
345+
deactivated = true;
340346
if (updateConnectionTimeout) {
341347
clearTimeout(updateConnectionTimeout);
342348
}

0 commit comments

Comments
 (0)