From f10bc4ee4faf55915ee83b59729dfc2ded50a284 Mon Sep 17 00:00:00 2001 From: mei23 Date: Fri, 4 Jun 2021 22:05:23 +0900 Subject: [PATCH] Wait shutdown in test --- test/fetch-resource.ts | 6 +++--- test/utils.ts | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/test/fetch-resource.ts b/test/fetch-resource.ts index 18626b9530..d9fe7aea06 100644 --- a/test/fetch-resource.ts +++ b/test/fetch-resource.ts @@ -12,7 +12,7 @@ process.env.NODE_ENV = 'test'; import * as assert from 'assert'; import * as childProcess from 'child_process'; -import { async, launchServer, signup, post, api, simpleGet } from './utils'; +import { async, launchServer, signup, post, api, simpleGet, shutdownServer } from './utils'; // Request Accept const ONLY_AP = 'application/activity+json'; @@ -38,8 +38,8 @@ describe('Fetch resource', () => { }); })); - after(() => { - p.kill(); + after(async () => { + await shutdownServer(p); }); describe('Common', () => { diff --git a/test/utils.ts b/test/utils.ts index 5ae287d6e8..85c40e02f4 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -2,6 +2,7 @@ import * as childProcess from 'child_process'; import fetch from 'node-fetch'; import * as http from 'http'; import loadConfig from '../src/config/load'; +import { SIGKILL } from 'constants'; const port = loadConfig().port; @@ -26,6 +27,22 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce }; } +export function shutdownServer(p: childProcess.ChildProcess, timeout = 20 * 1000) { + return new Promise((res, rej) => { + const t = setTimeout(() => { + p.kill(SIGKILL); + res('force exit'); + }, timeout); + + p.once('exit', () => { + clearTimeout(t); + res('exited'); + }); + + p.kill(); + }); +} + export const api = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => { const auth = me ? { i: me.token