Add async API to context

and add test cases for async
This commit is contained in:
HirokiUchikawa 2018-06-07 21:51:11 +09:00
parent ed1d34e678
commit 41a04a2849
4 changed files with 420 additions and 222 deletions

View File

@ -154,30 +154,45 @@ function createContext(id,seed) {
obj.get = function(key) { obj.get = function(key) {
var keyPath = parseKey(key); var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage); var context = getContextStorage(keyPath.storage);
if(key === keyPath.key){ if(!keyPath.storage){
return context.get(scope, keyPath.key); return context.get(scope, keyPath.key);
}else{ }else{
return context.getAsync(scope, keyPath.key); throw new Error(keyPath.storage + " does not support get(). Use getAsync()");
} }
}; };
obj.set = function(key, value) { obj.set = function(key, value) {
var keyPath = parseKey(key); var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage); var context = getContextStorage(keyPath.storage);
if(key === keyPath.key){ if(!keyPath.storage){
return context.set(scope, keyPath.key, value); return context.set(scope, keyPath.key, value);
}else{ }else{
return context.setAsync(scope, keyPath.key, value); throw new Error(keyPath.storage + " does not support set(). Use setAsync()");
} }
}; };
obj.keys = function(storage) { obj.keys = function(storage) {
var storageName = parseStorage(storage); var storageName = parseStorage(storage);
var context = getContextStorage(storageName); var context = getContextStorage(storageName);
if(!storage){ if(!storageName){
return context.keys(scope); return context.keys(scope);
}else{ }else{
return context.keysAsync(scope); throw new Error(storageName + " does not support keys(). Use keysAsync()");
} }
}; };
obj.getAsync = function(key) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.getAsync(scope, keyPath.key);
};
obj.setAsync = function(key, value) {
var keyPath = parseKey(key);
var context = getContextStorage(keyPath.storage);
return context.setAsync(scope, keyPath.key, value);
};
obj.keysAsync = function(storage) {
var storageName = parseStorage(storage);
var context = getContextStorage(storageName);
return context.keysAsync(scope);
};
return obj; return obj;
} }

View File

