1/** 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16class Test { 17 constructor(numCheckpoints: int) { 18 this.numCheckpoints = numCheckpoints; 19 this.sequence = new Array<number>(); 20 } 21 22 check(): boolean { 23 if (this.result == Test.RESULT_FAILED) { 24 return false; 25 } 26 if (this.sequence.length != this.numCheckpoints) { 27 console.log("Test failed. Expected " + this.numCheckpoints + " checkpoints, but got " + this.sequence.length); 28 this.result = Test.RESULT_FAILED; 29 return false; 30 } 31 for (let i = 0; i < this.sequence.length; ++i) { 32 if (this.sequence[i] != i) { 33 console.log("Test failed. Expected " + i + "-th checkpoint to be " + i + ", but got " + this.sequence[i]); 34 this.result = Test.RESULT_FAILED; 35 return false; 36 } 37 } 38 return true; 39 } 40 41 fail(message: string): void { 42 this.result = Test.RESULT_FAILED; 43 console.log(message); 44 } 45 46 checkpoint(value: int) { 47 this.sequence.push(value); 48 } 49 50 private static RESULT_UNSET: int = 0; 51 private static RESULT_PASSED: int = 1; 52 private static RESULT_FAILED: int = 2; 53 54 result: int = Test.RESULT_UNSET; 55 failMessage: string = ""; 56 private sequence: Array<number>; 57 private numCheckpoints: int; 58} 59 60function check(): boolean { 61 return globalTest!.check(); 62} 63 64let globalTest: Test | null = null; 65 66function testSetTimeout(): void { 67 globalTest = new Test(3); 68 let sequence = new Array<number>(); 69 let delay = 100; 70 globalTest!.checkpoint(0); 71 let start = Date.now(); 72 setTimeout((): void => { 73 // To get time Date.now and libuv uses clock_gettime under the hood, but with different parameters. Date.now() uses CLOCK_REALTIME, but libuv uses CLOCK_MONOTONIC. 74 // When we calculate time interval there may be an error. For example time interval measured by Date.now may be 5.9ms round to 5 75 // and time interval measured by libuv is 6.1 round to 6. 76 // To avoid such error just add +1 to time interval. 77 let spentTime = Date.now() - start + 1; 78 if (spentTime < delay) { 79 globalTest!.fail("The callback is called after " + spentTime + "ms. Expected to be called after " + delay + "ms at least."); 80 } 81 globalTest!.checkpoint(2); 82 }, delay); 83 globalTest!.checkpoint(1); 84} 85 86function testClearTimeout(): void { 87 globalTest = new Test(2); 88 let sequence = new Array<number>(); 89 globalTest!.checkpoint(0); 90 let timerId = setTimeout((): void => { 91 globalTest!.fail("The callback should not be called."); 92 }, 0); 93 clearTimeout(timerId); 94 globalTest!.checkpoint(1); 95} 96 97function testSetInterval(): void { 98 globalTest = new Test(6); 99 let sequence = new Array<number>(); 100 let delay = 100; 101 let checkpoint = 2; 102 globalTest!.checkpoint(0); 103 let start = Date.now(); 104 let timerId: int; 105 timerId = setInterval((): void => { 106 // To get time Date.now and libuv uses clock_gettime under the hood, but with different parameters. Date.now() uses CLOCK_REALTIME, but libuv uses CLOCK_MONOTONIC. 107 // When we calculate time interval there may be an error. For example time interval measured by Date.now may be 5.9ms round to 5 108 // and time interval measured by libuv is 6.1 round to 6. 109 // To avoid such error just add +1 to time interval. 110 let spentTime = Date.now() - start + 1; 111 if (spentTime < delay) { 112 globalTest!.fail("The callback is called after " + spentTime + "ms. Expected to be called after " + delay + "ms at least."); 113 } 114 if (checkpoint == 5) { 115 clearInterval(timerId); 116 } 117 globalTest!.checkpoint(checkpoint); 118 ++checkpoint; 119 }, delay); 120 globalTest!.checkpoint(1); 121} 122