From 5d66bb879464adb2f23d764a3602f967748069d0 Mon Sep 17 00:00:00 2001
From: MeiMei <30769358+mei23@users.noreply.github.com>
Date: Sat, 5 Jun 2021 14:54:07 +0900
Subject: [PATCH] =?UTF-8?q?=E3=83=A9=E3=83=B3=E3=83=80=E3=83=A0=E3=81=AB?=
 =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=81=8C=E3=82=B3=E3=82=B1=E3=82=8B?=
 =?UTF-8?q?=E3=81=AE=E3=82=92=E4=BF=AE=E6=AD=A3=20(#7553)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Test shutdown

* Revert "Test shutdown"

This reverts commit 85182e7dd196cdd9ecb46cfb50adaabd04c5ba60.

* Skip beforeShutdown in test

* Wait shutdown in test

* Revert "Skip beforeShutdown in test"

This reverts commit 79c33ab53615e8fa4820d2abfc2494cba55c441c.

* Revert "Revert "Skip beforeShutdown in test""

This reverts commit 3423133a137c79b64f3ff6ef9dbe433a441a47b0.
---
 src/misc/before-shutdown.ts |  2 ++
 test/api-visibility.ts      |  6 +++---
 test/fetch-resource.ts      |  6 +++---
 test/mute.ts                |  6 +++---
 test/note.ts                |  6 +++---
 test/user-notes.ts          |  6 +++---
 test/utils.ts               | 17 +++++++++++++++++
 7 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/src/misc/before-shutdown.ts b/src/misc/before-shutdown.ts
index 8639d42b04..33abf5fb4d 100644
--- a/src/misc/before-shutdown.ts
+++ b/src/misc/before-shutdown.ts
@@ -56,6 +56,8 @@ const forceExitAfter = timeout => () => {
  * @param {string} signalOrEvent The exit signal or event name received on the process.
  */
 async function shutdownHandler(signalOrEvent) {
+	if (process.env.NODE_ENV === 'test') return process.exit(0);
+
 	console.warn(`Shutting down: received [${signalOrEvent}] signal`);
 
 	for (const listener of shutdownListeners) {
diff --git a/test/api-visibility.ts b/test/api-visibility.ts
index 5fbea02df6..c4ec321938 100644
--- a/test/api-visibility.ts
+++ b/test/api-visibility.ts
@@ -12,15 +12,15 @@ process.env.NODE_ENV = 'test';
 
 import * as assert from 'assert';
 import * as childProcess from 'child_process';
-import { async, signup, request, post, launchServer } from './utils';
+import { async, signup, request, post, launchServer, shutdownServer } from './utils';
 
 describe('API visibility', () => {
 	let p: childProcess.ChildProcess;
 
 	before(launchServer(g => p = g));
 
-	after(() => {
-		p.kill();
+	after(async () => {
+		await shutdownServer(p);
 	});
 
 	describe('Note visibility', async () => {
diff --git a/test/fetch-resource.ts b/test/fetch-resource.ts
index 79a5519ddb..e9d10d1ab3 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, request, simpleGet, port } from './utils';
+import { async, launchServer, signup, post, request, simpleGet, port, shutdownServer } from './utils';
 import * as openapi from '@redocly/openapi-core';
 
 // Request Accept
@@ -39,8 +39,8 @@ describe('Fetch resource', () => {
 		});
 	}));
 
-	after(() => {
-		p.kill();
+	after(async () => {
+		await shutdownServer(p);
 	});
 
 	describe('Common', () => {
diff --git a/test/mute.ts b/test/mute.ts
index 37b4a23048..38911b6e16 100644
--- a/test/mute.ts
+++ b/test/mute.ts
@@ -12,7 +12,7 @@ process.env.NODE_ENV = 'test';
 
 import * as assert from 'assert';
 import * as childProcess from 'child_process';
-import { async, signup, request, post, react, connectStream, launchServer } from './utils';
+import { async, signup, request, post, react, connectStream, launchServer, shutdownServer } from './utils';
 
 describe('Mute', () => {
 	let p: childProcess.ChildProcess;
@@ -28,8 +28,8 @@ describe('Mute', () => {
 		carol = await signup({ username: 'carol' });
 	}));
 
-	after(() => {
-		p.kill();
+	after(async () => {
+		await shutdownServer(p);
 	});
 
 	it('ミュート作成', async(async () => {
diff --git a/test/note.ts b/test/note.ts
index 70ebecff1c..3f17005771 100644
--- a/test/note.ts
+++ b/test/note.ts
@@ -12,7 +12,7 @@ process.env.NODE_ENV = 'test';
 
 import * as assert from 'assert';
 import * as childProcess from 'child_process';
-import { async, signup, request, post, uploadFile, launchServer } from './utils';
+import { async, signup, request, post, uploadFile, launchServer, shutdownServer } from './utils';
 import { Note } from '../src/models/entities/note';
 import { initDb } from '../src/db/postgre';
 
@@ -30,8 +30,8 @@ describe('Note', () => {
 		bob = await signup({ username: 'bob' });
 	}));
 
-	after(() => {
-		p.kill();
+	after(async () => {
+		await shutdownServer(p);
 	});
 
 	it('投稿できる', async(async () => {
diff --git a/test/user-notes.ts b/test/user-notes.ts
index 349d5fdb7c..4af8ce0cc7 100644
--- a/test/user-notes.ts
+++ b/test/user-notes.ts
@@ -12,7 +12,7 @@ process.env.NODE_ENV = 'test';
 
 import * as assert from 'assert';
 import * as childProcess from 'child_process';
-import { async, signup, request, post, uploadFile, launchServer } from './utils';
+import { async, signup, request, post, uploadFile, launchServer, shutdownServer } from './utils';
 
 describe('users/notes', () => {
 	let p: childProcess.ChildProcess;
@@ -37,8 +37,8 @@ describe('users/notes', () => {
 		});
 	}));
 
-	after(() => {
-		p.kill();
+	after(async() => {
+		await shutdownServer(p);
 	});
 
 	it('ファイルタイプ指定 (jpg)', async(async () => {
diff --git a/test/utils.ts b/test/utils.ts
index 193017e265..e4c96d0e15 100644
--- a/test/utils.ts
+++ b/test/utils.ts
@@ -5,6 +5,7 @@ const FormData = require('form-data');
 import * as childProcess from 'child_process';
 import * as http from 'http';
 import loadConfig from '../src/config/load';
+import { SIGKILL } from 'constants';
 
 export const port = loadConfig().port;
 
@@ -145,3 +146,19 @@ 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();
+	});
+}
-- 
GitLab