@ -16,6 +16,7 @@
var should = require("should"); var should = require("should");
var sinon = require('sinon'); var sinon = require('sinon');
var when = require("when")
var rewire = require("rewire"); var rewire = require("rewire");
var Context = require("../../../../../red/runtime/nodes/context/index"); var Context = require("../../../../../red/runtime/nodes/context/index");
@ -33,7 +34,7 @@ describe('context', function() {
var context1 = Context.get("1","flowA"); var context1 = Context.get("1","flowA");
should.not.exist(context1.get("foo")); should.not.exist(context1.get("foo"));
context1.set("foo","test"); context1.set("foo","test");
context1.get("foo").should.eql("test"); context1.get("foo").should.equal("test");
}); });
it('stores local property - creates parent properties',function() { it('stores local property - creates parent properties',function() {
var context1 = Context.get("1","flowA"); var context1 = Context.get("1","flowA");
@ -56,13 +57,13 @@ describe('context', function() {
var context1 = Context.get("1","flowA"); var context1 = Context.get("1","flowA");
should.not.exist(context1.flow.get("foo")); should.not.exist(context1.flow.get("foo"));
context1.flow.set("foo","test"); context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test"); context1.flow.get("foo").should.equal("test");
}); });
it('stores global property',function() { it('stores global property',function() {
var context1 = Context.get("1","flowA"); var context1 = Context.get("1","flowA");
should.not.exist(context1.global.get("foo")); should.not.exist(context1.global.get("foo"));
context1.global.set("foo","test"); context1.global.set("foo","test");
context1.global.get("foo").should.eql("test"); context1.global.get("foo").should.equal("test");
}); });
it('keeps local context local', function() { it('keeps local context local', function() {
@ -73,7 +74,7 @@ describe('context', function() {
should.not.exist(context2.get("foo")); should.not.exist(context2.get("foo"));
context1.set("foo","test"); context1.set("foo","test");
context1.get("foo").should.eql("test"); context1.get("foo").should.equal("test");
should.not.exist(context2.get("foo")); should.not.exist(context2.get("foo"));
}); });
it('flow context accessible to all flow nodes', function() { it('flow context accessible to all flow nodes', function() {
@ -84,8 +85,8 @@ describe('context', function() {
should.not.exist(context2.flow.get("foo")); should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test"); context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test"); context1.flow.get("foo").should.equal("test");
context2.flow.get("foo").should.eql("test"); context2.flow.get("foo").should.equal("test");
}); });
it('flow context not shared to nodes on other flows', function() { it('flow context not shared to nodes on other flows', function() {
@ -96,7 +97,7 @@ describe('context', function() {
should.not.exist(context2.flow.get("foo")); should.not.exist(context2.flow.get("foo"));
context1.flow.set("foo","test"); context1.flow.set("foo","test");
context1.flow.get("foo").should.eql("test"); context1.flow.get("foo").should.equal("test");
should.not.exist(context2.flow.get("foo")); should.not.exist(context2.flow.get("foo"));
}); });
@ -108,15 +109,15 @@ describe('context', function() {
should.not.exist(context2.global.get("foo")); should.not.exist(context2.global.get("foo"));
context1.global.set("foo","test"); context1.global.set("foo","test");
context1.global.get("foo").should.eql("test"); context1.global.get("foo").should.equal("test");
context2.global.get("foo").should.eql("test"); context2.global.get("foo").should.equal("test");
}); });
it('deletes context',function() { it('deletes context',function() {
var context = Context.get("1","flowA"); var context = Context.get("1","flowA");
should.not.exist(context.get("foo")); should.not.exist(context.get("foo"));
context.set("foo","abc"); context.set("foo","abc");
context.get("foo").should.eql("abc"); context.get("foo").should.equal("abc");
return Context.delete("1","flowA").then(function(){ return Context.delete("1","flowA").then(function(){
context = Context.get("1","flowA"); context = Context.get("1","flowA");
@ -134,12 +135,12 @@ describe('context', function() {
context.set("foo","bar"); context.set("foo","bar");
keys = context.keys(); keys = context.keys();
keys.should.have.length(1); keys.should.have.length(1);
keys[0].should.eql("foo"); keys[0].should.equal("foo");
context.set("abc.def","bar"); context.set("abc.def","bar");
keys = context.keys(); keys = context.keys();
keys.should.have.length(2); keys.should.have.length(2);
keys[1].should.eql("abc"); keys[1].should.equal("abc");
}); });
it('should enumerate only context keys when GlobalContext was given', function() { it('should enumerate only context keys when GlobalContext was given', function() {
@ -148,42 +149,45 @@ describe('context', function() {
var context = Context.get("1","flowA"); var context = Context.get("1","flowA");
var keys = context.global.keys("global"); var keys = context.global.keys("global");
keys.should.have.length(1); keys.should.have.length(1);
keys[0].should.eql("foo"); keys[0].should.equal("foo");
}); });
}); });
it('should store data on memory when contextStorage is not defined', function() { it('should throw error when persistable key is passed', function() {
var context = Context.get("1","flow"); var context = Context.get("1","flow");
context.set("#nonexist.key1", "val1"); (function() {
context.get("#nonexist.key1").should.eql("val1"); context.set("#nonexist.key1", "val1");
context.flow.set("#nonexist.key2", "val2"); }).should.throw();
context.flow.get("#nonexist.key2").should.eql("val2"); (function() {
context.global.set("#nonexist.key1", "val3"); context.get("#nonexist.key1");
context.global.get("#nonexist.key1").should.eql("val3"); }).should.throw();
(function() {
context.keys("#nonexist");
}).should.throw();
}); });
}); });
describe('external context storage',function() { describe('external context storage',function() {
var sandbox = sinon.sandbox.create(); var sandbox = sinon.sandbox.create();
var stubGet = sandbox.stub(); var stubGetAsync = sandbox.stub().returns(when.resolve());
var stubSet = sandbox.stub(); var stubSetAsync = sandbox.stub().returns(when.resolve());
var stubKeys = sandbox.stub(); var stubKeysAsync = sandbox.stub().returns(when.resolve());
var stubDelete = sandbox.stub().returns(Promise.resolve()); var stubDelete = sandbox.stub().returns(when.resolve());
var stubClean = sandbox.stub().returns(Promise.resolve()); var stubClean = sandbox.stub().returns(when.resolve());
var stubOpen = sandbox.stub().returns(Promise.resolve()); var stubOpen = sandbox.stub().returns(when.resolve());
var stubClose = sandbox.stub().returns(Promise.resolve()); var stubClose = sandbox.stub().returns(when.resolve());
var stubGet2 = sandbox.stub(); var stubGetAsync2 = sandbox.stub().returns(when.resolve());
var stubSet2 = sandbox.stub(); var stubSetAsync2 = sandbox.stub().returns(when.resolve());
var stubKeys2 = sandbox.stub(); var stubKeysAsync2 = sandbox.stub().returns(when.resolve());
var stubDelete2 = sandbox.stub().returns(Promise.resolve()); var stubDelete2 = sandbox.stub().returns(when.resolve());
var stubClean2 = sandbox.stub().returns(Promise.resolve()); var stubClean2 = sandbox.stub().returns(when.resolve());
var stubOpen2 = sandbox.stub().returns(Promise.resolve()); var stubOpen2 = sandbox.stub().returns(when.resolve());
var stubClose2 = sandbox.stub().returns(Promise.resolve()); var stubClose2 = sandbox.stub().returns(when.resolve());
var testPlugin = function(config){ var testPlugin = function(config){
function Test(){} function Test(){}
Test.prototype.get = stubGet; Test.prototype.getAsync = stubGetAsync;
Test.prototype.set = stubSet; Test.prototype.setAsync = stubSetAsync;
Test.prototype.keys = stubKeys; Test.prototype.keysAsync = stubKeysAsync;
Test.prototype.delete = stubDelete; Test.prototype.delete = stubDelete;
Test.prototype.clean = stubClean; Test.prototype.clean = stubClean;
Test.prototype.open = stubOpen; Test.prototype.open = stubOpen;
@ -192,9 +196,9 @@ describe('context', function() {
}; };
var testPlugin2 = function(config){ var testPlugin2 = function(config){
function Test2(){} function Test2(){}
Test2.prototype.get = stubGet2; Test2.prototype.getAsync = stubGetAsync2;
Test2.prototype.set = stubSet2; Test2.prototype.setAsync = stubSetAsync2;
Test2.prototype.keys = stubKeys2; Test2.prototype.keysAsync = stubKeysAsync2;
Test2.prototype.delete = stubDelete2; Test2.prototype.delete = stubDelete2;
Test2.prototype.clean = stubClean2; Test2.prototype.clean = stubClean2;
Test2.prototype.open = stubOpen2; Test2.prototype.open = stubOpen2;
@ -251,24 +255,32 @@ describe('context', function() {
}); });
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
context.set("##%&.sign","sign1"); return when.all([
context.get("##%&.sign").should.eql("sign1"); context.setAsync("##%&.sign","sign1").then(function(){
context.set("#\u3042.file2","file2"); return context.getAsync("##%&.sign").should.finally.equal("sign1");
context.get("#\u3042.file2").should.eql("file2"); }),
context.set("#1.num","num3"); context.setAsync("#\u3042.file2","file2").then(function(){
context.get("#1.num").should.eql("num3"); return context.getAsync("#\u3042.file2").should.finally.equal("file2");
}),
context.setAsync("#1.num","num3").then(function(){
return context.getAsync("#1.num").should.finally.equal("num3");
})
]);
}); });
}); });
it('should ignore reserved storage name `_`', function() { it('should ignore reserved storage name `_`', function() {
Context.init({contextStorage:{_:{module:testPlugin}}}); Context.init({contextStorage:{_:{module:testPlugin}}});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
context.set("#_.foo","bar"); return when.all([
context.get("#_.foo"); context.setAsync("#_.foo","bar"),
context.keys("#_"); context.getAsync("#_.foo"),
stubSet.called.should.be.false(); context.keysAsync("#_")
stubGet.called.should.be.false(); ]).then(function(){
stubKeys.called.should.be.false(); stubSetAsync.called.should.be.false();
stubGetAsync.called.should.be.false();
stubKeysAsync.called.should.be.false();
});
}); });
}); });
it('should fail when using invalid default context', function(done) { it('should fail when using invalid default context', function(done) {
@ -314,94 +326,106 @@ describe('context', function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.get("#test.foo")); return when.all([
context.set("#test.foo","test"); context.setAsync("#test.foo","test"),
context.get("#test.foo"); context.getAsync("#test.foo"),
context.keys("#test"); context.keysAsync("#test")
stubSet.calledWithExactly("1:flow","foo","test").should.be.true(); ]).then(function(){
stubGet.calledWithExactly("1:flow","foo").should.be.true(); stubSetAsync.calledWithExactly("1:flow","foo","test").should.be.true();
stubKeys.calledWithExactly("1:flow").should.be.true(); stubGetAsync.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync.calledWithExactly("1:flow").should.be.true();
});
}); });
}); });
it('should store flow property to external context storage',function() { it('should store flow property to external context storage',function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.flow.get("#test.foo")); return when.all([
context.flow.set("#test.foo","test"); context.flow.setAsync("#test.foo","test"),
context.flow.get("#test.foo"); context.flow.getAsync("#test.foo"),
context.flow.keys("#test"); context.flow.keysAsync("#test")
stubSet.calledWithExactly("flow","foo","test").should.be.true(); ]).then(function(){
stubGet.calledWithExactly("flow","foo").should.be.true(); stubSetAsync.calledWithExactly("flow","foo","test").should.be.true();
stubKeys.calledWithExactly("flow").should.be.true(); stubGetAsync.calledWithExactly("flow","foo").should.be.true();
stubKeysAsync.calledWithExactly("flow").should.be.true();
});
}); });
}); });
it('should store global property to external context storage',function() { it('should store global property to external context storage',function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.global.get("#test.foo")); return when.all([
context.global.set("#test.foo","test"); context.global.setAsync("#test.foo","test"),
context.global.get("#test.foo"); context.global.getAsync("#test.foo"),
context.global.keys("#test"); context.global.keysAsync("#test")
stubSet.calledWithExactly("global","foo","test").should.be.true(); ]).then(function(){
stubGet.calledWithExactly("global","foo").should.be.true(); stubSetAsync.calledWithExactly("global","foo","test").should.be.true();
stubKeys.calledWithExactly("global").should.be.true(); stubGetAsync.calledWithExactly("global","foo").should.be.true();
stubKeysAsync.calledWithExactly("global").should.be.true();
});
}); });
}); });
it('should store data to the default context when non-existent context storage was specified', function() { it('should store data to the default context when non-existent context storage was specified', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.get("#nonexist.foo")); return when.all([
context.set("#nonexist.foo","test"); context.setAsync("#nonexist.foo","test"),
context.get("#nonexist.foo"); context.getAsync("#nonexist.foo"),
context.keys("#nonexist"); context.keysAsync("#nonexist")
stubGet.called.should.be.false(); ]).then(function(){
stubSet.called.should.be.false(); stubGetAsync.called.should.be.false();
stubKeys.called.should.be.false(); stubSetAsync.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","test").should.be.true(); stubKeysAsync.called.should.be.false();
stubGet2.calledWithExactly("1:flow","foo").should.be.true(); stubSetAsync2.calledWithExactly("1:flow","foo","test").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true(); stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
}); });
}); });
it('should use the default context', function() { it('should use the default context', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.get("#default.foo")); return when.all([
context.set("#default.foo","default"); context.setAsync("#default.foo","default"),
context.get("#default.foo"); context.getAsync("#default.foo"),
context.keys("#default"); context.keysAsync("#default")
stubGet.called.should.be.false(); ]).then(function(){
stubSet.called.should.be.false(); stubGetAsync.called.should.be.false();
stubKeys.called.should.be.false(); stubSetAsync.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","default").should.be.true(); stubKeysAsync.called.should.be.false();
stubGet2.calledWithExactly("1:flow","foo").should.be.true(); stubSetAsync2.calledWithExactly("1:flow","foo","default").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true(); stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
}); });
}); });
it('should use the alias of default context', function() { it('should use the alias of default context', function() {
Context.init({contextStorage:contextDefaultStorage}); Context.init({contextStorage:contextDefaultStorage});
return Context.load().then(function(){ return Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
should.not.exist(context.get("#.foo")); return when.all([
context.set("#.foo","alias"); context.setAsync("#.foo","alias"),
context.get("#.foo"); context.getAsync("#.foo"),
context.keys("#"); context.keysAsync("#")
stubGet.called.should.be.false(); ]).then(function(){
stubSet.called.should.be.false(); stubGetAsync.called.should.be.false();
stubKeys.called.should.be.false(); stubSetAsync.called.should.be.false();
stubSet2.calledWithExactly("1:flow","foo","alias").should.be.true(); stubKeysAsync.called.should.be.false();
stubGet2.calledWithExactly("1:flow","foo").should.be.true(); stubSetAsync2.calledWithExactly("1:flow","foo","alias").should.be.true();
stubKeys2.calledWithExactly("1:flow").should.be.true(); stubGetAsync2.calledWithExactly("1:flow","foo").should.be.true();
stubKeysAsync2.calledWithExactly("1:flow").should.be.true();
});
}); });
}); });
it('should throw an error using undefined storage for local context', function(done) { it('should throw an error using undefined storage for local context', function(done) {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
Context.load().then(function(){ Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
context.get("#nonexist.local"); context.getAsync("#nonexist.local");
should.fail(null, null, "An error was not thrown using undefined storage for local context"); should.fail(null, null, "An error was not thrown using undefined storage for local context");
}).catch(function(err) { }).catch(function(err) {
if (err.name === "ContextError") { if (err.name === "ContextError") {
@ -415,7 +439,7 @@ describe('context', function() {
Context.init({contextStorage:contextStorage}); Context.init({contextStorage:contextStorage});
Context.load().then(function(){ Context.load().then(function(){
var context = Context.get("1","flow"); var context = Context.get("1","flow");
context.flow.set("#nonexist.flow"); context.flow.setAsync("#nonexist.flow");
should.fail(null, null, "An error was not thrown using undefined storage for flow context"); should.fail(null, null, "An error was not thrown using undefined storage for flow context");
}).catch(function(err) { }).catch(function(err) {
if (err.name === "ContextError") { if (err.name === "ContextError") {
@ -464,32 +488,36 @@ describe('context', function() {
return Context.close(); return Context.close();
}); });
it('should work correctly with the valid key name',function() { it('should work correctly with the valid key name',function() {
context.set("#memory.azAZ09#_","valid"); return when.all([
context.get("#memory.azAZ09#_").should.eql("valid"); context.setAsync("#memory.azAZ09#_","valid"),
context.set("#memory.a.b","ab"); context.setAsync("#memory.a.b","ab")
context.get("#memory.a.b").should.eql("ab"); ]).then(function(){
context.getAsync("#memory.azAZ09#_").should.finally.equal("valid");
context.getAsync("#memory.a.b").should.finally.equal("ab");
});
}); });
it('should treat the key name without dot as a normal context',function() { it('should treat the key name without dot as a normal context',function() {
context.set("#memory","normal"); return context.setAsync("#memory","normal").then(function(){
context.get("#memory").should.eql("normal"); return context.getAsync("#memory").should.finally.equal("normal");
});
}); });
it('should fail when specifying invalid characters',function() { it('should fail when specifying invalid characters',function() {
(function() { (function() {
context.set("#memory.a.-","invalid1"); context.setAsync("#memory.a.-","invalid1");
}).should.throw(); }).should.throw();
(function() { (function() {
context.set("#memory.'abc","invalid2"); context.setAsync("#memory.'abc","invalid2");
}).should.throw(); }).should.throw();
}); });
it('should fail when specifying unnecesary space characters for key name',function() { it('should fail when specifying unnecesary space characters for key name',function() {
(function() { (function() {
context.set("# memory.space","space1"); context.setAsync("# memory.space","space1");
}).should.throw(); }).should.throw();
(function() { (function() {
context.set("#memory .space","space2"); context.setAsync("#memory .space","space2");
}).should.throw(); }).should.throw();
(function() { (function() {
context.set("#memory. space","space3"); context.setAsync("#memory. space","space3");
}).should.throw(); }).should.throw();
}); });
}); });
@ -500,8 +528,8 @@ describe('context', function() {
function returnModuleAndKey(input, expectedModule, expectedKey) { function returnModuleAndKey(input, expectedModule, expectedKey) {
var result = parseKey(input); var result = parseKey(input);
result.storage.should.eql(expectedModule); result.storage.should.equal(expectedModule);
result.key.should.eql(expectedKey); result.key.should.equal(expectedKey);
} }
it('should return module and key', function() { it('should return module and key', function() {

View File

@ -38,120 +38,275 @@ describe('localfilesystem',function() {
}); });
}); });
describe('#get/set',function() { describe('#getAsync/setAsync',function() {
it('should store property',function() { it('should store property',function() {
should.not.exist(context.get("nodeX","foo")); return context.getAsync("nodeX","foo").should.be.finally.undefined()
context.set("nodeX","foo","test"); .then(function(){
context.get("nodeX","foo").should.eql("test"); return context.setAsync("nodeX","foo","test");
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.equal("test");
});
}); });
it('should store property - creates parent properties',function() { it('should store property - creates parent properties',function() {
context.set("nodeX","foo.bar","test"); return context.setAsync("nodeX","foo.bar","test").then(function(){
context.get("nodeX","foo").should.eql({bar:"test"}); return context.getAsync("nodeX","foo").should.be.finally.eql({bar:"test"});
});
}); });
it('should delete property',function() { it('should delete property',function() {
context.set("nodeX","foo.abc.bar1","test1"); return context.setAsync("nodeX","foo.abc.bar1","test1")
context.set("nodeX","foo.abc.bar2","test2"); .then(function(){
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"}); return context.setAsync("nodeX","foo.abc.bar2","test2")
context.set("nodeX","foo.abc.bar1",undefined); }).then(function(){
context.get("nodeX","foo.abc").should.eql({bar2:"test2"}); return context.getAsync("nodeX","foo.abc").should.be.finally.eql({bar1:"test1",bar2:"test2"});
context.set("nodeX","foo.abc",undefined); }).then(function(){
should.not.exist(context.get("nodeX","foo.abc")); return context.setAsync("nodeX","foo.abc.bar1",undefined).then(function(){
context.set("nodeX","foo",undefined); return context.getAsync("nodeX","foo.abc").should.be.finally.eql({bar2:"test2"});
should.not.exist(context.get("nodeX","foo")); });
}).then(function(){
return context.setAsync("nodeX","foo.abc",undefined).then(function(){
return context.getAsync("nodeX","foo.abc").should.be.finally.undefined();
});
}).then(function(){
return context.setAsync("nodeX","foo",undefined).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.undefined();
});
});
}); });
it('should not shared context with other scope', function() { it('should not shared context with other scope', function() {
should.not.exist(context.get("nodeX","foo")); return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
should.not.exist(context.get("nodeY","foo")); context.getAsync("nodeY","foo").should.be.finally.undefined()
context.set("nodeX","foo","testX"); ]).then(function(){
context.set("nodeY","foo","testY"); return when.all([context.setAsync("nodeX","foo","testX"),
context.setAsync("nodeY","foo","testY")])
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("testX"),
context.getAsync("nodeY","foo").should.be.finally.equal("testY")]);
});
});
context.get("nodeX","foo").should.eql("testX"); it('should store string',function() {
context.get("nodeY","foo").should.eql("testY"); return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo","bar");
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("bar");
}).then(function(){
return context.setAsync("nodeX","foo","1");
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.String();
result.should.be.equal("1");
});
});
it('should store number',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",1);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Number();
result.should.be.equal(1);
});
});
it('should store null',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",null);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.null();
});
});
it('should store boolean',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",true);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.Boolean().and.true();
}).then(function(){
return context.setAsync("nodeX","foo",false);
}).then(function(){
return context.getAsync("nodeX","foo").should.be.finally.Boolean().and.false();
});
});
it('should store object',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",{obj:"bar"});
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Object();
result.should.eql({obj:"bar"});
});
});
it('should store array',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",["a","b","c"]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.eql(["a","b","c"]);
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.String();
result.should.equal("b");
});
});
it('should store array of arrays',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.have.length(3);
result[0].should.have.length(3);
result[1].should.have.length(4);
result[2].should.have.length(2);
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.Array();
result.should.have.length(4);
result.should.be.eql([1,2,3,4]);
});
});
it('should store array of objects',function() {
return context.getAsync("nodeX","foo").should.be.finally.undefined()
.then(function(){
return context.setAsync("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}]);
}).then(function(){
return context.getAsync("nodeX","foo")
}).then(function(result){
result.should.be.Array();
result.should.have.length(3);
result[0].should.be.Object();
result[1].should.be.Object();
result[2].should.be.Object();
}).then(function(){
return context.getAsync("nodeX","foo[1]")
}).then(function(result){
result.should.be.Object();
result.should.be.eql({obj:"bar2"});
});
}); });
}); });
describe('#keys',function() { describe('#keysAsync',function() {
it('should enumerate context keys', function() { it('should enumerate context keys', function() {
var keys = context.keys("nodeX"); return context.keysAsync("nodeX").then(function(result){
keys.should.be.an.Array(); result.should.be.an.Array();
keys.should.be.empty(); result.should.be.empty();
}).then(function(){
context.set("nodeX","foo","bar"); return context.setAsync("nodeX","foo","bar");
keys = context.keys("nodeX"); }).then(function(){
keys.should.have.length(1); return context.keysAsync("nodeX").then(function(result){
keys[0].should.eql("foo"); result.should.have.length(1);
result[0].should.equal("foo");
context.set("nodeX","abc.def","bar"); });
keys = context.keys("nodeX"); }).then(function(){
keys.should.have.length(2); return context.setAsync("nodeX","abc.def","bar");
keys[1].should.eql("abc"); }).then(function(){
return context.keysAsync("nodeX").then(function(result){
result.should.have.length(2);
result[1].should.equal("abc");
});
});
}); });
it('should enumerate context keys in each scopes', function() { it('should enumerate context keys in each scopes', function() {
var keysX = context.keys("nodeX"); return when.all([context.keysAsync("nodeX"),
keysX.should.be.an.Array(); context.keysAsync("nodeY")
keysX.should.be.empty(); ]).then(function(results){
results[0].should.be.an.Array();
var keysY = context.keys("nodeY"); results[0].should.be.empty();
keysY.should.be.an.Array(); results[1].should.be.an.Array();
keysY.should.be.empty(); results[1].should.be.empty();
}).then(function(){
context.set("nodeX","foo","bar"); return when.all([context.setAsync("nodeX","foo","bar"),
context.set("nodeY","hoge","piyo"); context.setAsync("nodeY","hoge","piyo")]);
keysX = context.keys("nodeX"); }).then(function(){
keysX.should.have.length(1); return when.all([context.keysAsync("nodeX"),
keysX[0].should.eql("foo"); context.keysAsync("nodeY")]);
}).then(function(results){
keysY = context.keys("nodeY"); results[0].should.have.length(1);
keysY.should.have.length(1); results[0][0].should.equal("foo");
keysY[0].should.eql("hoge"); results[1].should.have.length(1);
results[1][0].should.equal("hoge");
});
}); });
}); });
describe('#delete',function() { describe('#delete',function() {
it('should delete context',function() { it('should delete context',function() {
should.not.exist(context.get("nodeX","foo")); return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
should.not.exist(context.get("nodeY","foo")); context.getAsync("nodeY","foo").should.be.finally.undefined()
context.set("nodeX","foo","abc"); ]).then(function(){
context.set("nodeY","foo","abc"); return when.all([context.setAsync("nodeX","foo","abc"),
context.get("nodeX","foo").should.eql("abc"); context.setAsync("nodeY","foo","abc")]);
context.get("nodeY","foo").should.eql("abc"); }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
return context.delete("nodeX").then(function(){ context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
should.not.exist(context.get("nodeX","foo")); }).then(function(){
should.exist(context.get("nodeY","foo")); return context.delete("nodeX");
}) }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")]);
});
}); });
}); });
describe('#clean',function() { describe('#clean',function() {
it('should clean unnecessary context',function() { it('should clean unnecessary context',function() {
should.not.exist(context.get("nodeX","foo")); return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
should.not.exist(context.get("nodeY","foo")); context.getAsync("nodeY","foo").should.be.finally.undefined()
context.set("nodeX","foo","abc"); ]).then(function(values){
context.set("nodeY","foo","abc"); return when.all([context.setAsync("nodeX","foo","abc"),
context.get("nodeX","foo").should.eql("abc"); context.setAsync("nodeY","foo","abc")]);
context.get("nodeY","foo").should.eql("abc"); }).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
return context.clean([]).then(function(){ context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
should.not.exist(context.get("nodeX","foo")); }).then(function(){
should.not.exist(context.get("nodeY","foo")); return context.clean([]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
}); });
}); });
it('should not clean active context',function() {
should.not.exist(context.get("nodeX","foo"));
should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc");
context.get("nodeY","foo").should.eql("abc");
return context.clean(["nodeX"]).then(function(){ it('should not clean active context',function() {
should.exist(context.get("nodeX","foo")); return when.all([context.getAsync("nodeX","foo").should.be.finally.undefined(),
should.not.exist(context.get("nodeY","foo")); context.getAsync("nodeY","foo").should.be.finally.undefined()
]).then(function(){
return when.all([context.setAsync("nodeX","foo","abc"),
context.setAsync("nodeY","foo","abc")]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.equal("abc")])
}).then(function(){
return context.clean(["nodeX"]);
}).then(function(){
return when.all([context.getAsync("nodeX","foo").should.be.finally.equal("abc"),
context.getAsync("nodeY","foo").should.be.finally.undefined()]);
}); });
}); });
}); });

