-
Notifications
You must be signed in to change notification settings - Fork 645
fix: Prevent double scheduling events and ensure long-running events work correctly #3561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,19 +275,14 @@ describe('CronjobController', () => { | |
| }, | ||
| ); | ||
|
|
||
| const secondCronjobController = new CronjobController({ | ||
| messenger: controllerMessenger, | ||
| state: cronjobController.state, | ||
| stateManager: getMockStateManager(), | ||
| }); | ||
| expect(handleRequest).toHaveBeenCalledTimes(1); | ||
|
|
||
| secondCronjobController.init(); | ||
| jest.advanceTimersByTime(inMilliseconds(26, Duration.Hour)); | ||
|
|
||
| await new Promise((resolve) => originalProcessNextTick(resolve)); | ||
| expect(handleRequest).toHaveBeenCalledTimes(1); | ||
| expect(handleRequest).toHaveBeenCalledTimes(2); | ||
|
|
||
| cronjobController.destroy(); | ||
| secondCronjobController.destroy(); | ||
| }); | ||
|
|
||
| it('does not schedule cronjob that is too far in the future', () => { | ||
|
|
@@ -473,55 +468,6 @@ describe('CronjobController', () => { | |
| cronjobController.destroy(); | ||
| }); | ||
|
|
||
| it('handles scheduled event close to current time gracefully', () => { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find a good way to test this anymore since I eliminated the state update I relied on to replicate the race condition |
||
| const rootMessenger = getRootCronjobControllerMessenger(); | ||
| const controllerMessenger = | ||
| getRestrictedCronjobControllerMessenger(rootMessenger); | ||
|
|
||
| const cronjobController = new CronjobController({ | ||
| messenger: controllerMessenger, | ||
| stateManager: getMockStateManager(), | ||
| state: { | ||
| events: { | ||
| foo: { | ||
| id: 'foo', | ||
| recurring: false, | ||
| date: '2022-01-01T00:00:01.000Z', | ||
| schedule: '2022-01-01T00:00:01.000Z', | ||
| scheduledAt: new Date().toISOString(), | ||
| snapId: MOCK_SNAP_ID, | ||
| request: { | ||
| method: 'handleEvent', | ||
| params: ['p1'], | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| rootMessenger.subscribe('CronjobController:stateChange', () => { | ||
| jest.advanceTimersByTime(inMilliseconds(2, Duration.Second)); | ||
| }); | ||
|
|
||
| cronjobController.init(); | ||
|
|
||
| expect(rootMessenger.call).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'SnapController:handleRequest', | ||
| { | ||
| snapId: MOCK_SNAP_ID, | ||
| origin: METAMASK_ORIGIN, | ||
| handler: HandlerType.OnCronjob, | ||
| request: { | ||
| method: 'handleEvent', | ||
| params: ['p1'], | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| cronjobController.destroy(); | ||
| }); | ||
|
|
||
| it('handles the `snapInstalled` event', () => { | ||
| const rootMessenger = getRootCronjobControllerMessenger(); | ||
| const controllerMessenger = | ||
|
|
@@ -663,7 +609,7 @@ describe('CronjobController', () => { | |
| foo: { | ||
| id: 'foo', | ||
| recurring: false, | ||
| date: '2022-01-01T01:00:00Z', | ||
| date: '2022-01-01T01:00Z', | ||
| schedule: '2022-01-01T01:00Z', | ||
| scheduledAt: new Date().toISOString(), | ||
| snapId: MOCK_SNAP_ID, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -621,6 +621,13 @@ export class CronjobController extends BaseController< | |
| // immediately. | ||
| if (event.recurring && eventDate <= now) { | ||
| this.#execute(event); | ||
| continue; | ||
| } | ||
|
|
||
| // If the existing event has not passed, start the timer. | ||
| if (eventDate >= now) { | ||
| this.#startTimer(event); | ||
|
||
| continue; | ||
| } | ||
|
|
||
| this.#schedule(event); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test did not seem to do what the title says, I re-used it to ensure that both of the fixes in this PR work