fix: make sure monotonicTime() value is reasonable (#24518)

https://github.com/microsoft/playwright/issues/24432
This commit is contained in:
Andrey Lushnikov 2023-07-31 09:42:08 -07:00 committed by GitHub
parent d92fe16b76
commit f135b5f7a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,19 @@
* limitations under the License.
*/
// The `process.hrtime()` returns a time from some arbitrary
// date in the past; on certain systems, this is the time from the system boot.
// The `monotonicTime()` converts this to milliseconds.
//
// For a Linux server with uptime of 36 days, the `monotonicTime()` value
// will be 36 * 86400 * 1000 = 3_110_400_000, which is larger than
// the maximum value that `setTimeout` accepts as an argument: 2_147_483_647.
//
// To make the `monotonicTime()` a reasonable value, we anchor
// it to the time of the first import of this utility.
const initialTime = process.hrtime();
export function monotonicTime(): number {
const [seconds, nanoseconds] = process.hrtime();
const [seconds, nanoseconds] = process.hrtime(initialTime);
return seconds * 1000 + (nanoseconds / 1000 | 0) / 1000;
}