Added tests about timeouts and implemented it in jsonprpc2
authorniv-openerp <nicolas.vanhoren@openerp.com>
Fri, 11 Oct 2013 13:30:54 +0000 (15:30 +0200)
committerniv-openerp <nicolas.vanhoren@openerp.com>
Fri, 11 Oct 2013 13:30:54 +0000 (15:30 +0200)
bzr revid: nicolas.vanhoren@openerp.com-20131011133054-0dx3wxqcqbdocnlf

addons/web/static/src/js/openerpframework.js
addons/web/static/test/jsonrpc.js

index 062ecb8..ab10d30 100644 (file)
@@ -817,7 +817,7 @@ var genericJsonRpc = function(fct_name, params, fct) {
             return result.result;
         }
     }, function() {
-        console.error("JsonRPC communication error", _.toArray(arguments));
+        //console.error("JsonRPC communication error", _.toArray(arguments));
         var def = $.Deferred();
         return def.reject.apply(def, ["communication"].concat(_.toArray(arguments)));
     });
@@ -899,11 +899,29 @@ openerp.jsonpRpc = function(url, fct_name, params, settings) {
             });
             // append the iframe to the DOM (will trigger the first load)
             $form.after($iframe);
+            if (settings.timeout) {
+                realSetTimeout(function() {
+                    deferred.reject({});
+                }, settings.timeout);
+            }
             return deferred;
         }
     });
 };
 
+var realSetTimeout = function(fct, millis) {
+    var finished = new Date().getTime() + millis;
+    var wait = function() {
+        var current = new Date().getTime();
+        if (current < finished) {
+            setTimeout(wait, finished - current);
+        } else {
+            fct();
+        }
+    };
+    setTimeout(wait, millis);
+};
+
 openerp.Session = openerp.Class.extend(openerp.PropertiesMixin, {
     triggers: {
         'request': 'Request sent',
index f8a1d32..93aa989 100644 (file)
@@ -111,6 +111,42 @@ function (test) {
             notEqual(result, origin_tmp, "Values in the different sessions should be different");
         });
     });*/
+    test('timeout-jsonrpc', {asserts: 1}, function () {
+        var session = new openerp.Session();
+        return session.rpc("/gen_session_id", {}, {timeout: 1}).then(function() {
+            ok(false, "the request incorrectly succeeded");
+            return $.when();
+        }, function(a, e) {
+            e.preventDefault();
+            ok(true, "the request correctly failed");
+            return $.when();
+        });
+    });
+    test('timeout-jsonprpc', {asserts: 1}, function () {
+        var session = new openerp.Session();
+        session.origin_server = false;
+        return session.rpc("/gen_session_id", {}, {timeout: 1}).then(function() {
+            ok(false, "the request incorrectly succeeded");
+            return $.when();
+        }, function(a, e) {
+            e.preventDefault();
+            ok(true, "the request correctly failed");
+            return $.when();
+        });
+    });
+    // desactivated because the phantomjs runner crash
+    test('timeout-jsonprpc2', {asserts: 1}, function () {
+        var session = new openerp.Session();
+        session.origin_server = false;
+        return session.rpc("/gen_session_id", {}, {force2step: true, timeout: 1}).then(function() {
+            ok(false, "the request incorrectly succeeded");
+            return $.when();
+        }, function(a, e) {
+            e.preventDefault();
+            ok(true, "the request correctly failed");
+            return $.when();
+        });
+    });
 });
 
 var login = "admin";