View File

@ -35,7 +35,7 @@ describe('memory',function() {
it('should store property',function() { it('should store property',function() {
should.not.exist(context.get("nodeX","foo")); should.not.exist(context.get("nodeX","foo"));
context.set("nodeX","foo","test"); context.set("nodeX","foo","test");
context.get("nodeX","foo").should.eql("test"); context.get("nodeX","foo").should.equal("test");
}); });
it('should store property - creates parent properties',function() { it('should store property - creates parent properties',function() {
@ -61,8 +61,8 @@ describe('memory',function() {
context.set("nodeX","foo","testX"); context.set("nodeX","foo","testX");
context.set("nodeY","foo","testY"); context.set("nodeY","foo","testY");
context.get("nodeX","foo").should.eql("testX"); context.get("nodeX","foo").should.equal("testX");
context.get("nodeY","foo").should.eql("testY"); context.get("nodeY","foo").should.equal("testY");
}); });
}); });
@ -75,12 +75,12 @@ describe('memory',function() {
context.set("nodeX","foo","bar"); context.set("nodeX","foo","bar");
keys = context.keys("nodeX"); keys = context.keys("nodeX");
keys.should.have.length(1); keys.should.have.length(1);
keys[0].should.eql("foo"); keys[0].should.equal("foo");
context.set("nodeX","abc.def","bar"); context.set("nodeX","abc.def","bar");
keys = context.keys("nodeX"); keys = context.keys("nodeX");
keys.should.have.length(2); keys.should.have.length(2);
keys[1].should.eql("abc"); keys[1].should.equal("abc");
}); });
it('should enumerate context keys in each scopes', function() { it('should enumerate context keys in each scopes', function() {
@ -96,11 +96,11 @@ describe('memory',function() {
context.set("nodeY","hoge","piyo"); context.set("nodeY","hoge","piyo");
keysX = context.keys("nodeX"); keysX = context.keys("nodeX");
keysX.should.have.length(1); keysX.should.have.length(1);
keysX[0].should.eql("foo"); keysX[0].should.equal("foo");
keysY = context.keys("nodeY"); keysY = context.keys("nodeY");
keysY.should.have.length(1); keysY.should.have.length(1);
keysY[0].should.eql("hoge"); keysY[0].should.equal("hoge");
}); });
it('should enumerate only context keys when GlobalContext was given', function() { it('should enumerate only context keys when GlobalContext was given', function() {
@ -114,7 +114,7 @@ describe('memory',function() {
context.setGlobalContext(data); context.setGlobalContext(data);
keys = context.keys("global"); keys = context.keys("global");
keys.should.have.length(1); keys.should.have.length(1);
keys[0].should.eql("foo"); keys[0].should.equal("foo");
}); });
}); });
@ -124,8 +124,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo")); should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc"); context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc"); context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc"); context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.eql("abc"); context.get("nodeY","foo").should.equal("abc");
return context.delete("nodeX").then(function(){ return context.delete("nodeX").then(function(){
should.not.exist(context.get("nodeX","foo")); should.not.exist(context.get("nodeX","foo"));
@ -140,8 +140,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo")); should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc"); context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc"); context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc"); context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.eql("abc"); context.get("nodeY","foo").should.equal("abc");
return context.clean([]).then(function(){ return context.clean([]).then(function(){
should.not.exist(context.get("nodeX","foo")); should.not.exist(context.get("nodeX","foo"));
@ -153,8 +153,8 @@ describe('memory',function() {
should.not.exist(context.get("nodeY","foo")); should.not.exist(context.get("nodeY","foo"));
context.set("nodeX","foo","abc"); context.set("nodeX","foo","abc");
context.set("nodeY","foo","abc"); context.set("nodeY","foo","abc");
context.get("nodeX","foo").should.eql("abc"); context.get("nodeX","foo").should.equal("abc");
context.get("nodeY","foo").should.eql("abc"); context.get("nodeY","foo").should.equal("abc");
return context.clean(["nodeX"]).then(function(){ return context.clean(["nodeX"]).then(function(){
should.exist(context.get("nodeX","foo")); should.exist(context.get("nodeX","foo"));
@ -173,7 +173,7 @@ describe('memory',function() {
foo: "bar" foo: "bar"
} }
context.setGlobalContext(data); context.setGlobalContext(data);
context.get("global","foo").should.eql("bar"); context.get("global","foo").should.equal("bar");
}); });
}); });
}); });