Skip to content

Commit

Permalink
#50: Try to close connections correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed May 18, 2014
1 parent 6839c15 commit aa645ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/modules/miners/bfgminer.js
Expand Up @@ -75,13 +75,15 @@ module.exports = Module.extend({
host: self.config.host,
port: self.config.port
}, function () {
socket.setTimeout(30000);
socket.on('data', function (rawData) {
data += rawData.toString();
});
socket.on('end', function () {
socket.removeAllListeners();
if (!errored) {
try {
socket.end();
resolve(JSON.parse(data.replace('\x00', '')));
} catch (e) {
reject(e);
Expand All @@ -96,6 +98,7 @@ module.exports = Module.extend({

socket.on('error', function (err) {
socket.removeAllListeners();
socket.destroy();
errored = true;
reject(err);
});
Expand Down
25 changes: 20 additions & 5 deletions test/specs/lib/modules/miners/bfgminerSpec.js
Expand Up @@ -207,6 +207,9 @@ describe('modules/miners/bfgminer', function () {

netEmitter.options = options;

netEmitter.destroy = sinon.stub();
netEmitter.end = sinon.stub();
netEmitter.setTimeout = sinon.stub();
netEmitter.write = function (written) {
var packet1,
packet2;
Expand Down Expand Up @@ -252,13 +255,16 @@ describe('modules/miners/bfgminer', function () {
{ config: config },
'some command',
'some parameters'
)).to.eventually.deep.equal(response).notify(function () {
)).to.eventually.deep.equal(response).notify(function (e) {
expect(netEmitter.options).to.deep.equal(config);
expect(netEmitter.written).to.deep.equal({
command: 'some command',
parameter: 'some parameters'
});
done();
expect(netEmitter.setTimeout).to.have.been.calledOnce;
expect(netEmitter.setTimeout).to.have.been.calledWith(30000);
expect(netEmitter.end).to.have.been.calledOnce;
done(e);
});
});

Expand All @@ -267,23 +273,32 @@ describe('modules/miners/bfgminer', function () {

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

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

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

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

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

Expand Down

0 comments on commit aa645ce

Please sign in to comment.