Skip to content

Commit

Permalink
test: ensure that sockets are removed from internal set (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jan 29, 2020
1 parent cfc73ae commit 28b910e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/factories/createInternalHttpTerminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import http from 'http';
import delay from 'delay';
import type {
HttpTerminatorType,
HttpTerminatorConfigurationInputType,
InternalHttpTerminatorType,
} from '../types';
import Logger from '../Logger';

Expand All @@ -16,7 +16,7 @@ const configurationDefaults = {
gracefulTerminationTimeout: 1000,
};

export default (configurationInput: HttpTerminatorConfigurationInputType): HttpTerminatorType => {
export default (configurationInput: HttpTerminatorConfigurationInputType): InternalHttpTerminatorType => {
const configuration = {
...configurationDefaults,
...configurationInput,
Expand Down Expand Up @@ -91,6 +91,7 @@ export default (configurationInput: HttpTerminatorConfigurationInputType): HttpT
continue;
}

// $FlowFixMe
const serverResponse = socket._httpMessage;

if (serverResponse) {
Expand All @@ -105,6 +106,7 @@ export default (configurationInput: HttpTerminatorConfigurationInputType): HttpT
}

for (const socket of secureSockets) {
// $FlowFixMe
const serverResponse = socket._httpMessage;

if (serverResponse) {
Expand Down Expand Up @@ -138,6 +140,8 @@ export default (configurationInput: HttpTerminatorConfigurationInputType): HttpT
};

return {
secureSockets,
sockets,
terminate,
};
};
9 changes: 9 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// @flow

import {
Socket,
} from 'net';
import type {
Server as HttpServer,
} from 'http';
Expand All @@ -22,3 +25,9 @@ export type HttpTerminatorConfigurationInputType = {|
export type HttpTerminatorType = {|
+terminate: () => Promise<void>,
|};

export type InternalHttpTerminatorType = {|
...HttpTerminatorType,
secureSockets: Set<Socket>,
sockets: Set<Socket>,
|};
23 changes: 23 additions & 0 deletions test/http-terminator/factories/createInternalHttpTerminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,26 @@ test('ongoing requests receive {connection: close} header (new request reusing a
t.is(response1.headers.connection, 'close');
t.is(response1.body, 'baz');
});

test('empties internal socket collection', async (t) => {
// eslint-disable-next-line ava/use-t-well
t.timeout(500);

const httpServer = await createHttpServer((incomingMessage, outgoingMessage) => {
outgoingMessage.end('foo');
});

const terminator = createInternalHttpTerminator({
gracefulTerminationTimeout: 150,
server: httpServer.server,
});

await got(httpServer.url);

await delay(50);

t.is(terminator.sockets.size, 0);
t.is(terminator.secureSockets.size, 0);

await terminator.terminate();
});

0 comments on commit 28b910e

Please sign in to comment.