From 7ca6df8feee3e7a9c5c6215b0d89af6011d974bf Mon Sep 17 00:00:00 2001 From: Kris Borchers Date: Tue, 19 Feb 2013 10:49:53 -0600 Subject: [PATCH 1/2] Pipeline:Rest - Add timeout setting for pipes --- src/pipeline/adapters/rest.js | 23 +++++++++++++++---- .../unit/pipeline/pipeline-mockjax-config.js | 10 ++++++++ tests/unit/pipeline/pipeline-rest.js | 22 ++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/pipeline/adapters/rest.js b/src/pipeline/adapters/rest.js index 6f83861..9cf1adf 100644 --- a/src/pipeline/adapters/rest.js +++ b/src/pipeline/adapters/rest.js @@ -29,6 +29,7 @@ @param {String} [settings.pageConfig.nextIdentifier="next"] - the name of the next link header, content var or web link rel @param {Function} [settings.pageConfig.parameterProvider] - a function for handling custom parameter placement within header and body based paging - for header paging, the function receives a jqXHR object and for body paging, the function receives the JSON formatted body as an object. the function should then return an object containing keys named for the previous/nextIdentifier options and whos values are either a map of parameters and values or a properly formatted query string @param {String} [settings.recordId="id"] - the name of the field used to uniquely identify a "record" in the data + @param {Number} [settings.timeout=60] - the amount of time, in seconds, to wait before timing out a connection and firing the complete callback for that request @returns {Object} The created pipe */ AeroGear.Pipeline.adapters.Rest = function( pipeName, settings ) { @@ -50,7 +51,8 @@ recordId = settings.recordId || "id", authenticator = settings.authenticator || null, type = "Rest", - pageConfig = settings.pageConfig; + pageConfig = settings.pageConfig, + timeout = settings.timeout ? settings.timeout * 1000 : 60000; // Privileged Methods /** @@ -95,6 +97,16 @@ return recordId; }; + /** + Returns the value of the private timeout var + @private + @augments Rest + @returns {Number} + */ + this.getTimeout = function() { + return timeout; + }; + /** Returns the value of the private pageConfig var @private @@ -311,7 +323,8 @@ url: url, statusCode: options.statusCode, complete: options.complete, - headers: options.headers + headers: options.headers, + timeout: this.getTimeout() }; if( options.jsonp ) { @@ -403,7 +416,8 @@ error: error, statusCode: options.statusCode, complete: options.complete, - headers: options.headers + headers: options.headers, + timeout: this.getTimeout() }); // Stringify data if we actually want to POST/PUT JSON data @@ -495,7 +509,8 @@ error: error, statusCode: options.statusCode, complete: options.complete, - headers: options.headers + headers: options.headers, + timeout: this.getTimeout() }; return $.ajax( this.addAuthIdentifier( $.extend( {}, ajaxSettings, extraOptions ) ) ); diff --git a/tests/unit/pipeline/pipeline-mockjax-config.js b/tests/unit/pipeline/pipeline-mockjax-config.js index e4c77f3..164ff05 100644 --- a/tests/unit/pipeline/pipeline-mockjax-config.js +++ b/tests/unit/pipeline/pipeline-mockjax-config.js @@ -611,4 +611,14 @@ $.mockjax({ } }); +$.mockjax({ + url: "long", + type: "GET", + headers: { + "Content-Type": "application/json" + }, + responseText: "returned", + responseTime: 6000 +}); + })( jQuery ); diff --git a/tests/unit/pipeline/pipeline-rest.js b/tests/unit/pipeline/pipeline-rest.js index 99e1d79..053ef77 100644 --- a/tests/unit/pipeline/pipeline-rest.js +++ b/tests/unit/pipeline/pipeline-rest.js @@ -822,4 +822,26 @@ asyncTest( "body - custom identifiers and query parameters with parameter provid } }); }); + +module( "Pipeline: Rest - Misc" ); + +var longPipe = AeroGear.Pipeline({ + name: "long", + type: "Rest", + settings: { + timeout: 5 + } + }).pipes.long; + +asyncTest( "timeout", function() { + expect( 1 ); + + longPipe.read({ + complete: function( jqXHR, textStatus ) { + equal( textStatus, "timeout", "Timeout Triggered Complete Callback" ); + start(); + } + }); +}); + })( jQuery ); From cd02eee53e14eeed6a447067df90b649c969af12 Mon Sep 17 00:00:00 2001 From: Kris Borchers Date: Tue, 19 Feb 2013 11:00:44 -0600 Subject: [PATCH 2/2] Pipeline:Rest - Add success callback to timeout test to ensure it isn't called on timeout --- tests/unit/pipeline/pipeline-rest.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit/pipeline/pipeline-rest.js b/tests/unit/pipeline/pipeline-rest.js index 053ef77..4bd216e 100644 --- a/tests/unit/pipeline/pipeline-rest.js +++ b/tests/unit/pipeline/pipeline-rest.js @@ -837,6 +837,9 @@ asyncTest( "timeout", function() { expect( 1 ); longPipe.read({ + success: function( data, textStatus, jqXHR ) { + ok( true, "This should not happen and will cause the test to fail if it does" ); + }, complete: function( jqXHR, textStatus ) { equal( textStatus, "timeout", "Timeout Triggered Complete Callback" ); start();