Commit 6b4c1794 authored by Tristan Cavelier's avatar Tristan Cavelier

cancellers added to jIO

parent 7cd7e8bc
...@@ -26,12 +26,15 @@ constants.http_status_text = { ...@@ -26,12 +26,15 @@ constants.http_status_text = {
"0": "Unknown", "0": "Unknown",
"550": "Internal JIO Error", "550": "Internal JIO Error",
"551": "Internal Storage Error", "551": "Internal Storage Error",
"555": "Cancelled",
"Unknown": "Unknown", "Unknown": "Unknown",
"Internal JIO Error": "Internal JIO Error", "Internal JIO Error": "Internal JIO Error",
"Internal Storage Error": "Internal Storage Error", "Internal Storage Error": "Internal Storage Error",
"Cancelled": "Cancelled",
"unknown": "Unknown", "unknown": "Unknown",
"internal_jio_error": "Internal JIO Error", "internal_jio_error": "Internal JIO Error",
"internal_storage_error": "Internal Storage Error", "internal_storage_error": "Internal Storage Error",
"cancelled": "Cancelled",
"200": "Ok", "200": "Ok",
"201": "Created", "201": "Created",
...@@ -138,12 +141,15 @@ constants.http_status = { ...@@ -138,12 +141,15 @@ constants.http_status = {
"0": 0, "0": 0,
"550": 550, "550": 550,
"551": 551, "551": 551,
"555": 555,
"Unknown": 0, "Unknown": 0,
"Internal JIO Error": 550, "Internal JIO Error": 550,
"Internal Storage Error": 551, "Internal Storage Error": 551,
"Cancelled": 555,
"unknown": 0, "unknown": 0,
"internal_jio_error": 550, "internal_jio_error": 550,
"internal_storage_error": 551, "internal_storage_error": 551,
"cancelled": 555,
"200": 200, "200": 200,
"201": 201, "201": 201,
...@@ -250,12 +256,15 @@ constants.http_action = { ...@@ -250,12 +256,15 @@ constants.http_action = {
"0": "error", "0": "error",
"550": "error", "550": "error",
"551": "error", "551": "error",
"555": "error",
"Unknown": "error", "Unknown": "error",
"Internal JIO Error": "error", "Internal JIO Error": "error",
"Internal Storage Error": "error", "Internal Storage Error": "error",
"Cancelled": "error",
"unknown": "error", "unknown": "error",
"internal_jio_error": "error", "internal_jio_error": "error",
"internal_storage_error": "error", "internal_storage_error": "error",
"cancelled": "error",
"200": "success", "200": "success",
"201": "success", "201": "success",
......
...@@ -58,6 +58,13 @@ function enableJobMaker(jio, shared, options) { ...@@ -58,6 +58,13 @@ function enableJobMaker(jio, shared, options) {
job.command.storage = function () { job.command.storage = function () {
return shared.createRestApi.apply(null, arguments); return shared.createRestApi.apply(null, arguments);
}; };
job.command.setCanceller = function (canceller) {
job.cancellers["command:canceller"] = canceller;
};
job.cancellers = job.cancellers || {};
job.cancellers["job:canceller"] = function () {
shared.emit("job:reject", job, ["cancelled"]);
};
} }
function createJobFromRest(param) { function createJobFromRest(param) {
......
...@@ -61,6 +61,7 @@ function enableRestAPI(jio, shared) { // (jio, shared, options) ...@@ -61,6 +61,7 @@ function enableRestAPI(jio, shared) { // (jio, shared, options)
param.solver.reject = reject; param.solver.reject = reject;
param.solver.notify = notify; param.solver.notify = notify;
}, function () { }, function () {
if (!param.cancellers) { return; }
var k; var k;
for (k in param.cancellers) { for (k in param.cancellers) {
if (param.cancellers.hasOwnProperty(k)) { if (param.cancellers.hasOwnProperty(k)) {
......
...@@ -66,6 +66,9 @@ ...@@ -66,6 +66,9 @@
}, },
free: function () { free: function () {
delete fakestorage[that._id + '/' + method]; delete fakestorage[that._id + '/' + method];
},
setCanceller: function () {
return command.setCanceller.apply(command, arguments);
} }
}; };
}; };
......
/*jslint indent: 2, maxlen: 80, nomen: true */ /*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, window, exports, require, jIO, fake_storage, ok, module, test, /*global define, window, exports, require, jIO, fake_storage, ok, module, test,
stop, start, deepEqual, FileReader, Blob, setTimeout, clearTimeout, stop, start, deepEqual, FileReader, Blob, setTimeout, clearTimeout,
localStorage */ localStorage, test_util */
(function (dependencies, module) { (function (dependencies, factory) {
"use strict"; "use strict";
if (typeof define === 'function' && define.amd) { if (typeof define === "function" && define.amd) {
return define(dependencies, module); return define(dependencies, factory);
} }
if (typeof exports === 'object') { if (typeof module === "object" && module !== null &&
return module(require('fakestorage'), require('jio')); typeof module.exports === "object" && module.exports !== null &&
typeof require === "function") {
return factory(dependencies.map(require));
} }
module(fake_storage, jIO); factory(fake_storage, jIO, test_util);
}(['fakestorage', 'jio', 'sinon_qunit'], function (fake_storage, jIO) { }([
'fakestorage',
'jio',
'test_util',
'sinon_qunit'
], function (fake_storage, jIO, util) {
"use strict"; "use strict";
var test_name, JIO = jIO.JIO, commands = fake_storage.commands; var test_name, JIO = jIO.JIO, commands = fake_storage.commands;
...@@ -353,6 +360,40 @@ ...@@ -353,6 +360,40 @@
}, 50); }, 50);
}); });
test('should be cancelled', 1, function () {
var time_array = [], put_promise,
start = util.starter(1000),
jio = new JIO({
"type": "fake",
"id": "Cancel Err"
}, {
"workspace": {}
});
stop();
put_promise = jio.put({"_id": "a"});
put_promise.then(start, function (answer) {
time_array.push(answer);
deepEqual(time_array, ["cancelled", {
"error": "cancelled",
"id": "a",
"message": "Command failed",
"method": "put",
"reason": "unknown",
"result": "error",
"status": 555,
"statusText": "Cancelled"
}]);
start();
});
setTimeout(function () {
commands['Cancel Err/put'].setCanceller(function () {
time_array.push("cancelled");
});
put_promise.cancel();
}, 50);
});
module('JIO parameters'); module('JIO parameters');
test('should throw error when giving no parameter to `post`', 1, function () { test('should throw error when giving no parameter to `post`', 1, function () {
......
/*jslint indent: 2, maxlen: 80 */ /*jslint indent: 2, maxlen: 80 */
/*global define, exports, window, require, localStorage, start, ok, deepEqual, /*global define, exports, window, require, localStorage, start, ok, deepEqual,
sinon */ sinon, setTimeout, clearTimeout */
(function (dependencies, module) { (function (dependencies, module) {
"use strict"; "use strict";
...@@ -20,6 +20,28 @@ ...@@ -20,6 +20,28 @@
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Tools // Tools
/**
* Creates a QUnit.start wrapper that allows to trigger start several times.
*
* @param {Number} num Timeout in ms
* @return {Function} The wrapper
*/
function starter(num) {
var started = false, ident;
function startFn() {
if (!started) {
started = true;
clearTimeout(ident);
start();
}
}
if (num) {
ident = setTimeout(startFn, num);
}
return startFn;
}
exports.starter = starter;
/** /**
* Test if the string is an Uuid * Test if the string is an Uuid
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment