Best JavaScript code snippet using wpt
aflprep_tee.any.js
Source:aflprep_tee.any.js
1'use strict';2test(() => {3 const rs = new ReadableStream();4 const result = rs.tee();5 assert_true(Array.isArray(result), 'return value should be an array');6 assert_equals(result.length, 2, 'array should have length 2');7 assert_equals(result[0].constructor, ReadableStream, '0th element should be a ReadableStream');8 assert_equals(result[1].constructor, ReadableStream, '1st element should be a ReadableStream');9}, 'ReadableStream teeing: rs.tee() returns an array of two ReadableStreams');10promise_test(t => {11 const rs = new ReadableStream({12 start(c) {13 c.enqueue('a');14 c.enqueue('b');15 c.close();16 }17 });18 const branch = rs.tee();19 const branch1 = branch[0];20 const branch2 = branch[1];21 const reader1 = branch1.getReader();22 const reader2 = branch2.getReader();23 reader2.closed.then(t.unreached_func('branch2 should not be closed'));24 return Promise.all([25 reader1.closed,26 reader1.read().then(r => {27 assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch1 should be correct');28 }),29 reader1.read().then(r => {30 assert_object_equals(r, { value: 'b', done: false }, 'second chunk from branch1 should be correct');31 }),32 reader1.read().then(r => {33 assert_object_equals(r, { value: undefined, done: true }, 'third read() from branch1 should be done');34 }),35 reader2.read().then(r => {36 assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch2 should be correct');37 })38 ]);39}, 'ReadableStream teeing: should be able to read one branch to the end without affecting the other');40promise_test(() => {41 const theObject = { the: 'test object' };42 const rs = new ReadableStream({43 start(c) {44 c.enqueue(theObject);45 }46 });47 const branch = rs.tee();48 const branch1 = branch[0];49 const branch2 = branch[1];50 const reader1 = branch1.getReader();51 const reader2 = branch2.getReader();52 return Promise.all([reader1.read(), reader2.read()]).then(values => {53 assert_object_equals(values[0], values[1], 'the values should be equal');54 });55}, 'ReadableStream teeing: values should be equal across each branch');56promise_test(t => {57 const theError = { name: 'boo!' };58 const rs = new ReadableStream({59 start(c) {60 c.enqueue('a');61 c.enqueue('b');62 },63 pull() {64 throw theError;65 }66 });67 const branches = rs.tee();68 const reader1 = branches[0].getReader();69 const reader2 = branches[1].getReader();70 reader1.label = 'reader1';71 reader2.label = 'reader2';72 return Promise.all([73 promise_rejects_exactly(t, theError, reader1.closed),74 promise_rejects_exactly(t, theError, reader2.closed),75 reader1.read().then(r => {76 assert_object_equals(r, { value: 'a', done: false }, 'should be able to read the first chunk in branch1');77 }),78 reader1.read().then(r => {79 assert_object_equals(r, { value: 'b', done: false }, 'should be able to read the second chunk in branch1');80 return promise_rejects_exactly(t, theError, reader2.read());81 })82 .then(() => promise_rejects_exactly(t, theError, reader1.read()))83 ]);84}, 'ReadableStream teeing: errors in the source should propagate to both branches');85promise_test(() => {86 const rs = new ReadableStream({87 start(c) {88 c.enqueue('a');89 c.enqueue('b');90 c.close();91 }92 });93 const branches = rs.tee();94 const branch1 = branches[0];95 const branch2 = branches[1];96 branch1.cancel();97 return Promise.all([98 readableStreamToArray(branch1).then(chunks => {99 assert_array_equals(chunks, [], 'branch1 should have no chunks');100 }),101 readableStreamToArray(branch2).then(chunks => {102 assert_array_equals(chunks, ['a', 'b'], 'branch2 should have two chunks');103 })104 ]);105}, 'ReadableStream teeing: canceling branch1 should not impact branch2');106promise_test(() => {107 const rs = new ReadableStream({108 start(c) {109 c.enqueue('a');110 c.enqueue('b');111 c.close();112 }113 });114 const branches = rs.tee();115 const branch1 = branches[0];116 const branch2 = branches[1];117 branch2.cancel();118 return Promise.all([119 readableStreamToArray(branch1).then(chunks => {120 assert_array_equals(chunks, ['a', 'b'], 'branch1 should have two chunks');121 }),122 readableStreamToArray(branch2).then(chunks => {123 assert_array_equals(chunks, [], 'branch2 should have no chunks');124 })125 ]);126}, 'ReadableStream teeing: canceling branch2 should not impact branch1');127promise_test(() => {128 const reason1 = new Error('We\'re wanted men.');129 const reason2 = new Error('I have the death sentence on twelve systems.');130 let resolve;131 const promise = new Promise(r => resolve = r);132 const rs = new ReadableStream({133 cancel(reason) {134 assert_array_equals(reason, [reason1, reason2],135 'the cancel reason should be an array containing those from the branches');136 resolve();137 }138 });139 const branch = rs.tee();140 const branch1 = branch[0];141 const branch2 = branch[1];142 branch1.cancel(reason1);143 branch2.cancel(reason2);144 return promise;145}, 'ReadableStream teeing: canceling both branches should aggregate the cancel reasons into an array');146promise_test(() => {147 const reason1 = new Error('This little one\'s not worth the effort.');148 const reason2 = new Error('Come, let me get you something.');149 let resolve;150 const promise = new Promise(r => resolve = r);151 const rs = new ReadableStream({152 cancel(reason) {153 assert_array_equals(reason, [reason1, reason2],154 'the cancel reason should be an array containing those from the branches');155 resolve();156 }157 });158 const branch = rs.tee();159 const branch1 = branch[0];160 const branch2 = branch[1];161 return Promise.all([162 branch2.cancel(reason2),163 branch1.cancel(reason1),164 promise165 ]);166}, 'ReadableStream teeing: canceling both branches in reverse order should aggregate the cancel reasons into an array');167promise_test(t => {168 const theError = { name: 'I\'ll be careful.' };169 const rs = new ReadableStream({170 cancel() {171 throw theError;172 }173 });174 const branch = rs.tee();175 const branch1 = branch[0];176 const branch2 = branch[1];177 return Promise.all([178 promise_rejects_exactly(t, theError, branch1.cancel()),179 promise_rejects_exactly(t, theError, branch2.cancel())180 ]);181}, 'ReadableStream teeing: failing to cancel the original stream should cause cancel() to reject on branches');182promise_test(t => {183 const theError = { name: 'You just watch yourself!' };184 let controller;185 const stream = new ReadableStream({ start(c) { controller = c; } });186 const [branch1, branch2] = stream.tee();187 controller.error(theError);188 return Promise.all([189 promise_rejects_exactly(t, theError, branch1.cancel()),190 promise_rejects_exactly(t, theError, branch2.cancel())191 ]);192}, 'ReadableStream teeing: erroring a teed stream should properly handle canceled branches');193promise_test(t => {194 let controller;195 const stream = new ReadableStream({ start(c) { controller = c; } });196 const [branch1, branch2] = stream.tee();197 const error = new Error();198 error.name = 'distinctive';199 controller.enqueue();200 controller.enqueue();201 return delay(0).then(() => {202 controller.error(error);203 const reader1 = branch1.getReader();204 const reader2 = branch2.getReader();205 return Promise.all([206 promise_rejects_exactly(t, error, reader1.closed, 'reader1.closed should reject'),207 promise_rejects_exactly(t, error, reader2.closed, 'reader2.closed should reject')208 ]);209 });210}, 'ReadableStream teeing: erroring a teed stream should error both branches');211promise_test(() => {212 let controller;213 const rs = new ReadableStream({214 start(c) {215 controller = c;216 }217 });218 const branches = rs.tee();219 const reader1 = branches[0].getReader();220 const reader2 = branches[1].getReader();221 const promise = Promise.all([reader1.closed, reader2.closed]);222 controller.close();223 return promise;224}, 'ReadableStream teeing: closing the original should immediately close the branches');225promise_test(t => {226 let controller;227 const rs = new ReadableStream({228 start(c) {229 controller = c;230 }231 });232 const branches = rs.tee();233 const reader1 = branches[0].getReader();234 const reader2 = branches[1].getReader();235 const theError = { name: 'boo!' };236 const promise = Promise.all([237 promise_rejects_exactly(t, theError, reader1.closed),238 promise_rejects_exactly(t, theError, reader2.closed)239 ]);240 controller.error(theError);241 return promise;242}, 'ReadableStream teeing: erroring the original should immediately error the branches');243promise_test(async t => {244 let controller;245 const rs = new ReadableStream({246 start(c) {247 controller = c;248 }249 });250 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());251 const cancelPromise = reader2.cancel();252 controller.enqueue('a');253 const read1 = await reader1.read();254 assert_object_equals(read1, { value: 'a', done: false }, 'first read() from branch1 should fulfill with the chunk');255 controller.close();256 const read2 = await reader1.read();257 assert_object_equals(read2, { value: undefined, done: true }, 'second read() from branch1 should be done');258 await Promise.all([259 reader1.closed,260 cancelPromise261 ]);262}, 'ReadableStream teeing: canceling branch1 should finish when branch2 reads until end of stream');263promise_test(async t => {264 let controller;265 const theError = { name: 'boo!' };266 const rs = new ReadableStream({267 start(c) {268 controller = c;269 }270 });271 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());272 const cancelPromise = reader2.cancel();273 controller.error(theError);274 await Promise.all([275 promise_rejects_exactly(t, theError, reader1.read()),276 cancelPromise277 ]);278}, 'ReadableStream teeing: canceling branch1 should finish when original stream errors');279promise_test(async () => {280 const rs = new ReadableStream({});281 const [branch1, branch2] = rs.tee();282 const cancel1 = branch1.cancel();283 await flushAsyncEvents();284 const cancel2 = branch2.cancel();285 await Promise.all([cancel1, cancel2]);286}, 'ReadableStream teeing: canceling both branches in sequence with delay');287promise_test(async t => {288 const theError = { name: 'boo!' };289 const rs = new ReadableStream({290 cancel() {291 throw theError;292 }293 });294 const [branch1, branch2] = rs.tee();295 const cancel1 = branch1.cancel();296 await flushAsyncEvents();297 const cancel2 = branch2.cancel();298 await Promise.all([299 promise_rejects_exactly(t, theError, cancel1),300 promise_rejects_exactly(t, theError, cancel2)301 ]);302}, 'ReadableStream teeing: failing to cancel when canceling both branches in sequence with delay');303test(t => {304 const oldReadableStream = ReadableStream;305 const getReader = ReadableStream.prototype.getReader;306 const origRS = new ReadableStream();307 ReadableStream = function() {308 throw new Error('global ReadableStream constructor called');309 };310 t.add_cleanup(() => {311 ReadableStream = oldReadableStream;312 });313 const [rs1, rs2] = origRS.tee();314 assert_not_equals(getReader.call(rs1), undefined, 'getReader should work on rs1');315 assert_not_equals(getReader.call(rs2), undefined, 'getReader should work on rs2');316}, 'ReadableStreamTee should not use a modified ReadableStream constructor from the global object');317promise_test(t => {318 const rs = recordingReadableStream({}, { highWaterMark: 0 });319 rs.tee();320 return flushAsyncEvents().then(() => {321 assert_array_equals(rs.events, ['pull'], 'pull should only be called once');322 });323}, 'ReadableStreamTee should not pull more chunks than can fit in the branch queue');324promise_test(t => {325 const rs = recordingReadableStream({326 pull(controller) {327 controller.enqueue('a');328 }329 }, { highWaterMark: 0 });330 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());331 return Promise.all([reader1.read(), reader2.read()])332 .then(() => {333 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');334 });335}, 'ReadableStreamTee should only pull enough to fill the emptiest queue');336promise_test(t => {337 const rs = recordingReadableStream({}, { highWaterMark: 0 });338 const theError = { name: 'boo!' };339 rs.controller.error(theError);340 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());341 return flushAsyncEvents().then(() => {342 assert_array_equals(rs.events, [], 'pull should not be called');343 return Promise.all([344 promise_rejects_exactly(t, theError, reader1.closed),345 promise_rejects_exactly(t, theError, reader2.closed)346 ]);347 });348}, 'ReadableStreamTee should not pull when original is already errored');349for (const branch of [1, 2]) {350 promise_test(t => {351 const rs = recordingReadableStream({}, { highWaterMark: 0 });352 const theError = { name: 'boo!' };353 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());354 return flushAsyncEvents().then(() => {355 assert_array_equals(rs.events, ['pull'], 'pull should be called once');356 rs.controller.enqueue('a');357 const reader = (branch === 1) ? reader1 : reader2;358 return reader.read();359 }).then(() => flushAsyncEvents()).then(() => {360 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');361 rs.controller.error(theError);362 return Promise.all([363 promise_rejects_exactly(t, theError, reader1.closed),364 promise_rejects_exactly(t, theError, reader2.closed)365 ]);366 }).then(() => flushAsyncEvents()).then(() => {367 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');368 });369 }, `ReadableStreamTee stops pulling when original stream errors while branch ${branch} is reading`);370}371promise_test(t => {372 const rs = recordingReadableStream({}, { highWaterMark: 0 });373 const theError = { name: 'boo!' };374 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());375 return flushAsyncEvents().then(() => {376 assert_array_equals(rs.events, ['pull'], 'pull should be called once');377 rs.controller.enqueue('a');378 return Promise.all([reader1.read(), reader2.read()]);379 }).then(() => flushAsyncEvents()).then(() => {380 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');381 rs.controller.error(theError);382 return Promise.all([383 promise_rejects_exactly(t, theError, reader1.closed),384 promise_rejects_exactly(t, theError, reader2.closed)385 ]);386 }).then(() => flushAsyncEvents()).then(() => {387 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');388 });...
tee.any.js
Source:tee.any.js
1// META: global=window,worker,jsshell2// META: script=../resources/rs-utils.js3// META: script=../resources/test-utils.js4// META: script=../resources/recording-streams.js5'use strict';6test(() => {7 const rs = new ReadableStream();8 const result = rs.tee();9 assert_true(Array.isArray(result), 'return value should be an array');10 assert_equals(result.length, 2, 'array should have length 2');11 assert_equals(result[0].constructor, ReadableStream, '0th element should be a ReadableStream');12 assert_equals(result[1].constructor, ReadableStream, '1st element should be a ReadableStream');13}, 'ReadableStream teeing: rs.tee() returns an array of two ReadableStreams');14promise_test(t => {15 const rs = new ReadableStream({16 start(c) {17 c.enqueue('a');18 c.enqueue('b');19 c.close();20 }21 });22 const branch = rs.tee();23 const branch1 = branch[0];24 const branch2 = branch[1];25 const reader1 = branch1.getReader();26 const reader2 = branch2.getReader();27 reader2.closed.then(t.unreached_func('branch2 should not be closed'));28 return Promise.all([29 reader1.closed,30 reader1.read().then(r => {31 assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch1 should be correct');32 }),33 reader1.read().then(r => {34 assert_object_equals(r, { value: 'b', done: false }, 'second chunk from branch1 should be correct');35 }),36 reader1.read().then(r => {37 assert_object_equals(r, { value: undefined, done: true }, 'third read() from branch1 should be done');38 }),39 reader2.read().then(r => {40 assert_object_equals(r, { value: 'a', done: false }, 'first chunk from branch2 should be correct');41 })42 ]);43}, 'ReadableStream teeing: should be able to read one branch to the end without affecting the other');44promise_test(() => {45 const theObject = { the: 'test object' };46 const rs = new ReadableStream({47 start(c) {48 c.enqueue(theObject);49 }50 });51 const branch = rs.tee();52 const branch1 = branch[0];53 const branch2 = branch[1];54 const reader1 = branch1.getReader();55 const reader2 = branch2.getReader();56 return Promise.all([reader1.read(), reader2.read()]).then(values => {57 assert_object_equals(values[0], values[1], 'the values should be equal');58 });59}, 'ReadableStream teeing: values should be equal across each branch');60promise_test(t => {61 const theError = { name: 'boo!' };62 const rs = new ReadableStream({63 start(c) {64 c.enqueue('a');65 c.enqueue('b');66 },67 pull() {68 throw theError;69 }70 });71 const branches = rs.tee();72 const reader1 = branches[0].getReader();73 const reader2 = branches[1].getReader();74 reader1.label = 'reader1';75 reader2.label = 'reader2';76 return Promise.all([77 promise_rejects_exactly(t, theError, reader1.closed),78 promise_rejects_exactly(t, theError, reader2.closed),79 reader1.read().then(r => {80 assert_object_equals(r, { value: 'a', done: false }, 'should be able to read the first chunk in branch1');81 }),82 reader1.read().then(r => {83 assert_object_equals(r, { value: 'b', done: false }, 'should be able to read the second chunk in branch1');84 return promise_rejects_exactly(t, theError, reader2.read());85 })86 .then(() => promise_rejects_exactly(t, theError, reader1.read()))87 ]);88}, 'ReadableStream teeing: errors in the source should propagate to both branches');89promise_test(() => {90 const rs = new ReadableStream({91 start(c) {92 c.enqueue('a');93 c.enqueue('b');94 c.close();95 }96 });97 const branches = rs.tee();98 const branch1 = branches[0];99 const branch2 = branches[1];100 branch1.cancel();101 return Promise.all([102 readableStreamToArray(branch1).then(chunks => {103 assert_array_equals(chunks, [], 'branch1 should have no chunks');104 }),105 readableStreamToArray(branch2).then(chunks => {106 assert_array_equals(chunks, ['a', 'b'], 'branch2 should have two chunks');107 })108 ]);109}, 'ReadableStream teeing: canceling branch1 should not impact branch2');110promise_test(() => {111 const rs = new ReadableStream({112 start(c) {113 c.enqueue('a');114 c.enqueue('b');115 c.close();116 }117 });118 const branches = rs.tee();119 const branch1 = branches[0];120 const branch2 = branches[1];121 branch2.cancel();122 return Promise.all([123 readableStreamToArray(branch1).then(chunks => {124 assert_array_equals(chunks, ['a', 'b'], 'branch1 should have two chunks');125 }),126 readableStreamToArray(branch2).then(chunks => {127 assert_array_equals(chunks, [], 'branch2 should have no chunks');128 })129 ]);130}, 'ReadableStream teeing: canceling branch2 should not impact branch1');131promise_test(() => {132 const reason1 = new Error('We\'re wanted men.');133 const reason2 = new Error('I have the death sentence on twelve systems.');134 let resolve;135 const promise = new Promise(r => resolve = r);136 const rs = new ReadableStream({137 cancel(reason) {138 assert_array_equals(reason, [reason1, reason2],139 'the cancel reason should be an array containing those from the branches');140 resolve();141 }142 });143 const branch = rs.tee();144 const branch1 = branch[0];145 const branch2 = branch[1];146 branch1.cancel(reason1);147 branch2.cancel(reason2);148 return promise;149}, 'ReadableStream teeing: canceling both branches should aggregate the cancel reasons into an array');150promise_test(() => {151 const reason1 = new Error('This little one\'s not worth the effort.');152 const reason2 = new Error('Come, let me get you something.');153 let resolve;154 const promise = new Promise(r => resolve = r);155 const rs = new ReadableStream({156 cancel(reason) {157 assert_array_equals(reason, [reason1, reason2],158 'the cancel reason should be an array containing those from the branches');159 resolve();160 }161 });162 const branch = rs.tee();163 const branch1 = branch[0];164 const branch2 = branch[1];165 return Promise.all([166 branch2.cancel(reason2),167 branch1.cancel(reason1),168 promise169 ]);170}, 'ReadableStream teeing: canceling both branches in reverse order should aggregate the cancel reasons into an array');171promise_test(t => {172 const theError = { name: 'I\'ll be careful.' };173 const rs = new ReadableStream({174 cancel() {175 throw theError;176 }177 });178 const branch = rs.tee();179 const branch1 = branch[0];180 const branch2 = branch[1];181 return Promise.all([182 promise_rejects_exactly(t, theError, branch1.cancel()),183 promise_rejects_exactly(t, theError, branch2.cancel())184 ]);185}, 'ReadableStream teeing: failing to cancel the original stream should cause cancel() to reject on branches');186test(() => {187 let controller;188 const stream = new ReadableStream({ start(c) { controller = c; } });189 const [branch1, branch2] = stream.tee();190 controller.error("error");191 branch1.cancel().catch(_=>_);192 branch2.cancel().catch(_=>_);193}, 'ReadableStream teeing: erroring a teed stream should properly handle canceled branches');194promise_test(t => {195 let controller;196 const stream = new ReadableStream({ start(c) { controller = c; } });197 const [branch1, branch2] = stream.tee();198 const error = new Error();199 error.name = 'distinctive';200 // Ensure neither branch is waiting in ReadableStreamDefaultReaderRead().201 controller.enqueue();202 controller.enqueue();203 return delay(0).then(() => {204 // This error will have to be detected via [[closedPromise]].205 controller.error(error);206 const reader1 = branch1.getReader();207 const reader2 = branch2.getReader();208 return Promise.all([209 promise_rejects_exactly(t, error, reader1.closed, 'reader1.closed should reject'),210 promise_rejects_exactly(t, error, reader2.closed, 'reader2.closed should reject')211 ]);212 });213}, 'ReadableStream teeing: erroring a teed stream should error both branches');214promise_test(() => {215 let controller;216 const rs = new ReadableStream({217 start(c) {218 controller = c;219 }220 });221 const branches = rs.tee();222 const reader1 = branches[0].getReader();223 const reader2 = branches[1].getReader();224 const promise = Promise.all([reader1.closed, reader2.closed]);225 controller.close();226 return promise;227}, 'ReadableStream teeing: closing the original should immediately close the branches');228promise_test(t => {229 let controller;230 const rs = new ReadableStream({231 start(c) {232 controller = c;233 }234 });235 const branches = rs.tee();236 const reader1 = branches[0].getReader();237 const reader2 = branches[1].getReader();238 const theError = { name: 'boo!' };239 const promise = Promise.all([240 promise_rejects_exactly(t, theError, reader1.closed),241 promise_rejects_exactly(t, theError, reader2.closed)242 ]);243 controller.error(theError);244 return promise;245}, 'ReadableStream teeing: erroring the original should immediately error the branches');246promise_test(async t => {247 let controller;248 const rs = new ReadableStream({249 start(c) {250 controller = c;251 }252 });253 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());254 const cancelPromise = reader2.cancel();255 controller.enqueue('a');256 const read1 = await reader1.read();257 assert_object_equals(read1, { value: 'a', done: false }, 'first read() from branch1 should fulfill with the chunk');258 controller.close();259 const read2 = await reader1.read();260 assert_object_equals(read2, { value: undefined, done: true }, 'second read() from branch1 should be done');261 await Promise.all([262 reader1.closed,263 cancelPromise264 ]);265}, 'ReadableStream teeing: canceling branch1 should finish when branch2 reads until end of stream');266promise_test(async t => {267 let controller;268 const theError = { name: 'boo!' };269 const rs = new ReadableStream({270 start(c) {271 controller = c;272 }273 });274 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());275 const cancelPromise = reader2.cancel();276 controller.error(theError);277 await Promise.all([278 promise_rejects_exactly(t, theError, reader1.read()),279 cancelPromise280 ]);281}, 'ReadableStream teeing: canceling branch1 should finish when original stream errors');282test(t => {283 // Copy original global.284 const oldReadableStream = ReadableStream;285 const getReader = ReadableStream.prototype.getReader;286 const origRS = new ReadableStream();287 // Replace the global ReadableStream constructor with one that doesn't work.288 ReadableStream = function() {289 throw new Error('global ReadableStream constructor called');290 };291 t.add_cleanup(() => {292 ReadableStream = oldReadableStream;293 });294 // This will probably fail if the global ReadableStream constructor was used.295 const [rs1, rs2] = origRS.tee();296 // These will definitely fail if the global ReadableStream constructor was used.297 assert_not_equals(getReader.call(rs1), undefined, 'getReader should work on rs1');298 assert_not_equals(getReader.call(rs2), undefined, 'getReader should work on rs2');299}, 'ReadableStreamTee should not use a modified ReadableStream constructor from the global object');300promise_test(t => {301 const rs = recordingReadableStream({}, { highWaterMark: 0 });302 // Create two branches, each with a HWM of 1. This should result in one303 // chunk being pulled, not two.304 rs.tee();305 return flushAsyncEvents().then(() => {306 assert_array_equals(rs.events, ['pull'], 'pull should only be called once');307 });308}, 'ReadableStreamTee should not pull more chunks than can fit in the branch queue');309promise_test(t => {310 const rs = recordingReadableStream({311 pull(controller) {312 controller.enqueue('a');313 }314 }, { highWaterMark: 0 });315 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());316 return Promise.all([reader1.read(), reader2.read()])317 .then(() => {318 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');319 });320}, 'ReadableStreamTee should only pull enough to fill the emptiest queue');321promise_test(t => {322 const rs = recordingReadableStream({}, { highWaterMark: 0 });323 const theError = { name: 'boo!' };324 rs.controller.error(theError);325 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());326 return flushAsyncEvents().then(() => {327 assert_array_equals(rs.events, [], 'pull should not be called');328 return Promise.all([329 promise_rejects_exactly(t, theError, reader1.closed),330 promise_rejects_exactly(t, theError, reader2.closed)331 ]);332 });333}, 'ReadableStreamTee should not pull when original is already errored');334for (const branch of [1, 2]) {335 promise_test(t => {336 const rs = recordingReadableStream({}, { highWaterMark: 0 });337 const theError = { name: 'boo!' };338 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());339 return flushAsyncEvents().then(() => {340 assert_array_equals(rs.events, ['pull'], 'pull should be called once');341 rs.controller.enqueue('a');342 const reader = (branch === 1) ? reader1 : reader2;343 return reader.read();344 }).then(() => flushAsyncEvents()).then(() => {345 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');346 rs.controller.error(theError);347 return Promise.all([348 promise_rejects_exactly(t, theError, reader1.closed),349 promise_rejects_exactly(t, theError, reader2.closed)350 ]);351 }).then(() => flushAsyncEvents()).then(() => {352 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');353 });354 }, `ReadableStreamTee stops pulling when original stream errors while branch ${branch} is reading`);355}356promise_test(t => {357 const rs = recordingReadableStream({}, { highWaterMark: 0 });358 const theError = { name: 'boo!' };359 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());360 return flushAsyncEvents().then(() => {361 assert_array_equals(rs.events, ['pull'], 'pull should be called once');362 rs.controller.enqueue('a');363 return Promise.all([reader1.read(), reader2.read()]);364 }).then(() => flushAsyncEvents()).then(() => {365 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');366 rs.controller.error(theError);367 return Promise.all([368 promise_rejects_exactly(t, theError, reader1.closed),369 promise_rejects_exactly(t, theError, reader2.closed)370 ]);371 }).then(() => flushAsyncEvents()).then(() => {372 assert_array_equals(rs.events, ['pull', 'pull'], 'pull should be called twice');373 });...
Using AI Code Generation
1var rs = new ReadableStream({2 start: function(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 controller.close();7 }8});9var [branch1, branch2] = rs.tee();10var reader1 = branch1.getReader();11var reader2 = branch2.getReader();12const { ReadableStream, WritableStream, TransformStream, ByteLengthQueuingStrategy, CountQueuingStrategy } = require('web-streams-polyfill/ponyfill');13const rs = new ReadableStream({14 start: function(controller) {15 controller.enqueue('a');16 controller.enqueue('b');17 controller.enqueue('c');18 controller.close();19 }20});21const [branch1, branch2] = rs.tee();22const reader1 = branch1.getReader();23const reader2 = branch2.getReader();
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 controller.enqueue('a');4 controller.enqueue('b');5 controller.enqueue('c');6 controller.close();7 }8});9var [branch1, branch2] = rs.tee();10var reader1 = branch1.getReader();11var reader2 = branch2.getReader();12const reader = rs.getReader({ mode: "byob" });13const view = new Uint8Array(new ArrayBuffer(2));14const { value, done } = await reader.read(view);15const view = new Uint8Array(new ArrayBuffer(2));16const byobRequest = rs.byobRequest;17byobRequest.respond(1);18const view = new Uint8Array(new ArrayBuffer(2));19const byobRequest = rs.byobRequest;20byobRequest.respondWithNewView(view);21const rs = new ReadableStream({22 start(controller) {23 controller.close();24 }25});26const reader = rs.getReader();27const rs = new ReadableStream({28 start(controller) {29 controller.enqueue("a");30 controller.enqueue("b");31 controller.enqueue("c");32 }33});
Using AI Code Generation
1var rs = new ReadableStream({2 start: function(controller) {3 controller.enqueue("a");4 controller.enqueue("b");5 controller.enqueue("c");6 controller.close();7 }8});9var [branch1, branch2] = rs.tee();10var reader1 = branch1.getReader();11var reader2 = branch2.getReader();12var read1 = reader1.read();13var read2 = reader2.read();14read1.then(function(result1) {15 console.log(result1);16 return read2;17}).then(function(result2) {18 console.log(result2);19 return reader1.read();20}).then(function(result3) {21 console.log(result3);22 return reader2.read();23}).then(function(result4) {24 console.log(result4);25});
Using AI Code Generation
1const { ReadableStream, ReadableStreamTee } = require("streams");2const rs = new ReadableStream({3 start(controller) {4 controller.enqueue("a");5 controller.enqueue("b");6 controller.enqueue("c");7 controller.close();8 }9});10const [branch1, branch2] = ReadableStreamTee(rs);11const reader1 = branch1.getReader();12const reader2 = branch2.getReader();13const { ReadableStream, ReadableStreamTee } = require("streams");14const rs = new ReadableStream({15 start(controller) {16 controller.enqueue("a");17 controller.enqueue("b");18 controller.enqueue("c");19 controller.close();20 }21});22const [branch1, branch2] = ReadableStreamTee(rs);23const reader1 = branch1.getReader();24const reader2 = branch2.getReader();25console.log(reader1
Using AI Code Generation
1import { ReadableStreamTee } from './readable-stream-tee.js';2const rs = new ReadableStream({3 start(controller) {4 console.log('[start]');5 controller.enqueue('a');6 controller.enqueue('b');7 controller.enqueue('c');8 },9 pull(controller) {10 console.log('[pull]');11 controller.enqueue('d');12 controller.close();13 },14 cancel(reason) {15 console.log('[cancel]', reason);16 }17});18const [branch1, branch2] = ReadableStreamTee(rs);19const reader1 = branch1.getReader();20const reader2 = branch2.getReader();21const read1 = reader => {22 reader.read().then(({ value, done }) => {23 if (done) {24 return console.log(`Stream 1 done`);25 }26 console.log(`Stream 1: ${value}`);27 read1(reader);28 });29};30const read2 = reader => {31 reader.read().then(({ value, done }) => {32 if (done) {33 return console.log(`Stream 2 done`);34 }35 console.log(`Stream 2: ${value}`);36 read2(reader);37 });38};39read1(reader1);40read2(reader2);41import { ReadableStreamTee } from './readable-stream-tee.js';42const rs = new ReadableStream({43 start(controller) {44 console.log('[start]');45 controller.enqueue('a');46 controller.enqueue('b');47 controller.enqueue('c');48 },49 pull(controller) {50 console.log('[pull]');51 controller.enqueue('d');52 controller.close();53 },54 cancel(reason) {55 console.log('[cancel]', reason);56 }57});
Using AI Code Generation
1var rs = new ReadableStream({2 start(controller) {3 }4});5var [branch1, branch2] = rs.tee();6var reader1 = branch1.getReader();7var reader2 = branch2.getReader();8var rs = new ReadableStream({9 start(controller) {10 }11});12var [branch1, branch2] = rs.tee();13var reader1 = branch1.getReader();14var reader2 = branch2.getReader();15var rs = new ReadableStream({16 start(controller) {17 }18});19var [branch1, branch2] = rs.tee();20var reader1 = branch1.getReader();21var reader2 = branch2.getReader();22console.log(reader
Using AI Code Generation
1const ReadableStreamTee = require('./readable-stream-tee.js');2const { ReadableStream } = require('./readable-stream.js');3const { WritableStream } = require('./writable-stream.js');4const { CountQueuingStrategy } = require('./count-queuing-strategy.js');5const rs = new ReadableStream({6 start(controller) {7 for (let i = 0; i < 10; ++i) {8 controller.enqueue(i);9 }10 controller.close();11 }12}, new CountQueuingStrategy({ highWaterMark: 5 }));13const [branch1, branch2] = ReadableStreamTee(rs);14const ws1 = new WritableStream({15 write(chunk) {16 console.log(`branch1: ${chunk}`);17 },18 close() {19 console.log('branch1 closed');20 }21});22const ws2 = new WritableStream({23 write(chunk) {24 console.log(`branch2: ${chunk}`);25 },26 close() {27 console.log('branch2 closed');28 }29});30branch1.pipeTo(ws1);31branch2.pipeTo(ws2);
Using AI Code Generation
1var rs = new ReadableStream();2var [branch1, branch2] = rs.tee();3var reader1 = branch1.getReader();4var reader2 = branch2.getReader();5var readAndLog = function(reader) {6 return reader.read().then(function(result) {7 if (result.done) {8 console.log("Stream complete");9 return;10 }11 console.log("Read: " + result.value);12 return readAndLog(reader);13 });14}15readAndLog(reader1);16readAndLog(reader2);
Using AI Code Generation
1import ReadableStreamTee from 'web-streams-polyfill/ponyfill/es6';2import ReadableStream from 'web-streams-polyfill/ponyfill/es6';3import WritableStream from 'web-streams-polyfill/ponyfill/es6';4import CountQueuingStrategy from 'web-streams-polyfill/ponyfill/es6';5import ByteLengthQueuingStrategy from 'web-streams-polyfill/ponyfill/es6';6import ReadableByteStreamController from 'web-streams-polyfill/ponyfill/es6';7const { ReadableStream, WritableStream, CountQueuingStrategy, ByteLengthQueuingStrategy, ReadableByteStreamController } = globalThis;8const start = async () => {9 const rs = new ReadableStream({
Using AI Code Generation
1function testReadableStreamTee() {2 const rs = new ReadableStream({3 start(controller) {4 controller.enqueue('a');5 controller.enqueue('b');6 controller.enqueue('c');7 controller.close();8 }9 });10 const [branch1, branch2] = rs.tee();11 const reader1 = branch1.getReader();12 const reader2 = branch2.getReader();13 let result1 = '';14 let result2 = '';15 const readAll = reader => {16 return reader.read().then(({ done, value }) => {17 if (done) {18 return;19 }20 result1 += value;21 result2 += value;22 return readAll(reader);23 });24 };25 return Promise.all([26 readAll(reader1),27 readAll(reader2)28 ]).then(() => {29 if (result1 === 'abc' && result2 === 'abc') {30 console.log('ReadableStreamTee test passed.');31 } else {32 console.log('ReadableStreamTee test failed.');33 }34 });35}36testReadableStreamTee();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!