|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const stream = require('stream'); |
| 6 | + |
| 7 | +const writable = new stream.Writable(); |
| 8 | + |
| 9 | +writable._writev = common.mustCall((chunks, cb) => { |
| 10 | + assert.equal(chunks.length, 2, 'two chunks to write'); |
| 11 | + cb(); |
| 12 | +}, 1); |
| 13 | + |
| 14 | +writable._write = common.mustCall((chunk, encoding, cb) => { |
| 15 | + cb(); |
| 16 | +}, 1); |
| 17 | + |
| 18 | +// first cork |
| 19 | +writable.cork(); |
| 20 | +assert.strictEqual(writable._writableState.corked, 1); |
| 21 | +assert.strictEqual(writable._writableState.bufferedRequestCount, 0); |
| 22 | + |
| 23 | +// cork again |
| 24 | +writable.cork(); |
| 25 | +assert.strictEqual(writable._writableState.corked, 2); |
| 26 | + |
| 27 | +// the first chunk is buffered |
| 28 | +writable.write('first chunk'); |
| 29 | +assert.strictEqual(writable._writableState.bufferedRequestCount, 1); |
| 30 | + |
| 31 | +// first uncork does nothing |
| 32 | +writable.uncork(); |
| 33 | +assert.strictEqual(writable._writableState.corked, 1); |
| 34 | +assert.strictEqual(writable._writableState.bufferedRequestCount, 1); |
| 35 | + |
| 36 | +process.nextTick(uncork); |
| 37 | + |
| 38 | +// the second chunk is buffered, because we uncork at the end of tick |
| 39 | +writable.write('second chunk'); |
| 40 | +assert.strictEqual(writable._writableState.corked, 1); |
| 41 | +assert.strictEqual(writable._writableState.bufferedRequestCount, 2); |
| 42 | + |
| 43 | +function uncork() { |
| 44 | + // second uncork flushes the buffer |
| 45 | + writable.uncork(); |
| 46 | + assert.strictEqual(writable._writableState.corked, 0); |
| 47 | + assert.strictEqual(writable._writableState.bufferedRequestCount, 0); |
| 48 | + |
| 49 | + // verify that end() uncorks correctly |
| 50 | + writable.cork(); |
| 51 | + writable.write('third chunk'); |
| 52 | + writable.end(); |
| 53 | + |
| 54 | + // end causes an uncork() as well |
| 55 | + assert.strictEqual(writable._writableState.corked, 0); |
| 56 | + assert.strictEqual(writable._writableState.bufferedRequestCount, 0); |
| 57 | +} |
0 commit comments