fix(xen-api): fix sync test in watchTask (#2868)

This commit is contained in:
Julien Fontanet 2018-04-12 18:02:51 +02:00 committed by GitHub
parent eb090e4874
commit dcd007c5c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -182,20 +182,20 @@ const EMPTY_ARRAY = freezeObject([])
// ------------------------------------------------------------------- // -------------------------------------------------------------------
const getTaskResult = (task, onSuccess, onFailure) => { const getTaskResult = task => {
const { status } = task const { status } = task
if (status === 'cancelled') { if (status === 'cancelled') {
return [onFailure(new Cancel('task canceled'))] return Promise.reject(new Cancel('task canceled'))
} }
if (status === 'failure') { if (status === 'failure') {
return [onFailure(wrapError(task.error_info))] return Promise.reject(wrapError(task.error_info))
} }
if (status === 'success') { if (status === 'success') {
// the result might be: // the result might be:
// - empty string // - empty string
// - an opaque reference // - an opaque reference
// - an XML-RPC value // - an XML-RPC value
return [onSuccess(task.result)] return Promise.resolve(task.result)
} }
} }
@ -642,11 +642,11 @@ export class Xapi extends EventEmitter {
let watcher = watchers[ref] let watcher = watchers[ref]
if (watcher === undefined) { if (watcher === undefined) {
// sync check if the task is already settled // sync check if the task is already settled
const task = this.objects.all[ref] const task = this._objectsByRefs[ref]
if (task !== undefined) { if (task !== undefined) {
const result = getTaskResult(task, Promise.resolve, Promise.reject) const result = getTaskResult(task)
if (result) { if (result !== undefined) {
return result[0] return result
} }
} }
@ -793,11 +793,12 @@ export class Xapi extends EventEmitter {
const taskWatchers = this._taskWatchers const taskWatchers = this._taskWatchers
const taskWatcher = taskWatchers[ref] const taskWatcher = taskWatchers[ref]
if ( if (taskWatcher !== undefined) {
taskWatcher !== undefined && const result = getTaskResult(object)
getTaskResult(object, taskWatcher.resolve, taskWatcher.reject) if (result !== undefined) {
) { taskWatcher.resolve(result)
delete taskWatchers[ref] delete taskWatchers[ref]
}
} }
} }
} }