From c94d42b58b737dbcba19aaeb21940ba8ee6efe1f Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 16 Nov 2022 09:09:12 +0100 Subject: [PATCH] document, add tests Signed-off-by: Johan Haals --- packages/backend-app-api/api-report.md | 2 +- .../implementations/lifecycleService.test.ts | 47 +++++++++++++++++++ .../implementations/lifecycleService.ts | 6 ++- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 packages/backend-app-api/src/services/implementations/lifecycleService.test.ts diff --git a/packages/backend-app-api/api-report.md b/packages/backend-app-api/api-report.md index 5d200be93c..5008b1efe1 100644 --- a/packages/backend-app-api/api-report.md +++ b/packages/backend-app-api/api-report.md @@ -67,7 +67,7 @@ export type HttpRouterFactoryOptions = { indexPlugin?: string; }; -// @public (undocumented) +// @public export const lifecycleFactory: ( options?: undefined, ) => ServiceFactory; diff --git a/packages/backend-app-api/src/services/implementations/lifecycleService.test.ts b/packages/backend-app-api/src/services/implementations/lifecycleService.test.ts new file mode 100644 index 0000000000..c0b69a3bef --- /dev/null +++ b/packages/backend-app-api/src/services/implementations/lifecycleService.test.ts @@ -0,0 +1,47 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { getVoidLogger } from '@backstage/backend-common'; +import { BackendLifecycleImpl } from './lifecycleService'; + +describe('lifecycleService', () => { + it('should execute registered shutdown hook', async () => { + const service = new BackendLifecycleImpl(getVoidLogger()); + const hook = jest.fn(); + service.addShutdownHook({ + pluginId: 'test', + fn: async () => { + hook(); + }, + }); + // should not execute the hook more than once. + await service.shutdown(); + await service.shutdown(); + await service.shutdown(); + expect(hook).toHaveBeenCalledTimes(1); + }); + + it('should not throw errors', async () => { + const service = new BackendLifecycleImpl(getVoidLogger()); + service.addShutdownHook({ + pluginId: 'test', + fn: async () => { + throw new Error('oh no'); + }, + }); + await expect(service.shutdown()).resolves.toBeUndefined(); + }); +}); diff --git a/packages/backend-app-api/src/services/implementations/lifecycleService.ts b/packages/backend-app-api/src/services/implementations/lifecycleService.ts index 0ee0946469..7d41a7a276 100644 --- a/packages/backend-app-api/src/services/implementations/lifecycleService.ts +++ b/packages/backend-app-api/src/services/implementations/lifecycleService.ts @@ -25,7 +25,7 @@ import { import { Logger } from 'winston'; const CALLBACKS = ['SIGTERM', 'SIGINT', 'beforeExit']; -class BackendLifecycleImpl { +export class BackendLifecycleImpl { constructor(private readonly logger: Logger) { CALLBACKS.map(signal => process.on(signal, () => this.shutdown())); } @@ -76,7 +76,9 @@ class PluginScopedLifecycleImpl implements BackendLifecycle { } } -/** @public */ +/** + * Allows plugins to register shutdown hooks that are run when the process is about to exit. + * @public */ export const lifecycleFactory = createServiceFactory({ service: lifecycleServiceRef, deps: {