From ac8c8ba2b974ff6fbfca1fa1ac13b1535ac7fa2d Mon Sep 17 00:00:00 2001 From: LAN Xingcan Date: Mon, 11 Apr 2022 11:31:09 +0800 Subject: [PATCH 1/2] fix: avoid endless sleep loop when timeout --- src/utils/waitFor.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/waitFor.js b/src/utils/waitFor.js index b314baaf0..551b2d907 100644 --- a/src/utils/waitFor.js +++ b/src/utils/waitFor.js @@ -11,6 +11,10 @@ module.exports = ( const checkCondition = async (resolve, reject) => { totalWait += delay + if (fulfilled) { + return + } + await sleep(delay) try { @@ -38,6 +42,7 @@ module.exports = ( timeoutId = setTimeout(() => { if (!fulfilled) { + fulfilled = true return reject(new KafkaJSTimeout(timeoutMessage)) } }, maxWait) From 23f5f406ac02e8e92fb8b773c402e5674cb4a13d Mon Sep 17 00:00:00 2001 From: Tommy Brunn Date: Mon, 2 May 2022 14:41:39 +0200 Subject: [PATCH 2/2] Verify waitFor does not reschedule the check after timing out --- src/utils/waitFor.spec.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/utils/waitFor.spec.js b/src/utils/waitFor.spec.js index 03fdb7cc0..eebfe4360 100644 --- a/src/utils/waitFor.spec.js +++ b/src/utils/waitFor.spec.js @@ -1,3 +1,4 @@ +const sleep = require('./sleep') const waitFor = require('./waitFor') describe('Utils > waitFor', () => { @@ -23,10 +24,15 @@ describe('Utils > waitFor', () => { }) it('rejects the promise if the callback never succeeds', async () => { - await expect(waitFor(() => false, { delay: 1, maxWait: 2 })).rejects.toHaveProperty( + const condition = jest.fn().mockReturnValue(false) + await expect(waitFor(condition, { delay: 1, maxWait: 2 })).rejects.toHaveProperty( 'message', 'Timeout' ) + + // Verify that the check is not rescheduled after a timeout + await sleep(10) + expect(condition).toHaveBeenCalledTimes(2) }) it('rejects the promise with a custom timeout message', async () => {