Permalink
Browse files

add performance check (maxDuration)

  • Loading branch information...
1 parent 78b7552 commit 4249ad429f0e8591865750802c1eddcf3250c972 @hgoebl hgoebl committed May 1, 2012
Showing with 78 additions and 13 deletions.
  1. +3 −1 .gitignore
  2. +33 −12 lib/ping.js
  3. +42 −0 test/pin.test.js
View
@@ -10,4 +10,6 @@ node_modules/
*.swp
*.vi
*~
-.DS_Store
+.DS_Store
+.idea
+*.iml
View
@@ -38,8 +38,13 @@ function Ping(url, driver) {
return ~self.successCodes.indexOf(res.statusCode + '');
};
- this.register(errorCheck, statusCheck, textCheck);
-};
+ var perfCheck = function(err, res, body, info) {
+ if (typeof self.maxDuration !== 'number') return true;
+ return info.duration <= self.maxDuration;
+ };
+
+ this.register(errorCheck, statusCheck, textCheck, perfCheck);
+}
/**
* Ping inhertis from EventEmitter.
@@ -49,7 +54,7 @@ Ping.prototype.__proto__ = EventEmitter.prototype;
/**
* Set ping interval.
*
- * @param {Number} ms
+ * @param {Number} interval
* @returns `this`
* @api public
*/
@@ -79,17 +84,18 @@ Ping.prototype.register = function() {
/**
* Check if request is "valid" or not.
*
- * @param {Object} error
- * @param {Object} response
+ * @param {Object} err
+ * @param {Object} res
* @param {String} body
+ * @param {Object} info additional info about the ping, e.g. duration
* @returns {Boolean}
* @api private
*/
-Ping.prototype.isValid = function(err, res, body) {
+Ping.prototype.isValid = function(err, res, body, info) {
var ret = true;
this.validators.forEach(function(validator) {
- if (!validator(err, res, body)) ret = false;
+ if (!validator(err, res, body, info)) ret = false;
});
return ret;
@@ -108,9 +114,21 @@ Ping.prototype.text = function(text) {
};
/**
+ * Set max duration in milliseconds.
+ *
+ * @param {Number} maxDuration
+ * @returns {Object} `this`
+ * @api public
+ */
+Ping.prototype.maxDuration = function(maxDuration) {
+ this.maxDuration = maxDuration;
+ return this;
+};
+
+/**
* Register up callback.
*
- * @param {Function} callback
+ * @param {Function} fn callback
* @returns `this`
* @api public
*/
@@ -124,7 +142,7 @@ Ping.prototype.up = function(fn) {
/**
* Register down callback.
*
- * @param {Function} callback
+ * @param {Function} fn callback
* @returns `this`
* @api public
*/
@@ -157,11 +175,14 @@ Ping.prototype.monitor = function() {
* @api private
*/
Ping.prototype.check = function() {
- var self = this;
+ var self = this,
+ start = Date.now();
this.driver.get(this.url, function(err, res, body) {
- var success = self.isValid(err, res, body);
- success ? self.emit('up', res) : self.emit('down', err, res);
+ var duration = Date.now() - start,
+ info = {duration: duration},
+ success = self.isValid(err, res, body, info);
+ success ? self.emit('up', res, info) : self.emit('down', err, res, info);
});
};
View
@@ -134,5 +134,47 @@ describe('pin', function() {
});
});
});
+
+ describe('when maxDuration to check is present', function () {
+ it('emits up if duration is less than max', function (done) {
+ var driver = fakeDriver(function (url, fn) {
+ fn(null, {statusCode: 200});
+ });
+
+ var end = false;
+
+ pin('http://google.com/', driver)
+ .interval(10)
+ .maxDuration(1000)
+ .down(function () {
+ throw Error('down should not be called');
+ })
+ .up(function (res, info) {
+ info.duration.should.be.below(1000);
+ if (!end) (end = true) && done();
+ });
+ });
+
+ it('emits down if duration is more than max', function (done) {
+ var driver = fakeDriver(function (url, fn) {
+ setTimeout(function () {
+ fn(null, {statusCode: 200});
+ }, 20);
+ });
+
+ var end = false;
+
+ pin('http://google.com/', driver)
+ .interval(10)
+ .maxDuration(10)
+ .up(function () {
+ throw Error('up should not be called');
+ })
+ .down(function (err, res, info) {
+ info.duration.should.be.above(10);
+ if (!end) (end = true) && done();
+ });
+ });
+ });
});
});

0 comments on commit 4249ad4

Please sign in to comment.