How to use abortPromise1 method in wpt

Best JavaScript code snippet using wpt

aborting.any.js

Source:aborting.any.js Github

copy

Full Screen

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

Full Screen

Full Screen

aborting.js

Source:aborting.js Github

copy

Full Screen

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

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');3 if (data.statusCode == 200) {4 wpt.abortTest(data.data.testId, function(err, data) {5 if (data.statusCode == 200) {6 console.log('Test aborted');7 }8 });9 }10});11var wpt = require('webpagetest');12var wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');13 if (data.statusCode == 200) {14 wpt.abortTest(data.data.testId).then(function(data) {15 if (data.statusCode == 200) {16 console.log('Test aborted');17 }18 });19 }20});21var wpt = require('webpagetest');22var wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');23 if (data.statusCode == 200) {24 wpt.abortTest(data.data.testId).then(function(data) {25 if (data.statusCode == 200) {26 console.log('Test aborted');27 }28 }).catch(function(err) {29 console.log(err);30 });31 }32});33var wpt = require('webpagetest');34var wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');35wpt.getLocations(function(err, data) {36 console.log(data);37});38var wpt = require('webpagetest');39var wpt = new WebPageTest('www.webpagetest.org', 'API_KEY');40wpt.getLocations().then(function(data) {41 console.log(data);42}).catch(function(err) {43 console.log(err);44});

Full Screen

Using AI Code Generation

copy

Full Screen

1wpt.abortPromise1("testID")2.then(function(data) {3 console.log(data);4})5.catch(function(err) {6 console.log(err);7});8wpt.abortPromise2("testID")9.then(function(data) {10 console.log(data);11})12.catch(function(err) {13 console.log(err);14});15wpt.abortCallback("testID", function(err, data) {16 if (err) {17 console.log(err);18 } else {19 console.log(data);20 }21});22(async () => {23 try {24 const data = await wpt.abortAsync("testID");25 console.log(data);26 } catch (err) {27 console.log(err);28 }29})();30wpt.abort("testID", function(err, data) {31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36});37wpt.abort("testID")38.then(function(data) {39 console.log(data);40})41.catch(function(err) {42 console.log(err);43});44(async () => {45 try {46 const data = await wpt.abortAsync("testID");47 console.log(data);48 } catch (err) {49 console.log(err);50 }51})();52wpt.abortPromise1("testID")53.then(function(data) {54 console.log(data);55})56.catch(function(err) {57 console.log(err);58});59wpt.abortPromise2("testID")60.then(function(data) {61 console.log(data);62})63.catch(function(err) {64 console.log(err);65});66wpt.abortCallback("testID", function(err, data) {67 if (err) {68 console.log(err);69 } else {70 console.log(data);71 }72});73(async () => {74 try {75 const data = await wpt.abortAsync("testID");76 console.log(data);77 } catch (err) {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3var options = {4};5wpt.runTest(options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test initiated:', data.data.testId);8 var testId = data.data.testId;9 setTimeout(function() {10 wpt.abortTest(testId, function(err, data) {11 if (err) return console.error(err);12 console.log('Test aborted:', data.data);13 });14 }, 10000);15});16var wpt = require('webpagetest');17var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');18var options = {19};20wpt.runTest(options, function(err, data) {21 if (err) return console.error(err);22 console.log('Test initiated:', data.data.testId);23 var testId = data.data.testId;24 setTimeout(function() {25 wpt.abortTest(testId)26 .then(function(data) {27 console.log('Test aborted:', data.data);28 })29 .catch(function(err) {30 console.error(err);31 });32 }, 10000);33});34var wpt = require('webpagetest');35var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');36var options = {

Full Screen

Using AI Code Generation

copy

Full Screen

1let wpt = require('wpt')2let wpt = new wpt();3wpt.abortPromise1(1000,1000);4let wpt = require('wpt')5let wpt = new wpt();6wpt.abortPromise2(1000,1000);7let wpt = require('wpt')8let wpt = new wpt();9wpt.abortCallback(1000,1000);10let wpt = require('wpt')11let wpt = new wpt();12wpt.abortCallback1(1000,1000);13let wpt = require('wpt')14let wpt = new wpt();15wpt.abortCallback2(1000,1000);16let wpt = require('wpt')17let wpt = new wpt();18wpt.abortCallback3(1000,1000);19let wpt = require('wpt')20let wpt = new wpt();21wpt.abortCallback4(1000,1000);22let wpt = require('wpt')23let wpt = new wpt();24wpt.abortCallback5(1000,1000);25let wpt = require('wpt')26let wpt = new wpt();27wpt.abortCallback6(1000,1000);28let wpt = require('wpt')29let wpt = new wpt();30wpt.abortCallback7(1000,1000);31let wpt = require('wpt')32let wpt = new wpt();33wpt.abortCallback8(1000,1000);

Full Screen

Using AI Code Generation

copy

Full Screen

1wpt.abortPromise1();2wpt.abortPromise2();3wpt.abortPromise3();4wpt.abortPromise4();5wpt.abortPromise5();6wpt.abortPromise6();7wpt.abortPromise7();8wpt.abortPromise8();9wpt.abortPromise9();10wpt.abortPromise10();11wpt.abortPromise11();12wpt.abortPromise12();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.9e6e4b6a4b6f4b6f4b6f4b6f4b6f4b6f');3 if (err) return console.error(err);4 var testId = data.data.testId;5 wpt.stopTest(testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org', 'A.9e6e4b6a4b6f4b6f4b6f4b6f4b6f4b6f');12 if (err) return console.error(err);13 var testId = data.data.testId;14 wpt.stopTest(testId)15 .then(function(data) {16 console.log(data);17 })18 .catch(function(err) {19 console.error(err);20 });21});22var wpt = require('webpagetest');23var wpt = new WebPageTest('www.webpagetest.org', 'A.9e6e4b6a4b6f4b6f4b6f4b6f4b6f4b6f');24 if (err) return console.error(err);25 var testId = data.data.testId;26 wpt.stopTest(testId)27 .then(console.log)28 .catch(console.error);29});30var wpt = require('webpagetest');31var wpt = new WebPageTest('www.webpagetest.org', 'A.9e6e4b6a4b6f4b6f4b6f4b6f4b6f4b6f

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful