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 // 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 // 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) { if (this.autoload) {
this.loadDatabase(options.onload || (err => { this.autoloadPromise = this.loadDatabaseAsync()
if (err) throw err 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) else expiredDocsIds.push(doc._id)
}) })
for (const _id of expiredDocsIds) { for (const _id of expiredDocsIds) {
await new Promise((resolve, reject) => { await this._removeAsync({ _id: _id }, {})
this._remove({ _id: _id }, {}, err => {
if (err) return reject(err)
return resolve()
})
})
} }
} else validDocs.push(...docs) } else validDocs.push(...docs)
return validDocs return validDocs
@ -405,7 +405,7 @@ class Datastore extends EventEmitter {
} }
insertAsync (...args) { 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 option is set, check whether we need to insert the doc
if (upsert) { if (upsert) {
// Need to use an internal function not tied to the executor to avoid deadlock const cursor = new Cursor(this, query, x => x, true)
const cursor = new Cursor(this, query)
const docs = await new Promise((resolve, reject) => { // Need to use an internal function not tied to the executor to avoid deadlock
cursor.limit(1)._exec((err, docs) => { const docs = await cursor.limit(1)._execAsync()
if (err) reject(err)
else resolve(docs)
})
})
if (docs.length !== 1) { if (docs.length !== 1) {
let toBeInserted let toBeInserted
@ -552,13 +547,8 @@ class Datastore extends EventEmitter {
// strip it from all operators and update it according to updateQuery // strip it from all operators and update it according to updateQuery
toBeInserted = model.modify(model.deepCopy(query, true), updateQuery) toBeInserted = model.modify(model.deepCopy(query, true), updateQuery)
} }
const newDoc = await this._insertAsync(toBeInserted)
return new Promise((resolve, reject) => { return { numAffected: 1, affectedDocuments: newDoc, upsert: true }
this._insert(toBeInserted, (err, newDoc) => {
if (err) return reject(err)
return resolve({ numAffected: 1, affectedDocuments: newDoc, upsert: true })
})
})
} }
} }
// Perform the update // Perform the update
@ -603,7 +593,7 @@ class Datastore extends EventEmitter {
} }
updateAsync (...args) { 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) { 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: [] }) this.db.executor.push({ this: this, fn: this.persistCachedDatabase, arguments: [] })
} }
compactDatafileAsync () {
return this.db.executor.pushAsync(() => this.persistCachedDatabaseAsync())
}
/** /**
* Set automatic compaction every interval ms * Set automatic compaction every interval ms
* @param {Number} interval in milliseconds, with an enforced minimum of 5 seconds * @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 whilst = callbackify(whilstAsync)
const wait = delay => new Promise(resolve => {
setTimeout(resolve, delay)
})
module.exports.whilst = whilst module.exports.whilst = whilst
module.exports.apply = apply module.exports.apply = apply
module.exports.waterfall = waterfall module.exports.waterfall = waterfall
module.exports.each = each module.exports.each = each
module.exports.wait = wait

Loading…
Cancel
Save