11cb0ef41Sopenharmony_ci// META: global=window,worker 21cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js 31cb0ef41Sopenharmony_ci'use strict'; 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_cipromise_test(() => { 61cb0ef41Sopenharmony_ci let flushCalled = false; 71cb0ef41Sopenharmony_ci const ts = new TransformStream({ 81cb0ef41Sopenharmony_ci transform() { }, 91cb0ef41Sopenharmony_ci flush() { 101cb0ef41Sopenharmony_ci flushCalled = true; 111cb0ef41Sopenharmony_ci } 121cb0ef41Sopenharmony_ci }); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci return ts.writable.getWriter().close().then(() => { 151cb0ef41Sopenharmony_ci return assert_true(flushCalled, 'closing the writable triggers the transform flush immediately'); 161cb0ef41Sopenharmony_ci }); 171cb0ef41Sopenharmony_ci}, 'TransformStream flush is called immediately when the writable is closed, if no writes are queued'); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_cipromise_test(() => { 201cb0ef41Sopenharmony_ci let flushCalled = false; 211cb0ef41Sopenharmony_ci let resolveTransform; 221cb0ef41Sopenharmony_ci const ts = new TransformStream({ 231cb0ef41Sopenharmony_ci transform() { 241cb0ef41Sopenharmony_ci return new Promise(resolve => { 251cb0ef41Sopenharmony_ci resolveTransform = resolve; 261cb0ef41Sopenharmony_ci }); 271cb0ef41Sopenharmony_ci }, 281cb0ef41Sopenharmony_ci flush() { 291cb0ef41Sopenharmony_ci flushCalled = true; 301cb0ef41Sopenharmony_ci return new Promise(() => {}); // never resolves 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci }, undefined, { highWaterMark: 1 }); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci const writer = ts.writable.getWriter(); 351cb0ef41Sopenharmony_ci writer.write('a'); 361cb0ef41Sopenharmony_ci writer.close(); 371cb0ef41Sopenharmony_ci assert_false(flushCalled, 'closing the writable does not immediately call flush if writes are not finished'); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci let rsClosed = false; 401cb0ef41Sopenharmony_ci ts.readable.getReader().closed.then(() => { 411cb0ef41Sopenharmony_ci rsClosed = true; 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci return delay(0).then(() => { 451cb0ef41Sopenharmony_ci assert_false(flushCalled, 'closing the writable does not asynchronously call flush if writes are not finished'); 461cb0ef41Sopenharmony_ci resolveTransform(); 471cb0ef41Sopenharmony_ci return delay(0); 481cb0ef41Sopenharmony_ci }).then(() => { 491cb0ef41Sopenharmony_ci assert_true(flushCalled, 'flush is eventually called'); 501cb0ef41Sopenharmony_ci assert_false(rsClosed, 'if flushPromise does not resolve, the readable does not become closed'); 511cb0ef41Sopenharmony_ci }); 521cb0ef41Sopenharmony_ci}, 'TransformStream flush is called after all queued writes finish, once the writable is closed'); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_cipromise_test(() => { 551cb0ef41Sopenharmony_ci let c; 561cb0ef41Sopenharmony_ci const ts = new TransformStream({ 571cb0ef41Sopenharmony_ci start(controller) { 581cb0ef41Sopenharmony_ci c = controller; 591cb0ef41Sopenharmony_ci }, 601cb0ef41Sopenharmony_ci transform() { 611cb0ef41Sopenharmony_ci }, 621cb0ef41Sopenharmony_ci flush() { 631cb0ef41Sopenharmony_ci c.enqueue('x'); 641cb0ef41Sopenharmony_ci c.enqueue('y'); 651cb0ef41Sopenharmony_ci } 661cb0ef41Sopenharmony_ci }); 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ci const reader = ts.readable.getReader(); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci const writer = ts.writable.getWriter(); 711cb0ef41Sopenharmony_ci writer.write('a'); 721cb0ef41Sopenharmony_ci writer.close(); 731cb0ef41Sopenharmony_ci return reader.read().then(result1 => { 741cb0ef41Sopenharmony_ci assert_equals(result1.value, 'x', 'the first chunk read is the first one enqueued in flush'); 751cb0ef41Sopenharmony_ci assert_equals(result1.done, false, 'the first chunk read is the first one enqueued in flush'); 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci return reader.read().then(result2 => { 781cb0ef41Sopenharmony_ci assert_equals(result2.value, 'y', 'the second chunk read is the second one enqueued in flush'); 791cb0ef41Sopenharmony_ci assert_equals(result2.done, false, 'the second chunk read is the second one enqueued in flush'); 801cb0ef41Sopenharmony_ci }); 811cb0ef41Sopenharmony_ci }); 821cb0ef41Sopenharmony_ci}, 'TransformStream flush gets a chance to enqueue more into the readable'); 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_cipromise_test(() => { 851cb0ef41Sopenharmony_ci let c; 861cb0ef41Sopenharmony_ci const ts = new TransformStream({ 871cb0ef41Sopenharmony_ci start(controller) { 881cb0ef41Sopenharmony_ci c = controller; 891cb0ef41Sopenharmony_ci }, 901cb0ef41Sopenharmony_ci transform() { 911cb0ef41Sopenharmony_ci }, 921cb0ef41Sopenharmony_ci flush() { 931cb0ef41Sopenharmony_ci c.enqueue('x'); 941cb0ef41Sopenharmony_ci c.enqueue('y'); 951cb0ef41Sopenharmony_ci return delay(0); 961cb0ef41Sopenharmony_ci } 971cb0ef41Sopenharmony_ci }); 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ci const reader = ts.readable.getReader(); 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci const writer = ts.writable.getWriter(); 1021cb0ef41Sopenharmony_ci writer.write('a'); 1031cb0ef41Sopenharmony_ci writer.close(); 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci return Promise.all([ 1061cb0ef41Sopenharmony_ci reader.read().then(result1 => { 1071cb0ef41Sopenharmony_ci assert_equals(result1.value, 'x', 'the first chunk read is the first one enqueued in flush'); 1081cb0ef41Sopenharmony_ci assert_equals(result1.done, false, 'the first chunk read is the first one enqueued in flush'); 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci return reader.read().then(result2 => { 1111cb0ef41Sopenharmony_ci assert_equals(result2.value, 'y', 'the second chunk read is the second one enqueued in flush'); 1121cb0ef41Sopenharmony_ci assert_equals(result2.done, false, 'the second chunk read is the second one enqueued in flush'); 1131cb0ef41Sopenharmony_ci }); 1141cb0ef41Sopenharmony_ci }), 1151cb0ef41Sopenharmony_ci reader.closed.then(() => { 1161cb0ef41Sopenharmony_ci assert_true(true, 'readable reader becomes closed'); 1171cb0ef41Sopenharmony_ci }) 1181cb0ef41Sopenharmony_ci ]); 1191cb0ef41Sopenharmony_ci}, 'TransformStream flush gets a chance to enqueue more into the readable, and can then async close'); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ciconst error1 = new Error('error1'); 1221cb0ef41Sopenharmony_cierror1.name = 'error1'; 1231cb0ef41Sopenharmony_ci 1241cb0ef41Sopenharmony_cipromise_test(t => { 1251cb0ef41Sopenharmony_ci const ts = new TransformStream({ 1261cb0ef41Sopenharmony_ci flush(controller) { 1271cb0ef41Sopenharmony_ci controller.error(error1); 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci }); 1301cb0ef41Sopenharmony_ci return promise_rejects_exactly(t, error1, ts.writable.getWriter().close(), 'close() should reject'); 1311cb0ef41Sopenharmony_ci}, 'error() during flush should cause writer.close() to reject'); 132