Best JavaScript code snippet using wpt
cancel.js
Source:cancel.js
1'use strict';2if (self.importScripts) {3 self.importScripts('../resources/test-utils.js');4 self.importScripts('../resources/rs-utils.js');5 self.importScripts('/resources/testharness.js');6}7promise_test(() => {8 const randomSource = new RandomPushSource();9 let cancellationFinished = false;10 const rs = new ReadableStream({11 start(c) {12 randomSource.ondata = c.enqueue.bind(c);13 randomSource.onend = c.close.bind(c);14 randomSource.onerror = c.error.bind(c);15 },16 pull() {17 randomSource.readStart();18 },19 cancel() {20 randomSource.readStop();21 return new Promise(resolve => {22 setTimeout(() => {23 cancellationFinished = true;24 resolve();25 }, 1);26 });27 }28 });29 const reader = rs.getReader();30 // We call delay multiple times to avoid cancelling too early for the31 // source to enqueue at least one chunk.32 const cancel = delay(5).then(() => delay(5)).then(() => delay(5)).then(() => {33 const cancelPromise = reader.cancel();34 assert_false(cancellationFinished, 'cancellation in source should happen later');35 return cancelPromise;36 });37 return readableStreamToArray(rs, reader).then(chunks => {38 assert_greater_than(chunks.length, 0, 'at least one chunk should be read');39 for (let i = 0; i < chunks.length; i++) {40 assert_equals(chunks[i].length, 128, 'chunk ' + i + ' should have 128 bytes');41 }42 return cancel;43 }).then(() => {44 assert_true(cancellationFinished, 'it returns a promise that is fulfilled when the cancellation finishes');45 });46}, 'ReadableStream cancellation: integration test on an infinite stream derived from a random push source');47test(() => {48 let recordedReason;49 const rs = new ReadableStream({50 cancel(reason) {51 recordedReason = reason;52 }53 });54 const passedReason = new Error('Sorry, it just wasn\'t meant to be.');55 rs.cancel(passedReason);56 assert_equals(recordedReason, passedReason,57 'the error passed to the underlying source\'s cancel method should equal the one passed to the stream\'s cancel');58}, 'ReadableStream cancellation: cancel(reason) should pass through the given reason to the underlying source');59promise_test(() => {60 const rs = new ReadableStream({61 start(c) {62 c.enqueue('a');63 c.close();64 },65 cancel() {66 assert_unreached('underlying source cancel() should not have been called');67 }68 });69 const reader = rs.getReader();70 return rs.cancel().then(() => {71 assert_unreached('cancel() should be rejected');72 }, e => {73 assert_equals(e.name, 'TypeError', 'cancel() should be rejected with a TypeError');74 }).then(() => {75 return reader.read();76 }).then(result => {77 assert_object_equals(result, { value: 'a', done: false }, 'read() should still work after the attempted cancel');78 return reader.closed;79 });80}, 'ReadableStream cancellation: cancel() on a locked stream should fail and not call the underlying source cancel');81promise_test(() => {82 let cancelReceived = false;83 const cancelReason = new Error('I am tired of this stream, I prefer to cancel it');84 const rs = new ReadableStream({85 cancel(reason) {86 cancelReceived = true;87 assert_equals(reason, cancelReason, 'cancellation reason given to the underlying source should be equal to the one passed');88 }89 });90 return rs.cancel(cancelReason).then(() => {91 assert_true(cancelReceived);92 });93}, 'ReadableStream cancellation: should fulfill promise when cancel callback went fine');94promise_test(() => {95 const rs = new ReadableStream({96 cancel() {97 return 'Hello';98 }99 });100 return rs.cancel().then(v => {101 assert_equals(v, undefined, 'cancel() return value should be fulfilled with undefined');102 });103}, 'ReadableStream cancellation: returning a value from the underlying source\'s cancel should not affect the fulfillment value of the promise returned by the stream\'s cancel');104promise_test(() => {105 const thrownError = new Error('test');106 let cancelCalled = false;107 const rs = new ReadableStream({108 cancel() {109 cancelCalled = true;110 throw thrownError;111 }112 });113 return rs.cancel('test').then(() => {114 assert_unreached('cancel should reject');115 }, e => {116 assert_true(cancelCalled);117 assert_equals(e, thrownError);118 });119}, 'ReadableStream cancellation: should reject promise when cancel callback raises an exception');120promise_test(() => {121 const cancelReason = new Error('test');122 const rs = new ReadableStream({123 cancel(error) {124 assert_equals(error, cancelReason);125 return delay(1);126 }127 });128 return rs.cancel(cancelReason);129}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should fulfill when that one does (1)');130promise_test(() => {131 let resolveSourceCancelPromise;132 let sourceCancelPromiseHasFulfilled = false;133 const rs = new ReadableStream({134 cancel() {135 const sourceCancelPromise = new Promise(resolve => resolveSourceCancelPromise = resolve);136 sourceCancelPromise.then(() => {137 sourceCancelPromiseHasFulfilled = true;138 });139 return sourceCancelPromise;140 }141 });142 setTimeout(() => resolveSourceCancelPromise('Hello'), 1);143 return rs.cancel().then(value => {144 assert_true(sourceCancelPromiseHasFulfilled, 'cancel() return value should be fulfilled only after the promise returned by the underlying source\'s cancel');145 assert_equals(value, undefined, 'cancel() return value should be fulfilled with undefined');146 });147}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should fulfill when that one does (2)');148promise_test(() => {149 let rejectSourceCancelPromise;150 let sourceCancelPromiseHasRejected = false;151 const rs = new ReadableStream({152 cancel() {153 const sourceCancelPromise = new Promise((resolve, reject) => rejectSourceCancelPromise = reject);154 sourceCancelPromise.catch(() => {155 sourceCancelPromiseHasRejected = true;156 });157 return sourceCancelPromise;158 }159 });160 const errorInCancel = new Error('Sorry, it just wasn\'t meant to be.');161 setTimeout(() => rejectSourceCancelPromise(errorInCancel), 1);162 return rs.cancel().then(() => {163 assert_unreached('cancel() return value should be rejected');164 }, r => {165 assert_true(sourceCancelPromiseHasRejected, 'cancel() return value should be rejected only after the promise returned by the underlying source\'s cancel');166 assert_equals(r, errorInCancel, 'cancel() return value should be rejected with the underlying source\'s rejection reason');167 });168}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should reject when that one does');169promise_test(() => {170 const rs = new ReadableStream({171 start() {172 return new Promise(() => {});173 },174 pull() {175 assert_unreached('pull should not have been called');176 }177 });178 return Promise.all([rs.cancel(), rs.getReader().closed]);179}, 'ReadableStream cancellation: cancelling before start finishes should prevent pull() from being called');...
cancel.any.js
Source:cancel.any.js
1// META: global=window,worker,jsshell2// META: script=../resources/test-utils.js3// META: script=../resources/rs-utils.js4'use strict';5promise_test(t => {6 const randomSource = new RandomPushSource();7 let cancellationFinished = false;8 const rs = new ReadableStream({9 start(c) {10 randomSource.ondata = c.enqueue.bind(c);11 randomSource.onend = c.close.bind(c);12 randomSource.onerror = c.error.bind(c);13 },14 pull() {15 randomSource.readStart();16 },17 cancel() {18 randomSource.readStop();19 return new Promise(resolve => {20 t.step_timeout(() => {21 cancellationFinished = true;22 resolve();23 }, 1);24 });25 }26 });27 const reader = rs.getReader();28 // We call delay multiple times to avoid cancelling too early for the29 // source to enqueue at least one chunk.30 const cancel = delay(5).then(() => delay(5)).then(() => delay(5)).then(() => {31 const cancelPromise = reader.cancel();32 assert_false(cancellationFinished, 'cancellation in source should happen later');33 return cancelPromise;34 });35 return readableStreamToArray(rs, reader).then(chunks => {36 assert_greater_than(chunks.length, 0, 'at least one chunk should be read');37 for (let i = 0; i < chunks.length; i++) {38 assert_equals(chunks[i].length, 128, 'chunk ' + i + ' should have 128 bytes');39 }40 return cancel;41 }).then(() => {42 assert_true(cancellationFinished, 'it returns a promise that is fulfilled when the cancellation finishes');43 });44}, 'ReadableStream cancellation: integration test on an infinite stream derived from a random push source');45test(() => {46 let recordedReason;47 const rs = new ReadableStream({48 cancel(reason) {49 recordedReason = reason;50 }51 });52 const passedReason = new Error('Sorry, it just wasn\'t meant to be.');53 rs.cancel(passedReason);54 assert_equals(recordedReason, passedReason,55 'the error passed to the underlying source\'s cancel method should equal the one passed to the stream\'s cancel');56}, 'ReadableStream cancellation: cancel(reason) should pass through the given reason to the underlying source');57promise_test(() => {58 const rs = new ReadableStream({59 start(c) {60 c.enqueue('a');61 c.close();62 },63 cancel() {64 assert_unreached('underlying source cancel() should not have been called');65 }66 });67 const reader = rs.getReader();68 return rs.cancel().then(() => {69 assert_unreached('cancel() should be rejected');70 }, e => {71 assert_equals(e.name, 'TypeError', 'cancel() should be rejected with a TypeError');72 }).then(() => {73 return reader.read();74 }).then(result => {75 assert_object_equals(result, { value: 'a', done: false }, 'read() should still work after the attempted cancel');76 return reader.closed;77 });78}, 'ReadableStream cancellation: cancel() on a locked stream should fail and not call the underlying source cancel');79promise_test(() => {80 let cancelReceived = false;81 const cancelReason = new Error('I am tired of this stream, I prefer to cancel it');82 const rs = new ReadableStream({83 cancel(reason) {84 cancelReceived = true;85 assert_equals(reason, cancelReason, 'cancellation reason given to the underlying source should be equal to the one passed');86 }87 });88 return rs.cancel(cancelReason).then(() => {89 assert_true(cancelReceived);90 });91}, 'ReadableStream cancellation: should fulfill promise when cancel callback went fine');92promise_test(() => {93 const rs = new ReadableStream({94 cancel() {95 return 'Hello';96 }97 });98 return rs.cancel().then(v => {99 assert_equals(v, undefined, 'cancel() return value should be fulfilled with undefined');100 });101}, 'ReadableStream cancellation: returning a value from the underlying source\'s cancel should not affect the fulfillment value of the promise returned by the stream\'s cancel');102promise_test(() => {103 const thrownError = new Error('test');104 let cancelCalled = false;105 const rs = new ReadableStream({106 cancel() {107 cancelCalled = true;108 throw thrownError;109 }110 });111 return rs.cancel('test').then(() => {112 assert_unreached('cancel should reject');113 }, e => {114 assert_true(cancelCalled);115 assert_equals(e, thrownError);116 });117}, 'ReadableStream cancellation: should reject promise when cancel callback raises an exception');118promise_test(() => {119 const cancelReason = new Error('test');120 const rs = new ReadableStream({121 cancel(error) {122 assert_equals(error, cancelReason);123 return delay(1);124 }125 });126 return rs.cancel(cancelReason);127}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should fulfill when that one does (1)');128promise_test(t => {129 let resolveSourceCancelPromise;130 let sourceCancelPromiseHasFulfilled = false;131 const rs = new ReadableStream({132 cancel() {133 const sourceCancelPromise = new Promise(resolve => resolveSourceCancelPromise = resolve);134 sourceCancelPromise.then(() => {135 sourceCancelPromiseHasFulfilled = true;136 });137 return sourceCancelPromise;138 }139 });140 t.step_timeout(() => resolveSourceCancelPromise('Hello'), 1);141 return rs.cancel().then(value => {142 assert_true(sourceCancelPromiseHasFulfilled, 'cancel() return value should be fulfilled only after the promise returned by the underlying source\'s cancel');143 assert_equals(value, undefined, 'cancel() return value should be fulfilled with undefined');144 });145}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should fulfill when that one does (2)');146promise_test(t => {147 let rejectSourceCancelPromise;148 let sourceCancelPromiseHasRejected = false;149 const rs = new ReadableStream({150 cancel() {151 const sourceCancelPromise = new Promise((resolve, reject) => rejectSourceCancelPromise = reject);152 sourceCancelPromise.catch(() => {153 sourceCancelPromiseHasRejected = true;154 });155 return sourceCancelPromise;156 }157 });158 const errorInCancel = new Error('Sorry, it just wasn\'t meant to be.');159 t.step_timeout(() => rejectSourceCancelPromise(errorInCancel), 1);160 return rs.cancel().then(() => {161 assert_unreached('cancel() return value should be rejected');162 }, r => {163 assert_true(sourceCancelPromiseHasRejected, 'cancel() return value should be rejected only after the promise returned by the underlying source\'s cancel');164 assert_equals(r, errorInCancel, 'cancel() return value should be rejected with the underlying source\'s rejection reason');165 });166}, 'ReadableStream cancellation: if the underlying source\'s cancel method returns a promise, the promise returned by the stream\'s cancel should reject when that one does');167promise_test(() => {168 const rs = new ReadableStream({169 start() {170 return new Promise(() => {});171 },172 pull() {173 assert_unreached('pull should not have been called');174 }175 });176 return Promise.all([rs.cancel(), rs.getReader().closed]);...
Using AI Code Generation
1var wpt = require('../lib/webpagetest');2var util = require('util');3var wpt = new WebPageTest('www.webpagetest.org');4var location = 'Dulles:Chrome';5var options = {6};7var testId = '170320_9Q_1d1f8a0c6e3d0b3f6d3a3a3c3f3f3f3f';8wpt.errorInCancel(testId, function(err, data) {9 if (err) return console.error(err);10 console.log(util.inspect(data, false, null));11});12## wpt.getTestResults(testId, callback)13var wpt = require('../lib/webpagetest');14var util = require('util');15var wpt = new WebPageTest('www.webpagetest.org');16var location = 'Dulles:Chrome';17var options = {18};19var testId = '170320_9Q_1d1f8a0c6e3d0b3f6d3a3a3c3f3f3f3f';20wpt.getTestResults(testId, function(err, data) {21 if (err) return console.error(err);22 console.log(util.inspect(data, false, null));23});24## wpt.getLocations(callback)25var wpt = require('../lib/webpagetest');26var util = require('util');27var wpt = new WebPageTest('www.web
Using AI Code Generation
1const api = require('wpt-api');2const wpt = new api('API_KEY');3wpt.errorInCancel('test_id', 'location', 'error_message', (err, data) => {4 if (err) {5 console.log(err);6 }7 else {8 console.log(data);9 }10});
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var webpagetest = new wpt(options);5var testId = '150805_7Y_6e2f6c9f9b9b9c4f4f4f4f4f4f4f4f4f';6var test = webpagetest.test(url, {7}, function (err, data) {8 if (err) {9 console.log(err);10 } else {11 console.log(data);12 webpagetest.errorInCancel(data.data.testId, function (err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18 });19 }20});
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!!