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); 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 // Public: Voids all command functions so they do not interact
// with anything after disconnect has been called. // with anything after disconnect has been called.
// //

View File

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

View File

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

View File

@ -62,16 +62,5 @@ Driver.prototype.setupCommands = function(commands, proxy) {
// Returns nothing // Returns nothing
Driver.prototype.start = function(callback) { Driver.prototype.start = function(callback) {
Logger.info("Driver " + this.name + " started."); Logger.info("Driver " + this.name + " started.");
callback(null); callback();
};
// 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);
}; };

View File

@ -303,7 +303,10 @@ Robot.prototype.halt = function(callback) {
for (var d in this.devices) { for (var d in this.devices) {
var device = this.devices[d]; var device = this.devices[d];
fns.push(device.halt.bind(device));
fns.push(function(callback) {
device.halt.call(device, callback);
});
} }
Async.parallel(fns, function() { Async.parallel(fns, function() {
@ -311,7 +314,10 @@ Robot.prototype.halt = function(callback) {
for (var c in this.connections) { for (var c in this.connections) {
var connection = this.connections[c]; var connection = this.connections[c];
fns.push(connection.disconnect.bind(connection));
fns.push(function(callback) {
connection.disconnect.call(connection, callback);
});
} }
Async.parallel(fns, 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() { describe("#_noop", function() {
var hello; var hello;

View File

@ -74,13 +74,15 @@ describe("Connection", function() {
describe("#disconnect", function() { describe("#disconnect", function() {
beforeEach(function() { beforeEach(function() {
stub(Logger, 'info').returns(true); stub(Logger, 'info').returns(true);
stub(connection.adaptor, 'disconnect').returns(true); stub(connection, 'removeAllListeners');
connection.adaptor.disconnect = stub().returns(true);
connection.disconnect(); connection.disconnect();
}); });
afterEach(function() { afterEach(function() {
connection.adaptor.disconnect.restore(); connection.removeAllListeners.restore();
Logger.info.restore(); Logger.info.restore();
}); });
@ -92,5 +94,9 @@ describe("Connection", function() {
it("tells the adaptor to disconnect", function() { it("tells the adaptor to disconnect", function() {
expect(connection.adaptor.disconnect).to.be.called; 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() { describe("#halt", function() {
beforeEach(function() { beforeEach(function() {
stub(driver, 'halt').returns(true); stub(Logger, 'info');
driver.halt = stub().returns(true);
device.removeAllListeners = spy();
device.halt();
}); });
afterEach(function() { afterEach(function() {
driver.halt.restore(); Logger.info.restore();
}); });
it("halts the driver", function() { it("halts the driver", function() {
device.halt();
expect(driver.halt).to.be.called; expect(driver.halt).to.be.called;
}); });
it("logs that it's halt the device", function() { it("logs that it's halt the device", function() {
var message = "Halting device 'ping'."; var message = "Halting device 'ping'.";
stub(Logger, 'info');
device.halt();
expect(Logger.info).to.be.calledWith(message); 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; 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;
})
});
}); });