web-locks-worklet-batch-update.tentative.https.sub.html (3410B)
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 worklet1 = await sharedStorage.createWorklet('resources/simple-module.js'); 14 let worklet2 = await sharedStorage.createWorklet('resources/simple-module.js'); 15 16 const ancestor_key1 = token(); 17 let url1_0 = generateURL("/shared-storage/resources/frame0.html", 18 [ancestor_key1]); 19 let url1_1 = generateURL("/shared-storage/resources/frame1.html", 20 [ancestor_key1]); 21 22 const ancestor_key2 = token(); 23 let url2_0 = generateURL("/shared-storage/resources/frame0.html", 24 [ancestor_key2]); 25 let url2_1 = generateURL("/shared-storage/resources/frame1.html", 26 [ancestor_key2]); 27 28 // Invoke `selectURL()` to perform the following steps: 29 // 1. Acquires the lock. 30 // 2. Reads the current value at the given key. 31 // 3. Waits for 500ms. 32 // 4. Sets the shared storage value to the read value appended with the given letter. 33 // 5. Releases the lock. 34 // 35 // In parallel, invoke another `selectURL()`, which subsequently invokes 36 // `sharedStorage.batchUpdate()` that: 37 // - Acquires the same named lock. 38 // - Executes two `append` methods, each appending the same letter. 39 // 40 // Expected behavior: After both of them finish, the value at the given key 41 // should contain the letter repeated three times. 42 // 43 // This demonstrates that: 44 // 1. The `withLock` option is effective, preventing the `batchUpdate()` 45 // method interfering with the "get and set" operation. If the lock were not 46 // used, the final value would likely be a single letter. 47 // 2. `batchUpdate()` correctly executes all `append` methods within the 48 // batch. 49 // 50 // Note: This test remains valid even if the `batchUpdate()` call happens 51 // outside the critical section protected by the lock within the worklet. The 52 // test effectively demonstrates mutual exclusion as long as there's a 53 // reasonable chance for `batchUpdate()` to occur while the worklet is still 54 // running. 55 let select_url_result1 = await worklet1.selectURL( 56 "get-wait-set-within-lock", 57 [{url: url1_0}, {url: url1_1}], 58 {data: {'key': 'key', 59 'lock_name': 'lock1', 60 'append_letter': 'a'}, 61 resolveToConfig: true}); 62 63 let select_url_result2 = await worklet2.selectURL( 64 "batch-update-with-two-append-methods-with-batch-lock-option", 65 [{url: url2_0}, {url: url2_1}], 66 {data: {'key': 'key', 67 'lock_name': 'lock1', 68 'append_letter': 'a'}, 69 resolveToConfig: true}); 70 71 attachFencedFrame(select_url_result1, 'opaque-ads'); 72 const result1 = await nextValueFromServer(ancestor_key1); 73 assert_equals(result1, "frame1_loaded"); 74 75 attachFencedFrame(select_url_result2, 'opaque-ads'); 76 const result2 = await nextValueFromServer(ancestor_key2); 77 assert_equals(result2, "frame1_loaded"); 78 79 await verifyKeyValueForOrigin('key', 'aaa', location.origin); 80 81 await deleteKeyForOrigin('key', location.origin); 82 }, 'Test for batchUpdate() with a batch lock in a SharedStorageWorklet context'); 83 84 </script> 85 </body>