Skip to content

Commit

Permalink
#50: Correctly handle timeouts for bfgminer sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed May 19, 2014
1 parent aa645ce commit 2b67cda
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
19 changes: 12 additions & 7 deletions lib/modules/miners/bfgminer.js
Expand Up @@ -71,11 +71,19 @@ module.exports = Module.extend({

parameter = parameter || '';
return new Bluebird(function (resolve, reject) {
var onError = function (err) {
err = err || new Error('ETIMEOUT');

socket.removeAllListeners();
socket.destroy();
errored = true;
reject(err);
};

socket = net.connect({
host: self.config.host,
port: self.config.port
}, function () {
socket.setTimeout(30000);
socket.on('data', function (rawData) {
data += rawData.toString();
});
Expand All @@ -96,12 +104,9 @@ module.exports = Module.extend({
}));
});

socket.on('error', function (err) {
socket.removeAllListeners();
socket.destroy();
errored = true;
reject(err);
});
socket.setTimeout(self.config.interval);
socket.on('error', onError);
socket.on('timeout', onError);
});
},

Expand Down
26 changes: 22 additions & 4 deletions test/specs/lib/modules/miners/bfgminerSpec.js
Expand Up @@ -196,12 +196,12 @@ describe('modules/miners/bfgminer', function () {
});

describe('sendCommand', function () {
var config = { host: 'some host', port: 1234 },
var config = { host: 'some host', port: 1234, interval: 5000 },
response = { some: 'response' },
netEmitter,
responseString = JSON.stringify(response) + '\x00',
netModuleStub = {},
netConnect = function (responseData, throwErrorOnConnection, throwErrorOnTransmission) {
netConnect = function (responseData, throwErrorOnConnection, throwErrorOnTransmission, throwTimeout) {
return function (options, callback) {
netEmitter = new EventEmitter();

Expand Down Expand Up @@ -234,6 +234,10 @@ describe('modules/miners/bfgminer', function () {
setTimeout(function () {
netEmitter.emit('error', new Error('Error on connection'));
}, 10);
} else if (throwTimeout) {
setTimeout(function () {
netEmitter.emit('timeout');
}, 10);
} else {
setTimeout(callback, 20);
}
Expand All @@ -256,13 +260,16 @@ describe('modules/miners/bfgminer', function () {
'some command',
'some parameters'
)).to.eventually.deep.equal(response).notify(function (e) {
expect(netEmitter.options).to.deep.equal(config);
expect(netEmitter.options).to.deep.equal({
host: 'some host',
port: 1234
});
expect(netEmitter.written).to.deep.equal({
command: 'some command',
parameter: 'some parameters'
});
expect(netEmitter.setTimeout).to.have.been.calledOnce;
expect(netEmitter.setTimeout).to.have.been.calledWith(30000);
expect(netEmitter.setTimeout).to.have.been.calledWith(5000);
expect(netEmitter.end).to.have.been.calledOnce;
done(e);
});
Expand Down Expand Up @@ -300,6 +307,17 @@ describe('modules/miners/bfgminer', function () {
done(e);
});
});

it('should callback with an error when a timeout occurs', function (done) {
netModuleStub.connect = netConnect(responseString, false, false, true);

expect(BfgAdapter.prototype.sendCommand.call({
config: config
}, 'some command')).to.be.rejectedWith('ETIMEOUT').notify(function (e) {
expect(netEmitter.destroy).to.have.been.calledOnce;
done(e);
});
});
});

describe('handleSummaryResponse', function () {
Expand Down

0 comments on commit 2b67cda

Please sign in to comment.