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

Adjust test-http-content-length and docs to consider countdown #17201

Closed
Closed
Show file tree
Hide file tree
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
29 changes: 25 additions & 4 deletions doc/guides/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ platforms.

### The *common* API

Make use of the helpers from the `common` module as much as possible.
Make use of the helpers from the `common` module as much as possible. Please refer
to the [common file documentation](https://github.com/nodejs/node/tree/master/test/common)
for the full details of the helpers.

One interesting case is `common.mustCall`. The use of `common.mustCall` may
avoid the use of extra variables and the corresponding assertions. Let's explain
this with a real test from the test suite.
#### common.mustCall

One interesting case is `common.mustCall`. The use of `common.mustCall` may avoid
the use of extra variables and the corresponding assertions. Let's explain this
with a real test from the test suite.

```javascript
'use strict';
Expand Down Expand Up @@ -189,6 +193,23 @@ const server = http.createServer(common.mustCall(function(req, res) {
});

```
#### Countdown Module

The common [Countdown module](https://github.com/nodejs/node/tree/master/test/common#countdown-module) provides a simple countdown mechanism for tests that
require a particular action to be taken after a given number of completed tasks
(for instance, shutting down an HTTP server after a specific number of requests).

```javascript
const Countdown = require('../common/countdown');

const countdown = new Countdown(2, function() {
console.log('.');
});

countdown.dec();
countdown.dec(); // The countdown callback will be invoked now.
```


### Flags

Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-http-content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require('../common');
const assert = require('assert');
const http = require('http');
const Countdown = require('../common/countdown');

const expectedHeadersMultipleWrites = {
'connection': 'close',
Expand All @@ -18,8 +19,8 @@ const expectedHeadersEndNoData = {
'content-length': '0',
};

let receivedRequests = 0;
const totalRequests = 3;

const countdown = new Countdown(3, () => server.close());

const server = http.createServer(function(req, res) {
res.removeHeader('Date');
Expand All @@ -42,8 +43,7 @@ const server = http.createServer(function(req, res) {
throw new Error('Unreachable');
}

receivedRequests++;
if (totalRequests === receivedRequests) server.close();
countdown.dec();
});

server.listen(0, function() {
Expand Down