Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Send a request with keep-alive
const req = httpGetAsync(server.url, agent);
// Wait until the request is accepted by the server
await pEvent(server.server, 'request');
// Stop the server
const stop = server.stop();
// Now notify the request to finish in next cycle with setImmediate
setImmediate(() => {
emitter.emit('finish');
});
// The in-flight task can finish before the grace period
await req;
// Wait until the server is stopped
await stop;
// No more new connections are accepted
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
});
it('stops server with grace period and inflight request', async () => {
const serverOptions = givenHttpServerConfig() as HttpServerOptions;
serverOptions.gracePeriodForClose = 1000;
const {emitter, deferredRequestHandler} = createDeferredRequestHandler();
server = new HttpServer(deferredRequestHandler, serverOptions);
await server.start();
const agent = new Agent({keepAlive: true});
// Send a request with keep-alive
const req = httpGetAsync(server.url, agent);
// Wait until the request is accepted by the server
await pEvent(server.server, 'request');
// Stop the server
const stop = server.stop();
// Now notify the request to finish in next cycle with setImmediate
setImmediate(() => {
emitter.emit('finish');
});
// The in-flight task can finish before the grace period
await req;
// Wait until the server is stopped
await stop;
// No more new connections are accepted
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
});
it('stops server with shorter grace period and inflight request', async () => {
const serverOptions = givenHttpServerConfig() as HttpServerOptions;
serverOptions.gracePeriodForClose = 10;
const {deferredRequestHandler} = createDeferredRequestHandler();
server = new HttpServer(deferredRequestHandler, serverOptions);
await server.start();
const agent = new Agent({keepAlive: true});
// Send a request with keep-alive
const req = httpGetAsync(server.url, agent);
// Wait until the request is accepted by the server
await pEvent(server.server, 'request');
// Stop the server
await server.stop();
// No more new connections are accepted
await expect(httpGetAsync(server.url)).to.be.rejectedWith(/ECONNREFUSED/);
// We never send `finish` to the pending request
// The inflight request is aborted as it takes longer than the grace period
await expect(req).to.be.rejectedWith(/socket hang up/);
});
skipOnTravis(it, 'supports HTTP over IPv6', async () => {
server = new HttpServer(dummyRequestHandler, {host: '::1'});
await server.start();
expect(getAddressFamily(server)).to.equal('IPv6');
const response = await httpGetAsync(server.url);
expect(response.statusCode).to.equal(200);
});