web-locks-window-batch-update.tentative.https.sub.html (2799B)
1 <!doctype html> 2 <script src="/resources/testharness.js"></script> 3 <script src="/resources/testharnessreport.js"></script> 4 <script src="/common/utils.js"></script> 5 <script src="/shared-storage/resources/util.js"></script> 6 <script src="/fenced-frame/resources/utils.js"></script> 7 8 <body> 9 <script> 10 'use strict'; 11 12 promise_test(async t => { 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 // Invoke `selectURL()` to perform the following steps: 22 // 1. Acquires the lock. 23 // 2. Reads the current value at the given key. 24 // 3. Waits for 500ms. 25 // 4. Sets the shared storage value to the read value appended with the given letter. 26 // 5. Releases the lock. 27 // 28 // After 100ms, invoke `sharedStorage.batchUpdate()` that: 29 // - Acquires the same named lock. 30 // - Executes two `append` methods, each appending the same letter. 31 // 32 // Expected behavior: After both of them finish, the value at the given key 33 // should contain the letter repeated three times. 34 // 35 // This demonstrates that: 36 // 1. The `withLock` option is effective, preventing the `batchUpdate()` 37 // method interfering with the "get and set" operation. If the lock were not 38 // used, the final value would likely be a single letter. 39 // 2. `batchUpdate()` correctly executes all `append` methods within the 40 // batch. 41 // 42 // Note: This test remains valid even if the `batchUpdate()` call happens 43 // outside the critical section protected by the lock within the worklet. The 44 // test effectively demonstrates mutual exclusion as long as there's a 45 // reasonable chance for `batchUpdate()` to occur while the worklet is still 46 // running. 47 let select_url_result = await worklet.selectURL( 48 "get-wait-set-within-lock", 49 [{url: url0}, {url: url1}], 50 {data: {'key': 'key', 51 'lock_name': 'lock1', 52 'append_letter': 'a'}, 53 resolveToConfig: true}); 54 55 // Busy wait for 100ms. 56 const startWaitTime = Date.now(); 57 while (Date.now() - startWaitTime < 100) {} 58 59 sharedStorage.batchUpdate([ 60 new SharedStorageAppendMethod('key', 'a'), 61 new SharedStorageAppendMethod('key', 'a') 62 ], {withLock: 'lock1'}); 63 64 attachFencedFrame(select_url_result, 'opaque-ads'); 65 const result = await nextValueFromServer(ancestor_key); 66 assert_equals(result, "frame1_loaded"); 67 68 await verifyKeyValueForOrigin('key', 'aaa', location.origin); 69 70 await deleteKeyForOrigin('key', location.origin); 71 }, 'Test for batchUpdate() with a batch lock in a Window context'); 72 73 </script> 74 </body>