Best JavaScript code snippet using wpt
aborting.js
Source:aborting.js
1'use strict';2if (self.importScripts) {3 self.importScripts('/resources/testharness.js');4 self.importScripts('../resources/test-utils.js');5 self.importScripts('../resources/recording-streams.js');6}7const error1 = new Error('error1');8error1.name = 'error1';9const error2 = new Error('error2');10error2.name = 'error2';11promise_test(t => {12 const ws = new WritableStream({13 write: t.unreached_func('write() should not be called')14 });15 const writer = ws.getWriter();16 const writePromise = writer.write('a');17 const readyPromise = writer.ready;18 writer.abort(error1);19 assert_equals(writer.ready, readyPromise, 'the ready promise property should not change');20 return Promise.all([21 promise_rejects(t, new TypeError(), readyPromise, 'the ready promise should reject with a TypeError'),22 promise_rejects(t, new TypeError(), writePromise, 'the write() promise should reject with a TypeError')23 ]);24}, 'Aborting a WritableStream before it starts should cause the writer\'s unsettled ready promise to reject');25promise_test(t => {26 const ws = new WritableStream();27 const writer = ws.getWriter();28 writer.write('a');29 const readyPromise = writer.ready;30 return readyPromise.then(() => {31 writer.abort(error1);32 assert_not_equals(writer.ready, readyPromise, 'the ready promise property should change');33 return promise_rejects(t, new TypeError(), writer.ready, 'the ready promise should reject with a TypeError');34 });35}, 'Aborting a WritableStream should cause the writer\'s fulfilled ready promise to reset to a rejected one');36promise_test(t => {37 const ws = new WritableStream();38 const writer = ws.getWriter();39 writer.releaseLock();40 return promise_rejects(t, new TypeError(), writer.abort(), 'abort() should reject with a TypeError');41}, 'abort() on a released writer rejects');42promise_test(t => {43 const ws = recordingWritableStream();44 return delay(0)45 .then(() => {46 const writer = ws.getWriter();47 const abortPromise = writer.abort();48 return Promise.all([49 promise_rejects(t, new TypeError(), writer.write(1), 'write(1) must reject with a TypeError'),50 promise_rejects(t, new TypeError(), writer.write(2), 'write(2) must reject with a TypeError'),51 abortPromise52 ]);53 })54 .then(() => {55 assert_array_equals(ws.events, ['abort', undefined]);56 });57}, 'Aborting a WritableStream immediately prevents future writes');58promise_test(t => {59 const ws = recordingWritableStream();60 const results = [];61 return delay(0)62 .then(() => {63 const writer = ws.getWriter();64 results.push(65 writer.write(1),66 promise_rejects(t, new TypeError(), writer.write(2), 'write(2) must reject with a TypeError'),67 promise_rejects(t, new TypeError(), writer.write(3), 'write(3) must reject with a TypeError')68 );69 const abortPromise = writer.abort();70 results.push(71 promise_rejects(t, new TypeError(), writer.write(4), 'write(4) must reject with a TypeError'),72 promise_rejects(t, new TypeError(), writer.write(5), 'write(5) must reject with a TypeError')73 );74 return abortPromise;75 }).then(() => {76 assert_array_equals(ws.events, ['write', 1, 'abort', undefined]);77 return Promise.all(results);78 });79}, 'Aborting a WritableStream prevents further writes after any that are in progress');80promise_test(() => {81 const ws = new WritableStream({82 abort() {83 return 'Hello';84 }85 });86 const writer = ws.getWriter();87 return writer.abort('a').then(value => {88 assert_equals(value, undefined, 'fulfillment value must be undefined');89 });90}, 'Fulfillment value of ws.abort() call must be undefined even if the underlying sink returns a non-undefined value');91promise_test(t => {92 const ws = new WritableStream({93 abort() {94 throw error1;95 }96 });97 const writer = ws.getWriter();98 return promise_rejects(t, error1, writer.abort(undefined),99 'rejection reason of abortPromise must be the error thrown by abort');100}, 'WritableStream if sink\'s abort throws, the promise returned by writer.abort() rejects');101promise_test(t => {102 const ws = new WritableStream({103 abort() {104 throw error1;105 }106 });107 return promise_rejects(t, error1, ws.abort(undefined),108 'rejection reason of abortPromise must be the error thrown by abort');109}, 'WritableStream if sink\'s abort throws, the promise returned by ws.abort() rejects');110promise_test(t => {111 let resolveWritePromise;112 const ws = new WritableStream({113 write() {114 return new Promise(resolve => {115 resolveWritePromise = resolve;116 });117 },118 abort() {119 throw error1;120 }121 });122 const writer = ws.getWriter();123 writer.write().catch(() => {});124 return flushAsyncEvents().then(() => {125 const abortPromise = writer.abort(undefined);126 resolveWritePromise();127 return promise_rejects(t, error1, abortPromise,128 'rejection reason of abortPromise must be the error thrown by abort');129 });130}, 'WritableStream if sink\'s abort throws, for an abort performed during a write, the promise returned by ' +131 'ws.abort() rejects');132promise_test(() => {133 const ws = recordingWritableStream();134 const writer = ws.getWriter();135 return writer.abort(error1).then(() => {136 assert_array_equals(ws.events, ['abort', error1]);137 });138}, 'Aborting a WritableStream passes through the given reason');139promise_test(t => {140 const ws = new WritableStream();141 const writer = ws.getWriter();142 const abortPromise = writer.abort(error1);143 const events = [];144 writer.ready.catch(() => {145 events.push('ready');146 });147 writer.closed.catch(() => {148 events.push('closed');149 });150 return Promise.all([151 abortPromise,152 promise_rejects(t, new TypeError(), writer.write(), 'writing should reject with a TypeError'),153 promise_rejects(t, new TypeError(), writer.close(), 'closing should reject with a TypeError'),154 promise_rejects(t, new TypeError(), writer.abort(), 'aborting should reject with a TypeError'),155 promise_rejects(t, new TypeError(), writer.ready, 'ready should reject with a TypeError'),156 promise_rejects(t, new TypeError(), writer.closed, 'closed should reject with a TypeError')157 ]).then(() => {158 assert_array_equals(['ready', 'closed'], events, 'ready should reject before closed');159 });160}, 'Aborting a WritableStream puts it in an errored state, with a TypeError as the stored error');161promise_test(t => {162 const ws = new WritableStream();163 const writer = ws.getWriter();164 const writePromise = promise_rejects(t, new TypeError(), writer.write('a'),165 'writing should reject with a TypeError');166 writer.abort(error1);167 return writePromise;168}, 'Aborting a WritableStream causes any outstanding write() promises to be rejected with a TypeError');169promise_test(t => {170 const ws = recordingWritableStream();171 const writer = ws.getWriter();172 const closePromise = writer.close();173 const abortPromise = writer.abort(error1);174 return Promise.all([175 promise_rejects(t, new TypeError(), writer.closed, 'closed should reject with a TypeError'),176 promise_rejects(t, new TypeError(), closePromise, 'close() should reject with a TypeError'),177 abortPromise178 ]).then(() => {179 assert_array_equals(ws.events, ['abort', error1]);180 });181}, 'Closing but then immediately aborting a WritableStream causes the stream to error');182promise_test(() => {183 let resolveClose;184 const ws = new WritableStream({185 close() {186 return new Promise(resolve => {187 resolveClose = resolve;188 });189 }190 });191 const writer = ws.getWriter();192 const closePromise = writer.close();193 return delay(0).then(() => {194 const abortPromise = writer.abort(error1);195 resolveClose();196 return Promise.all([197 writer.closed,198 abortPromise,199 closePromise200 ]);201 });202}, 'Closing a WritableStream and aborting it while it closes causes the stream to ignore the abort attempt');203promise_test(() => {204 const ws = new WritableStream();205 const writer = ws.getWriter();206 writer.close();207 return delay(0).then(() => writer.abort());208}, 'Aborting a WritableStream after it is closed is a no-op');209promise_test(t => {210 // Testing that per https://github.com/whatwg/streams/issues/620#issuecomment-263483953 the fallback to close was211 // removed.212 // Cannot use recordingWritableStream since it always has an abort213 let closeCalled = false;214 const ws = new WritableStream({215 close() {216 closeCalled = true;217 }218 });219 const writer = ws.getWriter();220 writer.abort();221 return promise_rejects(t, new TypeError(), writer.closed, 'closed should reject with a TypeError').then(() => {222 assert_false(closeCalled, 'close must not have been called');223 });224}, 'WritableStream should NOT call underlying sink\'s close if no abort is supplied (historical)');225promise_test(() => {226 let thenCalled = false;227 const ws = new WritableStream({228 abort() {229 return {230 then(onFulfilled) {231 thenCalled = true;232 onFulfilled();233 }234 };235 }236 });237 const writer = ws.getWriter();238 return writer.abort().then(() => assert_true(thenCalled, 'then() should be called'));239}, 'returning a thenable from abort() should work');240promise_test(t => {241 const ws = new WritableStream({242 write() {243 return flushAsyncEvents();244 }245 });246 const writer = ws.getWriter();247 return writer.ready.then(() => {248 const writePromise = writer.write('a');249 writer.abort(error1);250 let closedRejected = false;251 return Promise.all([252 writePromise.then(() => assert_false(closedRejected, '.closed should not resolve before write()')),253 promise_rejects(t, new TypeError(), writer.closed, '.closed should reject').then(() => {254 closedRejected = true;255 })256 ]);257 });258}, '.closed should not resolve before fulfilled write()');259promise_test(t => {260 const ws = new WritableStream({261 write() {262 return Promise.reject(error1);263 }264 });265 const writer = ws.getWriter();266 return writer.ready.then(() => {267 const writePromise = writer.write('a');268 const abortPromise = writer.abort(error2);269 let closedRejected = false;270 return Promise.all([271 promise_rejects(t, error1, writePromise, 'write() should reject')272 .then(() => assert_false(closedRejected, '.closed should not resolve before write()')),273 promise_rejects(t, new TypeError(), writer.closed, '.closed should reject')274 .then(() => {275 closedRejected = true;276 }),277 abortPromise278 ]);279 });280}, '.closed should not resolve before rejected write(); write() error should not overwrite abort() error');281promise_test(t => {282 const ws = new WritableStream({283 write() {284 return flushAsyncEvents();285 }286 }, new CountQueuingStrategy(4));287 const writer = ws.getWriter();288 return writer.ready.then(() => {289 const settlementOrder = [];290 return Promise.all([291 writer.write('1').then(() => settlementOrder.push(1)),292 promise_rejects(t, new TypeError(), writer.write('2'), 'first queued write should be rejected')293 .then(() => settlementOrder.push(2)),294 promise_rejects(t, new TypeError(), writer.write('3'), 'second queued write should be rejected')295 .then(() => settlementOrder.push(3)),296 writer.abort(error1)297 ]).then(() => assert_array_equals([1, 2, 3], settlementOrder, 'writes should be satisfied in order'));298 });299}, 'writes should be satisfied in order when aborting');300promise_test(t => {301 const ws = new WritableStream({302 write() {303 return Promise.reject(error1);304 }305 }, new CountQueuingStrategy(4));306 const writer = ws.getWriter();307 return writer.ready.then(() => {308 const settlementOrder = [];309 return Promise.all([310 promise_rejects(t, error1, writer.write('1'), 'in-flight write should be rejected')311 .then(() => settlementOrder.push(1)),312 promise_rejects(t, new TypeError(), writer.write('2'), 'first queued write should be rejected')313 .then(() => settlementOrder.push(2)),314 promise_rejects(t, new TypeError(), writer.write('3'), 'second queued write should be rejected')315 .then(() => settlementOrder.push(3)),316 writer.abort(error2)317 ]).then(() => assert_array_equals([1, 2, 3], settlementOrder, 'writes should be satisfied in order'));318 });319}, 'writes should be satisfied in order after rejected write when aborting');320promise_test(t => {321 const ws = new WritableStream({322 write() {323 return Promise.reject(error1);324 }325 });326 const writer = ws.getWriter();327 return writer.ready.then(() => {328 return Promise.all([329 promise_rejects(t, error1, writer.write('a'), 'writer.write() should reject with error from underlying write()'),330 promise_rejects(t, new TypeError(), writer.close(),331 'writer.close() should reject with error from underlying write()'),332 writer.abort()333 ]);334 });335}, 'close() should reject with TypeError when abort() is first error');336promise_test(() => {337 let resolveWrite;338 const ws = recordingWritableStream({339 write() {340 return new Promise(resolve => {341 resolveWrite = resolve;342 });343 }344 });345 const writer = ws.getWriter();346 return writer.ready.then(() => {347 writer.write('a');348 const abortPromise = writer.abort('b');349 return flushAsyncEvents().then(() => {350 assert_array_equals(ws.events, ['write', 'a'], 'abort should not be called while write is in-flight');351 resolveWrite();352 return abortPromise.then(() => {353 assert_array_equals(ws.events, ['write', 'a', 'abort', 'b'], 'abort should be called after the write finishes');354 });355 });356 });357}, 'underlying abort() should not be called until underlying write() completes');358promise_test(() => {359 let resolveClose;360 const ws = recordingWritableStream({361 close() {362 return new Promise(resolve => {363 resolveClose = resolve;364 });365 }366 });367 const writer = ws.getWriter();368 return writer.ready.then(() => {369 writer.close();370 const abortPromise = writer.abort();371 return flushAsyncEvents().then(() => {372 assert_array_equals(ws.events, ['close'], 'abort should not be called while close is in-flight');373 resolveClose();374 return abortPromise.then(() => {375 assert_array_equals(ws.events, ['close'], 'abort should not be called');376 });377 });378 });379}, 'underlying abort() should not be called if underlying close() has started');380promise_test(t => {381 let rejectClose;382 let abortCalled = false;383 const ws = new WritableStream({384 close() {385 return new Promise((resolve, reject) => {386 rejectClose = reject;387 });388 },389 abort() {390 abortCalled = true;391 }392 });393 const writer = ws.getWriter();394 return writer.ready.then(() => {395 const closePromise = writer.close();396 const abortPromise = writer.abort();397 return flushAsyncEvents().then(() => {398 assert_false(abortCalled, 'underlying abort should not be called while close is in-flight');399 rejectClose(error1);400 return promise_rejects(t, error1, abortPromise, 'abort should reject with the same reason').then(() => {401 return promise_rejects(t, error1, closePromise, 'close should reject with the same reason');402 }).then(() => {403 assert_false(abortCalled, 'underlying abort should not be called after close completes');404 });405 });406 });407}, 'if underlying close() has started and then rejects, the abort() and close() promises should reject with the ' +408 'underlying close rejection reason');409promise_test(t => {410 let resolveWrite;411 const ws = recordingWritableStream({412 write() {413 return new Promise(resolve => {414 resolveWrite = resolve;415 });416 }417 });418 const writer = ws.getWriter();419 return writer.ready.then(() => {420 writer.write('a');421 const closePromise = writer.close();422 const abortPromise = writer.abort('b');423 return flushAsyncEvents().then(() => {424 assert_array_equals(ws.events, ['write', 'a'], 'abort should not be called while write is in-flight');425 resolveWrite();426 return abortPromise.then(() => {427 assert_array_equals(ws.events, ['write', 'a', 'abort', 'b'], 'abort should be called after write completes');428 return promise_rejects(t, new TypeError(), closePromise, 'promise returned by close() should be rejected');429 });430 });431 });432}, 'an abort() that happens during a write() should trigger the underlying abort() even with a close() queued');433promise_test(t => {434 const ws = new WritableStream({435 write() {436 return new Promise(() => {});437 }438 });439 const writer = ws.getWriter();440 return writer.ready.then(() => {441 writer.write('a');442 writer.abort();443 writer.releaseLock();444 const writer2 = ws.getWriter();445 return promise_rejects(t, new TypeError(), writer2.ready,446 'ready of the second writer should be rejected with a TypeError');447 });448}, 'if a writer is created for a stream with a pending abort, its ready should be rejected with a TypeError');449promise_test(() => {450 const ws = new WritableStream();451 const writer = ws.getWriter();452 return writer.ready.then(() => {453 const closePromise = writer.close();454 const abortPromise = writer.abort();455 const events = [];456 return Promise.all([457 closePromise.then(() => { events.push('close'); }),458 abortPromise.then(() => { events.push('abort'); })459 ]).then(() => {460 assert_array_equals(events, ['close', 'abort']);461 });462 });463}, 'writer close() promise should resolve before abort() promise');464promise_test(t => {465 const ws = new WritableStream({466 write(chunk, controller) {467 controller.error(error1);468 return new Promise(() => {});469 }470 });471 const writer = ws.getWriter();472 return writer.ready.then(() => {473 writer.write('a');474 return promise_rejects(t, error1, writer.ready, 'writer.ready should reject');475 });476}, 'writer.ready should reject on controller error without waiting for underlying write');477promise_test(t => {478 let rejectWrite;479 const ws = new WritableStream({480 write() {481 return new Promise((resolve, reject) => {482 rejectWrite = reject;483 });484 }485 });486 let writePromise;487 let abortPromise;488 const events = [];489 const writer = ws.getWriter();490 writer.closed.catch(() => {491 events.push('closed');492 });493 // Wait for ws to start494 return flushAsyncEvents().then(() => {495 writePromise = writer.write('a');496 writePromise.catch(() => {497 events.push('writePromise');498 });499 abortPromise = writer.abort(error1);500 abortPromise.then(() => {501 events.push('abortPromise');502 });503 const writePromise2 = writer.write('a');504 return Promise.all([505 promise_rejects(t, new TypeError(), writePromise2, 'writePromise2 must reject with an error indicating abort'),506 promise_rejects(t, new TypeError(), writer.ready, 'writer.ready must reject with an error indicating abort'),507 flushAsyncEvents()508 ]);509 }).then(() => {510 assert_array_equals(events, [], 'writePromise, abortPromise and writer.closed must not be rejected yet');511 rejectWrite(error2);512 return Promise.all([513 promise_rejects(t, error2, writePromise,514 'writePromise must reject with the error returned from the sink\'s write method'),515 abortPromise,516 promise_rejects(t, new TypeError(), writer.closed,517 'writer.closed must reject with an error indicating abort'),518 flushAsyncEvents()519 ]);520 }).then(() => {521 assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],522 'writePromise, abortPromise and writer.closed must settle');523 const writePromise3 = writer.write('a');524 return Promise.all([525 promise_rejects(t, new TypeError(), writePromise3,526 'writePromise3 must reject with an error indicating abort'),527 promise_rejects(t, new TypeError(), writer.ready,528 'writer.ready must be still rejected with the error indicating abort')529 ]);530 }).then(() => {531 writer.releaseLock();532 return Promise.all([533 promise_rejects(t, new TypeError(), writer.ready,534 'writer.ready must be rejected with an error indicating release'),535 promise_rejects(t, new TypeError(), writer.closed,536 'writer.closed must be rejected with an error indicating release')537 ]);538 });539}, 'writer.abort() while there is an in-flight write, and then finish the write with rejection');540promise_test(t => {541 let resolveWrite;542 let controller;543 const ws = new WritableStream({544 write(chunk, c) {545 controller = c;546 return new Promise(resolve => {547 resolveWrite = resolve;548 });549 }550 });551 let writePromise;552 let abortPromise;553 const events = [];554 const writer = ws.getWriter();555 writer.closed.catch(() => {556 events.push('closed');557 });558 // Wait for ws to start559 return flushAsyncEvents().then(() => {560 writePromise = writer.write('a');561 writePromise.then(() => {562 events.push('writePromise');563 });564 abortPromise = writer.abort(error1);565 abortPromise.then(() => {566 events.push('abortPromise');567 });568 const writePromise2 = writer.write('a');569 return Promise.all([570 promise_rejects(t, new TypeError(), writePromise2, 'writePromise2 must reject with an error indicating abort'),571 promise_rejects(t, new TypeError(), writer.ready, 'writer.ready must reject with an error indicating abort'),572 flushAsyncEvents()573 ]);574 }).then(() => {575 assert_array_equals(events, [], 'writePromise, abortPromise and writer.closed must not be fulfilled/rejected yet');576 // This error is too late to change anything. abort() has already changed the stream state to 'erroring'.577 controller.error(error2);578 const writePromise3 = writer.write('a');579 return Promise.all([580 promise_rejects(t, new TypeError(), writePromise3,581 'writePromise3 must reject with an error indicating abort'),582 promise_rejects(t, new TypeError(), writer.ready,583 'writer.ready must be still rejected with the error indicating abort'),584 flushAsyncEvents()585 ]);586 }).then(() => {587 assert_array_equals(588 events, [],589 'writePromise, abortPromise and writer.closed must not be fulfilled/rejected yet even after ' +590 'controller.error() call');591 resolveWrite();592 return Promise.all([593 writePromise,594 abortPromise,595 promise_rejects(t, new TypeError(), writer.closed,596 'writer.closed must reject with an error indicating abort'),597 flushAsyncEvents()598 ]);599 }).then(() => {600 assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],601 'writePromise, abortPromise and writer.closed must settle');602 const writePromise4 = writer.write('a');603 return Promise.all([604 writePromise,605 promise_rejects(t, new TypeError(), writePromise4,606 'writePromise4 must reject with an error indicating abort'),607 promise_rejects(t, new TypeError(), writer.ready,608 'writer.ready must be still rejected with the error indicating abort')609 ]);610 }).then(() => {611 writer.releaseLock();612 return Promise.all([613 promise_rejects(t, new TypeError(), writer.ready,614 'writer.ready must be rejected with an error indicating release'),615 promise_rejects(t, new TypeError(), writer.closed,616 'writer.closed must be rejected with an error indicating release')617 ]);618 });619}, 'writer.abort(), controller.error() while there is an in-flight write, and then finish the write');620promise_test(t => {621 let resolveClose;622 let controller;623 const ws = new WritableStream({624 start(c) {625 controller = c;626 },627 close() {628 return new Promise(resolve => {629 resolveClose = resolve;630 });631 }632 });633 let closePromise;634 let abortPromise;635 const events = [];636 const writer = ws.getWriter();637 writer.closed.then(() => {638 events.push('closed');639 });640 // Wait for ws to start641 return flushAsyncEvents().then(() => {642 closePromise = writer.close();643 closePromise.then(() => {644 events.push('closePromise');645 });646 abortPromise = writer.abort(error1);647 abortPromise.then(() => {648 events.push('abortPromise');649 });650 return Promise.all([651 promise_rejects(t, new TypeError(), writer.close(),652 'writer.close() must reject with an error indicating already closing'),653 promise_rejects(t, new TypeError(), writer.ready, 'writer.ready must reject with an error indicating abort'),654 flushAsyncEvents()655 ]);656 }).then(() => {657 assert_array_equals(events, [], 'closePromise, abortPromise and writer.closed must not be fulfilled/rejected yet');658 controller.error(error2);659 return Promise.all([660 promise_rejects(t, new TypeError(), writer.close(),661 'writer.close() must reject with an error indicating already closing'),662 promise_rejects(t, new TypeError(), writer.ready,663 'writer.ready must be still rejected with the error indicating abort'),664 flushAsyncEvents()665 ]);666 }).then(() => {667 assert_array_equals(668 events, [],669 'closePromise, abortPromise and writer.closed must not be fulfilled/rejected yet even after ' +670 'controller.error() call');671 resolveClose();672 return Promise.all([673 closePromise,674 abortPromise,675 writer.closed,676 flushAsyncEvents()677 ]);678 }).then(() => {679 assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],680 'closedPromise, abortPromise and writer.closed must fulfill');681 return Promise.all([682 promise_rejects(t, new TypeError(), writer.close(),683 'writer.close() must reject with an error indicating already closing'),684 promise_rejects(t, new TypeError(), writer.ready,685 'writer.ready must be still rejected with the error indicating abort')686 ]);687 }).then(() => {688 writer.releaseLock();689 return Promise.all([690 promise_rejects(t, new TypeError(), writer.close(),691 'writer.close() must reject with an error indicating release'),692 promise_rejects(t, new TypeError(), writer.ready,693 'writer.ready must be rejected with an error indicating release'),694 promise_rejects(t, new TypeError(), writer.closed,695 'writer.closed must be rejected with an error indicating release')696 ]);697 });698}, 'writer.abort(), controller.error() while there is an in-flight close, and then finish the close');699promise_test(t => {700 let resolveWrite;701 let controller;702 const ws = recordingWritableStream({703 write(chunk, c) {704 controller = c;705 return new Promise(resolve => {706 resolveWrite = resolve;707 });708 }709 });710 let writePromise;711 let abortPromise;712 const events = [];713 const writer = ws.getWriter();714 writer.closed.catch(() => {715 events.push('closed');716 });717 // Wait for ws to start718 return flushAsyncEvents().then(() => {719 writePromise = writer.write('a');720 writePromise.then(() => {721 events.push('writePromise');722 });723 controller.error(error2);724 const writePromise2 = writer.write('a');725 return Promise.all([726 promise_rejects(t, error2, writePromise2,727 'writePromise2 must reject with the error passed to the controller\'s error method'),728 promise_rejects(t, error2, writer.ready,729 'writer.ready must reject with the error passed to the controller\'s error method'),730 flushAsyncEvents()731 ]);732 }).then(() => {733 assert_array_equals(events, [], 'writePromise and writer.closed must not be fulfilled/rejected yet');734 abortPromise = writer.abort(error1);735 abortPromise.catch(() => {736 events.push('abortPromise');737 });738 const writePromise3 = writer.write('a');739 return Promise.all([740 promise_rejects(t, error2, writePromise3,741 'writePromise3 must reject with the error passed to the controller\'s error method'),742 flushAsyncEvents()743 ]);744 }).then(() => {745 assert_array_equals(746 events, [],747 'writePromise and writer.closed must not be fulfilled/rejected yet even after writer.abort()');748 resolveWrite();749 return Promise.all([750 promise_rejects(t, error2, abortPromise,751 'abort() must reject with the error passed to the controller\'s error method'),752 promise_rejects(t, error2, writer.closed,753 'writer.closed must reject with the error passed to the controller\'s error method'),754 flushAsyncEvents()755 ]);756 }).then(() => {757 assert_array_equals(events, ['writePromise', 'abortPromise', 'closed'],758 'writePromise, abortPromise and writer.closed must fulfill/reject');759 assert_array_equals(ws.events, ['write', 'a'], 'sink abort() should not be called');760 const writePromise4 = writer.write('a');761 return Promise.all([762 writePromise,763 promise_rejects(t, error2, writePromise4,764 'writePromise4 must reject with the error passed to the controller\'s error method'),765 promise_rejects(t, error2, writer.ready,766 'writer.ready must be still rejected with the error passed to the controller\'s error method')767 ]);768 }).then(() => {769 writer.releaseLock();770 return Promise.all([771 promise_rejects(t, new TypeError(), writer.ready,772 'writer.ready must be rejected with an error indicating release'),773 promise_rejects(t, new TypeError(), writer.closed,774 'writer.closed must be rejected with an error indicating release')775 ]);776 });777}, 'controller.error(), writer.abort() while there is an in-flight write, and then finish the write');778promise_test(t => {779 let resolveClose;780 let controller;781 const ws = new WritableStream({782 start(c) {783 controller = c;784 },785 close() {786 return new Promise(resolve => {787 resolveClose = resolve;788 });789 }790 });791 let closePromise;792 let abortPromise;793 const events = [];794 const writer = ws.getWriter();795 writer.closed.then(() => {796 events.push('closed');797 });798 // Wait for ws to start799 return flushAsyncEvents().then(() => {800 closePromise = writer.close();801 closePromise.then(() => {802 events.push('closePromise');803 });804 controller.error(error2);805 return flushAsyncEvents();806 }).then(() => {807 assert_array_equals(events, [], 'closePromise must not be fulfilled/rejected yet');808 abortPromise = writer.abort(error1);809 abortPromise.then(() => {810 events.push('abortPromise');811 });812 return Promise.all([813 promise_rejects(t, error2, writer.ready,814 'writer.ready must reject with the error passed to the controller\'s error method'),815 flushAsyncEvents()816 ]);817 }).then(() => {818 assert_array_equals(819 events, [],820 'closePromise and writer.closed must not be fulfilled/rejected yet even after writer.abort()');821 resolveClose();822 return Promise.all([823 closePromise,824 promise_rejects(t, error2, writer.ready,825 'writer.ready must be still rejected with the error passed to the controller\'s error method'),826 writer.closed,827 flushAsyncEvents()828 ]);829 }).then(() => {830 assert_array_equals(events, ['closePromise', 'abortPromise', 'closed'],831 'abortPromise, closePromise and writer.closed must fulfill/reject');832 }).then(() => {833 writer.releaseLock();834 return Promise.all([835 promise_rejects(t, new TypeError(), writer.ready,836 'writer.ready must be rejected with an error indicating release'),837 promise_rejects(t, new TypeError(), writer.closed,838 'writer.closed must be rejected with an error indicating release')839 ]);840 });841}, 'controller.error(), writer.abort() while there is an in-flight close, and then finish the close');842promise_test(t => {843 let resolveWrite;844 const ws = new WritableStream({845 write() {846 return new Promise(resolve => {847 resolveWrite = resolve;848 });849 }850 });851 const writer = ws.getWriter();852 return writer.ready.then(() => {853 const writePromise = writer.write('a');854 const closed = writer.closed;855 const abortPromise = writer.abort();856 writer.releaseLock();857 resolveWrite();858 return Promise.all([859 writePromise,860 abortPromise,861 promise_rejects(t, new TypeError(), closed, 'closed should reject')]);862 });863}, 'releaseLock() while aborting should reject the original closed promise');864// TODO(ricea): Consider removing this test if it is no longer useful.865promise_test(t => {866 let resolveWrite;867 let resolveAbort;868 let resolveAbortStarted;869 const abortStarted = new Promise(resolve => {870 resolveAbortStarted = resolve;871 });872 const ws = new WritableStream({873 write() {874 return new Promise(resolve => {875 resolveWrite = resolve;876 });877 },878 abort() {879 resolveAbortStarted();880 return new Promise(resolve => {881 resolveAbort = resolve;882 });883 }884 });885 const writer = ws.getWriter();886 return writer.ready.then(() => {887 const writePromise = writer.write('a');888 const closed = writer.closed;889 const abortPromise = writer.abort();890 resolveWrite();891 return abortStarted.then(() => {892 writer.releaseLock();893 assert_equals(writer.closed, closed, 'closed promise should not have changed');894 resolveAbort();895 return Promise.all([896 writePromise,897 abortPromise,898 promise_rejects(t, new TypeError(), closed, 'closed should reject')]);899 });900 });901}, 'releaseLock() during delayed async abort() should reject the writer.closed promise');902promise_test(() => {903 let resolveStart;904 const ws = recordingWritableStream({905 start() {906 return new Promise(resolve => {907 resolveStart = resolve;908 });909 }910 });911 const abortPromise = ws.abort('done');912 return flushAsyncEvents().then(() => {913 assert_array_equals(ws.events, [], 'abort() should not be called during start()');914 resolveStart();915 return abortPromise.then(() => {916 assert_array_equals(ws.events, ['abort', 'done'], 'abort() should be called after start() is done');917 });918 });919}, 'sink abort() should not be called until sink start() is done');920promise_test(() => {921 let resolveStart;922 let controller;923 const ws = recordingWritableStream({924 start(c) {925 controller = c;926 return new Promise(resolve => {927 resolveStart = resolve;928 });929 }930 });931 const abortPromise = ws.abort('done');932 controller.error(error1);933 resolveStart();934 return abortPromise.then(() =>935 assert_array_equals(ws.events, ['abort', 'done'],936 'abort() should still be called if start() errors the controller'));937}, 'if start attempts to error the controller after abort() has been called, then it should lose');938promise_test(() => {939 const ws = recordingWritableStream({940 start() {941 return Promise.reject(error1);942 }943 });944 return ws.abort('done').then(() =>945 assert_array_equals(ws.events, ['abort', 'done'], 'abort() should still be called if start() rejects'));946}, 'stream abort() promise should still resolve if sink start() rejects');947promise_test(t => {948 const ws = new WritableStream();949 const writer = ws.getWriter();950 const writerReady1 = writer.ready;951 writer.abort('a');952 const writerReady2 = writer.ready;953 assert_not_equals(writerReady1, writerReady2, 'abort() should replace the ready promise with a rejected one');954 return Promise.all([writerReady1,955 promise_rejects(t, new TypeError(), writerReady2, 'writerReady2 should reject')]);956}, 'writer abort() during sink start() should replace the writer.ready promise synchronously');957promise_test(t => {958 const events = [];959 const ws = recordingWritableStream();960 const writer = ws.getWriter();961 const writePromise1 = writer.write(1);962 const abortPromise = writer.abort('a');963 const writePromise2 = writer.write(2);964 const closePromise = writer.close();965 writePromise1.catch(() => events.push('write1'));966 abortPromise.then(() => events.push('abort'));967 writePromise2.catch(() => events.push('write2'));968 closePromise.catch(() => events.push('close'));969 return Promise.all([970 promise_rejects(t, new TypeError(), writePromise1, 'first write() should reject'),971 abortPromise,972 promise_rejects(t, new TypeError(), writePromise2, 'second write() should reject'),973 promise_rejects(t, new TypeError(), closePromise, 'close() should reject')974 ])975 .then(() => {976 assert_array_equals(events, ['write2', 'write1', 'abort', 'close'],977 'promises should resolve in the standard order');978 assert_array_equals(ws.events, ['abort', 'a'], 'underlying sink write() should not be called');979 });980}, 'promises returned from other writer methods should be rejected when writer abort() happens during sink start()');981promise_test(t => {982 let writeReject;983 let controller;984 const ws = new WritableStream({985 write(chunk, c) {986 controller = c;987 return new Promise((resolve, reject) => {988 writeReject = reject;989 });990 }991 });992 const writer = ws.getWriter();993 return writer.ready.then(() => {994 const writePromise = writer.write('a');995 const abortPromise = writer.abort();996 controller.error(error1);997 writeReject(error2);998 return Promise.all([999 promise_rejects(t, error2, writePromise, 'write() should reject with error2'),1000 abortPromise1001 ]);1002 });1003}, 'abort() should succeed despite rejection from write');1004promise_test(t => {1005 let closeReject;1006 let controller;1007 const ws = new WritableStream({1008 start(c) {1009 controller = c;1010 },1011 close() {1012 return new Promise((resolve, reject) => {1013 closeReject = reject;1014 });1015 }1016 });1017 const writer = ws.getWriter();1018 return writer.ready.then(() => {1019 const closePromise = writer.close();1020 const abortPromise = writer.abort();1021 controller.error(error1);1022 closeReject(error2);1023 return Promise.all([1024 promise_rejects(t, error2, closePromise, 'close() should reject with error2'),1025 promise_rejects(t, error2, abortPromise, 'abort() should reject with error2')1026 ]);1027 });1028}, 'abort() should be rejected with the rejection returned from close()');1029promise_test(t => {1030 let rejectWrite;1031 const ws = recordingWritableStream({1032 write() {1033 return new Promise((resolve, reject) => {1034 rejectWrite = reject;1035 });1036 }1037 });1038 const writer = ws.getWriter();1039 return writer.ready.then(() => {1040 const writePromise = writer.write('1');1041 const abortPromise = writer.abort(error2);1042 rejectWrite(error1);1043 return Promise.all([1044 promise_rejects(t, error1, writePromise, 'write should reject'),1045 abortPromise,1046 promise_rejects(t, new TypeError(), writer.closed, 'closed should reject with TypeError')1047 ]);1048 }).then(() => {1049 assert_array_equals(ws.events, ['write', '1', 'abort', error2], 'abort sink method should be called');1050 });1051}, 'a rejecting sink.write() should not prevent sink.abort() from being called');1052promise_test(() => {1053 const ws = recordingWritableStream({1054 start() {1055 return Promise.reject(error1);1056 }1057 });1058 return ws.abort(error2)1059 .then(() => {1060 assert_array_equals(ws.events, ['abort', error2]);1061 });1062}, 'when start errors after stream abort(), underlying sink abort() should be called anyway');1063promise_test(t => {1064 const ws = new WritableStream();1065 const abortPromise1 = ws.abort();1066 const abortPromise2 = ws.abort();1067 return Promise.all([1068 abortPromise1,1069 promise_rejects(t, new TypeError(), abortPromise2, 'second abort() should reject')1070 ]);1071}, 'when calling abort() twice on the same stream, the second call should reject');1072promise_test(t => {1073 let controller;1074 let resolveWrite;1075 const ws = recordingWritableStream({1076 start(c) {1077 controller = c;1078 },1079 write() {1080 return new Promise(resolve => {1081 resolveWrite = resolve;1082 });1083 }1084 });1085 const writer = ws.getWriter();1086 return writer.ready.then(() => {1087 const writePromise = writer.write('chunk');1088 controller.error(error1);1089 const abortPromise = writer.abort(error2);1090 resolveWrite();1091 return Promise.all([1092 writePromise,1093 promise_rejects(t, error1, abortPromise, 'abort() should reject')1094 ]).then(() => {1095 assert_array_equals(ws.events, ['write', 'chunk'], 'sink abort() should not be called');1096 });1097 });1098}, 'sink abort() should not be called if stream was erroring due to controller.error() before abort() was called');1099promise_test(t => {1100 let resolveWrite;1101 let size = 1;1102 const ws = recordingWritableStream({1103 write() {1104 return new Promise(resolve => {1105 resolveWrite = resolve;1106 });1107 }1108 }, {1109 size() {1110 return size;1111 },1112 highWaterMark: 11113 });1114 const writer = ws.getWriter();1115 return writer.ready.then(() => {1116 const writePromise1 = writer.write('chunk1');1117 size = NaN;1118 const writePromise2 = writer.write('chunk2');1119 const abortPromise = writer.abort(error2);1120 resolveWrite();1121 return Promise.all([1122 writePromise1,1123 promise_rejects(t, new RangeError(), writePromise2, 'second write() should reject'),1124 promise_rejects(t, new RangeError(), abortPromise, 'abort() should reject')1125 ]).then(() => {1126 assert_array_equals(ws.events, ['write', 'chunk1'], 'sink abort() should not be called');1127 });1128 });1129}, 'sink abort() should not be called if stream was erroring due to bad strategy before abort() was called');...
Using AI Code Generation
1const wpt = require('wpt');2const { AbortController } = require('abortcontroller-polyfill/dist/cjs-ponyfill');3const controller = new AbortController();4const signal = controller.signal;5const options = {6};7wpt.runTest(options, (err, data) => {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 }13});14setTimeout(() => {15 controller.abort();16}, 2000);17{ statusCode: 200,18 { statusCode: 200,19 median: { firstView: [Object], repeatView: [Object] } } }20wpt.runTest(options, callback)21wpt.runTest(options)
Using AI Code Generation
1const wpt = require('webpagetest');2const wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test Created: %s', data.data.testId);5 wpt.abortTest(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Test Aborted: %s', data.data.statusText);8 });9});10const wpt = require('webpagetest');11const wpt = new WebPageTest('www.webpagetest.org');12 if (err) return console.error(err);13 console.log('Test Created: %s', data.data.testId);14 wpt.abortTest(data.data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log('Test Aborted: %s', data.data.statusText);17 });18});19const wpt = require('webpagetest');20const wpt = new WebPageTest('www.webpagetest.org');21 if (err) return console.error(err);22 console.log('Test Created: %s', data.data.testId);23 wpt.abortTest(data.data.testId, function(err, data) {24 if (err) return console.error(err);25 console.log('Test Aborted: %s', data.data.statusText);26 });27});28const wpt = require('webpagetest');29const wpt = new WebPageTest('www.webpagetest.org');30 if (err) return console.error(err);31 console.log('Test Created: %s', data.data.testId);32 wpt.abortTest(data.data.testId, function(err, data) {33 if (err) return console.error(err);34 console.log('Test Aborted: %s', data.data
Using AI Code Generation
1const wpt = require('webpagetest');2const webPageTest = new wpt('WPT_API_KEY');3});4test.then((data) => {5 console.log(data);6}).catch((err) => {7 console.log(err);8});9test.abort();
Using AI Code Generation
1const wpt = require('webpagetest');2const wptInstance = wpt('API_KEY');3wptInstance.abortTest('TEST_ID', function(err, data) {4 console.log(data);5});6wptInstance.abortTest('TEST_ID')7 .then(data => {8 console.log(data);9 })10 .catch(err => {11 console.log(err);12 });13try {14 const data = await wptInstance.abortTest('TEST_ID');15 console.log(data);16} catch (err) {17 console.log(err);18}19const data = await wptInstance.abortTest('TEST_ID');20console.log(data);21const data = await wptInstance.abortTest('TEST_ID').catch(err => {22 console.log(err);23});24console.log(data);25try {26 const data = await wptInstance.abortTest('TEST_ID').catch(err => {27 console.log(err);28 });29 console.log(data);30} catch (err) {31 console.log(err);32}33try {34 const data = await wptInstance.abortTest('TEST_ID').catch(err => {35 console.log(err);36 });37 console.log(data);38} catch (err) {39 console.log(err);40}41try {42 const data = await wptInstance.abortTest('TEST_ID').catch(err => {43 console.log(err);44 });45 console.log(data);46} catch (err) {47 console.log(err);48}49try {50 const data = await wptInstance.abortTest('TEST_ID').catch(err => {51 console.log(err);52 });53 console.log(data);54} catch (err) {55 console.log(err);56}
Using AI Code Generation
1const wpt = require('webpagetest');2wptPromise.abortTest('Test ID');3const wpt = require('webpagetest');4wptPromise.getTestResults('Test ID');5const wpt = require('webpagetest');6wptPromise.getLocations();7const wpt = require('webpagetest');8wptPromise.getTesters();9const wpt = require('webpagetest');10wptPromise.getTesters();11const wpt = require('webpagetest');12wptPromise.getTesters();13const wpt = require('webpagetest');14wptPromise.getTesters();15const wpt = require('webpagetest');16wptPromise.getTesters();17const wpt = require('webpagetest');18wptPromise.getTesters();
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6');3wpt.abortTest('12345678', function(err, data) {4 console.log(data);5});6The following example shows how to use the abortTest() method of the WebPageTest module:7var WebPageTest = require('webpagetest');8var wpt = new WebPageTest('www.webpagetest.org', 'A.1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6');9wpt.abortTest('12345678', function(err, data) {10 console.log(data);11});12{13 "data": {14 }15}16getLocations()
Using AI Code Generation
1const wpt = require('wpt-api');2wpt.abortPromise('test', 'test', 'test')3 .then(function (data) {4 console.log(data);5 })6 .catch(function (error) {7 console.log(error);8 });9const wpt = require('wpt-api');10wpt.abort('test', 'test', 'test', function (error, data) {11 if (error) {12 console.log(error);13 } else {14 console.log(data);15 }16});17const wpt = require('wpt-api');18wpt.getLocationsPromise()19 .then(function (data) {20 console.log(data);21 })22 .catch(function (error) {23 console.log(error);24 });25wpt.getLocations(function (error, data) {26 if (error) {27 console.log(error);28 } else {29 console.log(data);30 }31});32wpt.getTestsPromise()33 .then(function (data) {34 console.log(data);35 })36 .catch(function (error) {37 console.log(error);38 });39wpt.getTests(function (error, data) {40 if (error) {41 console.log(error);42 } else {43 console.log(data);44 }45});46wpt.getTestPromise('test')47 .then(function (data) {48 console.log(data);49 })50 .catch(function (error) {51 console.log(error);52 });53wpt.getTest('test', function (error, data) {54 if (error) {55 console.log(error);56 } else {57 console.log(data);58 }59});60wpt.getTestStatusPromise('test')61 .then(function (data) {62 console.log(data);63 })64 .catch(function (error) {65 console.log(error);66 });67wpt.getTestStatus('test', function (error, data) {68 if (error) {69 console.log(error);70 } else {71 console.log(data);72 }73});74wpt.getTestResultsPromise('test')75 .then(function (data) {76 console.log(data);77 })78 .catch(function (error) {79 console.log(error);80 });81wpt.getTestResults('test', function (error, data) {82 if (error) {83 console.log(error);84 } else {85 console.log(data
Using AI Code Generation
1var wptAgent = require('wptAgent');2wptAgent.abortPromise()3.then(function(){4 console.log('Test aborted');5})6.catch(function(err){7 console.log('Error while aborting test',err);8});9var wptAgent = require('wptAgent');10wptAgent.abort()11.then(function(){12 console.log('Test aborted');13})14.catch(function(err){15 console.log('Error while aborting test',err);16});17var wptAgent = require('wptAgent');18wptAgent.abort()19.then(function(){20 console.log('Test aborted');21})22.catch(function(err){23 console.log('Error while aborting test',err);24});25var wptAgent = require('wptAgent');26wptAgent.abort()27.then(function(){28 console.log('Test aborted');29})30.catch(function(err){31 console.log('Error while aborting test',err);32});33var wptAgent = require('wptAgent');34wptAgent.abort()35.then(function(){36 console.log('Test aborted');37})38.catch(function(err){39 console.log('Error while aborting test',err);40});41var wptAgent = require('wptAgent');42wptAgent.abort()43.then(function(){44 console.log('Test aborted');45})46.catch(function(err){47 console.log('Error while aborting test',err);48});49var wptAgent = require('wptAgent');50wptAgent.abort()51.then(function(){52 console.log('Test aborted');53})54.catch(function(err){55 console.log('Error while aborting test',err);56});57var wptAgent = require('wptAgent');58wptAgent.abort()59.then(function(){60 console.log('Test aborted');61})62.catch(function(err){63 console.log('Error while aborting test',err);64});65var wptAgent = require('w
Using AI Code Generation
1abortPromise = wpt.abortTest(testId);2abortPromise.then(function (data) {3 console.log(data);4}).catch(function (err) {5 console.log(err);6});7locationsPromise = wpt.getLocations();8locationsPromise.then(function (data) {9 console.log(data);10}).catch(function (err) {11 console.log(err);12});13testersPromise = wpt.getTesters();14testersPromise.then(function (data) {15 console.log(data);16}).catch(function (err) {17 console.log(err);18});19testersPromise = wpt.getTesters();20testersPromise.then(function (data) {21 console.log(data);22}).catch(function (err) {23 console.log(err);24});25testersPromise = wpt.getTesters();26testersPromise.then(function (data) {27 console.log(data);28}).catch(function (err) {29 console.log(err);30});31testersPromise = wpt.getTesters();32testersPromise.then(function (data) {33 console.log(data);34}).catch(function (err) {35 console.log(err);36});37testersPromise = wpt.getTesters();38testersPromise.then(function (data) {39 console.log(data);40}).catch(function (err) {41 console.log(err);42});
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!!