All tests including the crash one pass

pull/2/head
Louis Chatriot 11 years ago
parent ff71988c8e
commit 35b46c286b
  1. 2
      lib/datastore.js
  2. 49
      test/persistence.test.js
  3. 6
      test_lac/loadAndCrash.test.js

@ -540,6 +540,6 @@ Datastore.prototype._remove = function (query, options, cb) {
Datastore.prototype.remove = function () { Datastore.prototype.remove = function () {
this.executor.push({ this: this, fn: this._remove, arguments: arguments }); this.executor.push({ this: this, fn: this._remove, arguments: arguments });
}; };
module.exports = Datastore; module.exports = Datastore;

@ -423,10 +423,11 @@ describe('Persistence', function () {
}); });
}); });
// This test is a bit complicated since it depends on the time actions take to execute // This test is a bit complicated since it depends on the time I/O actions take to execute
// It may not work as expected on all machines as it is timed for my machine // That depends on the machine and the load on the machine when the tests are run
// That's why it is skipped, but all versions of nedb pass this test // It is timed for my machine with nothing else running but may not work as expected on others (it will not fail but may not be a proof)
it.skip('If system crashes during a loadDatabase, the former version is not lost', function (done) { // Every new version of NeDB passes it on my machine before rtelease
it('If system crashes during a loadDatabase, the former version is not lost', function (done) {
var cp, N = 150000, toWrite = "", i; var cp, N = 150000, toWrite = "", i;
// Ensuring the state is clean // Ensuring the state is clean
@ -438,21 +439,37 @@ describe('Persistence', function () {
toWrite += model.serialize({ _id: customUtils.uid(16), hello: 'world' }) + '\n'; toWrite += model.serialize({ _id: customUtils.uid(16), hello: 'world' }) + '\n';
} }
fs.writeFileSync('workspace/lac.db', toWrite, 'utf8'); fs.writeFileSync('workspace/lac.db', toWrite, 'utf8');
console.log("================");
// Loading it in a separate process that'll crash before finishing the load // Loading it in a separate process that we will crash before finishing the loadDatabase
cp = child_process.fork('test_lac/loadAndCrash.test') cp = child_process.fork('test_lac/loadAndCrash.test')
cp.on('message', function (msg) {
// Let the child process enough time to crash // Kill the child process when we're at step 3 of persistCachedDatabase (during write to datafile)
setTimeout(function() {
cp.kill('SIGINT');
// If the timing is correct, only the temp datafile contains data
// The datafile was in the middle of being written and is empty
// Let the process crash be finished then load database without a crash, and test we didn't lose data
setTimeout(function () { setTimeout(function () {
// fs.readFileSync('workspace/lac.db', 'utf8').length.should.not.equal(0); var db = new Datastore({ filename: 'workspace/lac.db' });
console.log(fs.readFileSync('workspace/lac.db').length); db.loadDatabase(function (err) {
console.log(fs.readFileSync('workspace/lac.db~').length); assert.isNull(err);
done(); db.count({}, function (err, n) {
}, 100); // Data has not been lost
}); assert.isNull(err);
n.should.equal(150000);
// State is clean, the temp datafile has been erased and the datafile contains all the data
fs.existsSync('workspace/lac.db').should.equal(true);
fs.existsSync('workspace/lac.db~').should.equal(false);
done();
});
});
}, 100);
}, 2000);
}); });
}); });

@ -2,10 +2,4 @@ var Nedb = require('../lib/datastore.js')
, db = new Nedb({ filename: 'workspace/lac.db' }) , db = new Nedb({ filename: 'workspace/lac.db' })
; ;
// Simulate a crash in 100ms
setTimeout(function() {
process.send('crash');
process.exit();
}, 1870);
db.loadDatabase(); db.loadDatabase();
Loading…
Cancel
Save