From f6270f869d1d472c276f3e153d491f964ba6a4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brandon=20der=20Bl=C3=A4tter?= Date: Wed, 11 Dec 2024 18:00:21 -0800 Subject: [PATCH] fix: correct waitForCompletion behavior (#924) ## Description fixes #923 ## Related Issue #923 ## Motivation and Context it makes the new feature actually work ## How Has This Been Tested? modified test case added by #894 ## Screenshots (if appropriate): ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: - [x] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [x] I have added tests to cover my changes. - [x] All new and existing tests passed. - [ ] If my change introduces a breaking change, I have added a `!` after the type/scope in the title (see the Conventional Commits standard). --- src/job.ts | 3 ++- tests/cron.test.ts | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/job.ts b/src/job.ts index 59555ca0..f051d1ac 100644 --- a/src/job.ts +++ b/src/job.ts @@ -207,7 +207,8 @@ export class CronJob { } async fireOnTick() { - if (!this.waitForCompletion && this._isCallbackRunning) return; + // skip job if previous callback is still running + if (this.waitForCompletion && this._isCallbackRunning) return; this._isCallbackRunning = true; diff --git a/tests/cron.test.ts b/tests/cron.test.ts index c11e8f69..6202521a 100644 --- a/tests/cron.test.ts +++ b/tests/cron.test.ts @@ -1231,10 +1231,12 @@ describe('cron', () => { it('should track multiple running jobs correctly', async () => { const clock = sinon.useFakeTimers(); const executionOrder: number[] = []; + let started = 0; const job = new CronJob( '* * * * * *', async () => { + started++; await new Promise(resolve => { setTimeout(() => { callback(); @@ -1256,7 +1258,8 @@ describe('cron', () => { await clock.tickAsync(3500); - expect(executionOrder).toEqual([1, 2]); + expect(started).toBe(2); + expect(executionOrder).toEqual([1]); job.stop(); });