11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst stream = require('stream');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst writable = new stream.Writable();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciwritable._writev = common.mustCall((chunks, cb) => {
101cb0ef41Sopenharmony_ci  assert.strictEqual(chunks.length, 2);
111cb0ef41Sopenharmony_ci  cb();
121cb0ef41Sopenharmony_ci}, 1);
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciwritable._write = common.mustCall((chunk, encoding, cb) => {
151cb0ef41Sopenharmony_ci  cb();
161cb0ef41Sopenharmony_ci}, 1);
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci// first cork
191cb0ef41Sopenharmony_ciwritable.cork();
201cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.corked, 1);
211cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.bufferedRequestCount, 0);
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// cork again
241cb0ef41Sopenharmony_ciwritable.cork();
251cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.corked, 2);
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci// The first chunk is buffered
281cb0ef41Sopenharmony_ciwritable.write('first chunk');
291cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.bufferedRequestCount, 1);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci// First uncork does nothing
321cb0ef41Sopenharmony_ciwritable.uncork();
331cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.corked, 1);
341cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.bufferedRequestCount, 1);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciprocess.nextTick(uncork);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci// The second chunk is buffered, because we uncork at the end of tick
391cb0ef41Sopenharmony_ciwritable.write('second chunk');
401cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.corked, 1);
411cb0ef41Sopenharmony_ciassert.strictEqual(writable._writableState.bufferedRequestCount, 2);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cifunction uncork() {
441cb0ef41Sopenharmony_ci  // Second uncork flushes the buffer
451cb0ef41Sopenharmony_ci  writable.uncork();
461cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.corked, 0);
471cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.bufferedRequestCount, 0);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // Verify that end() uncorks correctly
501cb0ef41Sopenharmony_ci  writable.cork();
511cb0ef41Sopenharmony_ci  writable.write('third chunk');
521cb0ef41Sopenharmony_ci  writable.end();
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // End causes an uncork() as well
551cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.corked, 0);
561cb0ef41Sopenharmony_ci  assert.strictEqual(writable._writableState.bufferedRequestCount, 0);
571cb0ef41Sopenharmony_ci}
58