mirror of https://github.com/lukechilds/node.git
Browse Source
* Add common/countdown utility * Numerous improvements to http tests PR-URL: https://github.com/nodejs/node/pull/14315 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>v6
James M Snell
8 years ago
26 changed files with 390 additions and 473 deletions
@ -0,0 +1,27 @@ |
|||
/* eslint-disable required-modules */ |
|||
'use strict'; |
|||
|
|||
const assert = require('assert'); |
|||
const kLimit = Symbol('limit'); |
|||
const kCallback = Symbol('callback'); |
|||
|
|||
class Countdown { |
|||
constructor(limit, cb) { |
|||
assert.strictEqual(typeof limit, 'number'); |
|||
assert.strictEqual(typeof cb, 'function'); |
|||
this[kLimit] = limit; |
|||
this[kCallback] = cb; |
|||
} |
|||
|
|||
dec() { |
|||
assert(this[kLimit] > 0, 'Countdown expired'); |
|||
if (--this[kLimit] === 0) |
|||
this[kCallback](); |
|||
} |
|||
|
|||
get remaining() { |
|||
return this[kLimit]; |
|||
} |
|||
} |
|||
|
|||
module.exports = Countdown; |
@ -0,0 +1,15 @@ |
|||
'use strict'; |
|||
|
|||
const common = require('../common'); |
|||
const assert = require('assert'); |
|||
const Countdown = require('../common/countdown'); |
|||
|
|||
let done = ''; |
|||
|
|||
const countdown = new Countdown(2, common.mustCall(() => done = true)); |
|||
assert.strictEqual(countdown.remaining, 2); |
|||
countdown.dec(); |
|||
assert.strictEqual(countdown.remaining, 1); |
|||
countdown.dec(); |
|||
assert.strictEqual(countdown.remaining, 0); |
|||
assert.strictEqual(done, true); |
Loading…
Reference in new issue