|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const zlib = require('zlib'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +const bigData = new Buffer(10240).fill('x'); |
| 8 | + |
| 9 | +const opts = { |
| 10 | + level: 0, |
| 11 | + highWaterMark: 16 |
| 12 | +}; |
| 13 | + |
| 14 | +const deflater = zlib.createDeflate(opts); |
| 15 | + |
| 16 | +// shim deflater.flush so we can count times executed |
| 17 | +var flushCount = 0; |
| 18 | +var drainCount = 0; |
| 19 | + |
| 20 | +const flush = deflater.flush; |
| 21 | +deflater.flush = function(kind, callback) { |
| 22 | + flushCount++; |
| 23 | + flush.call(this, kind, callback); |
| 24 | +}; |
| 25 | + |
| 26 | +deflater.write(bigData); |
| 27 | + |
| 28 | +const ws = deflater._writableState; |
| 29 | +const beforeFlush = ws.needDrain; |
| 30 | +var afterFlush = ws.needDrain; |
| 31 | + |
| 32 | +deflater.flush(function(err) { |
| 33 | + afterFlush = ws.needDrain; |
| 34 | +}); |
| 35 | + |
| 36 | +deflater.on('drain', function() { |
| 37 | + drainCount++;; |
| 38 | +}); |
| 39 | + |
| 40 | +process.once('exit', function() { |
| 41 | + assert.equal(beforeFlush, true, |
| 42 | + 'before calling flush the writable stream should need to drain'); |
| 43 | + assert.equal(afterFlush, false, |
| 44 | + 'after calling flush the writable stream should not need to drain'); |
| 45 | + assert.equal(drainCount, 1, |
| 46 | + 'the deflater should have emitted a single drain event'); |
| 47 | + assert.equal(flushCount, 2, |
| 48 | + 'flush should be called twice'); |
| 49 | +}); |
0 commit comments