Best JavaScript code snippet using wpt
blob-composite-blob-reads.any.js
Source:blob-composite-blob-reads.any.js
1// META: title=IDB-backed composite blobs maintain coherency2// META: script=resources/support-promises.js3// META: timeout=long4// This test file is intended to help validate browser handling of complex blob5// scenarios where one or more levels of multipart blobs are used and varying6// IPC serialization strategies may be used depending on various complexity7// heuristics.8//9// A variety of approaches of reading the blob's contents are attempted for10// completeness:11// - `fetch-blob-url`: fetch of a URL created via URL.createObjectURL12// - Note that this is likely to involve multi-process behavior in a way that13// the next 2 currently will not unless their Blobs are round-tripped14// through a MessagePort.15// - `file-reader`: FileReader16// - `direct`: Blob.prototype.arrayBuffer()17function composite_blob_test({ blobCount, blobSize, name }) {18 // NOTE: In order to reduce the runtime of this test and due to the similarity19 // of the "file-reader" mechanism to the "direct", "file-reader" is commented20 // out, but if you are investigating failures detected by this test, you may21 // want to uncomment it.22 for (const mode of ["fetch-blob-url", /*"file-reader",*/ "direct"]) {23 promise_test(async testCase => {24 const key = "the-blobs";25 let memBlobs = [];26 for (let iBlob = 0; iBlob < blobCount; iBlob++) {27 memBlobs.push(new Blob([make_arraybuffer_contents(iBlob, blobSize)]));28 }29 const db = await createDatabase(testCase, db => {30 db.createObjectStore("blobs");31 });32 const write_tx = db.transaction("blobs", "readwrite");33 let store = write_tx.objectStore("blobs");34 store.put(memBlobs, key);35 // Make the blobs eligible for GC which is most realistic and most likely36 // to cause problems.37 memBlobs = null;38 await promiseForTransaction(testCase, write_tx);39 const read_tx = db.transaction("blobs");40 store = read_tx.objectStore("blobs");41 const read_req = store.get(key);42 await promiseForTransaction(testCase, read_tx);43 const diskBlobs = read_req.result;44 const compositeBlob = new Blob(diskBlobs);45 if (mode === "fetch-blob-url") {46 const blobUrl = URL.createObjectURL(compositeBlob);47 let urlResp = await fetch(blobUrl);48 let urlFetchArrayBuffer = await urlResp.arrayBuffer();49 urlResp = null;50 URL.revokeObjectURL(blobUrl);51 validate_arraybuffer_contents("fetched URL", urlFetchArrayBuffer, blobCount, blobSize);52 urlFetchArrayBuffer = null;53 } else if (mode === "file-reader") {54 let reader = new FileReader();55 let readerPromise = new Promise(resolve => {56 reader.onload = () => {57 resolve(reader.result);58 }59 })60 reader.readAsArrayBuffer(compositeBlob);61 let readArrayBuffer = await readerPromise;62 readerPromise = null;63 reader = null;64 validate_arraybuffer_contents("FileReader", readArrayBuffer, blobCount, blobSize);65 readArrayBuffer = null;66 } else if (mode === "direct") {67 let directArrayBuffer = await compositeBlob.arrayBuffer();68 validate_arraybuffer_contents("arrayBuffer", directArrayBuffer, blobCount, blobSize);69 }70 }, `Composite Blob Handling: ${name}: ${mode}`);71 }72}73// Create an ArrayBuffer whose even bytes are the index identifier and whose74// odd bytes are a sequence incremented by 3 (wrapping at 256) so that75// discontinuities at power-of-2 boundaries are more detectable.76function make_arraybuffer_contents(index, size) {77 const arr = new Uint8Array(size);78 for (let i = 0, counter = 0; i < size; i += 2, counter = (counter + 3) % 256) {79 arr[i] = index;80 arr[i + 1] = counter;81 }82 return arr.buffer;83}84function validate_arraybuffer_contents(source, buffer, blobCount, blobSize) {85 // Accumulate a list of problems we perceive so we can report what seems to86 // have happened all at once.87 const problems = [];88 const arr = new Uint8Array(buffer);89 const expectedLength = blobCount * blobSize;90 const actualCount = arr.length / blobSize;91 if (arr.length !== expectedLength) {92 problems.push(`ArrayBuffer only holds ${actualCount} blobs' worth instead of ${blobCount}.`);93 problems.push(`Actual ArrayBuffer is ${arr.length} bytes but expected ${expectedLength}`);94 }95 const counterBlobStep = (blobSize / 2 * 3) % 256;96 let expectedBlob = 0;97 let blobSeenSoFar = 0;98 let expectedCounter = 0;99 let counterDrift = 0;100 for (let i = 0; i < arr.length; i += 2) {101 if (arr[i] !== expectedBlob || blobSeenSoFar >= blobSize) {102 if (blobSeenSoFar !== blobSize) {103 problems.push(`Truncated blob ${expectedBlob} after ${blobSeenSoFar} bytes.`);104 } else {105 expectedBlob++;106 }107 if (expectedBlob !== arr[i]) {108 problems.push(`Expected blob ${expectedBlob} but found ${arr[i]}, compensating.`);109 expectedBlob = arr[i];110 }111 blobSeenSoFar = 0;112 expectedCounter = (expectedBlob * counterBlobStep) % 256;113 counterDrift = 0;114 }115 if (arr[i + 1] !== (expectedCounter + counterDrift) % 256) {116 const newDrift = expectedCounter - arr[i + 1];117 problems.push(`In blob ${expectedBlob} at ${blobSeenSoFar + 1} bytes in, counter drift now ${newDrift} was ${counterDrift}`);118 counterDrift = newDrift;119 }120 blobSeenSoFar += 2;121 expectedCounter = (expectedCounter + 3) % 256;122 }123 if (problems.length) {124 assert_true(false, `${source} blob payload problem: ${problems.join("\n")}`);125 } else {126 assert_true(true, `${source} blob payloads validated.`);127 }128}129composite_blob_test({130 blobCount: 16,131 blobSize: 256 * 1024,132 name: "Many blobs",...
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!!