Skip to content

Commit

Permalink
Wait shutdown in test
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 committed Jun 4, 2021
1 parent 80d7f26 commit f10bc4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions test/fetch-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -38,8 +38,8 @@ describe('Fetch resource', () => {
});
}));

after(() => {
p.kill();
after(async () => {
await shutdownServer(p);
});

describe('Common', () => {
Expand Down
17 changes: 17 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit f10bc4e

Please sign in to comment.