Basic test for persistCachgedDatabase

pull/2/head
Louis Chatriot 11 years ago
parent 2a4f55504d
commit d5ec5185eb
  1. 30
      lib/persistence.js
  2. 20
      test/persistence.test.js

@ -105,7 +105,7 @@ Persistence.prototype.persistCachedDatabase = function (cb) {
async.waterfall([
function (cb) {
fs.exists(self.tempFilename, function (exists) {
if (exists) {
if (exists) { // Shouldn't happen since ensureDatafileIntegrity removes the temp datafile
fs.unlink(self.tempFilename, function (err) { return cb(err); });
} else {
return cb();
@ -113,19 +113,27 @@ Persistence.prototype.persistCachedDatabase = function (cb) {
});
}
, function (cb) {
console.log("==== 3");
fs.rename(self.filename, self.tempFilename, function (err) {
console.log("==== 4");
if (err) { return cb(err); }
fs.writeFile(self.filename, toPersist, function (err) {
console.log("==== 5");
if (err) { return cb(err); }
fs.exists(self.filename, function (exists) {
if (exists) {
fs.rename(self.filename, self.tempFilename, function (err) { return cb(err); });
} else {
return cb();
}
});
}
, function (cb) {
fs.writeFile(self.filename, toPersist, function (err) { return cb(err); });
}
, function (cb) {
fs.exists(self.tempFilename, function (exists) {
if (exists) {
fs.unlink(self.tempFilename, function (err) { return cb(err); });
});
} else {
return cb();
}
});
}
], function (err) { return callback(err); })
], function (err) { if (err) { return callback(err); } else { return callback(null); } })
};

@ -376,6 +376,26 @@ describe('Persistence', function () {
done();
});
});
it('persistCachedDatabase should update the contents of the datafile', function (done) {
d.insert({ hello: 'world' }, function () {
d.find({}, function (err, docs) {
docs.length.should.equal(1);
fs.unlinkSync(testDb);
fs.existsSync(testDb).should.equal(false);
d.persistence.persistCachedDatabase(function (err) {
var contents = fs.readFileSync(testDb, 'utf8');
assert.isNull(err);
if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) {
throw "Datafile contents not as expected";
}
done();
});
});
});
});
it.skip('If system crashes during a loadDatabase, the former version is not lost', function (done) {

Loading…
Cancel
Save