Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ function Local(){

this.stop = function (callback) {
if(!this.pid) return callback();
var that = this;
this.killAllProcesses(function(error){
if(error) callback(new LocalError(error.toString()));
// A kill error for a process that has already exited (a repeat stop(), or
// taskkill on Windows reporting the pid as not found) still means the
// tunnel is stopped, so it is reported as success.
if(error && running(that.pid)) return callback(new LocalError(error.toString()));
that.pid = undefined;
that.isProcessRunning = false;
callback();
});
};
Expand Down
93 changes: 93 additions & 0 deletions test/local_stop_callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var expect = require('expect.js'),
childProcess = require('child_process'),
browserstack = require('../index');

// Regression tests for the stop() double callback.
//
// `Local.stop` used to fall through from its error branch, so a failed kill
// called back twice: once with a LocalError and then again with no error.
// These tests stub `killAllProcesses`, so no binary is started and no process
// is signalled, and assert that every path calls back exactly once.
describe('Local.stop callback', function () {
var bsLocal;

// Pid of a process that has already exited.
function deadPid() {
return childProcess.spawnSync(process.execPath, ['-e', '']).pid;
}

// Runs stop() with the given kill result and collects every callback
// invocation. Waits past the kill callback before reporting, so a second
// invocation would have happened by now if it were going to.
function stopWith(killError, done) {
var calls = [], kills = 0;
bsLocal.killAllProcesses = function (callback) {
kills++;
callback(killError);
};
bsLocal.stop(function (error) { calls.push(error); });
setTimeout(function () { done(calls, kills); }, 50);
}

beforeEach(function () {
bsLocal = new browserstack.Local();
});

it('reports a kill error exactly once while the process is still running', function (done) {
// The test runner's own pid is guaranteed to be running; the kill is stubbed.
bsLocal.pid = process.pid;
bsLocal.isProcessRunning = true;
stopWith(new Error('kill failed'), function (calls, kills) {
expect(kills).to.equal(1);
expect(calls.length).to.equal(1);
expect(calls[0]).to.be.an(Error);
expect(calls[0].name).to.equal('LocalError');
expect(calls[0].message).to.match(/kill failed/);
expect(bsLocal.pid).to.equal(process.pid);
done();
});
});

it('reports success exactly once when the kill fails because the process has already exited', function (done) {
bsLocal.pid = deadPid();
bsLocal.isProcessRunning = true;
stopWith(new Error('process not found'), function (calls) {
expect(calls).to.eql([undefined]);
expect(bsLocal.pid).to.be(undefined);
expect(bsLocal.isProcessRunning).to.be(false);
done();
});
});

it('reports success exactly once and clears the process state when the kill succeeds', function (done) {
bsLocal.pid = process.pid;
bsLocal.isProcessRunning = true;
stopWith(null, function (calls) {
expect(calls).to.eql([undefined]);
expect(bsLocal.pid).to.be(undefined);
expect(bsLocal.isProcessRunning).to.be(false);
done();
});
});

it('does not attempt a kill when there is no process', function (done) {
stopWith(new Error('should not be called'), function (calls, kills) {
expect(kills).to.equal(0);
expect(calls).to.eql([undefined]);
done();
});
});

it('does not attempt a second kill when stopped twice', function (done) {
bsLocal.pid = process.pid;
stopWith(null, function (firstCalls, firstKills) {
stopWith(new Error('should not be called'), function (secondCalls, secondKills) {
expect(firstKills).to.equal(1);
expect(secondKills).to.equal(0);
expect(firstCalls).to.eql([undefined]);
expect(secondCalls).to.eql([undefined]);
done();
});
});
});
});
Loading