Skip to content

Commit

Permalink
remove side effects on 'error' and 'exit' effects
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Jul 9, 2024
1 parent fcb88b0 commit 1babf70
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
61 changes: 59 additions & 2 deletions packages/account/src/test-utils/launchNode.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ErrorCode } from '@fuel-ts/errors';
import { safeExec, expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import { defaultSnapshotConfigs } from '@fuel-ts/utils';
import { defaultSnapshotConfigs, sleep } from '@fuel-ts/utils';
import { waitUntilUnreachable } from '@fuel-ts/utils/test-utils';
import * as childProcessMod from 'child_process';
import * as fsMod from 'fs';

import { Provider } from '../providers';

Expand All @@ -16,6 +17,14 @@ vi.mock('child_process', async () => {
};
});

vi.mock('fs', async () => {
const mod = await vi.importActual('fs');
return {
__esModule: true,
...mod,
};
});

/**
* @group node
*/
Expand Down Expand Up @@ -128,10 +137,58 @@ describe('launchNode', () => {
cleanup();
});

test('cleanup removes temporary directory', async () => {
const mkdirSyncSpy = vi.spyOn(fsMod, 'mkdirSync');
const { cleanup } = await launchNode();

expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);
const tempDirPath = mkdirSyncSpy.mock.calls[0][0];
cleanup();

// wait until cleanup finishes (done via events)
await sleep(1500);
expect(fsMod.existsSync(tempDirPath)).toBeFalsy();
});

test('temporary directory gets removed on error', async () => {
const mkdirSyncSpy = vi.spyOn(fsMod, 'mkdirSync');

const invalidCoin = {
asset_id: 'whatever',
tx_id: '',
output_index: 0,
tx_pointer_block_height: 0,
tx_pointer_tx_idx: 0,
owner: '',
amount: 0,
};

const { error } = await safeExec(async () =>
launchNode({
loggingEnabled: false,
snapshotConfig: {
...defaultSnapshotConfigs,
stateConfig: {
coins: [invalidCoin],
messages: [],
},
},
})
);
expect(error).toBeDefined();

expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);
const tempDirPath = mkdirSyncSpy.mock.calls[0][0];

// wait until cleanup finishes (done via events)
await sleep(1500);
expect(fsMod.existsSync(tempDirPath)).toBeFalsy();
});

test('calling cleanup multiple times does not retry process killing', async () => {
const killSpy = vi.spyOn(process, 'kill');

const { cleanup } = await launchNode();
const { cleanup } = await launchNode({ loggingEnabled: false });

cleanup();

Expand Down
20 changes: 11 additions & 9 deletions packages/account/src/test-utils/launchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ export const launchNode = async ({
});
}

const removeSideffects = () => {
child.stderr.removeAllListeners();

if (existsSync(tempDir)) {
rmSync(tempDir, { recursive: true });
}
};

child.on('error', removeSideffects);
child.on('exit', removeSideffects);

const childState = {
isDead: false,
};
Expand All @@ -222,15 +233,6 @@ export const launchNode = async ({
return;
}
childState.isDead = true;

// Remove all the listeners we've added.
child.stderr.removeAllListeners();

// Remove the temporary folder and all its contents.
if (existsSync(tempDir)) {
rmSync(tempDir, { recursive: true });
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
process.kill(-child.pid!);
};
Expand Down

0 comments on commit 1babf70

Please sign in to comment.