From 98748ec925945756c035a7287fe9f67f5b6fb092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Thu, 18 Jul 2024 16:06:19 +0200 Subject: [PATCH] fix: verification of `fuel-core` node killing (#2759) --- .changeset/neat-bikes-melt.md | 4 ++++ .../launchNode-singular-test.test.ts | 22 ------------------ .../account/src/test-utils/launchNode.test.ts | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 .changeset/neat-bikes-melt.md delete mode 100644 packages/account/src/test-utils/launchNode-singular-test.test.ts diff --git a/.changeset/neat-bikes-melt.md b/.changeset/neat-bikes-melt.md new file mode 100644 index 00000000000..7ff5dd841f7 --- /dev/null +++ b/.changeset/neat-bikes-melt.md @@ -0,0 +1,4 @@ +--- +--- + +fix: verification of all test nodes being killed diff --git a/packages/account/src/test-utils/launchNode-singular-test.test.ts b/packages/account/src/test-utils/launchNode-singular-test.test.ts deleted file mode 100644 index 17237e00692..00000000000 --- a/packages/account/src/test-utils/launchNode-singular-test.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { launchNode } from './launchNode'; - -/** - * The test runner creates a test environment per file, - * which we can use to isolate the faulty behavior. - */ -/** - * @group node - */ -describe('launchNode-singular-test', () => { - const killedNode = { - url: '', - }; - afterEach(async () => { - await expect(fetch(killedNode.url)).rejects.toThrow('fetch failed'); - }); - test('synchronous cleanup kills node before test runner exits', async () => { - const { cleanup, url } = await launchNode({ loggingEnabled: false }); - killedNode.url = url; - cleanup(); - }); -}); diff --git a/packages/account/src/test-utils/launchNode.test.ts b/packages/account/src/test-utils/launchNode.test.ts index 83530780948..52b59d2a69b 100644 --- a/packages/account/src/test-utils/launchNode.test.ts +++ b/packages/account/src/test-utils/launchNode.test.ts @@ -46,6 +46,29 @@ describe('launchNode', () => { await waitUntilUnreachable(url); }); + /** + * Spawning the child process in a detached state + * Results in the OS assigning a process group to the child. + * Combining that with `process.kill(-pid)`, + * which sends a "kill process group" signal to the OS, + * ensures that the node will be killed. + */ + it('spawns the fuel-core node in a detached state and kills the process group on cleanup', async () => { + const spawnSpy = vi.spyOn(childProcessMod, 'spawn'); + const killSpy = vi.spyOn(process, 'kill'); + + const { cleanup, pid } = await launchNode(); + + const spawnOptions = spawnSpy.mock.calls[0][2]; + expect(spawnOptions.detached).toBeTruthy(); + + cleanup(); + + expect(killSpy).toHaveBeenCalledTimes(1); + // adding a minus prefix kills the process group + expect(killSpy).toHaveBeenCalledWith(-pid); + }); + test('should start `fuel-core` node using system binary', async () => { const spawnSpy = vi.spyOn(childProcessMod, 'spawn');