-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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 dynamic port in test-cluster-dgram-reuse #12426
Conversation
Thanks Refs: #12376 I'm wondering, are both sockets guaranteed to be created on the same port in this case? |
@benjamingr Great question. I wonder if there's a way to dynamically get a port from the OS and assign it to a |
What you can do and what should work is creating the first socket, then using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should do what @addaleax suggested then.
@addaleax What do you think of this? // Creates the first socket
const socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
// Let the OS assign a random port using '0'
socket.bind(0, next);
// Creates the second socket using the same port from the previous one
dgram.createSocket({ type: 'udp4', reuseAddr: true }).bind(socket.address().port, next); I'm sure I am missing something here. Could you shed me some light? The test is failing with:
|
@rafaelfragosom I'm not sure port is available immediately, you might need to change it to something like:
Although I admit I never tested this. Edit: tested, this appears to be the case, you can create a third socket if you'd like. Here's a utility: function getPort(cb) {
const s = dgram.createSocket({ type: 'udp4', reuseAddr: true }).bind(0, () => {
cb(null, s.address().port);
});
} You can do that to get the port and then keep the test (inside the callback) as it was just changing |
@benjamingr It does work. I'm sending the commit. |
technically the test is now slightly different, but it tests the same thing so I'm going to LGTM. We need to give it 72 hours anyway so if other people feel differently they can speak up :) |
@benjamingr Thank you! This is my first contribution to a big project like node and it's been good so far. I'll get better at it. |
You're doing great :) Thanks for taking the time to contribute. |
d083e8e
to
627fd01
Compare
// Creates the first socket | ||
const socket = dgram.createSocket({ type: 'udp4', reuseAddr: true }); | ||
// Let the OS assign a random port to the first socket | ||
socket.bind(0, function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add common.mustCall()
to the callback here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I'm still getting used to the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cjihrig done.
Remove common.PORT from test-cluster-dgram-reuse to eliminate possibility that a dynamic port used in another test will collide with common.PORT. Refs: nodejs#12376
…euse Using the bind() callback to get the dynamically assigned port from the first socket and assigning it to the next cluster.
Fixes the wordwrap error of my last commit based on the guidelines
Adding the common.mustCall() to the callback as the test documentation explains it.
627fd01
to
bab6ccc
Compare
I wonder if after the proposed changes, the |
@cjihrig Any updates on your request? |
@benjamingr I'm gonna update this test with your suggestion. I was affraid of doing something like this, but since you suggested, I'm gonna do it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I'm not sure if the indentation on the second bind()
is right or not though.
Ops, I just landed #12901 which was a dupe of this. |
Closing this, sorry @rafaelfragosom. |
Use of common.PORT in parallel tests is not completely safe (because
the same port can be previously assigned to another test running in
parallel if that test uses port 0 to get an arbitrary available port).
Remove common.PORT from test-cluster-dgram-reuse.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
test cluster