From 8554dca6c74182ab753cb8f71347f2c97a869fb6 Mon Sep 17 00:00:00 2001 From: James Crosby Date: Wed, 10 Apr 2024 17:56:31 +0100 Subject: [PATCH] fix race condition in cookie.maxAge test (#242) --- test/cookie.test.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/test/cookie.test.js b/test/cookie.test.js index 42f4729..6f22d6e 100644 --- a/test/cookie.test.js +++ b/test/cookie.test.js @@ -568,19 +568,31 @@ test('Cookie', t => { t.equal('maxAge' in json, false) }) + // the clock ticks while we're testing, so to prevent occasional test + // failures where these tests tick over 1ms, take the time difference into + // account: + t.test('maxAge calculated from expires', t => { t.plan(2) + const startT = Date.now() const cookie = new Cookie({ expires: new Date(Date.now() + 1000) }) - t.equal(cookie.maxAge <= 1000, true) + const maxAge = cookie.maxAge + const duration = Date.now() - startT + + t.equal(maxAge <= 1000 && maxAge >= 1000 - duration, true) t.equal(cookie.originalMaxAge, null) }) t.test('maxAge set by maxAge', t => { t.plan(2) + const startT = Date.now() const cookie = new Cookie({ maxAge: 1000 }) - t.equal(cookie.maxAge, 1000) + const maxAge = cookie.maxAge + const duration = Date.now() - startT + + t.equal(maxAge <= 1000 && maxAge >= 1000 - duration, true) t.equal(cookie.originalMaxAge, 1000) }) })