Best JavaScript code snippet using wpt
query.tentative.https.any.js
Source:query.tentative.https.any.js
1// META: title=Web Locks API: navigator.locks.query method2// META: script=resources/helpers.js3'use strict';4// Returns an array of the modes for the locks with matching name.5function modes(list, name) {6 return list.filter(item => item.name === name).map(item => item.mode);7}8// Returns an array of the clientIds for the locks with matching name.9function clients(list, name) {10 return list.filter(item => item.name === name).map(item => item.clientId);11}12promise_test(async t => {13 const res = uniqueName(t);14 await navigator.locks.request(res, async lock1 => {15 // Attempt to request this again - should be blocked.16 let lock2_acquired = false;17 navigator.locks.request(res, lock2 => { lock2_acquired = true; });18 // Verify that it was blocked.19 await navigator.locks.request(res, {ifAvailable: true}, async lock3 => {20 assert_false(lock2_acquired, 'second request should be blocked');21 assert_equals(lock3, null, 'third request should have failed');22 const state = await navigator.locks.query();23 assert_own_property(state, 'pending', 'State has `pending` property');24 assert_true(Array.isArray(state.pending),25 'State `pending` property is an array');26 const pending_info = state.pending[0];27 assert_own_property(pending_info, 'name',28 'Pending info dictionary has `name` property');29 assert_own_property(pending_info, 'mode',30 'Pending info dictionary has `mode` property');31 assert_own_property(pending_info, 'clientId',32 'Pending info dictionary has `clientId` property');33 assert_own_property(state, 'held', 'State has `held` property');34 assert_true(Array.isArray(state.held),35 'State `held` property is an array');36 const held_info = state.held[0];37 assert_own_property(held_info, 'name',38 'Held info dictionary has `name` property');39 assert_own_property(held_info, 'mode',40 'Held info dictionary has `mode` property');41 assert_own_property(held_info, 'clientId',42 'Held info dictionary has `clientId` property');43 });44 });45}, 'query() returns dictionaries with expected properties');46promise_test(async t => {47 const res = uniqueName(t);48 await navigator.locks.request(res, async lock1 => {49 const state = await navigator.locks.query();50 assert_array_equals(modes(state.held, res), ['exclusive'],51 'Held lock should appear once');52 });53 await navigator.locks.request(res, {mode: 'shared'}, async lock1 => {54 const state = await navigator.locks.query();55 assert_array_equals(modes(state.held, res), ['shared'],56 'Held lock should appear once');57 });58}, 'query() reports individual held locks');59promise_test(async t => {60 const res1 = uniqueName(t);61 const res2 = uniqueName(t);62 await navigator.locks.request(res1, async lock1 => {63 await navigator.locks.request(res2, {mode: 'shared'}, async lock2 => {64 const state = await navigator.locks.query();65 assert_array_equals(modes(state.held, res1), ['exclusive'],66 'Held lock should appear once');67 assert_array_equals(modes(state.held, res2), ['shared'],68 'Held lock should appear once');69 });70 });71}, 'query() reports multiple held locks');72promise_test(async t => {73 const res = uniqueName(t);74 await navigator.locks.request(res, async lock1 => {75 // Attempt to request this again - should be blocked.76 let lock2_acquired = false;77 navigator.locks.request(res, lock2 => { lock2_acquired = true; });78 // Verify that it was blocked.79 await navigator.locks.request(res, {ifAvailable: true}, async lock3 => {80 assert_false(lock2_acquired, 'second request should be blocked');81 assert_equals(lock3, null, 'third request should have failed');82 const state = await navigator.locks.query();83 assert_array_equals(modes(state.pending, res), ['exclusive'],84 'Pending lock should appear once');85 assert_array_equals(modes(state.held, res), ['exclusive'],86 'Held lock should appear once');87 });88 });89}, 'query() reports pending and held locks');90promise_test(async t => {91 const res = uniqueName(t);92 await navigator.locks.request(res, {mode: 'shared'}, async lock1 => {93 await navigator.locks.request(res, {mode: 'shared'}, async lock2 => {94 const state = await navigator.locks.query();95 assert_array_equals(modes(state.held, res), ['shared', 'shared'],96 'Held lock should appear twice');97 });98 });99}, 'query() reports held shared locks with appropriate count');100promise_test(async t => {101 const res = uniqueName(t);102 await navigator.locks.request(res, async lock1 => {103 let lock2_acquired = false, lock3_acquired = false;104 navigator.locks.request(res, {mode: 'shared'},105 lock2 => { lock2_acquired = true; });106 navigator.locks.request(res, {mode: 'shared'},107 lock3 => { lock3_acquired = true; });108 await navigator.locks.request(res, {ifAvailable: true}, async lock4 => {109 assert_equals(lock4, null, 'lock should not be available');110 assert_false(lock2_acquired, 'second attempt should be blocked');111 assert_false(lock3_acquired, 'third attempt should be blocked');112 const state = await navigator.locks.query();113 assert_array_equals(modes(state.held, res), ['exclusive'],114 'Held lock should appear once');115 assert_array_equals(modes(state.pending, res), ['shared', 'shared'],116 'Pending lock should appear twice');117 });118 });119}, 'query() reports pending shared locks with appropriate count');120promise_test(async t => {121 const res1 = uniqueName(t);122 const res2 = uniqueName(t);123 await navigator.locks.request(res1, async lock1 => {124 await navigator.locks.request(res2, async lock2 => {125 const state = await navigator.locks.query();126 const res1_clients = clients(state.held, res1);127 const res2_clients = clients(state.held, res2);128 assert_equals(res1_clients.length, 1, 'Each lock should have one holder');129 assert_equals(res2_clients.length, 1, 'Each lock should have one holder');130 assert_array_equals(res1_clients, res2_clients,131 'Both locks should have same clientId');132 });133 });134}, 'query() reports the same clientId for held locks from the same context');135promise_test(async t => {136 const res = uniqueName(t);137 const worker = new Worker('resources/worker.js');138 t.add_cleanup(() => { worker.terminate(); });139 await postToWorkerAndWait(140 worker, {op: 'request', name: res, mode: 'shared'});141 await navigator.locks.request(res, {mode: 'shared'}, async lock => {142 const state = await navigator.locks.query();143 const res_clients = clients(state.held, res);144 assert_equals(res_clients.length, 2, 'Clients should have same resource');145 assert_not_equals(res_clients[0], res_clients[1],146 'Clients should have different ids');147 });148}, 'query() reports different ids for held locks from different contexts');149promise_test(async t => {150 const res1 = uniqueName(t);151 const res2 = uniqueName(t);152 const worker = new Worker('resources/worker.js');153 t.add_cleanup(() => { worker.terminate(); });154 // Acquire 1 in the worker.155 await postToWorkerAndWait(worker, {op: 'request', name: res1})156 // Acquire 2 here.157 await new Promise(resolve => {158 navigator.locks.request(res2, lock => {159 resolve();160 return new Promise(() => {}); // Never released.161 });162 });163 // Request 2 in the worker.164 postToWorkerAndWait(worker, {op: 'request', name: res2});165 assert_true((await postToWorkerAndWait(worker, {166 op: 'request', name: res2, ifAvailable: true167 })).failed, 'Lock request should have failed');168 // Request 1 here.169 navigator.locks.request(170 res1, t.unreached_func('Lock should not be acquired'));171 // Verify that we're seeing a deadlock.172 const state = await navigator.locks.query();173 const res1_held_clients = clients(state.held, res1);174 const res2_held_clients = clients(state.held, res2);175 const res1_pending_clients = clients(state.pending, res1);176 const res2_pending_clients = clients(state.pending, res2);177 assert_equals(res1_held_clients.length, 1);178 assert_equals(res2_held_clients.length, 1);179 assert_equals(res1_pending_clients.length, 1);180 assert_equals(res2_pending_clients.length, 1);181 assert_equals(res1_held_clients[0], res2_pending_clients[0]);182 assert_equals(res2_held_clients[0], res1_pending_clients[0]);...
query.test.ts
Source:query.test.ts
1import {2 beforeEachHandle,3 createWebLocksInstance,4 generateRandomId,5 LockInfo,6} from "./helpers";7// Returns an array of the modes for the locks with matching name.8function modes(list: LockInfo[], name: string) {9 return list.filter((item) => item.name === name).map((item) => item.mode);10}11// Returns an array of the clientIds for the locks with matching name.12function clients(list: LockInfo[], name: string) {13 return list.filter((item) => item.name === name).map((item) => item.clientId);14}15describe("Web Locks API: navigator.locks.query method", () => {16 beforeEachHandle();17 test("query() returns dictionaries with expected properties", async () => {18 const webLocks = createWebLocksInstance();19 const sourceName = generateRandomId();20 await webLocks.request(sourceName, async (lock1) => {21 // Attempt to request this again - should be blocked.22 let lock2_acquired = false;23 webLocks.request(sourceName, (lock2) => {24 lock2_acquired = true;25 });26 // Verify that it was blocked.27 await webLocks.request(28 sourceName,29 { ifAvailable: true },30 async (lock3) => {31 // second request should be blocked32 expect(lock2_acquired).toBeFalsy();33 // third request should have failed34 expect(lock3).toEqual(null);35 const state = await webLocks.query();36 // State has `pending` property37 expect(state).toHaveProperty("pending");38 // State `pending` property is an array39 expect(Array.isArray(state.pending)).toBeTruthy();40 const pending_info = state.pending[0];41 // Pending info dictionary has `name` property42 expect(pending_info).toHaveProperty("name");43 // Pending info dictionary has `mode` property44 expect(pending_info).toHaveProperty("mode");45 // Pending info dictionary has `clientId` property46 expect(pending_info).toHaveProperty("clientId");47 // State has `held` property48 expect(state).toHaveProperty("held");49 // State `held` property is an array50 expect(Array.isArray(state.held)).toBeTruthy();51 const held_info = state.held[0];52 // Held info dictionary has `name` property53 expect(held_info).toHaveProperty("name");54 // Held info dictionary has `mode` property55 expect(held_info).toHaveProperty("mode");56 // Held info dictionary has `clientId` property57 expect(held_info).toHaveProperty("clientId");58 }59 );60 });61 });62 test("query() reports individual held locks", async () => {63 const webLocks = createWebLocksInstance();64 const sourceName = generateRandomId();65 await webLocks.request(sourceName, async (lock1) => {66 const state = await webLocks.query();67 // Held lock should appear once68 expect(modes(state.held, sourceName)).toEqual(["exclusive"]);69 });70 await webLocks.request(sourceName, { mode: "shared" }, async (lock1) => {71 const state = await webLocks.query();72 // Held lock should appear once73 expect(modes(state.held, sourceName)).toEqual(["shared"]);74 });75 });76 test("query() reports multiple held locks", async () => {77 const webLocks = createWebLocksInstance();78 const sourceName1 = generateRandomId();79 const sourceName2 = generateRandomId();80 await webLocks.request(sourceName1, async (lock1) => {81 await webLocks.request(sourceName2, { mode: "shared" }, async (lock2) => {82 const state = await webLocks.query();83 // SHeld lock should appear once84 expect(modes(state.held, sourceName1)).toEqual(["exclusive"]);85 // Held lock should appear once86 expect(modes(state.held, sourceName2)).toEqual(["shared"]);87 });88 });89 });90 test("query() reports pending and held locks", async () => {91 const webLocks = createWebLocksInstance();92 const sourceName = generateRandomId();93 await webLocks.request(sourceName, async (lock1) => {94 // Attempt to request this again - should be blocked.95 let lock2_acquired = false;96 webLocks.request(sourceName, (lock2) => {97 lock2_acquired = true;98 });99 // Verify that it was blocked.100 await webLocks.request(101 sourceName,102 { ifAvailable: true },103 async (lock3) => {104 // second request should be blocked105 expect(lock2_acquired).toBeFalsy();106 // third request should have failed107 expect(lock3).toEqual(null);108 const state = await webLocks.query();109 // Pending lock should appear once110 expect(modes(state.pending, sourceName)).toEqual(["exclusive"]);111 // Held lock should appear once112 expect(modes(state.held, sourceName)).toEqual(["exclusive"]);113 }114 );115 });116 });117 test("query() reports held shared locks with appropriate count", async () => {118 const webLocks = createWebLocksInstance();119 const sourceName = generateRandomId();120 await webLocks.request(sourceName, { mode: "shared" }, async (lock1) => {121 await webLocks.request(sourceName, { mode: "shared" }, async (lock2) => {122 const state = await webLocks.query();123 // Held lock should appear twice124 expect(modes(state.held, sourceName)).toEqual(["shared", "shared"]);125 });126 });127 });128 test("query() reports pending shared locks with appropriate count", async () => {129 const webLocks = createWebLocksInstance();130 const sourceName = generateRandomId();131 await webLocks.request(sourceName, async (lock1) => {132 let lock2_acquired = false,133 lock3_acquired = false;134 webLocks.request(sourceName, { mode: "shared" }, (lock2) => {135 lock2_acquired = true;136 });137 webLocks.request(sourceName, { mode: "shared" }, (lock3) => {138 lock3_acquired = true;139 });140 await webLocks.request(141 sourceName,142 { ifAvailable: true },143 async (lock4) => {144 // lock should not be available145 expect(lock4).toEqual(null);146 // second attempt should be blocked147 expect(lock2_acquired).toBeFalsy();148 // third attempt should be blocked149 expect(lock3_acquired).toBeFalsy();150 const state = await webLocks.query();151 // Held lock should appear once152 expect(modes(state.held, sourceName)).toEqual(["exclusive"]);153 // Pending lock should appear twice154 expect(modes(state.pending, sourceName)).toEqual([155 "shared",156 "shared",157 ]);158 }159 );160 });161 });162 test("query() reports the same clientId for held locks from the same context", async () => {163 const webLocks = createWebLocksInstance();164 const sourceName1 = generateRandomId();165 const sourceName2 = generateRandomId();166 await webLocks.request(sourceName1, async (lock1) => {167 await webLocks.request(sourceName2, async (lock2) => {168 const state = await webLocks.query();169 const res1_clients = clients(state.held, sourceName1);170 const res2_clients = clients(state.held, sourceName2);171 // Each lock should have one holder172 expect(res1_clients.length).toEqual(1);173 // Each lock should have one holder174 expect(res2_clients.length).toEqual(1);175 // Both locks should have same clientId176 expect(res1_clients).toEqual(res2_clients);177 });178 });179 });180 // TODO: other method to replace181 test("query() reports different ids for held locks from different contexts", async () => {});...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3wpt.getLocations(function(err, data) {4 console.log(data);5});6{ [Error: getaddrinfo ENOTFOUND www.webpagetest.org www.webpagetest.org:80]7 port: 80 }8var wpt = require('wpt');9var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');10wpt.getLocations(function(err, data) {11 console.log(data);12});13{ [Error: getaddrinfo ENOTFOUND www.webpagetest.org www.webpagetest.org:80]14 port: 80 }15var wpt = new WebPageTest('
Using AI Code Generation
1var wpt = require('wpt');2wpt.get('res1_clients', function(data, err) {3 if (err) {4 console.log(err);5 }6 else {7 console.log(data);8 }9});10{ '0': 'Chrome',
Using AI Code Generation
1var wpt = require('./wpt.js');2wpt.res1_clients(function(err, data){3 console.log(data);4});5var request = require('request');6module.exports = {7 res1_clients: function(callback){8 if (!error && response.statusCode == 200) {9 callback(null, body);10 }else{11 callback(error, null);12 }13 })14 }15};16var wpt = require('./wpt.js');17wpt.res1_clients()18.then(function(data){19 console.log(data);20});21var request = require('request');22var Promise = require('bluebird');23module.exports = {24 res1_clients: function(){25 return new Promise(function(resolve, reject){26 if (!error && response.statusCode == 200) {27 resolve(body);28 }else{29 reject(error);30 }31 })32 });33 }34};
Using AI Code Generation
1var wptools = require('wptools');2var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){3 console.log(data);4});5var wptools = require('wptools');6var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){7 console.log(data);8});9var wptools = require('wptools');10var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){11 console.log(data);12});13var wptools = require('wptools');14var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){15 console.log(data);16});17var wptools = require('wptools');18var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){19 console.log(data);20});21var wptools = require('wptools');22var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){23 console.log(data);24});25var wptools = require('wptools');26var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){27 console.log(data);28});29var wptools = require('wptools');30var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){31 console.log(data);32});33var wptools = require('wptools');34var res1 = wptools.page('Barack Obama').get_res1_clients(function(data){35 console.log(data);36});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.0c2e2d1d9e2f2d2d2d2d2d2d2d2d2d2d');3 if (err) return console.log('Error: ' + err);4 console.log('Test status check: ' + data.statusText);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.log('Error: ' + err);7 console.log('Test completed');8 console.log(data.data.median.firstView);9 });10});11{ firstView: { bytesIn: 0, bytesOut: 0, requests: 0, responses_200: 0, responses_404: 0, responses_other: 0, result: 0, loadTime: 0, TTFB: 0, render: 0, fullyLoaded: 0, docTime: 0, domTime: 0, domElements: 0, score_cache: 0, score_cdn: 0, score_gzip: 0, score_cookies: 0, score_keep-alive: 0, score_minify: 0, score_combine: 0, score_compress: 0, score_etags: 0, score_progressive_jpeg: 0, cache_time: 0, score_cdn_provider: 0 },12 { bytesIn: 0,
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!!