From 8374f0ca17e00d93115b55db0f09947624aac294 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Tue, 1 Oct 2024 16:43:03 +0200 Subject: [PATCH] Test the database manager shutdown hook Signed-off-by: Eric Peterson --- .../database/DatabaseManager.test.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/backend-defaults/src/entrypoints/database/DatabaseManager.test.ts b/packages/backend-defaults/src/entrypoints/database/DatabaseManager.test.ts index 26c2d54231..c7e739c7c7 100644 --- a/packages/backend-defaults/src/entrypoints/database/DatabaseManager.test.ts +++ b/packages/backend-defaults/src/entrypoints/database/DatabaseManager.test.ts @@ -176,4 +176,35 @@ describe('DatabaseManagerImpl', () => { skip: true, }); }); + + it('registers a shutdown hook if root lifecycle service is provided', async () => { + // Given a database manager that is provided a rootLifecycle service + const rootLifecycle = { addShutdownHook: jest.fn() } as unknown as any; + const destroy = jest.fn(); + const connector1 = { + getClient: jest.fn().mockResolvedValue({ destroy }), + } satisfies Connector; + const impl = new DatabaseManagerImpl( + new ConfigReader({ + client: 'pg', + }), + { + pg: connector1, + }, + { rootLifecycle }, + ); + + // Then a shutdown hook should have been added + expect(rootLifecycle.addShutdownHook).toHaveBeenCalled(); + const shutdownHook = rootLifecycle.addShutdownHook.mock.calls[0][0]; + + // When a database client for a plugin is retrieved + await impl.forPlugin('plugin1', deps).getClient(); + + // And the shutdownhook is called + await shutdownHook(); + + // Then the destroy method should have been called on the resolved client + expect(destroy).toHaveBeenCalled(); + }); });