web-locks-pa-worklet-batch-update.tentative.https.window.js (3689B)
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.batchUpdate([ 32 new SharedStorageAppendMethod('key', 'a'), 33 new SharedStorageAppendMethod('key', 'a') 34 ], {withLock: 'lock1'}); 35 36 return {}; 37 ` 38 }); 39 40 let decisionLogicURL = createDecisionScriptURL(pa_uuid); 41 42 // Invoke `selectURL()` to perform the following steps: 43 // 1. Acquires the lock. 44 // 2. Reads the current value at the given key. 45 // 3. Waits for 500ms. 46 // 4. Sets the shared storage value to the read value appended with the given letter. 47 // 5. Releases the lock. 48 // 49 // After 100ms, run a Protected Audience auction that starts a worklet that: 50 // - Acquires the same named lock. 51 // - Executes two `append` methods, each appending the same letter. 52 // 53 // Expected behavior: After both of them finish, the value at the given key 54 // should contain the letter repeated three times. 55 // 56 // This demonstrates that: 57 // 1. The `withLock` option is effective, preventing the `batchUpdate()` 58 // method interfering with the "get and set" operation. If the lock were 59 // not used, the final value would likely be a single letter. 60 // 2. `batchUpdate()` correctly executes all `append` methods within the 61 // batch. 62 // 63 // Note: This test remains valid even if the `batchUpdate()` call happens 64 // outside the critical section protected by the lock within the worklet. The 65 // test effectively demonstrates mutual exclusion as long as there's a 66 // reasonable chance for `batchUpdate()` to occur while the worklet is still 67 // running. 68 let select_url_result = await worklet.selectURL( 69 "get-wait-set-within-lock", 70 [{url: url0}, {url: url1}], 71 {data: {'key': 'key', 72 'lock_name': 'lock1', 73 'append_letter': 'a'}, 74 resolveToConfig: true}); 75 76 // Busy wait for 100ms. 77 const startWaitTime = Date.now(); 78 while (Date.now() - startWaitTime < 100) {} 79 80 // Run a Protected Audience auction which triggers `append()` with the same 81 // lock and the same letter. 82 await joinGroupAndRunBasicFledgeTestExpectingNoWinner( 83 test, 84 { 85 uuid: pa_uuid, 86 interestGroupOverrides: { 87 name: pa_uuid, 88 biddingLogicURL: biddingLogicURL, 89 }, 90 auctionConfigOverrides: { 91 decisionLogicURL: decisionLogicURL 92 } 93 }); 94 95 attachFencedFrame(select_url_result, 'opaque-ads'); 96 const result = await nextValueFromServer(ancestor_key); 97 assert_equals(result, "frame1_loaded"); 98 99 await verifyKeyValueForOrigin('key', 'aaa', location.origin); 100 101 await deleteKeyForOrigin('key', location.origin); 102 }, 'Test for batchUpdate() with a batch lock in a Protected Audience Worklet context');