worker.js (3756B)
1 const maxNestingLevel = 5; 2 let expectedNestingLevel = 1; 3 let timer; 4 let isInterval = false; 5 let testStage = "ScriptLoaded"; 6 let stopIncreaseExpectedLevel = false; 7 let startClampedTimeStamp = 0; 8 let startRepeatingClamped = false; 9 let repeatCount = 0; 10 let maxRepeatTimes = 10; 11 12 let timerCallback = async () => { 13 let now = Date.now(); 14 if (WorkerTestUtils.currentTimerNestingLevel() !== expectedNestingLevel) { 15 postMessage({ 16 stage: testStage, 17 status: "FAIL", 18 msg: `current timer nesting level is ${WorkerTestUtils.currentTimerNestingLevel()}, expected ${expectedNestingLevel}`, 19 }); 20 if (isInterval) { 21 clearInterval(timer); 22 } 23 return; 24 } 25 26 if (!stopIncreaseExpectedLevel) { 27 if (expectedNestingLevel === maxNestingLevel) { 28 stopIncreaseExpectedLevel = true; 29 startClampedTimeStamp = now; 30 } else { 31 expectedNestingLevel = expectedNestingLevel + 1; 32 } 33 if (!isInterval) { 34 setTimeout(timerCallback, 0); 35 } 36 return; 37 } 38 39 // This is the first time the timeout is clamped, checking if it is clamped 40 // to at least 2ms. 41 if (repeatCount === 0) { 42 await Promise.resolve(true).then(() => { 43 if (WorkerTestUtils.currentTimerNestingLevel() !== expectedNestingLevel) { 44 postMessage({ 45 stage: testStage, 46 status: "FAIL", 47 msg: `Timer nesting level should be in effect for immediately resolved micro-tasks`, 48 }); 49 } 50 }); 51 if (now - startClampedTimeStamp < 2 ) { 52 startRepeatingClamped = true; 53 } else { 54 postMessage({ stage: testStage, status: "PASS", msg: "" }); 55 } 56 } 57 58 // If the first clamped timeout is less than 2ms, start to repeat the clamped 59 // timeout for 10 times. Then checking if total clamped time should be at least 60 // 25ms. 61 if (startRepeatingClamped) { 62 if (repeatCount === 10) { 63 if (now - startClampedTimeStamp < 25) { 64 postMessage({ 65 stage: testStage, 66 status: "FAIL", 67 msg: `total clamped time of repeating ten times should be at least 25ms(${now - startClampedTimeStamp})`, 68 }); 69 } else { 70 postMessage({ stage: testStage, status: "PASS", msg: "" }); 71 } 72 } else { 73 repeatCount = repeatCount + 1; 74 if (!isInterval) { 75 setTimeout(timerCallback, 0); 76 } 77 return; 78 } 79 } 80 81 // reset testing variables 82 repeatCount = 0; 83 startRepeatingClamped = false; 84 stopIncreaseExpectedLevel = false; 85 if (isInterval) { 86 clearInterval(timer); 87 } 88 }; 89 90 onmessage = async e => { 91 testStage = e.data; 92 switch (e.data) { 93 case "CheckInitialValue": 94 if (WorkerTestUtils.currentTimerNestingLevel() === 0) { 95 postMessage({ stage: testStage, status: "PASS", msg: "" }); 96 } else { 97 postMessage({ 98 stage: testStage, 99 status: "FAIL", 100 msg: `current timer nesting level should be 0(${WorkerTestUtils.currentTimerNestingLevel()}) after top level script loaded.`, 101 }); 102 } 103 break; 104 case "TestSetInterval": 105 expectedNestingLevel = 1; 106 isInterval = true; 107 timer = setInterval(timerCallback, 0); 108 break; 109 case "TestSetTimeout": 110 expectedNestingLevel = 1; 111 isInterval = false; 112 setTimeout(timerCallback, 0); 113 break; 114 case "CheckNoTimer": 115 if (WorkerTestUtils.currentTimerNestingLevel() === 0) { 116 postMessage({ stage: testStage, status: "PASS", msg: "" }); 117 } else { 118 postMessage({ 119 stage: testStage, 120 status: "FAIL", 121 msg: `current timer nesting level should be 0(${WorkerTestUtils.currentTimerNestingLevel()}) when there is no timer in queue.`, 122 }); 123 } 124 125 break; 126 } 127 }; 128 129 postMessage({ stage: testStage, status: "PASS" });