|
|
@ -264,7 +264,121 @@ describe('Persistence', function () { |
|
|
|
(function () { new Datastore({ filename: 'workspace/bad.db~' }); }).should.throw(); |
|
|
|
(function () { new Datastore({ filename: 'workspace/bad.db~' }); }).should.throw(); |
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
it('If system crashes during a loadDatabase, the former version is not lost', function (done) { |
|
|
|
it('If no file exists, ensureDatafileIntegrity creates an empty datafile', function (done) { |
|
|
|
|
|
|
|
var p = new Persistence({ db: { inMemoryOnly: false, filename: 'workspace/it.db' } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db')) { fs.unlinkSync('workspace/it.db'); } |
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db~')) { fs.unlinkSync('workspace/it.db~'); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFileSync('workspace/it.db', 'utf8').should.equal(''); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('If only datafile exists, ensureDatafileIntegrity will use it', function (done) { |
|
|
|
|
|
|
|
var p = new Persistence({ db: { inMemoryOnly: false, filename: 'workspace/it.db' } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db')) { fs.unlinkSync('workspace/it.db'); } |
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db~')) { fs.unlinkSync('workspace/it.db~'); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db', 'something', 'utf8'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFileSync('workspace/it.db', 'utf8').should.equal('something'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('If only temp datafile exists, ensureDatafileIntegrity will use it', function (done) { |
|
|
|
|
|
|
|
var p = new Persistence({ db: { inMemoryOnly: false, filename: 'workspace/it.db' } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db')) { fs.unlinkSync('workspace/it.db'); } |
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db~')) { fs.unlinkSync('workspace/it.db~'); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db~', 'something', 'utf8'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(false); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFileSync('workspace/it.db', 'utf8').should.equal('something'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('If both files exist and datafile is not empty, ensureDatafileIntegrity will use the datafile', function (done) { |
|
|
|
|
|
|
|
var p = new Persistence({ db: { inMemoryOnly: false, filename: 'workspace/it.db' } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db')) { fs.unlinkSync('workspace/it.db'); } |
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db~')) { fs.unlinkSync('workspace/it.db~'); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db', 'something', 'utf8'); |
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db~', 'other', 'utf8'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFileSync('workspace/it.db', 'utf8').should.equal('something'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('If both files exist and datafile is empty, ensureDatafileIntegrity will use the datafile', function (done) { |
|
|
|
|
|
|
|
var p = new Persistence({ db: { inMemoryOnly: false, filename: 'workspace/it.db' } }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db')) { fs.unlinkSync('workspace/it.db'); } |
|
|
|
|
|
|
|
if (fs.existsSync('workspace/it.db~')) { fs.unlinkSync('workspace/it.db~'); } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db', '', 'utf8'); |
|
|
|
|
|
|
|
fs.writeFileSync('workspace/it.db~', 'other', 'utf8'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
p.ensureDatafileIntegrity(function (err) { |
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db').should.equal(true); |
|
|
|
|
|
|
|
fs.existsSync('workspace/it.db~').should.equal(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFileSync('workspace/it.db', 'utf8').should.equal('other'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it.skip('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; |
|
|
|
|
|
|
|
|
|
|
|
// Creating a db file with 150k records (a bit long to load)
|
|
|
|
// Creating a db file with 150k records (a bit long to load)
|
|
|
|