Skip to content

Commit

Permalink
fix: use safe-timers only large than interval && add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Jun 5, 2017
1 parent da212ac commit 5b8e487
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
5 changes: 2 additions & 3 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const loadSchedule = require('./lib/load_schedule');
const parser = require('cron-parser');
const ms = require('humanize-ms');
const safetimers = require('safe-timers');
const MAX_SAFE_TIME = Math.pow(2, 31) - 1;
const SCHEDULE_HANDLER = Symbol.for('egg#scheduleHandler');

module.exports = agent => {
Expand Down Expand Up @@ -106,15 +105,15 @@ function startCron(interval, listener) {
}

function safeTimeout(fn, delay, ...args) {
if (delay >= MAX_SAFE_TIME) {
if (delay < safetimers.maxInterval) {
setTimeout(fn, delay, ...args);
} else {
safetimers.setTimeout(fn, delay, ...args);
}
}

function safeInterval(fn, delay, ...args) {
if (delay >= MAX_SAFE_TIME) {
if (delay < safetimers.maxInterval) {
setInterval(fn, delay, ...args);
} else {
safetimers.setInterval(fn, delay, ...args);
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/safe-timers/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const safetimers = require('safe-timers');

module.exports = agent => {
safetimers.maxInterval = 4000;

const proto = safetimers.Timeout.prototype;
const originFn = proto.reschedule;

proto.reschedule = function(...args) {
agent.logger.info('reschedule', ...args);
originFn.call(this, ...args);
};
}
10 changes: 10 additions & 0 deletions test/fixtures/safe-timers/app/schedule/interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

exports.schedule = {
type: 'worker',
interval: 4321,
};

exports.task = function* (ctx) {
ctx.logger.info('interval');
};
10 changes: 10 additions & 0 deletions test/fixtures/safe-timers/app/schedule/sub/cron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

exports.schedule = {
type: 'worker',
cron: '*/5 * * * * *',
};

exports.task = function* (ctx) {
ctx.logger.info('cron');
};
3 changes: 3 additions & 0 deletions test/fixtures/safe-timers/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.logrotator = false;
3 changes: 3 additions & 0 deletions test/fixtures/safe-timers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "safe-timers"
}
30 changes: 26 additions & 4 deletions test/schedule.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test/schedule.test.js', () => {

describe('schedule type worker', () => {
it('should support interval and cron', function* () {
app = mm.cluster({ baseDir: 'worker', workers: 2 });
app = mm.cluster({ baseDir: 'worker', workers: 2, cache: false });
yield app.ready();
yield sleep(5000);
const log = getLogContent('worker');
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('test/schedule.test.js', () => {
it('should support immediate', function* () {
app = mm.cluster({ baseDir: 'immediate', workers: 2 });
yield app.ready();
yield sleep(5000);
yield sleep(6000);
const log = getLogContent('immediate');
console.log(log);
assert(contains(log, 'immediate-interval') === 2);
Expand Down Expand Up @@ -203,14 +203,31 @@ describe('test/schedule.test.js', () => {
});
});


describe('export schedules', () => {
it('should export app.schedules', function* () {
app = mm.app({ baseDir: 'worker' });
app = mm.app({ baseDir: 'worker', cache: false });
yield app.ready();
assert(app.schedules);
});
});

describe('safe-timers', () => {
it('should support interval and cron', function* () {
app = mm.cluster({ baseDir: 'safe-timers', workers: 2, cache: false });
yield app.ready();
yield sleep(5000);

const log = getLogContent('safe-timers');
console.log(log);
assert(contains(log, 'interval') === 1);
assert(contains(log, 'cron') === 1);

const agentLog = getAgentLogContent('safe-timers');
console.log(agentLog);
assert(contains(agentLog, 'reschedule 4321') === 2);
assert(contains(agentLog, 'reschedule') === 4);
});
});
});

function sleep(time) {
Expand All @@ -229,6 +246,11 @@ function getErrorLogContent(name) {
return fs.readFileSync(logPath, 'utf8');
}

function getAgentLogContent(name) {
const logPath = path.join(__dirname, 'fixtures', name, 'logs', name, 'egg-agent.log');
return fs.readFileSync(logPath, 'utf8');
}

function contains(content, match) {
return content.split('\n').filter(line => line.indexOf(match) >= 0).length;
}

0 comments on commit 5b8e487

Please sign in to comment.