Skip to content

Commit

Permalink
Improve force close socket test reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
chill117 committed Apr 18, 2024
1 parent b2f9589 commit 7634b62
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions test/unit/lib/Server/close.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
const assert = require('assert');
const http = require('http');
const url = require('url');

describe('close([options])', function() {

let server;
beforeEach(function() {
server = this.helpers.createServer();
return server.onReady();
});

afterEach(function() {
if (server) return server.close();
});
describe.only('close([options])', function() {

describe('force', function() {

let server;
beforeEach(function() {
const numSockets = 3;
return Promise.all(Array.from(Array(numSockets)).map(() => {
return new Promise((resolve, reject) => {
const { hostname, port, path } = url.parse(`http://${server.options.host}:${server.options.port}/status`);
const req = http.request({
agent: new http.Agent({
keepAlive: true,
// Infinity is read as 50 sockets:
maxSockets: Infinity
}),
method: 'GET',
hostname,
port,
path,
}, () => resolve());
req.once('error', reject);
req.end();
});
}));
server = this.helpers.createServer();
return server.onReady();
});

beforeEach(function() {
server.bindToHook('status', (req, res, next) => {
// Do nothing so that the server does not automatically close the socket.
});
Array.from(Array(3)).forEach(() => {
const req = http.get(server.getUrl('/status'), () => {});
req.on('error', () => {});
});
return new Promise((resolve, reject) => {
setTimeout(resolve, 50);
});
});

describe('true', function() {
Expand All @@ -60,16 +46,26 @@ describe('close([options])', function() {

it('does not force-close all sockets', function() {
return server.close({ force: false }).then(() => {
let atleastOneSocketNotClosed = false;
Object.entries(server.sockets).forEach(([id, socket], index) => {
assert.notStrictEqual(socket, null);
if (socket) {
atleastOneSocketNotClosed = true;
}
});
assert.ok(atleastOneSocketNotClosed);
});
});
});
});

describe('store', function() {

let server;
beforeEach(function() {
server = this.helpers.createServer();
return server.onReady();
});

describe('true', function() {

it('closes the data store', function() {
Expand All @@ -81,10 +77,6 @@ describe('close([options])', function() {

describe('false', function() {

afterEach(function() {
return server.store.close();
});

it('does not close the data store', function() {
return server.close({ store: false }).then(() => {
assert.notStrictEqual(server.store, null);
Expand Down

0 comments on commit 7634b62

Please sign in to comment.