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

refactor test-http-exceptions file to use countdown #17199

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions test/parallel/test-http-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@

'use strict';
require('../common');
const Countdown = require('../common/countdown');
const http = require('http');
const NUMBER_OF_EXCEPTIONS = 4;
const countdown = new Countdown(NUMBER_OF_EXCEPTIONS, () => process.exit(0));
Copy link
Member

@joyeecheung joyeecheung Nov 21, 2017

Choose a reason for hiding this comment

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

This should be wrapped in common.mustCall, also should the listener of process.on('uncaughtException') and server.listen

This kind of reminds me: should we wrap the cb in common.mustCall in the countdown module by default? @jasnell

Copy link
Member

Choose a reason for hiding this comment

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

should we wrap the cb in common.mustCall in the countdown module by default?

Yeah, I've been thinking the same thing. It would be worthwhile I think

Copy link
Member

@joyeecheung joyeecheung Nov 21, 2017

Choose a reason for hiding this comment

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

Come to think of it, maybe this test does not need the countdown module and can just wrap the process.on('uncaughtException') listener in common.mustCall(..., NUMBER_OF_EXCEPTIONS), because every expected action is the same. The countdown module seems to be more suitable for actions that vary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joyeecheung unless i am overlooking something; I didn't wrap it in a mustCall since not invoking the callback will result in a timeout and hence a failure in the test anyways, but adding it does not hurt as well.

Copy link
Member

@Trott Trott Nov 22, 2017

Choose a reason for hiding this comment

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

If you do go with this (and I'm with @joyeecheung that this maybe doesn't need Countdown): Can you please wrap process.exit(0) in { and }?

Without { and }, then the result of process.exit(0) is the return value of the function. That function shouldn't have a return value.

Copy link
Contributor Author

@Bamieh Bamieh Nov 22, 2017

Choose a reason for hiding this comment

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

@Trott process.exit() terminates the process synchronously, so the return value will not get passed back from the invocation, so it does not really matter if you pass an explicit return or not. I'll do it anyways for convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding wrapping the countdown with a mustCall, i think its a good idea to decide soon before contributors start implementing more refactors

Copy link
Member

Choose a reason for hiding this comment

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

I think in general, use common.mustCall if the code calling countdown.dec() is always the same, and use countdown if the caller would be different. That way the code is cleaner. If we are trying to make the code cleaner by using countdown, then I see no reason not to make it even cleaner with common.mustCall

Copy link
Member

Choose a reason for hiding this comment

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

Also...can we implement common.mustCall on top of countdown?

Copy link
Contributor Author

@Bamieh Bamieh Nov 22, 2017

Choose a reason for hiding this comment

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

@joyeecheung i have implemented mustCall on top of countdown, but im not sure i fully understand your suggestion about replacing countdown with mustCall

process.on('uncaughtException',
           common.mustCall(onUncaughtException, NUMBER_OF_EXCEPTIONS)
);

however how are we going to tell the code to process.exit(0)? i cant see that happening without a counter


const server = http.createServer(function(req, res) {
intentionally_not_defined(); // eslint-disable-line no-undef
Expand All @@ -30,16 +33,15 @@ const server = http.createServer(function(req, res) {
res.end();
});


server.listen(0, function() {
for (let i = 0; i < 4; i += 1) {
for (let i = 0; i < NUMBER_OF_EXCEPTIONS; i += 1) {
http.get({ port: this.address().port, path: `/busy/${i}` });
}
});

let exception_count = 0;

process.on('uncaughtException', function(err) {
console.log(`Caught an exception: ${err}`);
if (err.name === 'AssertionError') throw err;
if (++exception_count === 4) process.exit(0);
countdown.dec();
});