Commit 345c0cd9 authored by Tristan Cavelier's avatar Tristan Cavelier

jio tests updated

parent a45f3eb0
/*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,
expect, deepEqual, FileReader, Blob, setTimeout, localStorage */ stop, start, deepEqual, FileReader, Blob, setTimeout, clearTimeout,
localStorage */
(function (dependencies, module) { (function (dependencies, module) {
"use strict"; "use strict";
...@@ -13,31 +14,23 @@ ...@@ -13,31 +14,23 @@
module(fake_storage, jIO); module(fake_storage, jIO);
}(['fakestorage', 'jio', 'sinon_qunit'], function (fake_storage, jIO) { }(['fakestorage', 'jio', 'sinon_qunit'], function (fake_storage, jIO) {
"use strict"; "use strict";
var JIO = jIO.JIO, commands = fake_storage.commands; var test_name, JIO = jIO.JIO, commands = fake_storage.commands;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Tests // Tests
module('JIO'); module('JIO');
/** test('should initialize itself without error', 1, function () {
* Tests the instance initialization var jio = new JIO(undefined, {
*/ "workspace": {}
test('Init', function () {
expect(1);
var workspace = {}, jio = new JIO(undefined, {
"workspace": workspace
}); });
// tests if jio is an object // tests if jio is an object
ok(typeof jio === 'object', 'Init ok!'); ok(typeof jio === 'object', 'instance is an object');
}); });
/** test('should throw error when giving no parameter to `post`', 1, function () {
* Tests a wrong command
*/
test('Wrong parameters', function () {
expect(2);
var result, jio = new JIO({ var result, jio = new JIO({
"type": "fake", "type": "fake",
"id": "Wrong para" "id": "Wrong para"
...@@ -56,6 +49,16 @@ ...@@ -56,6 +49,16 @@
"TypeError: JIO().post(): Argument 1 is not of type 'object'", "TypeError: JIO().post(): Argument 1 is not of type 'object'",
"Wrong parameter" "Wrong parameter"
); );
});
test_name = 'should not throw error when giving no param to `allDocs`';
test(test_name, 1, function () {
var result, jio = new JIO({
"type": "fake",
"id": "Good para"
}, {
"workspace": {}
});
try { try {
jio.allDocs(); // allDocs([options], [callbacks]); jio.allDocs(); // allDocs([options], [callbacks]);
...@@ -66,59 +69,16 @@ ...@@ -66,59 +69,16 @@
deepEqual(result, "No error thrown", "Good parameter"); deepEqual(result, "No error thrown", "Good parameter");
}); });
/** test('should return an error when a storage type is given', 1, function () {
* Tests asynchrony var jio = new JIO({
*/
test("Asynchrony", function () {
var workspace = {}, clock, jio, count = 0;
expect(8);
clock = this.sandbox.useFakeTimers();
jio = new JIO({
"type": "fake",
"id": "Asynchrony"
}, {
"workspace": workspace
});
jio.post({}).done(function () {
count += 1;
deepEqual(count, 6, "Command done");
}).progress(function () {
count += 1;
deepEqual(count, 3, "Command notifiy");
});
count += 1;
deepEqual(count, 1, "JIO post");
ok(!commands['Asynchrony/post'], "Command not called yet");
clock.tick(1);
count += 1;
deepEqual(count, 2, "Next instructions");
ok(commands['Asynchrony/post'], "Command called");
commands['Asynchrony/post'].notify();
count += 1;
deepEqual(count, 4, "Next timer");
commands['Asynchrony/post'].success({"id": "a"});
count += 1;
deepEqual(count, 5, "Command success requested");
clock.tick(1);
});
/**
* Tests a storage initialization error
*/
test('Description Error', function () {
var clock, jio;
expect(2);
clock = this.sandbox.useFakeTimers();
jio = new JIO({
"type": "blue" "type": "blue"
}, { }, {
"workspace": {} "workspace": {}
}); });
// Tests wrong storage type stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
start();
deepEqual(answer, { deepEqual(answer, {
"error": "internal_storage_error", "error": "internal_storage_error",
"message": "Check if the storage description respects the " + "message": "Check if the storage description respects the " +
...@@ -129,19 +89,21 @@ ...@@ -129,19 +89,21 @@
"result": "error", "result": "error",
"status": 551, "status": 551,
"statusText": "Internal Storage Error" "statusText": "Internal Storage Error"
}, "Unknown storage"); }, "Unknown storage error");
}); });
clock.tick(1); });
// Tests wrong storage description test('should return an error when a description is given', 1, function () {
jio = new JIO({ var jio = new JIO({
"type": "fake", "type": "fake",
"id": "" "id": ""
}, { }, {
"workspace": {} "workspace": {}
}); });
stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
start();
deepEqual(answer, { deepEqual(answer, {
"error": "internal_storage_error", "error": "internal_storage_error",
"message": "Check if the storage description respects the " + "message": "Check if the storage description respects the " +
...@@ -154,126 +116,194 @@ ...@@ -154,126 +116,194 @@
"statusText": "Internal Storage Error" "statusText": "Internal Storage Error"
}, "Initialization error"); }, "Initialization error");
}); });
clock.tick(1);
}); });
/** test('should fail after default command timeout', 2, function () {
* Tests a command which does not respond var i, called = false, jio = new JIO({
*/
test('No Response or Response Timeout', function () {
var clock, jio, state;
expect(5);
clock = this.sandbox.useFakeTimers();
jio = new JIO({
"type": "fake", "type": "fake",
"id": "1 No Respons" "id": "1 No Respons"
}, { }, {
"workspace": {} "workspace": {}
}); });
// tests default timeout stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
var message = (answer && answer.message) || "Timeout";
called = true;
if (i !== undefined) {
start();
clearTimeout(i);
}
delete answer.message;
deepEqual(answer, { deepEqual(answer, {
"error": "request_timeout", "error": "request_timeout",
"message": "Operation canceled after around " +
"10000 milliseconds of inactivity.",
"method": "post", "method": "post",
"result": "error", "result": "error",
"reason": "timeout", "reason": "timeout",
"status": 408, "status": 408,
"statusText": "Request Timeout" "statusText": "Request Timeout"
}, "Timeout error (default timeout)"); }, message);
}); });
clock.tick(1);
clock.tick(10000); // wait 10 seconds
commands['1 No Respons/post'].free();
jio = new JIO({ setTimeout(function () {
ok(!called, "callback " + (called ? "" : "not") + " called");
}, 9999);
setTimeout(function () {
commands['1 No Respons/post'].free();
}, 100);
i = setTimeout(function () {
i = undefined;
start();
ok(false, "No response");
}, 11000);
});
test('should fail after storage inactivity timeout', 2, function () {
var i, called = false, jio = new JIO({
"type": "fake", "type": "fake",
"id": "2 No Respons" "id": "2 No Respons"
}, { }, {
"workspace": {} "workspace": {}
}); });
// tests storage timeout stop();
state = "Not called yet";
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
state = "Called"; var message = (answer && answer.message) || "Timeout";
called = true;
if (i !== undefined) {
start();
clearTimeout(i);
}
delete answer.message;
deepEqual(answer, { deepEqual(answer, {
"error": "request_timeout", "error": "request_timeout",
"message": "Operation canceled after around " +
"10000 milliseconds of inactivity.",
"method": "post", "method": "post",
"result": "error", "result": "error",
"reason": "timeout", "reason": "timeout",
"status": 408, "status": 408,
"statusText": "Request Timeout" "statusText": "Request Timeout"
}, "Timeout error (storage timeout reset)"); }, message);
}); });
clock.tick(1); setTimeout(function () {
clock.tick(4999); // wait 5 seconds commands['2 No Respons/post'].notify();
commands['2 No Respons/post'].notify(); }, 5000);
clock.tick(5000); // wait 5 seconds
deepEqual(state, "Not called yet", "Check callback state.");
clock.tick(5000); // wait 5 seconds
commands['2 No Respons/post'].free();
jio = new JIO({ setTimeout(function () {
commands['2 No Respons/post'].free();
}, 6000);
setTimeout(function () {
ok(!called, "callback " + (called ? "" : "not") + " called");
}, 14999);
i = setTimeout(function () {
i = undefined;
start();
ok(false, "No response");
}, 16000);
});
test('should fail after jio option default timeout', 2, function () {
var i, called = false, jio = new JIO({
"type": "fake", "type": "fake",
"id": "3 No Respons" "id": "3 No Respons"
}, { }, {
"workspace": {}, "workspace": {},
"default_timeout": 2 "default_timeout": 2000
}); });
// tests jio option timeout stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
var message = (answer && answer.message) || "Timeout";
called = true;
if (i !== undefined) {
start();
clearTimeout(i);
}
delete answer.message;
deepEqual(answer, { deepEqual(answer, {
"error": "request_timeout", "error": "request_timeout",
"message": "Operation canceled after around " +
"2 milliseconds of inactivity.",
"method": "post", "method": "post",
"result": "error", "result": "error",
"reason": "timeout", "reason": "timeout",
"status": 408, "status": 408,
"statusText": "Request Timeout" "statusText": "Request Timeout"
}, "Timeout error (specific default timeout)"); }, message);
});
setTimeout(function () {
commands['3 No Respons/post'].free();
}, 100);
setTimeout(function () {
ok(!called, "callback " + (called ? "" : "not") + " called");
}, 1999);
i = setTimeout(function () {
i = undefined;
start();
ok(false, "No response");
}, 3000);
});
test('should fail after command option timeout', 2, function () {
var i, called = false, jio = new JIO({
"type": "fake",
"id": "4 No Respons"
}, {
"workspace": {},
"default_timeout": 2000
}); });
clock.tick(1);
clock.tick(1);
// tests command option timeout stop();
jio.post({}, {"timeout": 50}).always(function (answer) { jio.post({}, {"timeout": 3000}).always(function (answer) {
var message = (answer && answer.message) || "Timeout";
called = true;
if (i !== undefined) {
start();
clearTimeout(i);
}
delete answer.message;
deepEqual(answer, { deepEqual(answer, {
"error": "request_timeout", "error": "request_timeout",
"message": "Operation canceled after around " +
"50 milliseconds of inactivity.",
"method": "post", "method": "post",
"result": "error", "result": "error",
"reason": "timeout", "reason": "timeout",
"status": 408, "status": 408,
"statusText": "Request Timeout" "statusText": "Request Timeout"
}, "Timeout error (command timeout)"); }, message);
}); });
clock.tick(1);
clock.tick(49); setTimeout(function () {
commands['4 No Respons/post'].free();
}, 1000);
setTimeout(function () {
ok(!called, "callback " + (called ? "" : "not") + " called");
}, 2999);
i = setTimeout(function () {
i = undefined;
start();
ok(false, "No response");
}, 4000);
}); });
/** test('should fail when command succeed with a bad response', 1, function () {
* Tests wrong responses var jio = new JIO({
*/
test('Invalid Response', function () {
var clock, jio;
expect(2);
clock = this.sandbox.useFakeTimers();
jio = new JIO({
"type": "fake", "type": "fake",
"id": "1 Invalid Re" "id": "1 Invalid Re"
}, { }, {
"workspace": {} "workspace": {}
}); });
stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
start();
deepEqual(answer, { deepEqual(answer, {
"error": "internal_storage_error", "error": "internal_storage_error",
"message": "New document id have to be specified", "message": "New document id have to be specified",
...@@ -282,20 +312,24 @@ ...@@ -282,20 +312,24 @@
"reason": "invalid response", "reason": "invalid response",
"status": 551, "status": 551,
"statusText": "Internal Storage Error" "statusText": "Internal Storage Error"
}, "Invalid Post Response"); }, "response");
}); });
clock.tick(1); setTimeout(function () {
commands['1 Invalid Re/post'].success(); commands['1 Invalid Re/post'].success();
clock.tick(1); }, 50);
});
jio = new JIO({ test('should fail when command end with a bad error', 1, function () {
var jio = new JIO({
"type": "fake", "type": "fake",
"id": "2 Invalid Re" "id": "2 Invalid Re"
}, { }, {
"workspace": {} "workspace": {}
}); });
stop();
jio.post({}).always(function (answer) { jio.post({}).always(function (answer) {
start();
deepEqual(answer, { deepEqual(answer, {
"error": "internal_storage_error", "error": "internal_storage_error",
"message": "Unknown status \"undefined\"", "message": "Unknown status \"undefined\"",
...@@ -304,109 +338,49 @@ ...@@ -304,109 +338,49 @@
"result": "error", "result": "error",
"status": 551, "status": 551,
"statusText": "Internal Storage Error" "statusText": "Internal Storage Error"
}, "Invalid Post Error Response"); }, "response");
}); });
clock.tick(1); setTimeout(function () {
commands['2 Invalid Re/post'].error(); commands['2 Invalid Re/post'].error();
clock.tick(1); }, 50);
}); });
/** test('should succeed when giving a good `post` response', 1, function () {
* Tests valid responses var jio = new JIO({
*/
test('Valid Responses & Callbacks', function () {
var clock, jio, o = {};
expect(9);
clock = this.sandbox.useFakeTimers();
jio = new JIO({
"type": "fake", "type": "fake",
"id": "Valid Resp" "id": "Valid post"
}, { }, {
"workspace": {} "workspace": {}
}); });
// Tests post command callbacks post(metadata).always(onResponse) + stop();
// valid response. jio.post({}).always(function (answer) {
o.message = "Post Command: post(metadata).always(function (answer) {..}) " + start();
"+ valid response.";
jio.post({}).done(function () {
o.called = 'done';
}).always(function (answer) {
ok(o.called === 'done', "Done callback called first");
deepEqual(answer, { deepEqual(answer, {
"id": "document id a", "id": "document id a",
"method": "post", "method": "post",
"result": "success", "result": "success",
"status": 201, "status": 201,
"statusText": "Created" "statusText": "Created"
}, o.message); }, "response");
}); });
clock.tick(1); setTimeout(function () {
commands['Valid Resp/post'].success({"id": "document id a"}); commands['Valid post/post'].success({"id": "document id a"});
clock.tick(1); }, 50);
});
// Tests post command callbacks post(metadata).done(onSuccess).fail(onError) test('`getAttachment` should respond blob', 2, function () {
o.message = "Post Command: post(metadata).done(function (answer) {..})." + var jio = new JIO({
"fail(function (answer) {..})"; "type": "fake",
jio.post({}).done(function (answer) { "id": "Valid getA"
deepEqual(answer, { }, {
"id": "document id a", "workspace": {}
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, o.message);
}).fail(function (answer) {
deepEqual(answer, "Should not fail", o.message);
});
clock.tick(1);
commands['Valid Resp/post'].success({"id": "document id a"});
clock.tick(1);
// Tests post command callbacks post(metadata, onResponse)
o.message = "Post Command: post(metadata, function (err, response) {..})";
jio.post({}, function (err, response) {
if (err) {
return deepEqual(err, "Should not fail", o.message);
}
deepEqual(response, {
"id": "document id a",
"method": "post",
"result": "success",
"status": 201,
"statusText": "Created"
}, o.message);
});
clock.tick(1);
commands['Valid Resp/post'].success({"id": "document id a"});
clock.tick(1);
// Tests post command callbacks post(metadata, onSuccess, onError) + error
// response.
o.message = "Post Command: post(metadata, function (response) {..}, " +
"function (err) {..}) + valid error response.";
jio.post({}, function (response) {
deepEqual(response, "Should fail", o.message);
}, function (err) {
deepEqual(err, {
"status": 409,
"statusText": "Conflict",
"method": "post",
"error": "conflict",
"reason": "unknown",
"result": "error",
"message": ""
}, o.message);
}); });
clock.tick(1);
commands['Valid Resp/post'].error('conflict');
clock.tick(1);
// Tests getAttachment command string response stop();
jio.getAttachment({"_id": "a", "_attachment": "b"}). jio.getAttachment({"_id": "a", "_attachment": "b"}).
always(function (answer) { always(function (answer) {
start();
ok(answer.data instanceof Blob, ok(answer.data instanceof Blob,
"Get Attachment Command: Blob should be returned"); "Get Attachment Command: Blob should be returned");
delete answer.data; delete answer.data;
...@@ -419,46 +393,50 @@ ...@@ -419,46 +393,50 @@
"statusText": "Ok" "statusText": "Ok"
}); });
}); });
clock.tick(1); setTimeout(function () {
commands['Valid Resp/getAttachment'].success("ok", { commands['Valid getA/getAttachment'].success("ok", {
"data": "document id a" "data": "document id a"
}); });
clock.tick(1); }, 50);
});
// Tests notify responses
o.notified = true;
o.message = "Synchronous Notify";
jio.post({}).progress(function (answer) {
deepEqual(answer, o.answer, o.message);
});
clock.tick(1);
o.answer = undefined;
commands['Valid Resp/post'].notify();
o.answer = 'hoo';
commands['Valid Resp/post'].notify(o.answer);
o.answer = 'Forbidden!!!';
o.message = 'Notification forbidden after success';
setTimeout(commands['Valid Resp/post'].notify, 2);
commands['Valid Resp/post'].success();
clock.tick(2);
test('should be notified by the command', 4, function () {
var i = 0, jio = new JIO({
"type": "fake",
"id": "Valid noti"
}, {
"workspace": {}
});
stop();
jio.put({"_id": "a"}).then(function () {
start();
}, function (answer) {
start();
deepEqual(answer, "Error", "should not fail");
}, function (answer) {
deepEqual(answer, i, "notified");
ok(i < 3, (i < 3 ? "" : "not ") + "called before success");
i += 1;
});
setTimeout(function () {
var notify = commands['Valid noti/put'].notify;
notify(0);
notify(1);
commands['Valid noti/put'].success();
notify(2);
}, 50);
}); });
/** test('metadata values should be formatted on `post`', 1, function () {
* Tests metadata values var request, response, jio = new JIO({
*/
test('Metadata values', function () {
expect(9);
var o, clock = this.sandbox.useFakeTimers(), jio = new JIO({
"type": "fake", "type": "fake",
"id": "Metadata v" "id": "Metadata v"
}, { }, {
"workspace": {} "workspace": {}
}); });
o = {}; request = {
o.request = {
"_id": undefined, "_id": undefined,
"number": -13, "number": -13,
"date": new Date(0), "date": new Date(0),
...@@ -480,7 +458,7 @@ ...@@ -480,7 +458,7 @@
"wrong_array": [{}, null, {"blue": "green"}] "wrong_array": [{}, null, {"blue": "green"}]
}; };
o.response = { response = {
"number": -13, "number": -13,
"date": new Date(0).toJSON(), "date": new Date(0).toJSON(),
"boolean": true, "boolean": true,
...@@ -491,90 +469,134 @@ ...@@ -491,90 +469,134 @@
"toJSON": "hey!" "toJSON": "hey!"
}; };
jio.post(o.request); stop();
clock.tick(1); jio.post(request);
deepEqual( setTimeout(function () {
commands["Metadata v/post"].param, start();
o.response, deepEqual(
"Post" commands["Metadata v/post"].param,
); response,
commands["Metadata v/post"].success(); "Post"
clock.tick(1); );
commands["Metadata v/post"].success();
o.request._id = 'a'; }, 50);
o.response._id = 'a';
jio.put(o.request); // o.request._id = 'a';
clock.tick(1); // o.response._id = 'a';
deepEqual(commands["Metadata v/put"].param, o.response, "Put"); // jio.put(o.request);
commands["Metadata v/put"].success(); // clock.tick(1);
clock.tick(1); // deepEqual(commands["Metadata v/put"].param, o.response, "Put");
// commands["Metadata v/put"].success();
jio.get({ // clock.tick(1);
"_id": "a"
}); // jio.get({
clock.tick(1); // "_id": "a"
deepEqual(commands["Metadata v/get"].param, { // });
"_id": "a" // clock.tick(1);
}, "Get"); // deepEqual(commands["Metadata v/get"].param, {
commands["Metadata v/get"].success(); // "_id": "a"
clock.tick(1); // }, "Get");
// commands["Metadata v/get"].success();
jio.remove({ // clock.tick(1);
"_id": "a"
}); // jio.remove({
clock.tick(1); // "_id": "a"
deepEqual(commands["Metadata v/remove"].param, { // });
"_id": "a" // clock.tick(1);
}, "Remove"); // deepEqual(commands["Metadata v/remove"].param, {
commands["Metadata v/remove"].success(); // "_id": "a"
clock.tick(1); // }, "Remove");
// commands["Metadata v/remove"].success();
// clock.tick(1);
jio.allDocs(); });
clock.tick(1);
deepEqual(commands["Metadata v/allDocs"].param, {}, "AllDocs");
commands["Metadata v/allDocs"].success();
clock.tick(1);
o.request = { test('data should be converted to blob on `putAttachment`', 3, function () {
var request, jio = new JIO({
"type": "fake",
"id": "Metadata v"
}, {
"workspace": {}
});
request = {
"_id": "a", "_id": "a",
"_attachment": "body", "_attachment": "body",
"_data": "b", "_data": "b",
"_mimetype": "c" "_mimetype": "c"
}; };
jio.putAttachment(o.request);
clock.tick(1); stop();
ok(commands["Metadata v/putAttachment"].param._blob instanceof Blob, jio.putAttachment(request);
"Put Attachment + check blob"); setTimeout(function () {
deepEqual([ start();
commands["Metadata v/putAttachment"].param._id, ok(commands["Metadata v/putAttachment"].param._blob instanceof Blob,
commands["Metadata v/putAttachment"].param._attachment "param._blob should be a blob");
], ["a", "body"], "Put Attachment + check ids"); deepEqual(
commands["Metadata v/putAttachment"].success(); commands["Metadata v/putAttachment"].param._blob.type,
clock.tick(1); "c",
"param._blob type should be equal to request._mimetype"
o.request._blob = new Blob(['d'], {"type": "e"}); );
delete o.request._mimetype; deepEqual([
delete o.request._data; commands["Metadata v/putAttachment"].param._id,
jio.putAttachment(o.request); commands["Metadata v/putAttachment"].param._attachment
clock.tick(1); ], ["a", "body"], "param._id and param._attachment exist");
ok(commands["Metadata v/putAttachment"].param._blob === o.request._blob, commands["Metadata v/putAttachment"].success();
"Put Attachment with blob + check blob"); }, 50);
deepEqual([
commands["Metadata v/putAttachment"].param._id,
commands["Metadata v/putAttachment"].param._attachment
], ["a", "body"], "Put Attachment with blob + check ids");
commands["Metadata v/putAttachment"].success();
clock.tick(1);
}); });
/** test('blob should be given to param on `putAttachment`', 3, function () {
* Tests job retry var request = {}, jio = new JIO({
*/ "type": "fake",
test("Job Retry", function () { "id": "Metadata 2"
var clock, jio, state; }, {
expect(4); "workspace": {}
clock = this.sandbox.useFakeTimers(); });
request._id = "a";
request._attachment = "body";
request._blob = new Blob(['d'], {"type": "e"});
stop();
jio.putAttachment(request);
setTimeout(function () {
start();
ok(commands["Metadata 2/putAttachment"].param._blob === request._blob,
"param._blob should be the given blob");
deepEqual(
commands["Metadata 2/putAttachment"].param._blob.type,
"e",
"param._blob type should be equal to request._mimetype"
);
deepEqual([
commands["Metadata 2/putAttachment"].param._id,
commands["Metadata 2/putAttachment"].param._attachment
], ["a", "body"], "param._id and param._attachment exist");
commands["Metadata 2/putAttachment"].success();
}, 50);
});
test('no param should be given to `allDocs`', 1, function () {
var jio = new JIO({
"type": "fake",
"id": "Metadata v"
}, {
"workspace": {}
});
stop();
jio.allDocs();
setTimeout(function () {
start();
deepEqual(commands["Metadata v/allDocs"].param, {}, "No param given");
commands["Metadata v/allDocs"].success();
}, 50);
});
test("job should respond 3 retries to return an error", 4, function () {
var jio, state;
jio = new JIO({ jio = new JIO({
"type": "fake", "type": "fake",
"id": "1 Job Retry" "id": "1 Job Retry"
...@@ -582,6 +604,7 @@ ...@@ -582,6 +604,7 @@
"workspace": {} "workspace": {}
}); });
stop();
state = "Not called yet"; state = "Not called yet";
jio.get({"_id": "a"}).always(function (answer) { jio.get({"_id": "a"}).always(function (answer) {
state = "Called"; state = "Called";
...@@ -596,30 +619,29 @@ ...@@ -596,30 +619,29 @@
"statusText": "Internal Server Error" "statusText": "Internal Server Error"
}, "Error response"); }, "Error response");
}); });
clock.tick(1); setTimeout(function () {
commands['1 Job Retry/get'].retry('internal_server_error'); commands['1 Job Retry/get'].retry('internal_server_error');
clock.tick(1); }, 50); // wait 50 ms
deepEqual(state, "Not called yet", "Check callback state."); setTimeout(function () {
deepEqual(state, "Not called yet", "Check callback state.");
clock.tick(1999); }, 100); // wait 50 ms
commands['1 Job Retry/get'].retry('internal_server_error'); setTimeout(function () {
clock.tick(1); commands['1 Job Retry/get'].retry('internal_server_error');
deepEqual(state, "Not called yet", "Check callback state."); }, 2150); // wait 2050 ms
setTimeout(function () {
clock.tick(3999); deepEqual(state, "Not called yet", "Check callback state.");
commands['1 Job Retry/get'].retry('internal_server_error'); }, 2200); // wait 50 ms
clock.tick(1); setTimeout(function () {
deepEqual(state, "Called", "Check callback state."); commands['1 Job Retry/get'].retry('internal_server_error');
}, 6250); // wait 4050 ms
setTimeout(function () {
start();
deepEqual(state, "Called", "Check callback state.");
}, 6300); // wait 50 ms
}); });
/** test("Job Management", 8, function () {
* Tests job management var tmp, workspace = {}, jio, o = {};
*/
test("Job Management", function () {
var workspace = {}, clock, jio, o = {};
expect(8);
clock = this.sandbox.useFakeTimers();
jio = new JIO({ jio = new JIO({
"type": "fake", "type": "fake",
"id": "1 Job Manage" "id": "1 Job Manage"
...@@ -627,8 +649,9 @@ ...@@ -627,8 +649,9 @@
"workspace": workspace "workspace": workspace
}); });
stop();
// Launch a get command, check the workspace and then respond // Launch a get command, check the workspace and then respond
jio.get({"_id": "a"}, {"max_retry": 2, "timeout": 12}). jio.get({"_id": "a"}, {"max_retry": 2, "timeout": 1200}).
always(function (answer) { always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"id": "a", "id": "a",
...@@ -641,100 +664,103 @@ ...@@ -641,100 +664,103 @@
}); });
o.job1 = { o.job1 = {
"kwargs": {"_id": "a"}, "kwargs": {"_id": "a"},
"options": {"max_retry": 2, "timeout": 12}, "options": {"max_retry": 2, "timeout": 1200},
"storage_spec": {"type": "fake", "id": "1 Job Manage"}, "storage_spec": {"type": "fake", "id": "1 Job Manage"},
"method": "get", "method": "get",
"created": new Date(), //"created": new Date(),
"tried": 1, "tried": 1,
"state": "running", "state": "running",
"modified": new Date(), //"modified": new Date(),
"max_retry": 2, "max_retry": 2,
"timeout": 12, "timeout": 1200,
"id": 1 "id": 1
}; };
deepEqual(workspace, { tmp = workspace["jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}"];
"jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}": jIO.util. tmp = JSON.parse(tmp);
uniqueJSONStringify([o.job1]) delete tmp[0].created;
}, 'Job added, workspace have one job'); delete tmp[0].modified;
deepEqual(tmp, [o.job1], 'workspace have one job');
clock.tick(1); // now: 1 ms
commands["1 Job Manage/get"].success({"data": {"b": "c"}}); setTimeout(function () {
clock.tick(1); // now: 2 ms commands["1 Job Manage/get"].success({"data": {"b": "c"}});
}, 50); // wait 50 ms
deepEqual(workspace, {}, 'Job ended, empty workspace'); setTimeout(function () {
deepEqual(workspace, {}, 'Job ended, empty workspace');
// Launch a get command which launches another get command
// check workspace after every command and respond // Launch a get command which launches another get command
jio.get({"_id": "b"}, {"max_retry": 2, "timeout": 12}). // check workspace after every command and respond
always(function (answer) { jio.get({"_id": "b"}, {"max_retry": 2, "timeout": 1200}).
always(function (answer) {
deepEqual(answer, {
"id": "b",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok",
"data": {"c": "d"}
}, "First job respond");
});
o.job1.kwargs._id = 'b';
// o.job1.created = new Date();
// o.job1.modified = new Date();
}, 100); // wait 50 ms
setTimeout(function () {
commands["1 Job Manage/get"].storage({
"type": "fake",
"id": "2 Job Manage"
}).get({"_id": "c"}).always(function (answer) {
start();
deepEqual(answer, { deepEqual(answer, {
"id": "b", "id": "c",
"method": "get", "method": "get",
"result": "success", "result": "success",
"status": 200, "status": 200,
"statusText": "Ok", "statusText": "Ok",
"data": {"c": "d"} "data": {"d": "e"}
}, "First job respond"); }, "Second job respond");
}); });
o.job1.kwargs._id = 'b';
o.job1.created = new Date();
o.job1.modified = new Date();
clock.tick(1); // now: 3 ms
commands["1 Job Manage/get"].storage({
"type": "fake",
"id": "2 Job Manage"
}).get({"_id": "c"}).always(function (answer) {
deepEqual(answer, {
"id": "c",
"method": "get",
"result": "success",
"status": 200,
"statusText": "Ok",
"data": {"d": "e"}
}, "Second job respond");
});
o.job2 = { o.job2 = {
"kwargs": {"_id": "c"}, "kwargs": {"_id": "c"},
"options": {}, "options": {},
"storage_spec": {"type": "fake", "id": "2 Job Manage"}, "storage_spec": {"type": "fake", "id": "2 Job Manage"},
"method": "get", "method": "get",
"created": new Date(), //"created": new Date(),
"tried": 1, "tried": 1,
"state": "running", "state": "running",
"modified": new Date(), //"modified": new Date(),
"max_retry": 2, "max_retry": 2,
"timeout": 10000, "timeout": 10000,
"id": 2 "id": 2
}; };
deepEqual(workspace, { tmp = workspace["jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}"];
"jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}": jIO.util. tmp = JSON.parse(tmp);
uniqueJSONStringify([o.job1, o.job2]) delete tmp[0].created;
}, 'Job calls another job, workspace have two jobs'); delete tmp[0].modified;
delete tmp[1].created;
clock.tick(1); delete tmp[1].modified;
commands['1 Job Manage/get'].end(); deepEqual(tmp, [
deepEqual(workspace, { o.job1,
"jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}": jIO.util. o.job2
uniqueJSONStringify([o.job2]) ], 'Job calls another job, workspace have two jobs');
}, 'First Job ended, second still there'); }, 150); // wait 50 ms
setTimeout(function () {
commands['1 Job Manage/get'].success({"data": {"c": "d"}}); commands['1 Job Manage/get'].end();
commands['2 Job Manage/get'].success({"data": {"d": "e"}}); tmp = workspace["jio/jobs/{\"id\":\"1 Job Manage\",\"type\":\"fake\"}"];
tmp = JSON.parse(tmp);
deepEqual(workspace, {}, 'No more job in the queue'); delete tmp[0].created;
delete tmp[0].modified;
clock.tick(1); // success 1 and 2 deepEqual(tmp, [o.job2], 'First Job ended, second still there');
commands['1 Job Manage/get'].success({"data": {"c": "d"}});
commands['2 Job Manage/get'].success({"data": {"d": "e"}});
deepEqual(workspace, {}, 'No more job in the queue');
}, 200); // wait 50 ms
}); });
/** test('job state running, job recovery', 2, function () {
* Test job recovery var workspace, jio;
*/
test('Job Recovery', function () {
expect(4);
var workspace, clock, jio;
clock = this.sandbox.useFakeTimers();
////////////////////////////// //////////////////////////////
// Running job recovery // Running job recovery
...@@ -748,13 +774,14 @@ ...@@ -748,13 +774,14 @@
"workspace": workspace "workspace": workspace
}); });
stop();
// create a job // create a job
jio.post({}); jio.post({});
// copy workspace when job is running // copy workspace when job is running
workspace = jIO.util.deepClone(workspace); workspace = jIO.util.deepClone(workspace);
clock.tick(1); // now: 1 ms setTimeout(function () {
commands['Job Recove/post'].success({"id": "a"}); commands['Job Recove/post'].success({"id": "a"});
}, 50);
// create instance with copied workspace // create instance with copied workspace
jio = new JIO({ jio = new JIO({
"type": "fake", "type": "fake",
...@@ -763,22 +790,29 @@ ...@@ -763,22 +790,29 @@
"workspace": workspace "workspace": workspace
}); });
clock.tick(19998); // now: 19999 ms setTimeout(function () {
if (commands['Job Recove/post']) { if (commands['Job Recove/post']) {
ok(false, "Command called, job recovered to earlier"); ok(false, "Command called, job recovered to earlier");
} }
clock.tick(1); // now: 20000 ms }, 19999);
if (!commands['Job Recove/post']) {
ok(false, "Command not called, job recovery failed");
} else {
ok(true, "Command called, job recovery ok");
}
commands['Job Recove/post'].success({"id": "a"});
clock.tick(1); // now: 20001 ms
deepEqual(workspace, {}, 'No more job in the queue'); setTimeout(function () {
clock.tick(79999); // now: 100000 ms if (!commands['Job Recove/post']) {
ok(false, "Command not called, job recovery failed");
} else {
ok(true, "Command called, job recovery ok");
}
commands['Job Recove/post'].success({"id": "a"});
}, 20050);
setTimeout(function () {
start();
deepEqual(workspace, {}, 'No more job in the queue');
}, 20100);
});
test('job state waiting for time, job recovery', 2, function () {
var workspace, jio;
////////////////////////////// //////////////////////////////
// Waiting for time job recovery // Waiting for time job recovery
...@@ -786,53 +820,60 @@ ...@@ -786,53 +820,60 @@
// create instance // create instance
jio = new JIO({ jio = new JIO({
"type": "fake", "type": "fake",
"id": "Job Recove" "id": "Job Recovw"
}, { }, {
"workspace": workspace "workspace": workspace
}); });
stop();
// create a job // create a job
jio.post({}); jio.post({});
clock.tick(1); // now: 1 ms setTimeout(function () {
// copy workspace when job is waiting // copy workspace when job is waiting
commands['Job Recove/post'].retry(); commands['Job Recovw/post'].retry();
workspace = jIO.util.deepClone(workspace); workspace = jIO.util.deepClone(workspace);
clock.tick(2000); // now: 2001 ms }, 50);
commands['Job Recove/post'].success({"id": "a"}); setTimeout(function () {
commands['Job Recovw/post'].success({"id": "a"});
}, 2100);
// create instance with copied workspace // create instance with copied workspace
jio = new JIO({ setTimeout(function () {
"type": "fake", jio = new JIO({
"id": "Job Recove" "type": "fake",
}, { "id": "Job Recovw"
"workspace": workspace }, {
}); "workspace": workspace
});
clock.tick(17999); // now: 20000 ms
if (commands['Job Recove/post']) {
ok(false, "Command called, job recovered to earlier");
}
clock.tick(1); // now: 20001 ms
if (!commands['Job Recove/post']) {
ok(false, "Command not called, job recovery failed");
} else {
ok(true, "Command called, job recovery ok");
}
commands['Job Recove/post'].success({"id": "a"});
clock.tick(1); // now: 20002 ms
deepEqual(workspace, {}, 'No more job in the queue'); setTimeout(function () {
clock.tick(79998); // now: 100000 ms if (commands['Job Recovw/post']) {
ok(false, "Command called, job recovered to earlier");
}
}, 19999);
setTimeout(function () {
if (!commands['Job Recovw/post']) {
ok(false, "Command not called, job recovery failed");
} else {
ok(true, "Command called, job recovery ok");
}
commands['Job Recovw/post'].success({"id": "a"});
}, 20050);
setTimeout(function () {
start();
deepEqual(workspace, {}, 'No more job in the queue');
}, 20100);
}, 51);
////////////////////////////// //////////////////////////////
// XXX Waiting for jobs job recovery // XXX Waiting for jobs job recovery
}); });
test('Job Update', function () { test('Job Update', 5, function () {
expect(5); var jio, o = {};
var clock, jio, o = {};
clock = this.sandbox.useFakeTimers();
o.workspace = {}; o.workspace = {};
jio = new JIO({ jio = new JIO({
...@@ -842,6 +883,7 @@ ...@@ -842,6 +883,7 @@
"workspace": o.workspace "workspace": o.workspace
}); });
stop();
jio.put({"_id": "a"}).always(function (answer) { jio.put({"_id": "a"}).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"id": "a", "id": "a",
...@@ -852,35 +894,39 @@ ...@@ -852,35 +894,39 @@
}, "First put respond"); }, "First put respond");
}); });
clock.tick(1); setTimeout(function () {
o.first_put_command = commands["Job Update/put"]; o.first_put_command = commands["Job Update/put"];
ok(o.first_put_command, "First command called"); ok(o.first_put_command, "First command called");
o.first_put_command.free(); o.first_put_command.free();
}, 50);
jio.put({"_id": "a"}).always(function (answer) { setTimeout(function () {
deepEqual(answer, { jio.put({"_id": "a"}).always(function (answer) {
"id": "a", deepEqual(answer, {
"method": "put", "id": "a",
"result": "success", "method": "put",
"status": 204, "result": "success",
"statusText": "No Content" "status": 204,
}, "Second put respond"); "statusText": "No Content"
}); }, "Second put respond");
});
}, 51);
clock.tick(1); setTimeout(function () {
ok(commands['Job Update/put'] === undefined, ok(commands['Job Update/put'] === undefined,
'Second command not called'); 'Second command not called');
o.first_put_command.success(); o.first_put_command.success();
clock.tick(1); }, 100);
deepEqual(o.workspace, {}, 'No job in the queue'); setTimeout(function () {
start();
deepEqual(o.workspace, {}, 'No job in the queue');
}, 150);
}); });
test('Job Wait', function () { test('Job Wait', 6, function () {
expect(6); var jio, o = {};
var clock, jio, o = {};
clock = this.sandbox.useFakeTimers();
o.workspace = {}; o.workspace = {};
jio = new JIO({ jio = new JIO({
...@@ -890,6 +936,7 @@ ...@@ -890,6 +936,7 @@
"workspace": o.workspace "workspace": o.workspace
}); });
stop();
jio.put({"_id": "a"}).always(function (answer) { jio.put({"_id": "a"}).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"id": "a", "id": "a",
...@@ -900,38 +947,43 @@ ...@@ -900,38 +947,43 @@
}, "First put respond"); }, "First put respond");
}); });
clock.tick(1); setTimeout(function () {
o.first_put_command = commands["Job Wait/put"]; o.first_put_command = commands["Job Wait/put"];
ok(o.first_put_command, "First command called"); ok(o.first_put_command, "First command called");
o.first_put_command.free(); o.first_put_command.free();
}, 50);
jio.put({"_id": "a", "a": "b"}).always(function (answer) {
deepEqual(answer, {
"id": "a",
"method": "put",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Second put respond");
});
clock.tick(1);
ok(commands['Job Wait/put'] === undefined,
'Second command not called yet');
o.first_put_command.success();
clock.tick(1);
ok(commands['Job Wait/put'], 'Second command called');
commands['Job Wait/put'].success();
clock.tick(1);
deepEqual(o.workspace, {}, 'No job in the queue');
setTimeout(function () {
jio.put({"_id": "a", "a": "b"}).always(function (answer) {
deepEqual(answer, {
"id": "a",
"method": "put",
"result": "success",
"status": 204,
"statusText": "No Content"
}, "Second put respond");
});
}, 51);
setTimeout(function () {
ok(commands['Job Wait/put'] === undefined,
'Second command not called yet');
o.first_put_command.success();
}, 100);
setTimeout(function () {
ok(commands['Job Wait/put'], 'Second command called');
commands['Job Wait/put'].success();
}, 150);
setTimeout(function () {
start();
deepEqual(o.workspace, {}, 'No job in the queue');
}, 200);
}); });
test('Job Deny + Job condition addition', function () { test('Job Deny + Job condition addition', 2, function () {
expect(2); var jio, workspace = {};
var clock, jio, workspace = {};
clock = this.sandbox.useFakeTimers();
jIO.addJobRuleCondition('isGetMethod', function (job) { jIO.addJobRuleCondition('isGetMethod', function (job) {
return job.method === 'get'; return job.method === 'get';
...@@ -950,6 +1002,7 @@ ...@@ -950,6 +1002,7 @@
}] }]
}); });
stop();
jio.get({"_id": "a"}).always(function (answer) { jio.get({"_id": "a"}).always(function (answer) {
deepEqual(answer, { deepEqual(answer, {
"error": "precondition_failed", "error": "precondition_failed",
...@@ -963,10 +1016,10 @@ ...@@ -963,10 +1016,10 @@
}, "Get respond"); }, "Get respond");
}); });
clock.tick(1); setTimeout(function () {
start();
deepEqual(workspace, {}, 'No job in the queue'); deepEqual(workspace, {}, 'No job in the queue');
}, 50);
}); });
})); }));
/*jslint indent: 2, maxlen: 80 */ /*jslint indent: 2, maxlen: 80 */
/*global define, exports, window, localStorage, ok, deepEqual, sinon */ /*global define, exports, window, require, localStorage, start, ok, deepEqual,
sinon */
(function (dependencies, module) { (function (dependencies, module) {
"use strict"; "use strict";
...@@ -7,13 +8,13 @@ ...@@ -7,13 +8,13 @@
return define(dependencies, module); return define(dependencies, module);
} }
if (typeof exports === 'object') { if (typeof exports === 'object') {
module(exports); module(exports, require('sinon_qunit'));
} }
if (typeof window === 'object') { if (typeof window === 'object') {
window.test_util = {}; window.test_util = {};
module(window.test_util); module(window.test_util);
} }
}(['exports'], function (exports) { }(['exports', 'sinon_qunit'], function (exports) {
"use strict"; "use strict";
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
<script src="../lib/qunit/qunit.js"></script> <script src="../lib/qunit/qunit.js"></script>
<script src="../lib/sinon/sinon.js"></script> <script src="../lib/sinon/sinon.js"></script>
<script src="../lib/sinon/sinon-qunit.js"></script> <script src="../lib/sinon/sinon-qunit.js"></script>
<script src="../src/promy/promy.js"></script> <script src="../lib/rsvp/rsvp-custom.js"></script>
<script src="../src/sha256.amd.js"></script> <script src="../src/sha256.amd.js"></script>
<script src="../jio.js"></script> <script src="../jio.js"></script>
<script src="jio/util.js"></script> <script src="jio/util.js"></script>
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
require.config({ require.config({
"paths": { "paths": {
"promy": "../src/promy/promy", "rsvp": "../lib/rsvp/rsvp-custom.amd",
"sha256": "../src/sha256.amd", "sha256": "../src/sha256.amd",
"jio": "../jio", "jio": "../jio",
"jio_tests": "jio/tests", "jio_tests": "jio/tests",
......
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