Skip to content

Commit

Permalink
less flaky?
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Aug 25, 2024
1 parent ad339a2 commit 33fe101
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions test/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,6 @@ describe('timers', () => {
timers.clearTimeout(timer)
})

test('a FastTimer will only increment by the defined TICK_MS value', async (t) => {
t = tspl(t, { plan: 2 })

const startInternalClock = timers.now()

// The long running FastTimer will ensure that the internal clock is
// incremented by the TICK_MS value in the onTick function
const longRunningFastTimer = timers.setTimeout(() => {}, 1e10)

eventLoopBlocker(1000)

// wait to ensure the timer has fired in the next loop
await new Promise((resolve) => setTimeout(resolve, 1))

t.strictEqual(timers.now() - startInternalClock, 499)
await new Promise((resolve) => setTimeout(resolve, 1000))
t.strictEqual(timers.now() - startInternalClock, 1497)

timers.clearTimeout(longRunningFastTimer)
})

const getDelta = (start, target) => {
const end = process.hrtime.bigint()
const actual = (end - start) / 1_000_000n
Expand Down Expand Up @@ -195,4 +174,25 @@ describe('timers', () => {
setTimeout(() => t.ok(true), 3000)
await t.completed
})

test('a FastTimer will only increment by the defined TICK_MS value', async (t) => {
t = tspl(t, { plan: 2 })

const startInternalClock = timers.now()

// The long running FastTimer will ensure that the internal clock is
// incremented by the TICK_MS value in the onTick function
const longRunningFastTimer = timers.setTimeout(() => {}, 1e10)

eventLoopBlocker(1000)

// wait to ensure the timer has fired in the next loop
await new Promise((resolve) => setTimeout(resolve, 1))

t.strictEqual(timers.now() - startInternalClock, 499)
await new Promise((resolve) => setTimeout(resolve, 1000))
t.ok(timers.now() - startInternalClock <= 1497)

timers.clearTimeout(longRunningFastTimer)
})
})

0 comments on commit 33fe101

Please sign in to comment.