Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Improved reliability of LevelUp persistence tests.
Browse files Browse the repository at this point in the history
Still the 'database not open spurious failure remains'.
  • Loading branch information
mcollina committed Jun 27, 2014
1 parent fac586f commit eadbcb5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
9 changes: 8 additions & 1 deletion lib/persistence/levelup.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ function LevelUpPersistence(options, callback) {
this._subscriptions = this.db.sublevel("subscriptions");
this._offlinePackets = this.db.sublevel("offlinePackets");
this._subMatcher = new Matcher();
this._packetCounter = 0;
this._lastStoredPacketTime = Date.now();

var that = this;
var stream = this._subscriptions.createReadStream();
Expand Down Expand Up @@ -321,7 +323,12 @@ LevelUpPersistence.prototype.updateOfflinePacket = function(client, messageId, n
};

LevelUpPersistence.prototype._storePacket = function(client, packet, cb) {
var key = util.format("%s:%s:%d", client, new Date().toISOString(), Math.floor(Math.random() * 2048));
var currentTime = Date.now();
if (currentTime !== this._lastStoredPacketTime) {
this._packetCounter = 0;
}
this._lastStoredPacketTime = currentTime;
var key = util.format("%s:%d:%d", client, currentTime, ++this._packetCounter);
var ttl = {
ttl: this.options.ttl.subscriptions
};
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,22 @@
"author": "Matteo Collina <[email protected]>",
"license": "MIT",
"devDependencies": {
"mocha": "~1.20.0",
"async_bench": "~0.3.0",
"chai": "~1.9.1",
"sinon": "~1.7.3",
"sinon-chai": "~2.5.0",
"underscore": "~1.6.0",
"coveralls": "~2.10.0",
"dox-foundation": "~0.5.4",
"istanbul": "~0.2.6",
"jshint": "~2.4.4",
"microtime": "~0.5.0",
"tmp": "0.0.23",
"mocha": "~1.20.0",
"mongo-clean": "0.0.1",
"osenv": "^0.1.0",
"rimraf": "^2.2.8",
"sinon": "~1.7.3",
"sinon-chai": "~2.5.0",
"supertest": "~0.10.0",
"coveralls": "~2.10.0",
"istanbul": "~0.2.6",
"async_bench": "~0.3.0",
"mongo-clean": "0.0.1"
"tmp": "0.0.23",
"underscore": "~1.6.0"
},
"dependencies": {
"mqtt": "~0.3.10",
Expand Down
18 changes: 9 additions & 9 deletions test/persistence/levelup_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

var abstract = require("./abstract");
var LevelUp = require("../../").persistence.LevelUp;
var tmp = require("tmp");
var async = require("async");
var tmpdir = require("osenv").tmpdir();
var path = require("path");
var rimraf = require("rimraf");

describe("mosca.persistence.LevelUp", function() {

this.timeout(2000);

var opts = {
var opts = {
ttl: {
checkFrequency: 250,
subscriptions: 250,
Expand All @@ -19,14 +21,12 @@ describe("mosca.persistence.LevelUp", function() {

abstract(LevelUp, function(cb) {
var that = this;
tmp.dir(function (err, path) {
if (err) {
return cb(err);
}
opts.path = path.join(tmpdir, 'level_' + Date.now());
cb(null, opts);
});

opts.path = path;
cb(null, opts);
});
afterEach(function deleteLevel(done) {
rimraf(opts.path, done);
});

describe("two instances", function() {
Expand Down

0 comments on commit eadbcb5

Please sign in to comment.