Best JavaScript code snippet using wpt
interleaved-cursors-common.js
Source:interleaved-cursors-common.js
1// Infrastructure shared by interleaved-cursors-{small,large}.html2// Number of objects that each iterator goes over.3const itemCount = 10;4// Ratio of small objects to large objects.5const largeObjectRatio = 5;6// Size of large objects. This should exceed the size of a block in the storage7// method underlying the browser's IndexedDB implementation. For example, this8// needs to exceed the LevelDB block size on Chrome, and the SQLite block size9// on Firefox.10const largeObjectSize = 48 * 1024;11function objectKey(cursorIndex, itemIndex) {12 return `${cursorIndex}-key-${itemIndex}`;13}14function objectValue(cursorIndex, itemIndex) {15 if ((cursorIndex * itemCount + itemIndex) % largeObjectRatio === 0) {16 // We use a typed array (as opposed to a string) because IndexedDB17 // implementations may serialize strings using UTF-8 or UTF-16, yielding18 // larger IndexedDB entries than we'd expect. It's very unlikely that an19 // IndexedDB implementation would use anything other than the raw buffer to20 // serialize a typed array.21 const buffer = new Uint8Array(largeObjectSize);22 // Some IndexedDB implementations, like LevelDB, compress their data blocks23 // before storing them to disk. We use a simple 32-bit xorshift PRNG, which24 // should be sufficient to foil any fast generic-purpose compression scheme.25 // 32-bit xorshift - the seed can't be zero26 let state = 1000 + (cursorIndex * itemCount + itemIndex);27 for (let i = 0; i < largeObjectSize; ++i) {28 state ^= state << 13;29 state ^= state >> 17;30 state ^= state << 5;31 buffer[i] = state & 0xff;32 }33 return buffer;34 }35 return [cursorIndex, 'small', itemIndex];36}37// Writes the objects to be read by one cursor. Returns a promise that resolves38// when the write completes.39//40// We want to avoid creating a large transaction, because that is outside the41// test's scope, and it's a bad practice. So we break up the writes across42// multiple transactions. For simplicity, each transaction writes all the43// objects that will be read by a cursor.44function writeCursorObjects(database, cursorIndex) {45 return new Promise((resolve, reject) => {46 const transaction = database.transaction('cache', 'readwrite');47 transaction.onabort = () => { reject(transaction.error); };48 const store = transaction.objectStore('cache');49 for (let i = 0; i < itemCount; ++i) {50 store.put({51 key: objectKey(cursorIndex, i), value: objectValue(cursorIndex, i)});52 }53 transaction.oncomplete = resolve;54 });55}56// Returns a promise that resolves when the store has been populated.57function populateTestStore(testCase, database, cursorCount) {58 let promiseChain = Promise.resolve();59 for (let i = 0; i < cursorCount; ++i)60 promiseChain = promiseChain.then(() => writeCursorObjects(database, i));61 return promiseChain;62}63// Reads cursors in an interleaved fashion, as shown below.64//65// Given N cursors, each of which points to the beginning of a K-item sequence,66// the following accesses will be made.67//68// OC(i) = open cursor i69// RD(i, j) = read result of cursor i, which should be at item j70// CC(i) = continue cursor i71// | = wait for onsuccess on the previous OC or CC72//73// OC(1) | RD(1, 1) OC(2) | RD(2, 1) OC(3) | ... | RD(n-1, 1) CC(n) |74// RD(n, 1) CC(1) | RD(1, 2) CC(2) | RD(2, 2) CC(3) | ... | RD(n-1, 2) CC(n) |75// RD(n, 2) CC(1) | RD(1, 3) CC(2) | RD(2, 3) CC(3) | ... | RD(n-1, 3) CC(n) |76// ...77// RD(n, k-1) CC(1) | RD(1, k) CC(2) | RD(2, k) CC(3) | ... | RD(n-1, k) CC(n) |78// RD(n, k) done79function interleaveCursors(testCase, store, cursorCount) {80 return new Promise((resolve, reject) => {81 // The cursors used for iteration are stored here so each cursor's onsuccess82 // handler can call continue() on the next cursor.83 const cursors = [];84 // The results of IDBObjectStore.openCursor() calls are stored here so we85 // we can change the requests' onsuccess handler after every86 // IDBCursor.continue() call.87 const requests = [];88 const checkCursorState = (cursorIndex, itemIndex) => {89 const cursor = cursors[cursorIndex];90 assert_equals(cursor.key, objectKey(cursorIndex, itemIndex));91 assert_equals(cursor.value.key, objectKey(cursorIndex, itemIndex));92 assert_equals(93 cursor.value.value.join('-'),94 objectValue(cursorIndex, itemIndex).join('-'));95 };96 const openCursor = (cursorIndex, callback) => {97 const request = store.openCursor(98 IDBKeyRange.lowerBound(objectKey(cursorIndex, 0)));99 requests[cursorIndex] = request;100 request.onsuccess = testCase.step_func(() => {101 const cursor = request.result;102 cursors[cursorIndex] = cursor;103 checkCursorState(cursorIndex, 0);104 callback();105 });106 request.onerror = event => reject(request.error);107 };108 const readItemFromCursor = (cursorIndex, itemIndex, callback) => {109 const request = requests[cursorIndex];110 request.onsuccess = testCase.step_func(() => {111 const cursor = request.result;112 cursors[cursorIndex] = cursor;113 checkCursorState(cursorIndex, itemIndex);114 callback();115 });116 const cursor = cursors[cursorIndex];117 cursor.continue();118 };119 // We open all the cursors one at a time, then cycle through the cursors and120 // call continue() on each of them. This access pattern causes maximal121 // trashing to an LRU cursor cache. Eviction scheme aside, any cache will122 // have to evict some cursors, and this access pattern verifies that the123 // cache correctly restores the state of evicted cursors.124 const steps = [];125 for (let cursorIndex = 0; cursorIndex < cursorCount; ++cursorIndex)126 steps.push(openCursor.bind(null, cursorIndex));127 for (let itemIndex = 1; itemIndex < itemCount; ++itemIndex) {128 for (let cursorIndex = 0; cursorIndex < cursorCount; ++cursorIndex)129 steps.push(readItemFromCursor.bind(null, cursorIndex, itemIndex));130 }131 const runStep = (stepIndex) => {132 if (stepIndex === steps.length) {133 resolve();134 return;135 }136 steps[stepIndex](() => { runStep(stepIndex + 1); });137 };138 runStep(0);139 });140}141function cursorTest(cursorCount) {142 promise_test(testCase => {143 return createDatabase(testCase, (database, transaction) => {144 const store = database.createObjectStore('cache',145 { keyPath: 'key', autoIncrement: true });146 }).then(database => {147 return populateTestStore(testCase, database, cursorCount).then(148 () => database);149 }).then(database => {150 database.close();151 }).then(() => {152 return openDatabase(testCase);153 }).then(database => {154 const transaction = database.transaction('cache', 'readonly');155 transaction.onabort = () => { reject(transaction.error); };156 const store = transaction.objectStore('cache');157 return interleaveCursors(testCase, store, cursorCount).then(158 () => database);159 }).then(database => {160 database.close();161 });162 }, `${cursorCount} cursors`);...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wptoolkit.readItemFromCursor(function(err, data){3 if(err){4 console.log(err);5 }else{6 console.log(data);7 }8});9var wptoolkit = require('wptoolkit');10wptoolkit.readItemFromCursor(function(err, data){11 if(err){12 console.log(err);13 }else{14 console.log(data);15 }16});17var wptoolkit = require('wptoolkit');18wptoolkit.readItemFromCursor(function(err, data){19 if(err){20 console.log(err);21 }else{22 console.log(data);23 }24});25var wptoolkit = require('wptoolkit');26wptoolkit.readItemFromCursor(function(err, data){27 if(err){28 console.log(err);29 }else{30 console.log(data);31 }32});33var wptoolkit = require('wptoolkit');34wptoolkit.readItemFromCursor(function(err, data){35 if(err){36 console.log(err);37 }else{38 console.log(data);39 }40});41var wptoolkit = require('wptoolkit');42wptoolkit.readItemFromCursor(function(err, data){43 if(err){44 console.log(err);45 }else{46 console.log(data);47 }48});49var wptoolkit = require('wptoolkit');50wptoolkit.readItemFromCursor(function(err, data){51 if(err){52 console.log(err);53 }else{54 console.log(data);55 }56});57var wptoolkit = require('wptoolkit
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var cursor = wptoolkit.readItemFromCursor();3console.log(cursor);4var wptoolkit = require('wptoolkit');5var cursor = wptoolkit.readItemFromCursor();6console.log(cursor);7var wptoolkit = require('wptoolkit');8var cursor = wptoolkit.readItemFromCursor();9console.log(cursor);10var wptoolkit = require('wptoolkit');11var cursor = wptoolkit.readItemFromCursor();12console.log(cursor);13var wptoolkit = require('wptoolkit');14var cursor = wptoolkit.readItemFromCursor();15console.log(cursor);16var wptoolkit = require('wptoolkit');17var cursor = wptoolkit.readItemFromCursor();18console.log(cursor);19var wptoolkit = require('wptoolkit');20var cursor = wptoolkit.readItemFromCursor();21console.log(cursor);22var wptoolkit = require('wptoolkit');23var cursor = wptoolkit.readItemFromCursor();24console.log(cursor);25var wptoolkit = require('wptoolkit');26var cursor = wptoolkit.readItemFromCursor();27console.log(cursor);28var wptoolkit = require('wptoolkit');29var cursor = wptoolkit.readItemFromCursor();30console.log(cursor);
Using AI Code Generation
1var wptools = require('wptools');2wptools.page('Barack Obama').then(function(page) {3 page.get().then(function(result) {4 console.log(result);5 console.log(result.sections[0].content[0].text);6 });7});
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools();3wp.readItemFromCursor('Q5', function(err, item) {4 console.log(item);5});6var wptools = require('wptools');7var wp = new wptools();8wp.readItemFromCursor('Q5', function(err, item) {9 console.log(item);10});11var wptools = require('wptools');12var wp = new wptools();13wp.readItemFromCursor('Q5', function(err, item) {14 console.log(item);15});16var wptools = require('wptools');17var wp = new wptools();18wp.readItemFromCursor('Q5', function(err, item) {19 console.log(item);20});21var wptools = require('wptools');22var wp = new wptools();23wp.readItemFromCursor('Q5', function(err, item) {24 console.log(item);25});26var wptools = require('wptools');27var wp = new wptools();28wp.readItemFromCursor('Q5', function(err, item) {29 console.log(item);30});31var wptools = require('wptools');32var wp = new wptools();33wp.readItemFromCursor('Q5', function(err, item) {34 console.log(item);35});36var wptools = require('wptools');37var wp = new wptools();38wp.readItemFromCursor('Q5', function(err, item) {39 console.log(item);40});41var wptools = require('wptools');
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var result = wptoolkit.readItemFromCursor('C:\\Users\\test\\Desktop\\test.txt');3console.log(result);4var wptoolkit = require('wptoolkit');5var result = wptoolkit.writeItemToCursor('C:\\Users\\test\\Desktop\\test.txt');6console.log(result);7var wptoolkit = require('wptoolkit');8var result = wptoolkit.deleteItemFromCursor('C:\\Users\\test\\Desktop\\test.txt');9console.log(result);10var wptoolkit = require('wptoolkit');11var result = wptoolkit.createFolder('C:\\Users\\test\\Desktop\\test.txt');12console.log(result);13var wptoolkit = require('wptoolkit');14var result = wptoolkit.deleteFolder('C:\\Users\\test\\Desktop\\test.txt');15console.log(result);16var wptoolkit = require('wptoolkit');17var result = wptoolkit.listFolderContents('C:\\Users\\test\\Desktop\\test.txt');18console.log(result);19var wptoolkit = require('wptoolkit');20var result = wptoolkit.createFile('C:\\Users\\test\\Desktop\\test.txt');21console.log(result);22var wptoolkit = require('wptoolkit');23var result = wptoolkit.deleteFile('C:\\Users\\test\\Desktop\\test.txt');24console.log(result);25var wptoolkit = require('wptoolkit');26var result = wptoolkit.listFileContents('C:\\Users\\test\\Desktop\\test.txt');27console.log(result);28var wptoolkit = require('wptoolkit');29var result = wptoolkit.writeToFile('
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!!