mirror of
				https://github.com/node-red/node-red.git
				synced 2025-03-01 10:36:34 +00:00 
			
		
		
		
	Prevent the callback to be called twice
and add test cases
This commit is contained in:
		| @@ -30,19 +30,24 @@ Memory.prototype.close = function(){ | ||||
|  | ||||
| Memory.prototype.get = function(scope, key, callback) { | ||||
|     var value; | ||||
|     var error; | ||||
|     try{ | ||||
|         if(this.data[scope]){ | ||||
|             value = util.getMessageProperty(this.data[scope], key); | ||||
|         } | ||||
|     }catch(err){ | ||||
|         if(callback){ | ||||
|             callback(err); | ||||
|             error = err; | ||||
|         }else{ | ||||
|             throw err; | ||||
|         } | ||||
|     } | ||||
|     if(callback){ | ||||
|         if(error){ | ||||
|             callback(error); | ||||
|         } else { | ||||
|             callback(null, value); | ||||
|         } | ||||
|     } else { | ||||
|         return value; | ||||
|     } | ||||
| @@ -52,22 +57,24 @@ Memory.prototype.set =function(scope, key, value, callback) { | ||||
|     if(!this.data[scope]){ | ||||
|         this.data[scope] = {}; | ||||
|     } | ||||
|     var error; | ||||
|     try{ | ||||
|         util.setMessageProperty(this.data[scope],key,value); | ||||
|     }catch(err){ | ||||
|         if(callback){ | ||||
|             callback(err); | ||||
|             error = err; | ||||
|         }else{ | ||||
|             throw err; | ||||
|         } | ||||
|     } | ||||
|     if(callback){ | ||||
|         callback(null); | ||||
|         callback(error || null); | ||||
|     } | ||||
| }; | ||||
|  | ||||
| Memory.prototype.keys = function(scope, callback){ | ||||
|     var values = []; | ||||
|     var error; | ||||
|     try{ | ||||
|         if(this.data[scope]){ | ||||
|             if (scope !== "global") { | ||||
| @@ -80,13 +87,17 @@ Memory.prototype.keys = function(scope, callback){ | ||||
|         } | ||||
|     }catch(err){ | ||||
|         if(callback){ | ||||
|             callback(err); | ||||
|             error = err; | ||||
|         }else{ | ||||
|             throw err; | ||||
|         } | ||||
|     } | ||||
|     if(callback){ | ||||
|         if(error){ | ||||
|             callback(error); | ||||
|         } else { | ||||
|             callback(null, values); | ||||
|         } | ||||
|     } else { | ||||
|         return values; | ||||
|     } | ||||
|   | ||||
| @@ -32,6 +32,7 @@ describe('memory',function() { | ||||
|     }); | ||||
|  | ||||
|     describe('#get/set',function() { | ||||
|         describe('sync',function() { | ||||
|             it('should store property',function() { | ||||
|                 should.not.exist(context.get("nodeX","foo")); | ||||
|                 context.set("nodeX","foo","test"); | ||||
| @@ -64,9 +65,50 @@ describe('memory',function() { | ||||
|                 context.get("nodeX","foo").should.equal("testX"); | ||||
|                 context.get("nodeY","foo").should.equal("testY"); | ||||
|             }); | ||||
|  | ||||
|             it('should thorw the error if the error occurs', function() { | ||||
|                 try{ | ||||
|                     context.set("nodeX",".foo","test"); | ||||
|                     should.fail("Error was not thrown"); | ||||
|                 }catch(err){ | ||||
|                     should.exist(err); | ||||
|                     try{ | ||||
|                         context.get("nodeX",".foo"); | ||||
|                         should.fail("Error was not thrown"); | ||||
|                     }catch(err){ | ||||
|                         should.exist(err); | ||||
|                     } | ||||
|                 } | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         describe('async',function() { | ||||
|             it('should store property',function(done) { | ||||
|                 context.get("nodeX","foo",function(err, value){ | ||||
|                     should.not.exist(value); | ||||
|                     context.set("nodeX","foo","test",function(err){ | ||||
|                         context.get("nodeX","foo",function(err, value){ | ||||
|                             value.should.equal("test"); | ||||
|                             done(); | ||||
|                         }); | ||||
|                     }); | ||||
|                 }); | ||||
|             }); | ||||
|  | ||||
|             it('should pass the error to callback if the error occurs',function(done) { | ||||
|                 context.set("nodeX",".foo","test",function(err, value){ | ||||
|                     should.exist(err); | ||||
|                     context.get("nodeX",".foo",function(err){ | ||||
|                         should.exist(err); | ||||
|                         done(); | ||||
|                     }); | ||||
|                 }); | ||||
|             }); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     describe('#keys',function() { | ||||
|         describe('sync',function() { | ||||
|             it('should enumerate context keys', function() { | ||||
|                 var keys = context.keys("nodeX"); | ||||
|                 keys.should.be.an.Array(); | ||||
| @@ -102,7 +144,29 @@ describe('memory',function() { | ||||
|                 keysY.should.have.length(1); | ||||
|                 keysY[0].should.equal("hoge"); | ||||
|             }); | ||||
|         }); | ||||
|  | ||||
|         describe('async',function() { | ||||
|             it('should enumerate context keys', function(done) { | ||||
|                 context.keys("nodeX", function(err, keys) { | ||||
|                     keys.should.be.an.Array(); | ||||
|                     keys.should.be.empty(); | ||||
|                     context.set("nodeX", "foo", "bar", function(err) { | ||||
|                         context.keys("nodeX", function(err, keys) { | ||||
|                             keys.should.have.length(1); | ||||
|                             keys[0].should.equal("foo"); | ||||
|                             context.set("nodeX","abc.def","bar",function(err){ | ||||
|                                 context.keys("nodeX",function(err, keys){ | ||||
|                                     keys.should.have.length(2); | ||||
|                                     keys[1].should.equal("abc"); | ||||
|                                     done(); | ||||
|                                 }); | ||||
|                             }); | ||||
|                         }); | ||||
|                     }); | ||||
|                 }); | ||||
|             }); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     describe('#delete',function() { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user