Skip to content
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

PR to view bug replay #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { CronJob } = require('./lib/cron');

const job = new CronJob(
'*/5 * * * * *',
() => {
console.log('current time is', new Date().toISOString());
},
() => {
console.log('\x1b[41m%s\x1b[0m', 'job was stopped');
process.exit(1);
},
true,
'Europe/Moscow'
);

job.start();
34 changes: 33 additions & 1 deletion lib/time.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
const sinon = require('sinon');

const clock = sinon.useFakeTimers(Date.now());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used sinon to simulate the time change on the machine at the right moment


function scheduleTick() {
process.nextTick(() => {
clock.tick(1);
// clock.tick(0.001);
scheduleTick();
});
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setInterval not working when fake timers exists


scheduleTick();

let SEND_AT_CALLS = 0;

const CONSTRAINTS = [
[0, 59],
[0, 59],
Expand Down Expand Up @@ -147,6 +163,20 @@ function CronTime(luxon) {
*/
sendAt: function (i) {
var date = this.realDate ? this.source : luxon.DateTime.local();

if (SEND_AT_CALLS === 5) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The step is chosen absolutely randomly :)

console.log('\x1b[40m\x1b[31m%s\x1b[0m', 'add 7s to current time');
clock.tick(7000);
}

if (SEND_AT_CALLS === 7) {
console.log('\x1b[42m%s\x1b[0m', 'failed to repeat the bug');
process.exit(0);
}


SEND_AT_CALLS++;

if (this.zone) {
date = date.setZone(this.zone);
}
Expand Down Expand Up @@ -199,7 +229,9 @@ function CronTime(luxon) {
* Get the number of milliseconds in the future at which to fire our callbacks.
*/
getTimeout: function () {
return Math.max(-1, this.sendAt() - luxon.DateTime.local());
const timeout = Math.max(-1, this.sendAt() - luxon.DateTime.local());
console.log('timeout is', timeout);
return timeout;
},

/**
Expand Down