Commit 2722fe85 authored by Tristan Cavelier's avatar Tristan Cavelier

Update ReplicateStorage and Jio tests

parent bbf74f1f
...@@ -20,84 +20,82 @@ var newReplicateStorage = function ( spec, my ) { ...@@ -20,84 +20,82 @@ var newReplicateStorage = function ( spec, my ) {
return ''; return '';
}; };
priv.isTheLast = function () { priv.isTheLast = function (error_array) {
return (priv.return_value_array.length === priv.nb_storage); return (error_array.length === priv.nb_storage);
}; };
priv.doJob = function (command,errormessage) { priv.doJob = function (command,errormessage,nodocid) {
var done = false, error_array = [], i, var done = false, error_array = [], i,
onErrorDo = function (result) { error = function (err) {
priv.return_value_array.push(result);
if (!done) { if (!done) {
error_array.push(result); error_array.push(err);
if (priv.isTheLast()) { if (priv.isTheLast(error_array)) {
that.error ( that.error ({
{status:207, status:207,
statusText:'Multi-Status', statusText:'Multi-Status',
message:errormessage, error:'multi_status',
array:error_array}); message:'All '+errormessage+
(!nodocid?' "'+command.getDocId()+'"':' ') +
' requests have failed.',
reason:'requests fail',
array:error_array
});
} }
} }
}, },
onSuccessDo = function (result) { success = function (val) {
priv.return_value_array.push(result);
if (!done) { if (!done) {
done = true; done = true;
that.success (result); that.success (val);
} }
}; };
for (i = 0; i < priv.nb_storage; i+= 1) { for (i = 0; i < priv.nb_storage; i+= 1) {
var newcommand = command.clone(); var cloned_option = command.cloneOption();
var newstorage = that.newStorage(priv.storagelist[i]); that.addJob (command.getLabel(),priv.storagelist[i],
newcommand.onErrorDo (onErrorDo); command.cloneDoc(),cloned_option,success,error);
newcommand.onSuccessDo (onSuccessDo);
that.addJob (newstorage, newcommand);
} }
}; };
that.post = function (command) {
priv.doJob (command,'post');
that.end();
};
/** /**
* Save a document in several storages. * Save a document in several storages.
* @method saveDocument * @method put
*/ */
that.saveDocument = function (command) { that.put = function (command) {
priv.doJob ( priv.doJob (command,'put');
command,
'All save "'+ command.getPath() +'" requests have failed.');
that.end(); that.end();
}; };
/** /**
* Load a document from several storages, and send the first retreived * Load a document from several storages, and send the first retreived
* document. * document.
* @method loadDocument * @method get
*/ */
that.loadDocument = function (command) { that.get = function (command) {
priv.doJob ( priv.doJob (command,'get');
command,
'All load "'+ command.getPath() +'" requests have failed.');
that.end(); that.end();
}; };
/** /**
* Get a document list from several storages, and returns the first * Get a document list from several storages, and returns the first
* retreived document list. * retreived document list.
* @method getDocumentList * @method allDocs
*/ */
that.getDocumentList = function (command) { that.allDocs = function (command) {
priv.doJob ( priv.doJob (command,'allDocs',true);
command,
'All get document list requests have failed.');
that.end(); that.end();
}; };
/** /**
* Remove a document from several storages. * Remove a document from several storages.
* @method removeDocument * @method remove
*/ */
that.removeDocument = function (command) { that.remove = function (command) {
priv.doJob ( priv.doJob (command,'remove');
command,
'All remove "' + command.getPath() + '" requests have failed.');
that.end(); that.end();
}; };
......
...@@ -873,17 +873,17 @@ test ('Document load', function () { ...@@ -873,17 +873,17 @@ test ('Document load', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (message,doc,doc2) { o.mytest = function (message,doc,doc2) {
o.f = function (result) { o.f = function (err,val) {
var gooddoc = doc; var gooddoc = doc;
if (result && !result.status) { if (val) {
if (doc2 && result.content === doc2.content) { if (doc2 && val.content === doc2.content) {
gooddoc = doc2; gooddoc = doc2;
} }
} }
deepEqual (result,gooddoc,message); deepEqual (err || val,gooddoc,message);
}; };
o.t.spy(o,'f'); o.t.spy(o,'f');
o.jio.loadDocument('file',{success:o.f,error:o.f,max_retry:3}); o.jio.get('file',{max_retry:3},o.f);
o.clock.tick(10000); o.clock.tick(10000);
if (!o.f.calledOnce) { if (!o.f.calledOnce) {
if (o.f.called) { if (o.f.called) {
...@@ -897,9 +897,10 @@ test ('Document load', function () { ...@@ -897,9 +897,10 @@ test ('Document load', function () {
{type:'dummyallok',username:'1'}, {type:'dummyallok',username:'1'},
{type:'dummyallok',username:'2'}]}); {type:'dummyallok',username:'2'}]});
o.mytest('DummyStorageAllOK,OK: load same file',{ o.mytest('DummyStorageAllOK,OK: load same file',{
name:'file',content:'content', _id:'file',content:'content',
last_modified:15000, _last_modified:15000,
creation_date:10000}); _creation_date:10000
});
o.jio.stop(); o.jio.stop();
o.jio = JIO.newJio({type:'replicate',storagelist:[ o.jio = JIO.newJio({type:'replicate',storagelist:[
...@@ -907,13 +908,12 @@ test ('Document load', function () { ...@@ -907,13 +908,12 @@ test ('Document load', function () {
{type:'dummyallok'}]}); {type:'dummyallok'}]});
o.mytest('DummyStorageAllOK,3tries: load 2 different files', o.mytest('DummyStorageAllOK,3tries: load 2 different files',
{ {
name:'file',content:'content', _id:'file',content:'content',
last_modified:15000,creation_date:10000 _last_modified:15000,_creation_date:10000
},{ },{
name:'file',content:'content2', _id:'file',content:'content file',
last_modified:17000,creation_date:11000 _last_modified:17000,_creation_date:11000
}); });
o.jio.stop(); o.jio.stop();
}); });
...@@ -923,17 +923,14 @@ test ('Document save', function () { ...@@ -923,17 +923,14 @@ test ('Document save', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (message,value) { o.mytest = function (message,value) {
o.f = function (result) { o.f = function (err,val) {
if (!result) { if (err) {
result = 'done'; err = err.status;
} else {
result = 'fail';
} }
deepEqual (result,value,message); deepEqual (err || val,value,message);
}; };
o.t.spy(o,'f'); o.t.spy(o,'f');
o.jio.saveDocument('file','content',{ o.jio.put({_id:'file',content:'content'},{max_retry:3},o.f);
success:o.f,error:o.f,max_retry:3});
o.clock.tick(500); o.clock.tick(500);
if (!o.f.calledOnce) { if (!o.f.calledOnce) {
if (o.f.called) { if (o.f.called) {
...@@ -946,13 +943,13 @@ test ('Document save', function () { ...@@ -946,13 +943,13 @@ test ('Document save', function () {
o.jio = JIO.newJio({type:'replicate',storagelist:[ o.jio = JIO.newJio({type:'replicate',storagelist:[
{type:'dummyallok',username:'1'}, {type:'dummyallok',username:'1'},
{type:'dummyallok',username:'2'}]}); {type:'dummyallok',username:'2'}]});
o.mytest('DummyStorageAllOK,OK: save a file.','done'); o.mytest('DummyStorageAllOK,OK: save a file.',{ok:true,id:'file'});
o.jio.stop(); o.jio.stop();
o.jio = JIO.newJio({type:'replicate',storagelist:[ o.jio = JIO.newJio({type:'replicate',storagelist:[
{type:'dummyall3tries',username:'1'}, {type:'dummyall3tries',username:'1'},
{type:'dummyallok',username:'2'}]}); {type:'dummyallok',username:'2'}]});
o.mytest('DummyStorageAll3Tries,OK: save a file.','done'); o.mytest('DummyStorageAll3Tries,OK: save a file.',{ok:true,id:'file'});
o.jio.stop(); o.jio.stop();
}); });
...@@ -962,12 +959,12 @@ test ('Get Document List', function () { ...@@ -962,12 +959,12 @@ test ('Get Document List', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (message,value) { o.mytest = function (message,value) {
o.f = function (result) { o.f = function (err,val) {
deepEqual (objectifyDocumentArray(result), deepEqual (err || objectifyDocumentArray(val.rows),
objectifyDocumentArray(value),message); objectifyDocumentArray(value),message);
}; };
o.t.spy(o,'f'); o.t.spy(o,'f');
o.jio.getDocumentList('.',{success:o.f,error:o.f,max_retry:3}); o.jio.allDocs({max_retry:3},o.f);
o.clock.tick(10000); o.clock.tick(10000);
if (!o.f.calledOnce) { if (!o.f.calledOnce) {
if (o.f.called) { if (o.f.called) {
...@@ -980,10 +977,10 @@ test ('Get Document List', function () { ...@@ -980,10 +977,10 @@ test ('Get Document List', function () {
o.jio = JIO.newJio({type:'replicate',storagelist:[ o.jio = JIO.newJio({type:'replicate',storagelist:[
{type:'dummyall3tries',username:'1'}, {type:'dummyall3tries',username:'1'},
{type:'dummyallok',username:'2'}]}); {type:'dummyallok',username:'2'}]});
o.doc1 = {name:'file', o.doc1 = {id:'file',key:'file',value:{
last_modified:15000,creation_date:10000}; _last_modified:15000,_creation_date:10000}};
o.doc2 = {name:'memo', o.doc2 = {id:'memo',key:'memo',value:{
last_modified:25000,creation_date:20000}; _last_modified:25000,_creation_date:20000}};
o.mytest('DummyStorageAllOK,3tries: get document list.', o.mytest('DummyStorageAllOK,3tries: get document list.',
[o.doc1,o.doc2]); [o.doc1,o.doc2]);
o.jio.stop(); o.jio.stop();
...@@ -1002,16 +999,14 @@ test ('Remove document', function () { ...@@ -1002,16 +999,14 @@ test ('Remove document', function () {
var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this; var o = {}; o.clock = this.sandbox.useFakeTimers(); o.t = this;
o.clock.tick(base_tick); o.clock.tick(base_tick);
o.mytest = function (message,value) { o.mytest = function (message,value) {
o.f = function (result) { o.f = function (err,val) {
if (!result) { if (err) {
result = 'done'; err = err.status;
} else {
result = 'fail';
} }
deepEqual (result,value,message); deepEqual (err || val,value,message);
}; };
o.t.spy(o,'f'); o.t.spy(o,'f');
o.jio.removeDocument('file',{success:o.f,error:o.f,max_retry:3}); o.jio.remove({_id:'file'},{max_retry:3},o.f);
o.clock.tick(10000); o.clock.tick(10000);
if (!o.f.calledOnce) { if (!o.f.calledOnce) {
if (o.f.called) { if (o.f.called) {
...@@ -1024,7 +1019,7 @@ test ('Remove document', function () { ...@@ -1024,7 +1019,7 @@ test ('Remove document', function () {
o.jio = JIO.newJio({type:'replicate',storagelist:[ o.jio = JIO.newJio({type:'replicate',storagelist:[
{type:'dummyallok',username:'1'}, {type:'dummyallok',username:'1'},
{type:'dummyall3tries',username:'2'}]}); {type:'dummyall3tries',username:'2'}]});
o.mytest('DummyStorageAllOK,3tries: remove document.','done'); o.mytest('DummyStorageAllOK,3tries: remove document.',{ok:true,id:'file'});
o.jio.stop(); o.jio.stop();
}); });
......
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