diff --git a/doc/guides/writing-tests.md b/doc/guides/writing-tests.md index 19626799f73acb..06713496679015 100644 --- a/doc/guides/writing-tests.md +++ b/doc/guides/writing-tests.md @@ -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'; @@ -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 diff --git a/test/parallel/test-http-content-length.js b/test/parallel/test-http-content-length.js index 3d6882d92f04b3..e6ba3719f95ba6 100644 --- a/test/parallel/test-http-content-length.js +++ b/test/parallel/test-http-content-length.js @@ -2,6 +2,7 @@ require('../common'); const assert = require('assert'); const http = require('http'); +const Countdown = require('../common/countdown'); const expectedHeadersMultipleWrites = { 'connection': 'close', @@ -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'); @@ -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() {