Created the test for the load and crash bug

pull/2/head
Louis Chatriot 11 years ago
parent b4ae736d4d
commit a725b962b6
  1. 2
      test/mocha.opts
  2. 31
      test/persistence.test.js
  3. 11
      test_lac/loadAndCrash.test.js

@ -1,2 +1,2 @@
--reporter spec --reporter spec
--timeout 2000 --timeout 10000

@ -6,8 +6,10 @@ var should = require('chai').should()
, _ = require('underscore') , _ = require('underscore')
, async = require('async') , async = require('async')
, model = require('../lib/model') , model = require('../lib/model')
, customUtils = require('../lib/customUtils')
, Datastore = require('../lib/datastore') , Datastore = require('../lib/datastore')
, Persistence = require('../lib/persistence') , Persistence = require('../lib/persistence')
, child_process = require('child_process')
; ;
@ -247,5 +249,34 @@ 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
// But it will not be seen as a failed test. The worst is that the timing is off and it would have worked on your machine regardless of the load failsafe
// It is timed for my dev machine
describe.only('Prevent dataloss when persisting data', function () {
it('If system crashes during a loadDatabase, the former version is not lost', function (done) {
var cp, N = 150000, toWrite = "", i;
// Creating a db file with 150k records (a bit long to load)
for (i = 0; i < N; i += 1) {
toWrite += model.serialize({ _id: customUtils.uid(16), hello: 'world' }) + '\n';
}
fs.writeFileSync('workspace/rah.db', toWrite, 'utf8');
// Loading it in a separate process that'll crash before finishing the load
cp = child_process.fork('test_lac/loadAndCrash.test')
cp.on('message', function (msg) {
// Let the child process enough time to crash
setTimeout(function () {
fs.readFileSync('workspace/rah.db', 'utf8').length.should.not.equal(0);
done();
}, 100);
});
});
});
}); });

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