mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
fix: make sure Bun.sleep(Date) doesn't resolve prematurely (#8950)
* fix: make sure Bun.sleep(Date) doesn't return prematurely Fixes #8834. This makes Bun.sleep(new Date(x)) fulfill its promise only when Date.now() >= x. * resolve test now #8834 is fixed 11 ms is in fact the right limit. --------- Co-authored-by: John-David Dalton <john.david.dalton@gmail.com>
This commit is contained in:
@@ -352,8 +352,8 @@ JSC_DEFINE_HOST_FUNCTION(functionBunSleep,
|
||||
|
||||
if (millisecondsValue.inherits<JSC::DateInstance>()) {
|
||||
auto now = MonotonicTime::now();
|
||||
auto milliseconds = jsCast<JSC::DateInstance*>(millisecondsValue)->internalNumber() - now.approximateWallTime().secondsSinceEpoch().milliseconds();
|
||||
millisecondsValue = JSC::jsNumber(milliseconds > 0 ? milliseconds : 0);
|
||||
double milliseconds = jsCast<JSC::DateInstance*>(millisecondsValue)->internalNumber() - now.approximateWallTime().secondsSinceEpoch().milliseconds();
|
||||
millisecondsValue = JSC::jsNumber(milliseconds > 0 ? std::ceil(milliseconds) : 0);
|
||||
}
|
||||
|
||||
if (!millisecondsValue.isNumber()) {
|
||||
|
||||
@@ -152,9 +152,15 @@ it("Bun.sleep works with a Date object", async () => {
|
||||
var ten_ms = new Date();
|
||||
ten_ms.setMilliseconds(ten_ms.getMilliseconds() + 12);
|
||||
await Bun.sleep(ten_ms);
|
||||
// TODO: Fix https://github.com/oven-sh/bun/issues/8834
|
||||
// This should be .toBeGreaterThan(11), or maybe even 12
|
||||
expect(performance.now() - now).toBeGreaterThan(10);
|
||||
expect(performance.now() - now).toBeGreaterThan(11);
|
||||
});
|
||||
|
||||
it("Bun.sleep(Date) fulfills after Date", async () => {
|
||||
let ten_ms = new Date();
|
||||
ten_ms.setMilliseconds(ten_ms.getMilliseconds() + 12);
|
||||
await Bun.sleep(ten_ms);
|
||||
let now = new Date();
|
||||
expect(+now).toBeGreaterThanOrEqual(+ten_ms);
|
||||
});
|
||||
|
||||
it("node.js timers/promises setTimeout propagates exceptions", async () => {
|
||||
|
||||
Reference in New Issue
Block a user