test_cache_tons_of_fd.html (3166B)
1 <!-- Any copyright is dedicated to the Public Domain. 2 - http://creativecommons.org/publicdomain/zero/1.0/ --> 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>Test cache to create tons of fds</title> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 9 <script type="text/javascript" src="driver.js"></script> 10 </head> 11 <body> 12 <script class="testbody" type="text/javascript"> 13 function setupTestIframe() { 14 return new Promise(function(resolve) { 15 var iframe = document.createElement("iframe"); 16 iframe.src = "empty.html"; 17 iframe.onload = function() { 18 window.caches = iframe.contentWindow.caches; 19 resolve(); 20 }; 21 document.body.appendChild(iframe); 22 }); 23 } 24 25 function clearStorage() { 26 return new Promise(function(resolve) { 27 var qms = SpecialPowers.Services.qms; 28 var principal = SpecialPowers.wrap(document).nodePrincipal; 29 var request = qms.clearStoragesForPrincipal(principal); 30 var cb = SpecialPowers.wrapCallback(resolve); 31 request.callback = cb; 32 }); 33 } 34 35 async function testCreateTonsOfFD() { 36 const number_of_fd = 5120; 37 const name = "cacheTonsOfFD"; 38 const url = "foo.com"; 39 const body = "This is a body"; 40 41 info("Stage A: Cached a Request/Response pairs"); 42 let cache = await caches.open(name); 43 let request = new Request(url); 44 let response = new Response(body); 45 await cache.put(request, response); 46 47 info("Stage B: Read the cached response mutliple times"); 48 let promise_array = []; 49 for (let i = 0; i < number_of_fd; ++i) { 50 let promise = cache.match(request); 51 promise_array.push(promise); 52 } 53 let cached_response_array = []; 54 try { 55 cached_response_array = await Promise.all(promise_array); 56 } catch (e) { 57 throw new Error("Fail to open tons of files with error: " + e); 58 } 59 60 if (cached_response_array.length != number_of_fd) { 61 throw new Error("Fail to cache.match the cached responses"); 62 } 63 64 info("Stage C: Consume the cached body"); 65 for (let i = 0; i < number_of_fd; ++i) { 66 if (!cached_response_array[i]) { 67 // Reduce the checking message. 68 throw new Error("The cached response doesn't exist"); 69 } 70 71 let bodyText = ""; 72 try { 73 bodyText = await cached_response_array[i].text(); 74 } catch (e) { 75 throw new Error("Fail to consume the cached response's body with error: " + e); 76 } 77 78 if (bodyText != body) { 79 // Reduce the checking message. 80 throw new Error("The cached body doeen't be the same as original one"); 81 } 82 } 83 84 ok(true, "Doesn't crash or timeout"); 85 return Promise.resolve(); 86 } 87 88 SimpleTest.waitForExplicitFinish(); 89 SpecialPowers.pushPrefEnv({ 90 "set": [["dom.caches.enabled", true], 91 ["dom.caches.testing.enabled", true], 92 ["dom.quotaManager.testing", true]], 93 }, async function() { 94 await setupTestIframe(); 95 96 info("Stage 1: Clean storage."); 97 await clearStorage(); 98 99 info("Stage 2: Verify open lots of files at the same time doesn't crash " + 100 "the browser"); 101 try { 102 await testCreateTonsOfFD(); 103 } catch (e) { 104 ok(false, e); 105 } 106 107 await SimpleTest.finish(); 108 }); 109 </script> 110 </body> 111 </html>