All tests including the crash one pass

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

@ -423,10 +423,11 @@ describe('Persistence', function () {
});
});
// This test is a bit complicated since it depends on the time actions take to execute
// It may not work as expected on all machines as it is timed for my machine
// That's why it is skipped, but all versions of nedb pass this test
it.skip('If system crashes during a loadDatabase, the former version is not lost', function (done) {
// This test is a bit complicated since it depends on the time I/O actions take to execute
// That depends on the machine and the load on the machine when the tests are run
// 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)
// 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;
// Ensuring the state is clean
@ -439,21 +440,37 @@ describe('Persistence', function () {
}
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.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 () {
// fs.readFileSync('workspace/lac.db', 'utf8').length.should.not.equal(0);
console.log(fs.readFileSync('workspace/lac.db').length);
console.log(fs.readFileSync('workspace/lac.db~').length);
var db = new Datastore({ filename: 'workspace/lac.db' });
db.loadDatabase(function (err) {
assert.isNull(err);
db.count({}, function (err, n) {
// 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);
});
});
}, 100);
}, 2000);
});
});

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