Skip to content

Commit

Permalink
Restore test that runs a separate process
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed Jul 19, 2021
1 parent 9f9da89 commit f3d2975
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
41 changes: 21 additions & 20 deletions packages/apollo-server/src/__tests__/stoppable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const a: any = require('awaiting');
const request: any = require('requisition');
import fs from 'fs';
import { Stopper } from '../stoppable';
// import child from 'child_process'; FIXME
import child from 'child_process';
import path from 'path';
import { AddressInfo } from 'net';

Expand Down Expand Up @@ -253,24 +253,25 @@ Object.keys(schemes).forEach((schemeName) => {
expect(gracefully).toBe(true);
});

// FIXME restore
// describe('with in-flights finishing before grace period ends', function () {
// if (schemeName !== 'http') {
// return;
// }

// it('exits immediately', async () => {
// const file = path.join(__dirname, 'server.js');
// const server = child.spawn('node', [file, '500']);
// await a.event(server.stdout, 'data');
// const start = Date.now();
// const res = await request(
// `${schemeName}://localhost:${p}/250`,
// ).agent(scheme.agent({ keepAlive: true }));
// const body = await res.text();
// assert.equal(body, 'helloworld');
// assert.closeTo(Date.now() - start, 250, 100);
// });
// });
if (schemeName === 'http') {
it('with in-flights finishing before grace period ends', async () => {
const file = path.join(__dirname, 'stoppable', 'server.js');
const server = child.spawn('node', [file, '500']);
const [data] = await a.event(server.stdout, 'data');
const port = +data.toString();
expect(typeof port).toBe('number');
const start = Date.now();
const res = await request(
`${schemeName}://localhost:${port}/250`,
).agent(scheme.agent({ keepAlive: true }));
const body = await res.text();
expect(body).toBe('helloworld');
const elapsed = Date.now() - start;
expect(elapsed).toBeGreaterThanOrEqual(150);
expect(elapsed).toBeLessThanOrEqual(350);
// Wait for subprocess to go away.
await a.event(server, 'close');
});
}
});
});
23 changes: 12 additions & 11 deletions packages/apollo-server/src/__tests__/stoppable/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const http = require('http')
const stoppable = require('..')
const http = require('http');
const { Stopper } = require('../../../dist/stoppable.js');

const grace = Number(process.argv[2] || Infinity)
const grace = Number(process.argv[2] || Infinity);
let stopper;
const server = http.createServer((req, res) => {
const delay = parseInt(req.url.slice(1), 10)
res.writeHead(200)
res.write('hello')
setTimeout(() => res.end('world'), delay)
server.stop()
})
stoppable(server, grace)
server.listen(8000, () => console.log('listening'))
const delay = parseInt(req.url.slice(1), 10);
res.writeHead(200);
res.write('hello');
setTimeout(() => res.end('world'), delay);
stopper.stop(grace);
});
stopper = new Stopper(server);
server.listen(0, () => console.log(server.address().port));

0 comments on commit f3d2975

Please sign in to comment.