fix setAutocompaction interval type check + test

simpler check for errors in tests
pull/11/head
Timothée Rebours 3 years ago
parent 899bdf5a55
commit bf5e5e2218
  1. 4
      lib/datastore.js
  2. 60
      test/persistence.async.test.js

@ -342,8 +342,8 @@ class Datastore extends EventEmitter {
*/ */
setAutocompactionInterval (interval) { setAutocompactionInterval (interval) {
const minInterval = 5000 const minInterval = 5000
if (typeof interval !== 'number') throw new Error('Interval must be a number') if (Number.isNaN(Number(interval))) throw new Error('Interval must be a non-NaN number')
const realInterval = Math.max(interval || 0, minInterval) const realInterval = Math.max(Number(interval) || 0, minInterval)
this.stopAutocompaction() this.stopAutocompaction()

@ -12,6 +12,7 @@ const { execFile, fork } = require('child_process')
const { promisify } = require('util') const { promisify } = require('util')
const { ensureFileDoesntExistAsync } = require('../lib/storage') const { ensureFileDoesntExistAsync } = require('../lib/storage')
const { once } = require('events') const { once } = require('events')
const { wait } = require('./utils.test')
const Readable = require('stream').Readable const Readable = require('stream').Readable
describe('Persistence async', function () { describe('Persistence async', function () {
@ -346,13 +347,11 @@ describe('Persistence async', function () {
// Default corruptAlertThreshold // Default corruptAlertThreshold
d = new Datastore({ filename: corruptTestFilename }) d = new Datastore({ filename: corruptTestFilename })
await assert.rejects(() => d.loadDatabaseAsync(), err => { await assert.rejects(() => d.loadDatabaseAsync(), {
assert.notEqual(err, undefined) corruptionRate: 0.25,
assert.notEqual(err, null) corruptItems: 1,
assert.equal(err.corruptionRate, 0.25) dataLength: 4,
assert.equal(err.corruptItems, 1) message: '25% of the data file is corrupt, more than given corruptAlertThreshold (10%). Cautiously refusing to start NeDB to prevent dataloss.'
assert.equal(err.dataLength, 4)
return true
}) })
await fs.writeFile(corruptTestFilename, fakeData, 'utf8') await fs.writeFile(corruptTestFilename, fakeData, 'utf8')
@ -360,13 +359,11 @@ describe('Persistence async', function () {
await d.loadDatabaseAsync() await d.loadDatabaseAsync()
await fs.writeFile(corruptTestFilename, fakeData, 'utf8') await fs.writeFile(corruptTestFilename, fakeData, 'utf8')
d = new Datastore({ filename: corruptTestFilename, corruptAlertThreshold: 0 }) d = new Datastore({ filename: corruptTestFilename, corruptAlertThreshold: 0 })
await assert.rejects(() => d.loadDatabaseAsync(), err => { await assert.rejects(() => d.loadDatabaseAsync(), {
assert.notEqual(err, undefined) corruptionRate: 0.25,
assert.notEqual(err, null) corruptItems: 1,
assert.equal(err.corruptionRate, 0.25) dataLength: 4,
assert.equal(err.corruptItems, 1) message: '25% of the data file is corrupt, more than given corruptAlertThreshold (0%). Cautiously refusing to start NeDB to prevent dataloss.'
assert.equal(err.dataLength, 4)
return true
}) })
}) })
@ -380,6 +377,41 @@ describe('Persistence async', function () {
await compacted // should already be resolved when the function returns, but still awaiting for it await compacted // should already be resolved when the function returns, but still awaiting for it
}) })
it('setAutocompaction fails gracefully when passed a NaN', async () => {
assert.throws(() => {
d.setAutocompactionInterval(Number.NaN)
}, {
message: 'Interval must be a non-NaN number'
})
})
it('setAutocompaction fails gracefully when passed a string non castable to a number', async () => {
assert.throws(() => {
d.setAutocompactionInterval('a')
}, {
message: 'Interval must be a non-NaN number'
})
})
it('setAutocompaction works if passed a number castable to a number below 5000ms', async () => {
let i = 0
const backup = d.compactDatafile
d.compactDatafile = () => {
backup.call(d)
i++
}
try {
d.setAutocompactionInterval('0') // it should set the actual interval to 5000
await wait(6000)
assert.ok(i < 3)
assert.ok(i >= 1)
} finally {
d.compactDatafile = backup
d.stopAutocompaction()
assert.equal(d._autocompactionIntervalId, null)
}
})
describe('Serialization hooks', async () => { describe('Serialization hooks', async () => {
const as = s => `before_${s}_after` const as = s => `before_${s}_after`
const bd = s => s.substring(7, s.length - 6) const bd = s => s.substring(7, s.length - 6)

Loading…
Cancel
Save