サーバー起動処理を共通化

This commit is contained in:
rinsuki 2019-10-25 06:24:06 +09:00
parent 6e16c9389f
commit cc7cc56abe
6 changed files with 47 additions and 85 deletions

View File

@ -16,20 +16,12 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert'; import * as assert from 'assert';
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import { async, signup, request, post } from './utils'; import { async, signup, request, post, launchServer } from './utils';
describe('API visibility', () => { describe('API visibility', () => {
let p: childProcess.ChildProcess; let p: childProcess.ChildProcess;
before(done => { before(launchServer(g => p = g));
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
p.on('message', message => {
if (message === 'ok') done();
});
});
after(() => { after(() => {
p.kill(); p.kill();

View File

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert'; import * as assert from 'assert';
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import { async, signup, request, post, react, connectStream } from './utils'; import { async, signup, request, post, react, connectStream, launchServer } from './utils';
describe('Mute', () => { describe('Mute', () => {
let p: childProcess.ChildProcess; let p: childProcess.ChildProcess;
@ -26,21 +26,11 @@ describe('Mute', () => {
let bob: any; let bob: any;
let carol: any; let carol: any;
before(done => { before(launchServer(g => p = g, async () => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], { alice = await signup({ username: 'alice' });
stdio: ['inherit', 'inherit', 'ipc'], bob = await signup({ username: 'bob' });
env: { NODE_ENV: 'test', PATH: process.env.PATH } carol = await signup({ username: 'carol' });
}); }));
p.on('message', async message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
carol = await signup({ username: 'carol' });
done();
}
});
});
after(() => { after(() => {
p.kill(); p.kill();

View File

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert'; import * as assert from 'assert';
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import { async, signup, request, post, uploadFile } from './utils'; import { async, signup, request, post, uploadFile, launchServer } from './utils';
import { Note } from '../src/models/entities/note'; import { Note } from '../src/models/entities/note';
import { initDb } from '../src/db/postgre'; import { initDb } from '../src/db/postgre';
@ -27,23 +27,12 @@ describe('Note', () => {
let alice: any; let alice: any;
let bob: any; let bob: any;
before(done => { before(launchServer(g => p = g, async () => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], { const connection = await initDb(true);
stdio: ['inherit', 'inherit', 'ipc'], Notes = connection.getRepository(Note);
env: { NODE_ENV: 'test', PATH: process.env.PATH } alice = await signup({ username: 'alice' });
}); bob = await signup({ username: 'bob' });
p.on('message', message => { }));
if (message === 'ok') {
(p.channel as any).onread = () => {};
initDb(true).then(async connection => {
Notes = connection.getRepository(Note);
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
done();
});
}
});
});
after(() => { after(() => {
p.kill(); p.kill();

View File

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert'; import * as assert from 'assert';
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import { connectStream, signup, request, post } from './utils'; import { connectStream, signup, request, post, launchServer } from './utils';
import { Following } from '../built/models/entities/following'; import { Following } from '../built/models/entities/following';
const initDb = require('../built/db/postgre.js').initDb; const initDb = require('../built/db/postgre.js').initDb;
@ -24,21 +24,10 @@ describe('Streaming', () => {
let p: childProcess.ChildProcess; let p: childProcess.ChildProcess;
let Followings: any; let Followings: any;
beforeEach(done => { beforeEach(launchServer(g => p = g, async () => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], { const connection = await initDb(true);
stdio: ['inherit', 'inherit', 'ipc'], Followings = connection.getRepository(Following);
env: { NODE_ENV: 'test', PATH: process.env.PATH } }));
});
p.on('message', message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
initDb(true).then(async (connection: any) => {
Followings = connection.getRepository(Following);
done();
});
}
});
});
afterEach(() => { afterEach(() => {
p.kill(); p.kill();

View File

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert'; import * as assert from 'assert';
import * as childProcess from 'child_process'; import * as childProcess from 'child_process';
import { async, signup, request, post, uploadFile } from './utils'; import { async, signup, request, post, uploadFile, launchServer } from './utils';
describe('users/notes', () => { describe('users/notes', () => {
let p: childProcess.ChildProcess; let p: childProcess.ChildProcess;
@ -26,32 +26,20 @@ describe('users/notes', () => {
let pngNote: any; let pngNote: any;
let jpgPngNote: any; let jpgPngNote: any;
before(done => { before(launchServer(g => p = g, async () => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], { alice = await signup({ username: 'alice' });
stdio: ['inherit', 'inherit', 'ipc'], const jpg = await uploadFile(alice, __dirname + '/resources/Lenna.jpg');
env: { NODE_ENV: 'test', PATH: process.env.PATH } const png = await uploadFile(alice, __dirname + '/resources/Lenna.png');
jpgNote = await post(alice, {
fileIds: [jpg.id]
}); });
p.on('message', async message => { pngNote = await post(alice, {
if (message === 'ok') { fileIds: [png.id]
(p.channel as any).onread = () => {};
alice = await signup({ username: 'alice' });
const jpg = await uploadFile(alice, __dirname + '/resources/Lenna.jpg');
const png = await uploadFile(alice, __dirname + '/resources/Lenna.png');
jpgNote = await post(alice, {
fileIds: [jpg.id]
});
pngNote = await post(alice, {
fileIds: [png.id]
});
jpgPngNote = await post(alice, {
fileIds: [jpg.id, png.id]
});
done();
}
}); });
}); jpgPngNote = await post(alice, {
fileIds: [jpg.id, png.id]
});
}));
after(() => { after(() => {
p.kill(); p.kill();

View File

@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as WebSocket from 'ws'; import * as WebSocket from 'ws';
const fetch = require('node-fetch'); const fetch = require('node-fetch');
import * as req from 'request'; import * as req from 'request';
import * as childProcess from 'child_process';
export const async = (fn: Function) => (done: Function) => { export const async = (fn: Function) => (done: Function) => {
fn().then(() => { fn().then(() => {
@ -102,3 +103,16 @@ export function connectStream(user: any, channel: string, listener: (message: Re
}); });
}); });
} }
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
return (done: (err?: Error) => any) => {
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
callbackSpawnedProcess(p)
p.on('message', message => {
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e));
});
};
}