Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use common.mustCall in test-tls-connect-given-socket.js #12592

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions test/parallel/test-tls-connect-given-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,20 @@ const tls = require('tls');
const net = require('net');
const fs = require('fs');
const path = require('path');

let serverConnected = 0;
let clientConnected = 0;

const options = {
key: fs.readFileSync(path.join(common.fixturesDir, 'test_key.pem')),
cert: fs.readFileSync(path.join(common.fixturesDir, 'test_cert.pem'))
};

const server = tls.createServer(options, (socket) => {
serverConnected++;
const server = tls.createServer(options, common.mustCall((socket) => {
socket.end('Hello');
}).listen(0, () => {
}, 2)).listen(0, common.mustCall(() => {
let waiting = 2;
function establish(socket) {
function establish(socket, calls) {
const client = tls.connect({
rejectUnauthorized: false,
socket: socket
}, () => {
clientConnected++;
}, common.mustCall(() => {
let data = '';
client.on('data', common.mustCall((chunk) => {
data += chunk.toString();
Expand All @@ -61,7 +55,7 @@ const server = tls.createServer(options, (socket) => {
if (--waiting === 0)
server.close();
}));
});
}, calls));
assert(client.readable);
assert(client.writable);

Expand All @@ -72,14 +66,14 @@ const server = tls.createServer(options, (socket) => {

// Immediate death socket
const immediateDeath = net.connect(port);
establish(immediateDeath).destroy();
establish(immediateDeath, 0).destroy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to pass 0 to common.mustCall() as the expected argument. I would expect any common.mustCall() to be passed a value of 1 (or undefined) or higher. If the intention is that it should never be called, common.mustNotCall() should be used instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined will result in the default of 1. I think either we need to be OK with sending 0 to common.mustCall() (I am, at least in this case) or else add some logic to switch out whether common.mustCall() or common.mustNotCall() gets called. That works too, although might be a bit much. I'm fine with either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one was a bit of let's get it working. establish() is called multiple times with different expected results each time and this was the easiest to get things going. The test as written, however, could use a bit of a bigger refactoring.


// Outliving
const outlivingTCP = net.connect(port, common.mustCall(() => {
outlivingTLS.destroy();
next();
}));
const outlivingTLS = establish(outlivingTCP);
const outlivingTLS = establish(outlivingTCP, 0);

function next() {
// Already connected socket
Expand All @@ -91,9 +85,4 @@ const server = tls.createServer(options, (socket) => {
const connecting = net.connect(port);
establish(connecting);
}
});

process.on('exit', () => {
assert.strictEqual(serverConnected, 2);
assert.strictEqual(clientConnected, 2);
});
}));