From 06861ceb55c8e7e27805717722529c6f1bc52f75 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 12 May 2014 21:45:49 -0700 Subject: [PATCH] Add `Job#resetting` flag. --- test/saucelabs.js | 54 +++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/test/saucelabs.js b/test/saucelabs.js index 72b47142f..aae53527c 100644 --- a/test/saucelabs.js +++ b/test/saucelabs.js @@ -328,6 +328,18 @@ function onJobRemove(error, res, body) { this.emit('remove'); } +/** + * The `Job#remove` callback used by `Jobs#reset`. + * + * @private + */ +function onJobReset() { + this.attempts = 0; + this.failed = this.resetting = false; + this._timerId = this.id = this.result = this.testId = this.url = null; + this.emit('reset'); +} + /** * The `request.post` callback used by `Jobs#start`. * @@ -481,7 +493,7 @@ function Job(properties) { _.defaults(this.options, _.cloneDeep(jobOptions)); this.attempts = 0; - this.checking = this.failed = this.removing = this.restarting = this.running = this.starting = this.stopping = false; + this.checking = this.failed = this.removing = this.resetting = this.restarting = this.running = this.starting = this.stopping = false; this._timerId = this.id = this.result = this.testId = this.url = null; } @@ -495,25 +507,22 @@ util.inherits(Job, EventEmitter); * @param {Object} Returns the job instance. */ Job.prototype.remove = function(callback) { - if (this.running) { - return this.stop(_.partial(this.remove, callback)); - } this.once('remove', _.callback(callback)); if (this.removing) { return this; } - var onRemove = _.bind(onJobRemove, this); - this.removing = true; - if (this.id == null) { - _.defer(onRemove); - return this; - } - request.del(_.template('https://saucelabs.com/rest/v1/${user}/jobs/${id}', this), { - 'auth': { 'user': this.user, 'pass': this.pass } - }, onRemove); - return this; + var onRemove = _.bind(onJobRemove, this); + return this.stop(function() { + if (this.id == null) { + onRemove(); + return; + } + request.del(_.template('https://saucelabs.com/rest/v1/${user}/jobs/${id}', this), { + 'auth': { 'user': this.user, 'pass': this.pass } + }, onRemove); + }); }; /** @@ -524,17 +533,12 @@ Job.prototype.remove = function(callback) { * @param {Object} Returns the job instance. */ Job.prototype.reset = function(callback) { - if (this.id != null) { - return this.remove(_.partial(this.reset, callback)); - } - this.attempts = 0; - this.failed = false; - this._timerId = this.id = this.result = this.testId = this.url = null; - this.once('reset', _.callback(callback)); - _.defer(_.bind(this.emit, this, 'reset')); - - return this; + if (this.resetting) { + return this; + } + this.resetting = true; + return this.remove(onJobReset); }; /** @@ -592,7 +596,7 @@ Job.prototype.start = function(callback) { */ Job.prototype.status = function(callback) { this.once('status', _.callback(callback)); - if (this.checking || this.removing || this.restarting || this.starting || this.stopping) { + if (this.checking || this.removing || this.resetting || this.restarting || this.starting || this.stopping) { return this; } this.checking = true;