Path: blob/main/src/vs/workbench/services/lifecycle/test/electron-browser/lifecycleService.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { timeout } from '../../../../../base/common/async.js';7import { DisposableStore } from '../../../../../base/common/lifecycle.js';8import { runWithFakedTimers } from '../../../../../base/test/common/timeTravelScheduler.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';10import { ShutdownReason, WillShutdownJoinerOrder } from '../../common/lifecycle.js';11import { NativeLifecycleService } from '../../electron-browser/lifecycleService.js';12import { workbenchInstantiationService } from '../../../../test/electron-browser/workbenchTestServices.js';1314suite('Lifecycleservice', function () {1516let lifecycleService: TestLifecycleService;17const disposables = new DisposableStore();1819class TestLifecycleService extends NativeLifecycleService {2021testHandleBeforeShutdown(reason: ShutdownReason): Promise<boolean> {22return super.handleBeforeShutdown(reason);23}2425testHandleWillShutdown(reason: ShutdownReason): Promise<void> {26return super.handleWillShutdown(reason);27}28}2930setup(async () => {31const instantiationService = workbenchInstantiationService(undefined, disposables);32lifecycleService = disposables.add(instantiationService.createInstance(TestLifecycleService));33});3435teardown(async () => {36disposables.clear();37});3839test('onBeforeShutdown - final veto called after other vetos', async function () {40let vetoCalled = false;41let finalVetoCalled = false;4243const order: number[] = [];4445disposables.add(lifecycleService.onBeforeShutdown(e => {46e.veto(new Promise<boolean>(resolve => {47vetoCalled = true;48order.push(1);4950resolve(false);51}), 'test');52}));5354disposables.add(lifecycleService.onBeforeShutdown(e => {55e.finalVeto(() => {56return new Promise<boolean>(resolve => {57finalVetoCalled = true;58order.push(2);5960resolve(true);61});62}, 'test');63}));6465const veto = await lifecycleService.testHandleBeforeShutdown(ShutdownReason.QUIT);6667assert.strictEqual(veto, true);68assert.strictEqual(vetoCalled, true);69assert.strictEqual(finalVetoCalled, true);70assert.strictEqual(order[0], 1);71assert.strictEqual(order[1], 2);72});7374test('onBeforeShutdown - final veto not called when veto happened before', async function () {75let vetoCalled = false;76let finalVetoCalled = false;7778disposables.add(lifecycleService.onBeforeShutdown(e => {79e.veto(new Promise<boolean>(resolve => {80vetoCalled = true;8182resolve(true);83}), 'test');84}));8586disposables.add(lifecycleService.onBeforeShutdown(e => {87e.finalVeto(() => {88return new Promise<boolean>(resolve => {89finalVetoCalled = true;9091resolve(true);92});93}, 'test');94}));9596const veto = await lifecycleService.testHandleBeforeShutdown(ShutdownReason.QUIT);9798assert.strictEqual(veto, true);99assert.strictEqual(vetoCalled, true);100assert.strictEqual(finalVetoCalled, false);101});102103test('onBeforeShutdown - veto with error is treated as veto', async function () {104disposables.add(lifecycleService.onBeforeShutdown(e => {105e.veto(new Promise<boolean>((resolve, reject) => {106reject(new Error('Fail'));107}), 'test');108}));109110const veto = await lifecycleService.testHandleBeforeShutdown(ShutdownReason.QUIT);111112assert.strictEqual(veto, true);113});114115test('onBeforeShutdown - final veto with error is treated as veto', async function () {116disposables.add(lifecycleService.onBeforeShutdown(e => {117e.finalVeto(() => new Promise<boolean>((resolve, reject) => {118reject(new Error('Fail'));119}), 'test');120}));121122const veto = await lifecycleService.testHandleBeforeShutdown(ShutdownReason.QUIT);123124assert.strictEqual(veto, true);125});126127test('onWillShutdown - join', async function () {128let joinCalled = false;129130disposables.add(lifecycleService.onWillShutdown(e => {131e.join(new Promise(resolve => {132joinCalled = true;133134resolve();135}), { id: 'test', label: 'test' });136}));137138await lifecycleService.testHandleWillShutdown(ShutdownReason.QUIT);139140assert.strictEqual(joinCalled, true);141});142143test('onWillShutdown - join with error is handled', async function () {144let joinCalled = false;145146disposables.add(lifecycleService.onWillShutdown(e => {147e.join(new Promise((resolve, reject) => {148joinCalled = true;149150reject(new Error('Fail'));151}), { id: 'test', label: 'test' });152}));153154await lifecycleService.testHandleWillShutdown(ShutdownReason.QUIT);155156assert.strictEqual(joinCalled, true);157});158159test('onWillShutdown - join order', async function () {160return runWithFakedTimers({ useFakeTimers: true }, async () => {161const order: string[] = [];162163disposables.add(lifecycleService.onWillShutdown(e => {164e.join(async () => {165order.push('disconnect start');166await timeout(1);167order.push('disconnect end');168}, { id: 'test', label: 'test', order: WillShutdownJoinerOrder.Last });169170e.join((async () => {171order.push('default start');172await timeout(1);173order.push('default end');174})(), { id: 'test', label: 'test', order: WillShutdownJoinerOrder.Default });175}));176177await lifecycleService.testHandleWillShutdown(ShutdownReason.QUIT);178179assert.deepStrictEqual(order, [180'default start',181'default end',182'disconnect start',183'disconnect end'184]);185});186});187188test('willShutdown is set when shutting down', async function () {189let willShutdownSet = false;190191disposables.add(lifecycleService.onWillShutdown(e => {192e.join(new Promise(resolve => {193if (lifecycleService.willShutdown) {194willShutdownSet = true;195resolve();196}197}), { id: 'test', label: 'test' });198}));199200await lifecycleService.testHandleWillShutdown(ShutdownReason.QUIT);201202assert.strictEqual(willShutdownSet, true);203});204205ensureNoDisposablesAreLeakedInTestSuite();206});207208209