From 2b1310b82c1572c5b86267cb21dd84857a8e20c7 Mon Sep 17 00:00:00 2001 From: Louis Chatriot Date: Sun, 24 Nov 2013 16:07:27 +0100 Subject: [PATCH] persistCachedDatabase is tested and works as before in the non crash case --- test/persistence.test.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/test/persistence.test.js b/test/persistence.test.js index 8900cf8..7d31ac6 100644 --- a/test/persistence.test.js +++ b/test/persistence.test.js @@ -383,12 +383,40 @@ describe('Persistence', function () { d.find({}, function (err, docs) { docs.length.should.equal(1); - fs.unlinkSync(testDb); + if (fs.existsSync(testDb)) { fs.unlinkSync(testDb); } + if (fs.existsSync(testDb + '~')) { fs.unlinkSync(testDb + '~'); } fs.existsSync(testDb).should.equal(false); + fs.existsSync(testDb + '~').should.equal(false); d.persistence.persistCachedDatabase(function (err) { var contents = fs.readFileSync(testDb, 'utf8'); assert.isNull(err); + fs.existsSync(testDb).should.equal(true); + fs.existsSync(testDb + '~').should.equal(false); + if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) { + throw "Datafile contents not as expected"; + } + done(); + }); + }); + }); + }); + + it('persistCachedDatabase should update the contents of the datafile even if there is a temp datafile', function (done) { + d.insert({ hello: 'world' }, function () { + d.find({}, function (err, docs) { + docs.length.should.equal(1); + + if (fs.existsSync(testDb)) { fs.unlinkSync(testDb); } + fs.writeFileSync(testDb + '~', 'blabla', 'utf8'); + fs.existsSync(testDb).should.equal(false); + fs.existsSync(testDb + '~').should.equal(true); + + d.persistence.persistCachedDatabase(function (err) { + var contents = fs.readFileSync(testDb, 'utf8'); + assert.isNull(err); + fs.existsSync(testDb).should.equal(true); + fs.existsSync(testDb + '~').should.equal(false); if (!contents.match(/^{"hello":"world","_id":"[0-9a-zA-Z]{16}"}\n$/)) { throw "Datafile contents not as expected"; } @@ -398,7 +426,7 @@ describe('Persistence', function () { }); }); - it.skip('If system crashes during a loadDatabase, the former version is not lost', function (done) { + it.only('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) @@ -415,6 +443,7 @@ describe('Persistence', 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); + done(); }, 100); });