Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
aleafs committed Dec 4, 2013
1 parent 19020c1 commit eeda9e1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@ lib-cov
*.out
*.pid
*.gz
*.swp

pids
logs
Expand Down
15 changes: 2 additions & 13 deletions lib/httpha.js
Expand Up @@ -31,32 +31,20 @@ exports.create = function (options, helper) {
_options.interval.sort(function (x, y) {
return x - y;
});

if (!_options.interval[0] || _options.interval[0] < 1) {
_options.interval[0] = 5000;
}
if (!_options.interval[1]) {
_options.interval[1] = 16 * _options.interval[0];
}

/**
* @ 可选列表
*/
var _backup = [];

/**
* @ 在线列表
*/
var _online = [];

/**
* @ 请求计数
*/
var _reqnum = -1;

/**
* @ 心跳间隔
*/
/* {{{ function _modifyInterval() */
var _interval = _options.interval[0];
var _modifyInterval = function (method) {
if ('+' === method && _interval < _options.interval[1]) {
Expand All @@ -65,6 +53,7 @@ exports.create = function (options, helper) {
_interval = Math.max(_options.interval[0], parseInt(_interval / 2, 10));
}
};
/* }}} */

/* {{{ function heartbeat() */
var heartbeat = null;
Expand Down
37 changes: 34 additions & 3 deletions test/httpha.test.js
Expand Up @@ -86,11 +86,42 @@ describe('http status checker', function () {

it('should_http_status_checker_works_fine', function (done) {

var _fn = httpha.httpStatusChecker('status', 20);
_fn({'hostname' : '127.0.0.1', 'port' : P}, function (err, yes) {
/* {{{ function wait() */
var all = {};
var wait = function (idx, cb) {
all[idx] = true;

var _fn = httpha.httpStatusChecker(idx, 20);
_fn({'hostname' : '127.0.0.1', 'port' : P}, function (err, yes) {
cb(err, yes);
all[idx] = null;

process.nextTick(function () {
for (var i in all) {
if (all[i]) {
return;
}
}
done();
});
});
};
/* }}} */

wait('status', function (err, yes) {
should.ok(!err);
yes.should.eql(true);
done();
});

wait('/timeout', function (err, yes) {
err.should.have.property('name', 'RequestTimeout');
err.message.should.eql('HeartBeat request "/timeout" timeout after 20ms');
should.ok(!yes);
});

wait('/404', function (err, yes) {
should.ok(!err);
yes.should.eql(false);
});
});
});
Expand Down

0 comments on commit eeda9e1

Please sign in to comment.