Added tests for async version of datastore

pull/11/head
Timothée Rebours 3 years ago
parent 5f8850df1b
commit ba65fbe9b6
  1. 44
      lib/datastore.js
  2. 4
      lib/persistence.js
  3. 2046
      test/db.async.test.js
  4. 5
      test/utils.test.js

@ -76,9 +76,14 @@ class Datastore extends EventEmitter {
// Queue a load of the database right away and call the onload handler
// By default (no onload handler), if there is an error there, no operation will be possible so warn the user by throwing an exception
if (this.autoload) {
this.loadDatabase(options.onload || (err => {
if (err) throw err
}))
this.autoloadPromise = this.loadDatabaseAsync()
this.autoloadPromise
.then(() => {
if (options.onload) options.onload()
}, err => {
if (options.onload) options.onload(err)
else throw err
})
}
}
@ -298,12 +303,7 @@ class Datastore extends EventEmitter {
else expiredDocsIds.push(doc._id)
})
for (const _id of expiredDocsIds) {
await new Promise((resolve, reject) => {
this._remove({ _id: _id }, {}, err => {
if (err) return reject(err)
return resolve()
})
})
await this._removeAsync({ _id: _id }, {})
}
} else validDocs.push(...docs)
return validDocs
@ -405,7 +405,7 @@ class Datastore extends EventEmitter {
}
insertAsync (...args) {
return this.executor.push({ this: this, fn: this._insertAsync, arguments: args, async: true })
return this.executor.pushAsync(() => this._insertAsync(...args))
}
/**
@ -530,15 +530,10 @@ class Datastore extends EventEmitter {
// If upsert option is set, check whether we need to insert the doc
if (upsert) {
// Need to use an internal function not tied to the executor to avoid deadlock
const cursor = new Cursor(this, query)
const cursor = new Cursor(this, query, x => x, true)
const docs = await new Promise((resolve, reject) => {
cursor.limit(1)._exec((err, docs) => {
if (err) reject(err)
else resolve(docs)
})
})
// Need to use an internal function not tied to the executor to avoid deadlock
const docs = await cursor.limit(1)._execAsync()
if (docs.length !== 1) {
let toBeInserted
@ -552,13 +547,8 @@ class Datastore extends EventEmitter {
// strip it from all operators and update it according to updateQuery
toBeInserted = model.modify(model.deepCopy(query, true), updateQuery)
}
return new Promise((resolve, reject) => {
this._insert(toBeInserted, (err, newDoc) => {
if (err) return reject(err)
return resolve({ numAffected: 1, affectedDocuments: newDoc, upsert: true })
})
})
const newDoc = await this._insertAsync(toBeInserted)
return { numAffected: 1, affectedDocuments: newDoc, upsert: true }
}
}
// Perform the update
@ -603,7 +593,7 @@ class Datastore extends EventEmitter {
}
updateAsync (...args) {
return this.executor.pushAsync(() => this._updateAsync(args))
return this.executor.pushAsync(() => this._updateAsync(...args))
}
/**
@ -651,7 +641,7 @@ class Datastore extends EventEmitter {
}
removeAsync (...args) {
return this.executor.pushAsync(() => this._removeAsync(args))
return this.executor.pushAsync(() => this._removeAsync(...args))
}
}

@ -107,6 +107,10 @@ class Persistence {
this.db.executor.push({ this: this, fn: this.persistCachedDatabase, arguments: [] })
}
compactDatafileAsync () {
return this.db.executor.pushAsync(() => this.persistCachedDatabaseAsync())
}
/**
* Set automatic compaction every interval ms
* @param {Number} interval in milliseconds, with an enforced minimum of 5 seconds

File diff suppressed because it is too large Load Diff

@ -27,7 +27,12 @@ const whilstAsync = async (test, fn) => {
const whilst = callbackify(whilstAsync)
const wait = delay => new Promise(resolve => {
setTimeout(resolve, delay)
})
module.exports.whilst = whilst
module.exports.apply = apply
module.exports.waterfall = waterfall
module.exports.each = each
module.exports.wait = wait

Loading…
Cancel
Save