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

fix: use safe-timers only large than interval && add tests #21

Merged
merged 1 commit into from
Jun 6, 2017
Merged
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
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"
}
32 changes: 27 additions & 5 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 @@ -54,8 +54,8 @@ describe('test/schedule.test.js', () => {
yield sleep(5000);
const log = getLogContent('immediate');
console.log(log);
assert(contains(log, 'immediate-interval') === 2);
assert(contains(log, 'immediate-cron') === 2);
assert(contains(log, 'immediate-interval') >= 2);
assert(contains(log, 'immediate-cron') >= 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;
}