Merge pull request #226 from hybridgroup/fix/halt-and-disconnect

Restructure Halt/Disconnect Structure
This commit is contained in:
Adrian Zankich 2014-09-05 10:53:28 -07:00
commit 174fe2416f
9 changed files with 30 additions and 79 deletions

View File

@ -41,17 +41,6 @@ Adaptor.prototype.connect = function(callback) {
callback(null);
};
// Public: Disconnects from the adaptor
//
// callback - function to run when the adaptor is disconnected
//
// Returns nothing
Adaptor.prototype.disconnect = function(callback) {
Logger.info("Disconnecting from adaptor '" + this.name + "'.");
this.removeAllListeners();
callback(null);
};
// Public: Voids all command functions so they do not interact
// with anything after disconnect has been called.
//

View File

@ -75,6 +75,7 @@ Connection.prototype.connect = function(callback) {
Connection.prototype.disconnect = function(callback) {
var msg = this._logstring("Disconnecting from");
Logger.info(msg);
this.removeAllListeners();
return this.adaptor.disconnect(callback);
};

View File

@ -79,6 +79,7 @@ Device.prototype.start = function(callback) {
// Returns result of supplied callback
Device.prototype.halt = function(callback) {
Logger.info("Halting device '" + this.name + "'.");
this.removeAllListeners();
this.driver.halt(callback);
};

View File

@ -62,16 +62,5 @@ Driver.prototype.setupCommands = function(commands, proxy) {
// Returns nothing
Driver.prototype.start = function(callback) {
Logger.info("Driver " + this.name + " started.");
callback(null);
};
// Public: Halts the driver
//
// callback - function to be triggered when the driver is halted
//
// Returns nothing
Driver.prototype.halt = function(callback) {
Logger.info("Driver " + this.name + " halted.");
this.removeAllListeners();
callback(null);
callback();
};

View File

@ -303,7 +303,10 @@ Robot.prototype.halt = function(callback) {
for (var d in this.devices) {
var device = this.devices[d];
fns.push(device.halt.bind(device));
fns.push(function(callback) {
device.halt.call(device, callback);
});
}
Async.parallel(fns, function() {
@ -311,7 +314,10 @@ Robot.prototype.halt = function(callback) {
for (var c in this.connections) {
var connection = this.connections[c];
fns.push(connection.disconnect.bind(connection));
fns.push(function(callback) {
connection.disconnect.call(connection, callback);
});
}
Async.parallel(fns, callback);

View File

@ -47,29 +47,6 @@ describe("Adaptor", function() {
});
});
describe("#disconnect", function() {
var callback;
beforeEach(function() {
callback = spy();
stub(Logger, 'info');
adaptor.disconnect(callback);
});
afterEach(function() {
Logger.info.restore();
});
it("logs that it's disconnecting to the adaptor", function() {
var string = "Disconnecting from adaptor 'adaptor'.";
expect(Logger.info).to.be.calledWith(string);
});
it("triggers the callback", function() {
expect(callback).to.be.called;
})
});
describe("#_noop", function() {
var hello;

View File

@ -74,13 +74,15 @@ describe("Connection", function() {
describe("#disconnect", function() {
beforeEach(function() {
stub(Logger, 'info').returns(true);
stub(connection.adaptor, 'disconnect').returns(true);
stub(connection, 'removeAllListeners');
connection.adaptor.disconnect = stub().returns(true);
connection.disconnect();
});
afterEach(function() {
connection.adaptor.disconnect.restore();
connection.removeAllListeners.restore();
Logger.info.restore();
});
@ -92,5 +94,9 @@ describe("Connection", function() {
it("tells the adaptor to disconnect", function() {
expect(connection.adaptor.disconnect).to.be.called;
});
it("disconnects all event listeners", function() {
expect(connection.removeAllListeners).to.be.called;
});
});
});

View File

@ -101,26 +101,30 @@ describe("Device", function() {
describe("#halt", function() {
beforeEach(function() {
stub(driver, 'halt').returns(true);
stub(Logger, 'info');
driver.halt = stub().returns(true);
device.removeAllListeners = spy();
device.halt();
});
afterEach(function() {
driver.halt.restore();
Logger.info.restore();
});
it("halts the driver", function() {
device.halt();
expect(driver.halt).to.be.called;
});
it("logs that it's halt the device", function() {
var message = "Halting device 'ping'.";
stub(Logger, 'info');
device.halt();
expect(Logger.info).to.be.calledWith(message);
Logger.info.restore();
});
it("disconnects all event listeners", function() {
expect(device.removeAllListeners).to.be.called;
});
});

View File

@ -98,26 +98,4 @@ describe("Driver", function() {
expect(callback).to.be.called;
});
});
describe("#halt", function() {
var callback;
beforeEach(function() {
callback = spy();
stub(Logger, 'info');
driver.halt(callback);
});
afterEach(function() {
Logger.info.restore();
});
it("logs that it's halting the driver", function() {
expect(Logger.info).to.be.calledWith("Driver driver halted.")
});
it("triggers the callback", function() {
expect(callback).to.be.called;
})
});
});