Best JavaScript code snippet using wpt
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// META: script=../resources/rs-test-templates.js6'use strict';7test(() => {8 const rs = new ReadableStream({ type: 'bytes' });9 const result = rs.tee();10 assert_true(Array.isArray(result), 'return value should be an array');11 assert_equals(result.length, 2, 'array should have length 2');12 assert_equals(result[0].constructor, ReadableStream, '0th element should be a ReadableStream');13 assert_equals(result[1].constructor, ReadableStream, '1st element should be a ReadableStream');14}, 'ReadableStream teeing with byte source: rs.tee() returns an array of two ReadableStreams');15promise_test(async t => {16 const rs = new ReadableStream({17 type: 'bytes',18 start(c) {19 c.enqueue(new Uint8Array([0x01]));20 c.enqueue(new Uint8Array([0x02]));21 c.close();22 }23 });24 const [branch1, branch2] = rs.tee();25 const reader1 = branch1.getReader({ mode: 'byob' });26 const reader2 = branch2.getReader({ mode: 'byob' });27 reader2.closed.then(t.unreached_func('branch2 should not be closed'));28 {29 const result = await reader1.read(new Uint8Array(1));30 assert_equals(result.done, false, 'done');31 assert_typed_array_equals(result.value, new Uint8Array([0x01]), 'value');32 }33 {34 const result = await reader1.read(new Uint8Array(1));35 assert_equals(result.done, false, 'done');36 assert_typed_array_equals(result.value, new Uint8Array([0x02]), 'value');37 }38 {39 const result = await reader1.read(new Uint8Array(1));40 assert_equals(result.done, true, 'done');41 assert_typed_array_equals(result.value, new Uint8Array([0]).subarray(0, 0), 'value');42 }43 {44 const result = await reader2.read(new Uint8Array(1));45 assert_equals(result.done, false, 'done');46 assert_typed_array_equals(result.value, new Uint8Array([0x01]), 'value');47 }48 await reader1.closed;49}, 'ReadableStream teeing with byte source: should be able to read one branch to the end without affecting the other');50promise_test(async () => {51 let pullCount = 0;52 const enqueuedChunk = new Uint8Array([0x01]);53 const rs = new ReadableStream({54 type: 'bytes',55 pull(c) {56 ++pullCount;57 if (pullCount === 1) {58 c.enqueue(enqueuedChunk);59 }60 }61 });62 const [branch1, branch2] = rs.tee();63 const reader1 = branch1.getReader();64 const reader2 = branch2.getReader();65 const [result1, result2] = await Promise.all([reader1.read(), reader2.read()]);66 assert_equals(result1.done, false, 'reader1 done');67 assert_equals(result2.done, false, 'reader2 done');68 const view1 = result1.value;69 const view2 = result2.value;70 assert_typed_array_equals(view1, new Uint8Array([0x01]), 'reader1 value');71 assert_typed_array_equals(view2, new Uint8Array([0x01]), 'reader2 value');72 assert_not_equals(view1.buffer, view2.buffer, 'chunks should have different buffers');73 assert_not_equals(enqueuedChunk.buffer, view1.buffer, 'enqueued chunk and branch1\'s chunk should have different buffers');74 assert_not_equals(enqueuedChunk.buffer, view2.buffer, 'enqueued chunk and branch2\'s chunk should have different buffers');75}, 'ReadableStream teeing with byte source: chunks should be cloned for each branch');76promise_test(async () => {77 let pullCount = 0;78 const rs = new ReadableStream({79 type: 'bytes',80 pull(c) {81 ++pullCount;82 if (pullCount === 1) {83 c.byobRequest.view[0] = 0x01;84 c.byobRequest.respond(1);85 }86 }87 });88 const [branch1, branch2] = rs.tee();89 const reader1 = branch1.getReader({ mode: 'byob' });90 const reader2 = branch2.getReader();91 const buffer = new Uint8Array([42, 42, 42]).buffer;92 {93 const result = await reader1.read(new Uint8Array(buffer, 0, 1));94 assert_equals(result.done, false, 'done');95 assert_typed_array_equals(result.value, new Uint8Array([0x01, 42, 42]).subarray(0, 1), 'value');96 }97 {98 const result = await reader2.read();99 assert_equals(result.done, false, 'done');100 assert_typed_array_equals(result.value, new Uint8Array([0x01]), 'value');101 }102}, 'ReadableStream teeing with byte source: chunks for BYOB requests from branch 1 should be cloned to branch 2');103promise_test(async t => {104 const theError = { name: 'boo!' };105 const rs = new ReadableStream({106 type: 'bytes',107 start(c) {108 c.enqueue(new Uint8Array([0x01]));109 c.enqueue(new Uint8Array([0x02]));110 },111 pull() {112 throw theError;113 }114 });115 const [branch1, branch2] = rs.tee();116 const reader1 = branch1.getReader({ mode: 'byob' });117 const reader2 = branch2.getReader({ mode: 'byob' });118 {119 const result = await reader1.read(new Uint8Array(1));120 assert_equals(result.done, false, 'first read from branch1 should not be done');121 assert_typed_array_equals(result.value, new Uint8Array([0x01]), 'first read from branch1');122 }123 {124 const result = await reader1.read(new Uint8Array(1));125 assert_equals(result.done, false, 'second read from branch1 should not be done');126 assert_typed_array_equals(result.value, new Uint8Array([0x02]), 'second read from branch1');127 }128 await promise_rejects_exactly(t, theError, reader1.read(new Uint8Array(1)));129 await promise_rejects_exactly(t, theError, reader2.read(new Uint8Array(1)));130 await Promise.all([131 promise_rejects_exactly(t, theError, reader1.closed),132 promise_rejects_exactly(t, theError, reader2.closed)133 ]);134}, 'ReadableStream teeing with byte source: errors in the source should propagate to both branches');135promise_test(async () => {136 const rs = new ReadableStream({137 type: 'bytes',138 start(c) {139 c.enqueue(new Uint8Array([0x01]));140 c.enqueue(new Uint8Array([0x02]));141 c.close();142 }143 });144 const [branch1, branch2] = rs.tee();145 branch1.cancel();146 const [chunks1, chunks2] = await Promise.all([readableStreamToArray(branch1), readableStreamToArray(branch2)]);147 assert_array_equals(chunks1, [], 'branch1 should have no chunks');148 assert_equals(chunks2.length, 2, 'branch2 should have two chunks');149 assert_typed_array_equals(chunks2[0], new Uint8Array([0x01]), 'first chunk from branch2');150 assert_typed_array_equals(chunks2[1], new Uint8Array([0x02]), 'second chunk from branch2');151}, 'ReadableStream teeing with byte source: canceling branch1 should not impact branch2');152promise_test(async () => {153 const rs = new ReadableStream({154 type: 'bytes',155 start(c) {156 c.enqueue(new Uint8Array([0x01]));157 c.enqueue(new Uint8Array([0x02]));158 c.close();159 }160 });161 const [branch1, branch2] = rs.tee();162 branch2.cancel();163 const [chunks1, chunks2] = await Promise.all([readableStreamToArray(branch1), readableStreamToArray(branch2)]);164 assert_equals(chunks1.length, 2, 'branch1 should have two chunks');165 assert_typed_array_equals(chunks1[0], new Uint8Array([0x01]), 'first chunk from branch1');166 assert_typed_array_equals(chunks1[1], new Uint8Array([0x02]), 'second chunk from branch1');167 assert_array_equals(chunks2, [], 'branch2 should have no chunks');168}, 'ReadableStream teeing with byte source: canceling branch2 should not impact branch1');169templatedRSTeeCancel('ReadableStream teeing with byte source', (extras) => {170 return new ReadableStream({ type: 'bytes', ...extras });171});172promise_test(async () => {173 let controller;174 const rs = new ReadableStream({175 type: 'bytes',176 start(c) {177 controller = c;178 }179 });180 const [branch1, branch2] = rs.tee();181 const reader1 = branch1.getReader({ mode: 'byob' });182 const reader2 = branch2.getReader({ mode: 'byob' });183 const promise = Promise.all([reader1.closed, reader2.closed]);184 controller.close();185 // The branches are created with HWM 0, so we need to read from at least one of them186 // to observe the stream becoming closed.187 const read1 = await reader1.read(new Uint8Array(1));188 assert_equals(read1.done, true, 'first read from branch1 should be done');189 await promise;190}, 'ReadableStream teeing with byte source: closing the original should close the branches');191promise_test(async t => {192 let controller;193 const rs = new ReadableStream({194 type: 'bytes',195 start(c) {196 controller = c;197 }198 });199 const [branch1, branch2] = rs.tee();200 const reader1 = branch1.getReader({ mode: 'byob' });201 const reader2 = branch2.getReader({ mode: 'byob' });202 const theError = { name: 'boo!' };203 const promise = Promise.all([204 promise_rejects_exactly(t, theError, reader1.closed),205 promise_rejects_exactly(t, theError, reader2.closed)206 ]);207 controller.error(theError);208 await promise;209}, 'ReadableStream teeing with byte source: erroring the original should immediately error the branches');210promise_test(async t => {211 let controller;212 const rs = new ReadableStream({213 type: 'bytes',214 start(c) {215 controller = c;216 }217 });218 const [branch1, branch2] = rs.tee();219 const reader1 = branch1.getReader();220 const reader2 = branch2.getReader();221 const theError = { name: 'boo!' };222 const promise = Promise.all([223 promise_rejects_exactly(t, theError, reader1.read()),224 promise_rejects_exactly(t, theError, reader2.read())225 ]);226 controller.error(theError);227 await promise;228}, 'ReadableStream teeing with byte source: erroring the original should error pending reads from default reader');229promise_test(async t => {230 let controller;231 const rs = new ReadableStream({232 type: 'bytes',233 start(c) {234 controller = c;235 }236 });237 const [branch1, branch2] = rs.tee();238 const reader1 = branch1.getReader({ mode: 'byob' });239 const reader2 = branch2.getReader({ mode: 'byob' });240 const theError = { name: 'boo!' };241 const promise = Promise.all([242 promise_rejects_exactly(t, theError, reader1.read(new Uint8Array(1))),243 promise_rejects_exactly(t, theError, reader2.read(new Uint8Array(1)))244 ]);245 controller.error(theError);246 await promise;247}, 'ReadableStream teeing with byte source: erroring the original should error pending reads from BYOB reader');248promise_test(async () => {249 let controller;250 const rs = new ReadableStream({251 type: 'bytes',252 start(c) {253 controller = c;254 }255 });256 const [branch1, branch2] = rs.tee();257 const reader1 = branch1.getReader({ mode: 'byob' });258 const reader2 = branch2.getReader({ mode: 'byob' });259 const cancelPromise = reader2.cancel();260 controller.enqueue(new Uint8Array([0x01]));261 const read1 = await reader1.read(new Uint8Array(1));262 assert_equals(read1.done, false, 'first read() from branch1 should not be done');263 assert_typed_array_equals(read1.value, new Uint8Array([0x01]), 'first read() from branch1');264 controller.close();265 const read2 = await reader1.read(new Uint8Array(1));266 assert_equals(read2.done, true, 'second read() from branch1 should be done');267 await Promise.all([268 reader1.closed,269 cancelPromise270 ]);271}, 'ReadableStream teeing with byte source: canceling branch1 should finish when branch2 reads until end of stream');272promise_test(async t => {273 let controller;274 const theError = { name: 'boo!' };275 const rs = new ReadableStream({276 type: 'bytes',277 start(c) {278 controller = c;279 }280 });281 const [branch1, branch2] = rs.tee();282 const reader1 = branch1.getReader({ mode: 'byob' });283 const reader2 = branch2.getReader({ mode: 'byob' });284 const cancelPromise = reader2.cancel();285 controller.error(theError);286 await Promise.all([287 promise_rejects_exactly(t, theError, reader1.read(new Uint8Array(1))),288 cancelPromise289 ]);290}, 'ReadableStream teeing with byte source: canceling branch1 should finish when original stream errors');291promise_test(async () => {292 const rs = recordingReadableStream({ type: 'bytes' });293 // Create two branches, each with a HWM of 0. This should result in no chunks being pulled.294 rs.tee();295 await flushAsyncEvents();296 assert_array_equals(rs.events, [], 'pull should not be called');297}, 'ReadableStream teeing with byte source: should not pull any chunks if no branches are reading');298promise_test(async () => {299 const rs = recordingReadableStream({300 type: 'bytes',301 pull(controller) {302 controller.enqueue(new Uint8Array([0x01]));303 }304 });305 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));306 await Promise.all([307 reader1.read(new Uint8Array(1)),308 reader2.read(new Uint8Array(1))309 ]);310 assert_array_equals(rs.events, ['pull'], 'pull should be called once');311}, 'ReadableStream teeing with byte source: should only pull enough to fill the emptiest queue');312promise_test(async t => {313 const rs = recordingReadableStream({ type: 'bytes' });314 const theError = { name: 'boo!' };315 rs.controller.error(theError);316 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));317 await flushAsyncEvents();318 assert_array_equals(rs.events, [], 'pull should not be called');319 await Promise.all([320 promise_rejects_exactly(t, theError, reader1.closed),321 promise_rejects_exactly(t, theError, reader2.closed)322 ]);323}, 'ReadableStream teeing with byte source: should not pull when original is already errored');324for (const branch of [1, 2]) {325 promise_test(async t => {326 const rs = recordingReadableStream({ type: 'bytes' });327 const theError = { name: 'boo!' };328 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));329 await flushAsyncEvents();330 assert_array_equals(rs.events, [], 'pull should not be called');331 const reader = (branch === 1) ? reader1 : reader2;332 const read1 = reader.read(new Uint8Array(1));333 await flushAsyncEvents();334 assert_array_equals(rs.events, ['pull'], 'pull should be called once');335 rs.controller.error(theError);336 await Promise.all([337 promise_rejects_exactly(t, theError, read1),338 promise_rejects_exactly(t, theError, reader1.closed),339 promise_rejects_exactly(t, theError, reader2.closed)340 ]);341 await flushAsyncEvents();342 assert_array_equals(rs.events, ['pull'], 'pull should be called once');343 }, `ReadableStream teeing with byte source: stops pulling when original stream errors while branch ${branch} is reading`);344}345promise_test(async t => {346 const rs = recordingReadableStream({ type: 'bytes' });347 const theError = { name: 'boo!' };348 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));349 await flushAsyncEvents();350 assert_array_equals(rs.events, [], 'pull should not be called');351 const read1 = reader1.read(new Uint8Array(1));352 const read2 = reader2.read(new Uint8Array(1));353 await flushAsyncEvents();354 assert_array_equals(rs.events, ['pull'], 'pull should be called once');355 rs.controller.error(theError);356 await Promise.all([357 promise_rejects_exactly(t, theError, read1),358 promise_rejects_exactly(t, theError, read2),359 promise_rejects_exactly(t, theError, reader1.closed),360 promise_rejects_exactly(t, theError, reader2.closed)361 ]);362 await flushAsyncEvents();363 assert_array_equals(rs.events, ['pull'], 'pull should be called once');364}, 'ReadableStream teeing with byte source: stops pulling when original stream errors while both branches are reading');365promise_test(async () => {366 const rs = recordingReadableStream({ type: 'bytes' });367 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));368 const read1 = reader1.read(new Uint8Array([0x11]));369 const read2 = reader2.read(new Uint8Array([0x22]));370 const cancel1 = reader1.cancel();371 await flushAsyncEvents();372 const cancel2 = reader2.cancel();373 const result1 = await read1;374 assert_object_equals(result1, { value: undefined, done: true });375 const result2 = await read2;376 assert_object_equals(result2, { value: undefined, done: true });377 await Promise.all([cancel1, cancel2]);378}, 'ReadableStream teeing with byte source: canceling both branches in sequence with delay');379promise_test(async t => {380 const theError = { name: 'boo!' };381 const rs = new ReadableStream({382 type: 'bytes',383 cancel() {384 throw theError;385 }386 });387 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));388 const read1 = reader1.read(new Uint8Array([0x11]));389 const read2 = reader2.read(new Uint8Array([0x22]));390 const cancel1 = reader1.cancel();391 await flushAsyncEvents();392 const cancel2 = reader2.cancel();393 const result1 = await read1;394 assert_object_equals(result1, { value: undefined, done: true });395 const result2 = await read2;396 assert_object_equals(result2, { value: undefined, done: true });397 await Promise.all([398 promise_rejects_exactly(t, theError, cancel1),399 promise_rejects_exactly(t, theError, cancel2)400 ]);401}, 'ReadableStream teeing with byte source: failing to cancel when canceling both branches in sequence with delay');402promise_test(async () => {403 let cancelResolve;404 const cancelCalled = new Promise((resolve) => {405 cancelResolve = resolve;406 });407 const rs = recordingReadableStream({408 type: 'bytes',409 cancel() {410 cancelResolve();411 }412 });413 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));414 const read1 = reader1.read(new Uint8Array([0x11]));415 await flushAsyncEvents();416 const read2 = reader2.read(new Uint8Array([0x22]));417 await flushAsyncEvents();418 // We are reading into branch1's buffer.419 const byobRequest1 = rs.controller.byobRequest;420 assert_not_equals(byobRequest1, null);421 assert_typed_array_equals(byobRequest1.view, new Uint8Array([0x11]), 'byobRequest1.view');422 // Cancelling branch1 should not affect the BYOB request.423 const cancel1 = reader1.cancel();424 const result1 = await read1;425 assert_equals(result1.done, true);426 assert_equals(result1.value, undefined);427 await flushAsyncEvents();428 const byobRequest2 = rs.controller.byobRequest;429 assert_typed_array_equals(byobRequest2.view, new Uint8Array([0x11]), 'byobRequest2.view');430 // Cancelling branch1 should invalidate the BYOB request.431 const cancel2 = reader2.cancel();432 await cancelCalled;433 const byobRequest3 = rs.controller.byobRequest;434 assert_equals(byobRequest3, null);435 const result2 = await read2;436 assert_equals(result2.done, true);437 assert_equals(result2.value, undefined);438 await Promise.all([cancel1, cancel2]);439}, 'ReadableStream teeing with byte source: read from branch1 and branch2, cancel branch1, cancel branch2');440promise_test(async () => {441 let cancelResolve;442 const cancelCalled = new Promise((resolve) => {443 cancelResolve = resolve;444 });445 const rs = recordingReadableStream({446 type: 'bytes',447 cancel() {448 cancelResolve();449 }450 });451 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));452 const read1 = reader1.read(new Uint8Array([0x11]));453 await flushAsyncEvents();454 const read2 = reader2.read(new Uint8Array([0x22]));455 await flushAsyncEvents();456 // We are reading into branch1's buffer.457 const byobRequest1 = rs.controller.byobRequest;458 assert_not_equals(byobRequest1, null);459 assert_typed_array_equals(byobRequest1.view, new Uint8Array([0x11]), 'byobRequest1.view');460 // Cancelling branch2 should not affect the BYOB request.461 const cancel2 = reader2.cancel();462 const result2 = await read2;463 assert_equals(result2.done, true);464 assert_equals(result2.value, undefined);465 await flushAsyncEvents();466 const byobRequest2 = rs.controller.byobRequest;467 assert_typed_array_equals(byobRequest2.view, new Uint8Array([0x11]), 'byobRequest2.view');468 // Cancelling branch1 should invalidate the BYOB request.469 const cancel1 = reader1.cancel();470 await cancelCalled;471 const byobRequest3 = rs.controller.byobRequest;472 assert_equals(byobRequest3, null);473 const result1 = await read1;474 assert_equals(result1.done, true);475 assert_equals(result1.value, undefined);476 await Promise.all([cancel1, cancel2]);477}, 'ReadableStream teeing with byte source: read from branch1 and branch2, cancel branch2, cancel branch1');478promise_test(async () => {479 const rs = recordingReadableStream({ type: 'bytes' });480 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));481 const read1 = reader1.read(new Uint8Array([0x11]));482 await flushAsyncEvents();483 const read2 = reader2.read(new Uint8Array([0x22]));484 await flushAsyncEvents();485 // We are reading into branch1's buffer.486 assert_typed_array_equals(rs.controller.byobRequest.view, new Uint8Array([0x11]), 'first byobRequest.view');487 // Cancelling branch2 should not affect the BYOB request.488 reader2.cancel();489 const result2 = await read2;490 assert_equals(result2.done, true);491 assert_equals(result2.value, undefined);492 await flushAsyncEvents();493 assert_typed_array_equals(rs.controller.byobRequest.view, new Uint8Array([0x11]), 'second byobRequest.view');494 // Respond to the BYOB request.495 rs.controller.byobRequest.view[0] = 0x33;496 rs.controller.byobRequest.respond(1);497 // branch1 should receive the read chunk.498 const result1 = await read1;499 assert_equals(result1.done, false);500 assert_typed_array_equals(result1.value, new Uint8Array([0x33]), 'first read() from branch1');501}, 'ReadableStream teeing with byte source: read from branch1 and branch2, cancel branch2, enqueue to branch1');502promise_test(async () => {503 const rs = recordingReadableStream({ type: 'bytes' });504 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));505 const read1 = reader1.read(new Uint8Array([0x11]));506 await flushAsyncEvents();507 const read2 = reader2.read(new Uint8Array([0x22]));508 await flushAsyncEvents();509 // We are reading into branch1's buffer.510 assert_typed_array_equals(rs.controller.byobRequest.view, new Uint8Array([0x11]), 'first byobRequest.view');511 // Cancelling branch1 should not affect the BYOB request.512 reader1.cancel();513 const result1 = await read1;514 assert_equals(result1.done, true);515 assert_equals(result1.value, undefined);516 await flushAsyncEvents();517 assert_typed_array_equals(rs.controller.byobRequest.view, new Uint8Array([0x11]), 'second byobRequest.view');518 // Respond to the BYOB request.519 rs.controller.byobRequest.view[0] = 0x33;520 rs.controller.byobRequest.respond(1);521 // branch2 should receive the read chunk.522 const result2 = await read2;523 assert_equals(result2.done, false);524 assert_typed_array_equals(result2.value, new Uint8Array([0x33]), 'first read() from branch2');525}, 'ReadableStream teeing with byte source: read from branch1 and branch2, cancel branch1, respond to branch2');526promise_test(async () => {527 let pullCount = 0;528 const byobRequestDefined = [];529 const rs = new ReadableStream({530 type: 'bytes',531 pull(c) {532 ++pullCount;533 byobRequestDefined.push(c.byobRequest !== null);534 c.enqueue(new Uint8Array([pullCount]));535 }536 });537 const [branch1, _] = rs.tee();538 const reader1 = branch1.getReader({ mode: 'byob' });539 const result1 = await reader1.read(new Uint8Array([0x11]));540 assert_equals(result1.done, false, 'first read should not be done');541 assert_typed_array_equals(result1.value, new Uint8Array([0x1]), 'first read');542 assert_equals(pullCount, 1, 'pull() should be called once');543 assert_equals(byobRequestDefined[0], true, 'should have created a BYOB request for first read');544 reader1.releaseLock();545 const reader2 = branch1.getReader();546 const result2 = await reader2.read();547 assert_equals(result2.done, false, 'second read should not be done');548 assert_typed_array_equals(result2.value, new Uint8Array([0x2]), 'second read');549 assert_equals(pullCount, 2, 'pull() should be called twice');550 assert_equals(byobRequestDefined[1], false, 'should not have created a BYOB request for second read');551}, 'ReadableStream teeing with byte source: pull with BYOB reader, then pull with default reader');552promise_test(async () => {553 let pullCount = 0;554 const byobRequestDefined = [];555 const rs = new ReadableStream({556 type: 'bytes',557 pull(c) {558 ++pullCount;559 byobRequestDefined.push(c.byobRequest !== null);560 c.enqueue(new Uint8Array([pullCount]));561 }562 });563 const [branch1, _] = rs.tee();564 const reader1 = branch1.getReader();565 const result1 = await reader1.read();566 assert_equals(result1.done, false, 'first read should not be done');567 assert_typed_array_equals(result1.value, new Uint8Array([0x1]), 'first read');568 assert_equals(pullCount, 1, 'pull() should be called once');569 assert_equals(byobRequestDefined[0], false, 'should not have created a BYOB request for first read');570 reader1.releaseLock();571 const reader2 = branch1.getReader({ mode: 'byob' });572 const result2 = await reader2.read(new Uint8Array([0x22]));573 assert_equals(result2.done, false, 'second read should not be done');574 assert_typed_array_equals(result2.value, new Uint8Array([0x2]), 'second read');575 assert_equals(pullCount, 2, 'pull() should be called twice');576 assert_equals(byobRequestDefined[1], true, 'should have created a BYOB request for second read');577}, 'ReadableStream teeing with byte source: pull with default reader, then pull with BYOB reader');578promise_test(async () => {579 const rs = recordingReadableStream({580 type: 'bytes'581 });582 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));583 // Wait for each branch's start() promise to resolve.584 await flushAsyncEvents();585 const read2 = reader2.read(new Uint8Array([0x22]));586 const read1 = reader1.read(new Uint8Array([0x11]));587 await flushAsyncEvents();588 // branch2 should provide the BYOB request.589 const byobRequest = rs.controller.byobRequest;590 assert_typed_array_equals(byobRequest.view, new Uint8Array([0x22]), 'first BYOB request');591 byobRequest.view[0] = 0x01;592 byobRequest.respond(1);593 const result1 = await read1;594 assert_equals(result1.done, false, 'first read should not be done');595 assert_typed_array_equals(result1.value, new Uint8Array([0x1]), 'first read');596 const result2 = await read2;597 assert_equals(result2.done, false, 'second read should not be done');598 assert_typed_array_equals(result2.value, new Uint8Array([0x1]), 'second read');599}, 'ReadableStream teeing with byte source: read from branch2, then read from branch1');600promise_test(async () => {601 const rs = recordingReadableStream({ type: 'bytes' });602 const [branch1, branch2] = rs.tee();603 const reader1 = branch1.getReader();604 const reader2 = branch2.getReader({ mode: 'byob' });605 await flushAsyncEvents();606 const read1 = reader1.read();607 const read2 = reader2.read(new Uint8Array([0x22]));608 await flushAsyncEvents();609 // There should be no BYOB request.610 assert_equals(rs.controller.byobRequest, null, 'first BYOB request');611 // Close the stream.612 rs.controller.close();613 const result1 = await read1;614 assert_equals(result1.done, true, 'read from branch1 should be done');615 assert_equals(result1.value, undefined, 'read from branch1');616 // branch2 should get its buffer back.617 const result2 = await read2;618 assert_equals(result2.done, true, 'read from branch2 should be done');619 assert_typed_array_equals(result2.value, new Uint8Array([0x22]).subarray(0, 0), 'read from branch2');620}, 'ReadableStream teeing with byte source: read from branch1 with default reader, then close while branch2 has pending BYOB read');621promise_test(async () => {622 const rs = recordingReadableStream({ type: 'bytes' });623 const [branch1, branch2] = rs.tee();624 const reader1 = branch1.getReader({ mode: 'byob' });625 const reader2 = branch2.getReader();626 await flushAsyncEvents();627 const read2 = reader2.read();628 const read1 = reader1.read(new Uint8Array([0x11]));629 await flushAsyncEvents();630 // There should be no BYOB request.631 assert_equals(rs.controller.byobRequest, null, 'first BYOB request');632 // Close the stream.633 rs.controller.close();634 const result2 = await read2;635 assert_equals(result2.done, true, 'read from branch2 should be done');636 assert_equals(result2.value, undefined, 'read from branch2');637 // branch1 should get its buffer back.638 const result1 = await read1;639 assert_equals(result1.done, true, 'read from branch1 should be done');640 assert_typed_array_equals(result1.value, new Uint8Array([0x11]).subarray(0, 0), 'read from branch1');641}, 'ReadableStream teeing with byte source: read from branch2 with default reader, then close while branch1 has pending BYOB read');642promise_test(async () => {643 const rs = recordingReadableStream({ type: 'bytes' });644 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));645 await flushAsyncEvents();646 const read1 = reader1.read(new Uint8Array([0x11]));647 const read2 = reader2.read(new Uint8Array([0x22]));648 await flushAsyncEvents();649 // branch1 should provide the BYOB request.650 const byobRequest = rs.controller.byobRequest;651 assert_typed_array_equals(byobRequest.view, new Uint8Array([0x11]), 'first BYOB request');652 // Close the stream.653 rs.controller.close();654 byobRequest.respond(0);655 // Both branches should get their buffers back.656 const result1 = await read1;657 assert_equals(result1.done, true, 'first read should be done');658 assert_typed_array_equals(result1.value, new Uint8Array([0x11]).subarray(0, 0), 'first read');659 const result2 = await read2;660 assert_equals(result2.done, true, 'second read should be done');661 assert_typed_array_equals(result2.value, new Uint8Array([0x22]).subarray(0, 0), 'second read');662}, 'ReadableStream teeing with byte source: close when both branches have pending BYOB reads');663promise_test(async () => {664 const rs = recordingReadableStream({ type: 'bytes' });665 const [reader1, reader2] = rs.tee().map(branch => branch.getReader());666 const branch1Reads = [reader1.read(), reader1.read()];667 const branch2Reads = [reader2.read(), reader2.read()];668 await flushAsyncEvents();669 rs.controller.enqueue(new Uint8Array([0x11]));670 rs.controller.close();671 const result1 = await branch1Reads[0];672 assert_equals(result1.done, false, 'first read() from branch1 should be not done');673 assert_typed_array_equals(result1.value, new Uint8Array([0x11]), 'first chunk from branch1 should be correct');674 const result2 = await branch2Reads[0];675 assert_equals(result2.done, false, 'first read() from branch2 should be not done');676 assert_typed_array_equals(result2.value, new Uint8Array([0x11]), 'first chunk from branch2 should be correct');677 assert_object_equals(await branch1Reads[1], { value: undefined, done: true }, 'second read() from branch1 should be done');678 assert_object_equals(await branch2Reads[1], { value: undefined, done: true }, 'second read() from branch2 should be done');679}, 'ReadableStream teeing with byte source: enqueue() and close() while both branches are pulling');680promise_test(async () => {681 const rs = recordingReadableStream({ type: 'bytes' });682 const [reader1, reader2] = rs.tee().map(branch => branch.getReader({ mode: 'byob' }));683 const branch1Reads = [reader1.read(new Uint8Array(1)), reader1.read(new Uint8Array(1))];684 const branch2Reads = [reader2.read(new Uint8Array(1)), reader2.read(new Uint8Array(1))];685 await flushAsyncEvents();686 rs.controller.byobRequest.view[0] = 0x11;687 rs.controller.byobRequest.respond(1);688 rs.controller.close();689 const result1 = await branch1Reads[0];690 assert_equals(result1.done, false, 'first read() from branch1 should be not done');691 assert_typed_array_equals(result1.value, new Uint8Array([0x11]), 'first chunk from branch1 should be correct');692 const result2 = await branch2Reads[0];693 assert_equals(result2.done, false, 'first read() from branch2 should be not done');694 assert_typed_array_equals(result2.value, new Uint8Array([0x11]), 'first chunk from branch2 should be correct');695 const result3 = await branch1Reads[1];696 assert_equals(result3.done, true, 'second read() from branch1 should be done');697 assert_typed_array_equals(result3.value, new Uint8Array([0]).subarray(0, 0), 'second chunk from branch1 should be correct');698 const result4 = await branch2Reads[1];699 assert_equals(result4.done, true, 'second read() from branch2 should be done');700 assert_typed_array_equals(result4.value, new Uint8Array([0]).subarray(0, 0), 'second chunk from branch2 should be correct');...
Using AI Code Generation
1var wpt = require('../lib/webpagetest.js');2var wpt = new wpt('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 console.log('Test ID: ' + data.data.testId);7 wpt.getTestResults(data.data.testId, function(err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13 });14 }15});
Using AI Code Generation
1var page = require('webpage').create();2page.onResourceReceived = function(response) {3 if (response.stage === "end") {4 console.log("Response (#" + response.id + ", stage " + response.stage + "): " + response.url);5 console.log(" Content type: " + response.contentType);6 console.log(" Content size: " + response.bodySize);7 }8};9page.onResourceRequested = function(requestData, networkRequest) {10 console.log("Request (#" + requestData.id + "): " + requestData.url);11};12page.open(url, function(status) {13 if (status !== 'success') {14 console.log('Unable to access network');15 } else {16 var content = page.content;17 console.log(content);18 }19 phantom.exit();20});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.runTest(url, options, function(err, data) {6 if (err) return console.log(err);7 console.log('Test %d started:', data.data.testId);8 console.log('View your test at %sresult/%d/', wpt.rootUrl, data.data.testId);9 console.log('Polling for results...');10 wpt.getTestResults(data.data.testId, function(err, data) {11 if (err) return console.log(err);12 console.log('Test %d completed', data.data.testId);13 console.log('First View (load time): %d ms', data.data.runs[1].firstView.loadTime);14 console.log('First View (Speed Index): %d', data.data.runs[1].firstView.SpeedIndex);15 console.log('First View (visualComplete): %d', data.data.runs[1].firstView.visualComplete);16 console.log('First View (TTFB): %d ms', data.data.runs[1].firstView.TTFB);17 console.log('First View (render): %d ms', data.data.runs[1].firstView.render);18 console.log('First View (fullyLoaded): %d ms', data.data.runs[1].firstView.fullyLoaded);19 console.log('First View (bytesIn): %d', data.data.runs[1].firstView.bytesIn);20 console.log('First View (bytesOut): %d', data.data.runs[1].firstView.bytesOut);21 console.log('First View (requests): %d', data.data.runs[1].firstView.requests);22 console.log('First View (responses_200): %d', data.data.runs[1].firstView.responses_200);23 console.log('First View (responses_404): %d', data.data.runs[1].firstView.responses_404);24 console.log('First View (
Using AI Code Generation
1var wpt = require('./wpt.js');2var http = require('http');3http.createServer(function (req, res) {4 res.writeHead(200, {'Content-Type': 'text/plain'});5 wpt.enqueuedChunk("Hello World");6 res.end();7}).listen(8080, '
Using AI Code Generation
1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.2f2e0e7a6b0a1f7c9d1a8b7d0f9b6c7a');3 if (err) {4 console.log(err);5 }6 else {7 wpt.getTestResults(data.data.testId, function(err, data) {8 if (err) {9 console.log(err);10 }11 else {12 console.log(data);13 }14 });15 }16});
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!!