From 6af415eae45cf2b7faa24650f63e82212e7c239e Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Sun, 7 Aug 2011 20:54:05 +0100 Subject: use jQuery.deferred in the tests --- jarmon/jarmon.js | 19 +++---- jarmon/jarmon.test.js | 147 +++++++++++++++++++++++++++----------------------- 2 files changed, 89 insertions(+), 77 deletions(-) diff --git a/jarmon/jarmon.js b/jarmon/jarmon.js index 2efd7c5..7269e0e 100644 --- a/jarmon/jarmon.js +++ b/jarmon/jarmon.js @@ -362,9 +362,9 @@ jarmon.RrdQuery.prototype.getData = function(startTimeJs, endTimeJs, if (startTimeJs >= endTimeJs) { throw RangeError( - ['starttime must be less than endtime. ', - 'starttime: ', startTimeJs, - 'endtime: ', endTimeJs].join('')); + ['starttime must be less than endtime.', + 'starttime:', startTimeJs, + 'endtime:', endTimeJs].join(' ')); } var startTime = startTimeJs/1000; @@ -518,16 +518,17 @@ jarmon.RrdQueryRemote.prototype._callRemote = function(methodName, args) { if(res instanceof Error) { ret.reject(res); } else { - ret.resolve(res); + var rq = new jarmon.RrdQuery(res, self.unit); + try { + ret.resolve(rq[methodName].apply(rq, args)); + } catch(e) { + ret.reject(e); + } } return res; }); - return ret.pipe( - function(rrd) { - var rq = new jarmon.RrdQuery(rrd, self.unit); - return rq[methodName].apply(rq, args); - }); + return ret; }; diff --git a/jarmon/jarmon.test.js b/jarmon/jarmon.test.js index 5f832ec..90eb28a 100644 --- a/jarmon/jarmon.test.js +++ b/jarmon/jarmon.test.js @@ -13,14 +13,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * When url cannot be found, the deferred should errback with status * 404. **/ + var self = this; var d = new jarmon.downloadBinary('non-existent-file.html'); - d.addBoth( - function(self, ret) { + d.always( + function(ret) { self.resume(function() { Y.Assert.isInstanceOf(Error, ret); Y.Assert.areEqual(404, ret.message); }); - }, this); + }); this.wait(); }, @@ -30,15 +31,16 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * When url is found, the deferred should callback with an instance * of javascriptrrd.BinaryFile **/ + var self = this; var d = new jarmon.downloadBinary('testfile.bin'); - d.addBoth( - function(self, ret) { + d.always( + function(ret) { self.resume(function() { Y.Assert.isInstanceOf(jarmon.BinaryFile, ret); Y.Assert.areEqual( String.fromCharCode(0), ret.getRawData()); }); - }, this); + }); this.wait(); } @@ -57,12 +59,11 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.RRDFile", setUp: function() { - this.d = new jarmon.downloadBinary('build/test.rrd') - .addCallback( - function(self, binary) { + var self = this; + this.d = new jarmon.downloadBinary('build/test.rrd').pipe( + function(binary) { return new RRDFile(binary); - }, this) - .addErrback( + }, function(ret) { console.log(ret); }); @@ -73,13 +74,14 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * The generated rrd file should have a lastupdate date of * 1980-01-01 00:50:01 **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual( RRD_ENDTIME/1000+1, rrd.getLastUpdate()); }); - }, this); + }); this.wait(); }, @@ -89,8 +91,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * 'speed'. A RangeError is thrown if the requested index or dsName * doesnt exist. **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual(RRD_DSNAME, rrd.getDS(0).getName()); Y.Assert.areEqual( @@ -103,7 +106,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.assert(error instanceof RangeError); }); - }, this); + }); this.wait(); }, @@ -111,12 +114,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The generated rrd file should have a single RRA **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { Y.Assert.areEqual(RRD_RRACOUNT, rrd.getNrRRAs()); }); - }, this); + }); this.wait(); }, @@ -127,8 +131,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * rra.getEl throws a RangeError if asked for row which doesn't * exist. **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rra = rrd.getRRA(0); Y.Assert.areEqual('AVERAGE', rra.getCFName()); @@ -145,7 +150,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.assert(error instanceof RangeError); }); - }, this); + }); this.wait(); } })); @@ -154,12 +159,10 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { name: "jarmon.RrdQuery", setUp: function() { - this.d = new jarmon.downloadBinary('build/test.rrd') - .addCallback( - function(self, binary) { + this.d = new jarmon.downloadBinary('build/test.rrd').pipe( + function(binary) { return new RRDFile(binary); - }, this) - .addErrback( + }, function(ret) { console.log(ret); }); @@ -169,19 +172,21 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The starttime must be less than the endtime **/ - this.d.addCallback( - function(self, rrd) { - self.resume(function() { - var rq = new jarmon.RrdQuery(rrd, ''); - var error = null; - try { - rq.getData(1, 0); - } catch(e) { - error = e; - } - Y.Assert.isInstanceOf(RangeError, error); - }); - }, this); + var self = this; + this.d.done( + function(rrd) { + self.resume( + function() { + var rq = new jarmon.RrdQuery(rrd, ''); + var error; + try { + rq.getData(1, 0); + } catch(e) { + error = e; + } + Y.Assert.isInstanceOf(RangeError, error); + }); + }); this.wait(); }, @@ -191,8 +196,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Error is raised if the rrd file doesn't contain an RRA with the * requested consolidation function (CF) **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); var error = null; @@ -203,7 +209,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { } Y.Assert.isInstanceOf(TypeError, error); }); - }, this); + }); this.wait(); }, @@ -215,8 +221,9 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Result should include a data points with times > starttime and * <= endTime **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); @@ -255,7 +262,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { RRD_ENDTIME-RRD_STEP*1000*2, data.data[data.data.length-1][0]); }); - }, this); + }); this.wait(); }, @@ -264,14 +271,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * If the requested time range is outside the range of the RRD file * we should not get any values back **/ - this.d.addCallback( - function(self, rrd) { + var self = this; + this.d.done( + function(rrd) { self.resume(function() { var rq = new jarmon.RrdQuery(rrd, ''); var data = rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000); Y.Assert.areEqual(0, data.data.length); }); - }, this); + }); this.wait(); } @@ -289,12 +297,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { /** * The starttime must be less than the endtime **/ - this.rq.getData(1, 0).addBoth( - function(self, res) { + var self = this; + this.rq.getData(1, 0).fail( + function(res) { self.resume(function() { Y.Assert.isInstanceOf(RangeError, res); }); - }, this); + }); this.wait(); }, @@ -304,12 +313,13 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Error is raised if the rrd file doesn't contain an RRA with the * requested consolidation function (CF) **/ - this.rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO').addBoth( - function(self, res) { + var self = this; + this.rq.getData(RRD_STARTTIME, RRD_ENDTIME, 0, 'FOO').always( + function(res) { self.resume(function() { Y.Assert.isInstanceOf(TypeError, res); }); - }, this); + }); this.wait(); }, @@ -321,9 +331,10 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Result should include a data points with times > starttime and * <= endTime **/ + var self = this; this.rq.getData(RRD_STARTTIME + (RRD_STEP+1) * 1000, - RRD_ENDTIME - (RRD_STEP-1) * 1000).addBoth( - function(self, data) { + RRD_ENDTIME - (RRD_STEP-1) * 1000).always( + function(data) { self.resume(function() { /* We request data starting 1 STEP +1s after the RRD file first val and ending 1 STEP -1s @@ -356,7 +367,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { RRD_ENDTIME-RRD_STEP*1000*2, data.data[data.data.length-1][0]); }); - }, this); + }); this.wait(); }, @@ -365,15 +376,15 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * If the requested time range is outside the range of the RRD file * we should not get any values back **/ - this.rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000).addBoth( - function(self, data) { + var self = this; + this.rq.getData(RRD_ENDTIME, RRD_ENDTIME+1000).always( + function(data) { self.resume(function() { Y.Assert.areEqual(0, data.data.length); }); - }, this); + }); this.wait(); } - })); @@ -385,6 +396,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { * Test that a rendered chart has the correct dimensions, legend, * axis, labels etc **/ + var self = this; var $tpl = $( '
').appendTo($('body')); var c = new jarmon.Chart($tpl, jarmon.Chart.BASE_OPTIONS); @@ -395,12 +407,12 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { new jarmon.RrdQueryRemote('build/test.rrd', 'm/s'), true); var d = c.setTimeRange(RRD_STARTTIME, RRD_ENDTIME); - d.addCallback( - function(self) { + d.done( + function() { self.resume(function() { // TODO: write useful tests }); - }, this); + }); this.wait(); } })); @@ -433,8 +445,7 @@ YUI({ logInclude: { TestRunner: true } }).use('console', 'test', function(Y) { this.wait( function() { Y.Assert.areEqual(self.$tpl.find('.error').size(), 1); - }, 1000 - ); + }, 1000); }, test_drawUrlListDatasources: function () { -- cgit v1.1-4-g5e80