8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 860c619 commit c88f99fCopy full SHA for c88f99f
test/common/README.md
@@ -280,6 +280,42 @@ The realpath of the 'tmp' directory.
280
281
Name of the temp directory used by tests.
282
283
+## Countdown Module
284
+
285
+The `Countdown` module provides a simple countdown mechanism for tests that
286
+require a particular action to be taken after a given number of completed
287
+tasks (for instance, shutting down an HTTP server after a specific number of
288
+requests).
289
290
+<!-- eslint-disable strict, required-modules -->
291
+```js
292
+const Countdown = require('../common/countdown');
293
294
+function doSomething() {
295
+ console.log('.');
296
+}
297
298
+const countdown = new Countdown(2, doSomething);
299
+countdown.dec();
300
301
+```
302
303
+### new Countdown(limit, callback)
304
305
+* `limit` {number}
306
+* `callback` {function}
307
308
+Creates a new `Countdown` instance.
309
310
+### Countdown.prototype.dec()
311
312
+Decrements the `Countdown` counter.
313
314
+### Coutndown.prototype.remaining
315
316
+Specifies the remaining number of times `Countdown.prototype.dec()` must be
317
+called before the callback is invoked.
318
319
## WPT Module
320
321
The wpt.js module is a port of parts of
test/common/countdown.js
@@ -0,0 +1,27 @@
1
+/* eslint-disable required-modules */
2
+'use strict';
3
4
+const assert = require('assert');
5
+const kLimit = Symbol('limit');
6
+const kCallback = Symbol('callback');
7
8
+class Countdown {
9
+ constructor(limit, cb) {
10
+ assert.strictEqual(typeof limit, 'number');
11
+ assert.strictEqual(typeof cb, 'function');
12
+ this[kLimit] = limit;
13
+ this[kCallback] = cb;
14
+ }
15
16
+ dec() {
17
+ assert(this[kLimit] > 0, 'Countdown expired');
18
+ if (--this[kLimit] === 0)
19
+ this[kCallback]();
20
21
22
+ get remaining() {
23
+ return this[kLimit];
24
25
26
27
+module.exports = Countdown;
test/parallel/test-common-countdown.js
@@ -0,0 +1,15 @@
+const common = require('../common');
+let done = '';
+const countdown = new Countdown(2, common.mustCall(() => done = true));
+assert.strictEqual(countdown.remaining, 2);
+assert.strictEqual(countdown.remaining, 1);
+assert.strictEqual(countdown.remaining, 0);
+assert.strictEqual(done, true);
test/parallel/test-http-abort-client.js
@@ -2,42 +2,27 @@
const common = require('../common');
const http = require('http');
-const server = http.Server(function(req, res) {
- console.log('Server accepted request.');
+let serverRes;
+const server = http.Server((req, res) => {
+ serverRes = res;
res.writeHead(200);
res.write('Part of my res.');
res.destroy();
});
-server.listen(0, common.mustCall(function() {
+server.listen(0, common.mustCall(() => {
http.get({
- port: this.address().port,
+ port: server.address().port,
headers: { connection: 'keep-alive' }
- }, common.mustCall(function(res) {
+ }, common.mustCall((res) => {
server.close();
+ serverRes.destroy();
- console.log(`Got res: ${res.statusCode}`);
- console.dir(res.headers);
-
- res.on('data', function(chunk) {
- console.log(`Read ${chunk.length} bytes`);
- console.log(' chunk=%j', chunk.toString());
- });
28
- res.on('end', function() {
29
- console.log('Response ended.');
30
31
32
- res.on('aborted', function() {
33
- console.log('Response aborted.');
34
35
36
- res.socket.on('close', function() {
37
- console.log('socket closed, but not res');
38
39
40
- // it would be nice if this worked:
+ res.resume();
+ res.on('end', common.mustCall());
+ res.on('aborted', common.mustCall());
41
res.on('close', common.mustCall());
+ res.socket.on('close', common.mustCall());
42
}));
43
test/parallel/test-http-abort-queued.js
@@ -1,11 +1,11 @@
'use strict';
-require('../common');
const assert = require('assert');
let complete;
-const server = http.createServer(function(req, res) {
+const server = http.createServer((req, res) => {
// We should not see the queued /thatotherone request within the server
// as it should be aborted before it is sent.
assert.strictEqual(req.url, '/');
@@ -19,10 +19,8 @@ const server = http.createServer(function(req, res) {
-server.listen(0, function() {
- console.log('listen', server.address().port);
- const agent = new http.Agent({maxSockets: 1});
+server.listen(0, () => {
+ const agent = new http.Agent({ maxSockets: 1 });
assert.strictEqual(Object.keys(agent.sockets).length, 0);
const options = {
@@ -34,7 +32,7 @@ server.listen(0, function() {
};
const req1 = http.request(options);
- req1.on('response', function(res1) {
+ req1.on('response', (res1) => {
assert.strictEqual(Object.keys(agent.sockets).length, 1);
assert.strictEqual(Object.keys(agent.requests).length, 0);
@@ -48,7 +46,9 @@ server.listen(0, function() {
48
46
49
47
assert.strictEqual(Object.keys(agent.requests).length, 1);
50
51
- req2.on('error', function(err) {
+ // TODO(jasnell): This event does not appear to currently be triggered.
+ // is this handler actually required?
+ req2.on('error', (err) => {
52
// This is expected in response to our explicit abort call
53
assert.strictEqual(err.code, 'ECONNRESET');
54
@@ -59,25 +59,16 @@ server.listen(0, function() {
59
60
61
62
- console.log(`Got res: ${res1.statusCode}`);
63
- console.dir(res1.headers);
64
65
- res1.on('data', function(chunk) {
66
67
68
- complete();
69
+ res1.on('data', (chunk) => complete());
70
71
- res1.on('end', function() {
72
73
74
- setTimeout(function() {
+ res1.on('end', common.mustCall(() => {
+ setTimeout(common.mustCall(() => {
75
76
77
78
79
- }, 100);
80
+ }), 100);
+ }));
81
82
83
req1.end();
test/parallel/test-http-after-connect.js
@@ -2,61 +2,51 @@
-let clientResponses = 0;
-const server = http.createServer(common.mustCall(function(req, res) {
- console.error('Server got GET request');
+const server = http.createServer(common.mustCall((req, res) => {
req.resume();
res.write('');
- res.end(req.url);
- }, 50);
+ setTimeout(() => res.end(req.url), 50);
}, 2));
-server.on('connect', common.mustCall(function(req, socket) {
- console.error('Server got CONNECT request');
+const countdown = new Countdown(2, common.mustCall(() => server.close()));
+server.on('connect', common.mustCall((req, socket) => {
socket.write('HTTP/1.1 200 Connection established\r\n\r\n');
socket.resume();
- socket.on('end', function() {
- socket.end();
+ socket.on('end', () => socket.end());
const req = http.request({
method: 'CONNECT',
path: 'google.com:80'
- req.on('connect', common.mustCall(function(res, socket) {
- console.error('Client got CONNECT response');
+ req.on('connect', common.mustCall((res, socket) => {
socket.end();
2E06 code>
+ socket.on('end', common.mustCall(() => {
doRequest(0);
doRequest(1);
req.end();
-});
+}));
function doRequest(i) {
44
45
port: server.address().port,
path: `/request${i}`
- console.error('Client got GET response');
let data = '';
res.setEncoding('utf8');
- data += chunk;
+ res.on('data', (chunk) => data += chunk);
+ res.on('end', common.mustCall(() => {
55
assert.strictEqual(data, `/request${i}`);
56
- ++clientResponses;
57
- if (clientResponses === 2) {
58
- server.close();
- }
+ countdown.dec();
}