Skip to content

Commit

Permalink
net: rework test approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Dec 14, 2023
1 parent 8adfe2a commit 018c36a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ const { createMockedLookup } = require('../common/dns');
const assert = require('assert');
const { createConnection } = require('net');

// Test that all events are emitted when trying a single IP (which means autoselectfamily is bypassed)
//
// When testing this is MacOS, remember that the last connection will have no timeout at Node.js
// level but only at operating system one.
//
// The default for MacOS is 75 seconds. It can be changed by doing:
//
// sudo sysctl net.inet.tcp.keepinit=VALUE_IN_MS
//

// Test that all failure events are emitted when trying a single IP (which means autoselectfamily is bypassed)
{
const connection = createConnection({
host: 'example.org',
Expand All @@ -28,7 +37,10 @@ const { createConnection } = require('net');
assert.strictEqual(port, 10);
assert.strictEqual(family, 4);

assert.ok(error.code.match(/ECONNREFUSED|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/));
assert.ok(
error.code.match(/ECONNREFUSED|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/),
`Received unexpected error code ${error.code}`,
);
}));

connection.on('ready', common.mustNotCall());
Expand Down Expand Up @@ -67,36 +79,12 @@ const { createConnection } = require('net');
assert.strictEqual(port, expected.port);
assert.strictEqual(family, expected.family);

assert.ok(error.code.match(/ECONNREFUSED|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/), `Received unexpected error code ${error.code}`);
assert.ok(
error.code.match(/ECONNREFUSED|ENETUNREACH|EHOSTUNREACH|ETIMEDOUT/),
`Received unexpected error code ${error.code}`,
);
}, 2));

connection.on('ready', common.mustNotCall());
connection.on('error', common.mustCall());
}

// Test that if a connection attempt times out and the socket is destroyed before the
// next attempt starts then the process does not crash
{
const connection = createConnection({
host: 'example.org',
port: 443,
lookup: createMockedLookup(INET4_IP, INET6_IP),
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 10,
});

// Depending on the network, it might be impossible to obtain a timeout in 10ms,
// which is the minimum value allowed by network family autoselection.
// At the time of writing (Dec 2023), the network times out on local machine and in the Node CI,
// but it does not on GitHub actions runner.
// Therefore, we allow both a timeout or a successfull connections as pass.

connection.on('connectionAttemptTimeout', (address, port, family) => {
assert.strictEqual(address, INET4_IP);
assert.strictEqual(port, 443);
assert.strictEqual(family, 4);
connection.destroy();
});

connection.on('ready', () => connection.destroy());
}
49 changes: 49 additions & 0 deletions test/internet/test-net-autoselectfamily-events-timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const common = require('../common');
const { addresses: { INET6_IP, INET4_IP } } = require('../common/internet');
const { createMockedLookup } = require('../common/dns');

const assert = require('assert');
const { createConnection } = require('net');

//
// When testing this is MacOS, remember that the last connection will have no timeout at Node.js
// level but only at operating system one.
//
// The default for MacOS is 75 seconds. It can be changed by doing:
//
// sudo sysctl net.inet.tcp.keepinit=VALUE_IN_MS
//

// Test that if a connection attempt times out and the socket is destroyed before the
// next attempt starts then the process does not crash
{
const connection = createConnection({
host: 'example.org',
port: 443,
lookup: createMockedLookup(INET4_IP, INET6_IP),
autoSelectFamily: true,
autoSelectFamilyAttemptTimeout: 10,
});

const pass = common.mustCall();

connection.on('connectionAttemptTimeout', (address, port, family) => {
assert.strictEqual(address, INET4_IP);
assert.strictEqual(port, 443);
assert.strictEqual(family, 4);
connection.destroy();
pass()

Check failure on line 37 in test/internet/test-net-autoselectfamily-events-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
});

// Depending on the network, it might be impossible to obtain a timeout in 10ms,
// which is the minimum value allowed by network family autoselection.
// At the time of writing (Dec 2023), the network times out on local machine and in the Node CI,
// but it does not on GitHub actions runner.
// Therefore, after five seconds we just consider this test as passed.
setTimeout(() => {
pass()

Check failure on line 46 in test/internet/test-net-autoselectfamily-events-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
process.exit(0)

Check failure on line 47 in test/internet/test-net-autoselectfamily-events-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
}, 5000).unref()

Check failure on line 48 in test/internet/test-net-autoselectfamily-events-timeout.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Missing semicolon
}

0 comments on commit 018c36a

Please sign in to comment.