Fix review nit, add test

This commit is contained in:
David Humphrey 2014-05-22 15:09:33 -04:00
parent 185c05e742
commit e37ba7142c
2 changed files with 42 additions and 4 deletions

View File

@ -8,10 +8,14 @@ define(function(require) {
var createDB = (function() {
var pool = {};
return function getOrCreate(name) {
if(!pool[name]) {
var firstAccess = !pool.hasOwnProperty(name);
if(firstAccess) {
pool[name] = {};
}
return pool[name];
return {
firstAccess: firstAccess,
db: pool[name]
};
};
}());
@ -62,15 +66,16 @@ define(function(require) {
function Memory(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = createDB(this.name);
}
Memory.isSupported = function() {
return true;
};
Memory.prototype.open = function(callback) {
var result = createDB(this.name);
this.db = result.db;
asyncCallback(function() {
callback(null, true);
callback(null, result.firstAccess);
});
};
Memory.prototype.getReadOnlyContext = function() {

View File

@ -12,6 +12,39 @@ define(["Filer"], function(Filer) {
expect(memoryProvider.getReadWriteContext).to.be.a('function');
});
describe("Memory provider DBs are sharable", function() {
it("should share a single memory db when name is the same", function(done) {
var provider1;
var provider2;
var provider3;
var name1 = 'memory-db';
var name2 = 'memory-db2';
provider1 = new Filer.FileSystem.providers.Memory(name1);
provider1.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.true;
provider2 = new Filer.FileSystem.providers.Memory(name1);
provider2.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.false;
expect(provider1.db).to.equal(provider2.db);
provider3 = new Filer.FileSystem.providers.Memory(name2);
provider3.open(function(error, firstAccess) {
expect(error).not.to.exist;
expect(firstAccess).to.be.true;
expect(provider3.db).not.to.equal(provider2.db);
done();
});
});
});
});
});
});
describe("open an Memory provider", function() {
it("should open a new Memory database", function(done) {
var provider = new Filer.FileSystem.providers.Memory();