From 7e979b7e670e54820c3c7ba90bde7978c62a69ba Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 6 Aug 2013 23:25:49 -0700 Subject: [PATCH] Fixed Memory and Mongo persistence ready callback support. --- lib/persistence/memory.js | 8 +++++--- lib/persistence/mongo.js | 2 +- test/persistence/abstract.js | 24 +++++++++++++++++++----- test/persistence/levelup_spec.js | 5 ++--- test/persistence/memory_spec.js | 4 +--- test/persistence/mongo_spec.js | 6 +----- test/persistence/redis_spec.js | 6 +----- 7 files changed, 30 insertions(+), 25 deletions(-) diff --git a/lib/persistence/memory.js b/lib/persistence/memory.js index 424abcc..82103ad 100644 --- a/lib/persistence/memory.js +++ b/lib/persistence/memory.js @@ -13,16 +13,18 @@ var factory = function (location) { return new MemDOWN(location); }; * minus the `db`, which is set to MemDOWN for convenience. * * @api public + * @param {Object} options The options to create this persistance + * @param {Function} callback Called when ready. */ -function MemoryPersistence(options) { +function MemoryPersistence(options, callback) { if (!(this instanceof MemoryPersistence)) { - return new MemoryPersistence(options); + return new MemoryPersistence(options, callback); } options = options || {}; options.db = factory; options.path = "RAM"; - LevelUpPersistence.call(this, options); + LevelUpPersistence.call(this, options, callback); } util.inherits(MemoryPersistence, LevelUpPersistence); diff --git a/lib/persistence/mongo.js b/lib/persistence/mongo.js index 29fc503..1cc6bb7 100644 --- a/lib/persistence/mongo.js +++ b/lib/persistence/mongo.js @@ -39,7 +39,7 @@ var defaults = { */ function MongoPersistence(options, done) { if (!(this instanceof MongoPersistence)) { - return new MongoPersistence(options); + return new MongoPersistence(options, done); } diff --git a/test/persistence/abstract.js b/test/persistence/abstract.js index 480e194..a3319f2 100644 --- a/test/persistence/abstract.js +++ b/test/persistence/abstract.js @@ -3,18 +3,32 @@ var async = require("async"); var EventEmitter = require("events").EventEmitter; -module.exports = function(create) { +module.exports = function(create, buildOpts) { + var _opts; + + if (typeof buildOpts !== "function") { + _opts = buildOpts; + buildOpts = function(cb) { + cb(null, _opts); + }; + } beforeEach(function(done) { var that = this; - create.call(this, function(err, result, opts) { + buildOpts(function(err, opts) { if (err) { return done(err); } - that.instance = result; - that.opts = opts; - done(); + create(opts, function(err, result) { + if (err) { + return done(err); + } + + that.instance = result; + that.opts = opts; + done(); + }); }); }); diff --git a/test/persistence/levelup_spec.js b/test/persistence/levelup_spec.js index 2dc1774..e05dd2e 100644 --- a/test/persistence/levelup_spec.js +++ b/test/persistence/levelup_spec.js @@ -15,16 +15,15 @@ describe("mosca.persistence.LevelUp", function() { } }; - abstract(function(cb) { + abstract(LevelUp, function(cb) { var that = this; tmp.dir(function (err, path) { if (err) { return cb(err); } - that.path = path; opts.path = path; - cb(null, new LevelUp(opts), opts); + cb(null, opts); }); }); diff --git a/test/persistence/memory_spec.js b/test/persistence/memory_spec.js index f41a9c7..d90f4eb 100644 --- a/test/persistence/memory_spec.js +++ b/test/persistence/memory_spec.js @@ -13,7 +13,5 @@ describe("mosca.persistence.Memory", function() { } }; - abstract(function(cb) { - cb(null, new Memory(opts), opts); - }); + abstract(Memory, opts); }); diff --git a/test/persistence/mongo_spec.js b/test/persistence/mongo_spec.js index f1291f9..e83c95b 100644 --- a/test/persistence/mongo_spec.js +++ b/test/persistence/mongo_spec.js @@ -40,11 +40,7 @@ describe("mosca.persistence.Mongo", function() { this.secondInstance = null; }); - abstract(function(cb) { - new Mongo(opts, function(err, mongo) { - cb(err, mongo, opts); - }); - }); + abstract(Mongo, opts); describe("two clients", function() { diff --git a/test/persistence/redis_spec.js b/test/persistence/redis_spec.js index 5fa8ea5..60106c8 100644 --- a/test/persistence/redis_spec.js +++ b/test/persistence/redis_spec.js @@ -14,11 +14,7 @@ describe("mosca.persistence.Redis", function() { } }; - abstract(function(cb) { - new Redis(opts, function(err, instance) { - cb(null, instance, opts); - }); - }); + abstract(Redis, opts); afterEach(function(cb) { var flush = function() {