Skip to content

Commit

Permalink
Merge pull request #4 from koajs/timeout
Browse files Browse the repository at this point in the history
feat: add an option to set timeout when create a service
  • Loading branch information
fengmk2 committed Jun 9, 2015
2 parents 2452ab2 + 2d6844e commit 34b94ff
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
var debug = require('debug')('koa-ready');
var ready = require('ready');

module.exports = function(app) {
module.exports = function(app, opt) {
if (!app) return;

opt || (opt = {});

// unique ready id for app
app._ready_hash_id = app._ready_hash_id || Date.now();
app._ready_timeout = app._ready_timeout || opt.timeout || 10000;

// inject async method
app.async = async;
Expand All @@ -27,18 +30,24 @@ module.exports = function(app) {
/*
Create a async task
id: unique id for one task
isWeakDep: whether it's a weak dependency, default: false
- id: unique id for one task
- opt
- isWeakDep: whether it's a weak dependency, default: false
- timeout: emit `ready_timeout` when it's over timeout, default: 10000ms
*/

function async(id, isWeakDep) {
function async(id, opt) {
opt || (opt = {});

/* jshint validthis:true */
var self = this;

if (!id) {
throw new Error('Should specify id');
}
isWeakDep = isWeakDep === true;

var isWeakDep = opt.isWeakDep === true;
var timeout = opt.timeout || self._ready_timeout;

var cache = self._readyCache = self._readyCache || [];
var hashId = self._ready_hash_id;
Expand All @@ -50,14 +59,11 @@ function async(id, isWeakDep) {
debug('[%s] Register task id `%s`, isWeakDep %s', hashId, id, isWeakDep);
cache.push(id);

var timer = setTimeout(function () {
console.log('[APP-READY] 10 seconds later %s was still unable to finish.', id);
}, 10000);
var timer = setTimeout(this.emit.bind(this, 'ready_timeout', id), timeout);

return once(function(err) {
if (timer) {
clearTimeout(timer);
}
clearTimeout(timer);

if (self._readyError === true) return;
// fire callback after all register
setImmediate(function() {
Expand Down
54 changes: 51 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('koa-ready', function() {
var spyError = spy();
var spyReady = spy();

var endA = app.async('a', true);
var endA = app.async('a', {isWeakDep: true});
endA(new Error('error'));

var endB = app.async('b');
Expand Down Expand Up @@ -162,29 +162,39 @@ describe('koa-ready', function() {

it('should emit ready_stat when every task end', function(done) {
var data = [];
var timeout = [];
app.on('ready_stat', function(e) {
data.push(e);
});
app.on('ready_timeout', function(id) {
timeout.push(id);
});
var endA = app.async('a');
var endB = app.async('b');
var endC = app.async('c');
var endD = app.async('d');
var endE = app.async('e');
var endF = app.async('f', {timeout: 10});
setTimeout(endA, 1);
setTimeout(endB, 80);
setTimeout(endC, 10);
setTimeout(endD, 50);
setTimeout(endE, 11000);
setTimeout(endF, 50);

setTimeout(function() {
timeout.should.eql(['f', 'e']);
data.should.eql([{
id: 'a',
remain: ['b', 'c', 'd', 'e']
remain: ['b', 'c', 'd', 'e', 'f']
}, {
id: 'c',
remain: ['b', 'd', 'e']
remain: ['b', 'd', 'e', 'f']
}, {
id: 'd',
remain: ['b', 'e', 'f']
}, {
id: 'f',
remain: ['b', 'e']
}, {
id: 'b',
Expand All @@ -196,4 +206,42 @@ describe('koa-ready', function() {
done();
}, 11200);
});

it('should clearTimeout', function(done) {
var spyTimeout = spy();

app.on('ready_timeout', spyTimeout);

var endA = app.async('a', {timeout: 50});
var endB = app.async('b', {timeout: 50});
setTimeout(endA, 10);
setTimeout(endB, 10);

setTimeout(function() {
spyTimeout.called.should.be.false;
done();
}, 100);
});

it('should set timeout when init', function(done) {
var timeout = [];
var app = koa();
ready(app, {timeout: 10});

app.on('ready_timeout', function(id) {
timeout.push(id);
});

var endA = app.async('a');
var endB = app.async('b');
var endC = app.async('c');
setTimeout(endA, 100);
setTimeout(endB, 100);
setTimeout(endC, 5);

setTimeout(function() {
timeout.should.eql(['a', 'b']);
done();
}, 150);
});
});

0 comments on commit 34b94ff

Please sign in to comment.