web-locks-pa-worklet-modifier-method.tentative.https.window.js (3350B)
1 // META: script=/resources/testdriver.js 2 // META: script=/resources/testdriver-vendor.js 3 // META: script=/common/utils.js 4 // META: script=/fledge/tentative/resources/fledge-util.sub.js 5 // META: script=/common/subset-tests.js 6 // META: script=/shared-storage/resources/util.js 7 // META: script=/fenced-frame/resources/utils.js 8 // META: timeout=long 9 10 "use strict;" 11 12 subsetTest(promise_test, async test => { 13 let worklet = await sharedStorage.createWorklet('resources/simple-module.js'); 14 15 const ancestor_key = token(); 16 let url0 = generateURL("/shared-storage/resources/frame0.html", 17 [ancestor_key]); 18 let url1 = generateURL("/shared-storage/resources/frame1.html", 19 [ancestor_key]); 20 21 // Override the default resource path, as we are not running within the Fledge 22 // repository. 23 RESOURCE_PATH = '/fledge/tentative/resources/'; 24 25 const pa_uuid = generateUuid(test); 26 27 let biddingLogicURL = createBiddingScriptURL( 28 { 29 generateBid: 30 ` 31 sharedStorage.append('key', 'a', {withLock: 'lock1'}); 32 33 return {}; 34 ` 35 }); 36 37 let decisionLogicURL = createDecisionScriptURL(pa_uuid); 38 39 // Invoke `selectURL()` to perform the following steps: 40 // 1. Acquires the lock. 41 // 2. Reads the current value at the given key. 42 // 3. Waits for 500ms. 43 // 4. Sets the shared storage value to the read value appended with the given letter. 44 // 5. Releases the lock. 45 // 46 // After 100ms, run a Protected Audience auction which triggers `append()` 47 // with the same lock and the same letter. 48 // 49 // Expected behavior: After both of them finish, the value at the given key 50 // should contain the letter repeated twice. 51 // 52 // This demonstrates that the `withLock` option is effective, preventing the 53 // `append()` method interfering with the "get and set" operation. If the lock 54 // were not used, the final value would likely be a single letter. 55 // 56 // Note: This test remains valid even if the `append()` happens outside 57 // the critical section protected by the lock within the worklet. The test 58 // effectively demonstrates mutual exclusion as long as there's a reasonable 59 // chance for `append()` to occur while the worklet is still running. 60 let select_url_result = await worklet.selectURL( 61 "get-wait-set-within-lock", 62 [{url: url0}, {url: url1}], 63 {data: {'key': 'key', 64 'lock_name': 'lock1', 65 'append_letter': 'a'}, 66 resolveToConfig: true}); 67 68 // Busy wait for 100ms. 69 const startWaitTime = Date.now(); 70 while (Date.now() - startWaitTime < 100) {} 71 72 // Run a Protected Audience auction which triggers `append()`` with the same 73 // lock and the same letter. 74 await joinGroupAndRunBasicFledgeTestExpectingNoWinner( 75 test, 76 { 77 uuid: pa_uuid, 78 interestGroupOverrides: { 79 name: pa_uuid, 80 biddingLogicURL: biddingLogicURL, 81 }, 82 auctionConfigOverrides: { 83 decisionLogicURL: decisionLogicURL 84 } 85 }); 86 87 attachFencedFrame(select_url_result, 'opaque-ads'); 88 const result = await nextValueFromServer(ancestor_key); 89 assert_equals(result, "frame1_loaded"); 90 91 await verifyKeyValueForOrigin('key', 'aa', location.origin); 92 93 await deleteKeyForOrigin('key', location.origin); 94 }, 'Test for withLock option in a Protected Audience Worklet context');