Skip to content

Commit

Permalink
fix: close socket while pulling message timeout (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
gxcsoccer authored May 10, 2019
1 parent d1ca295 commit d477806
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ class Channel extends Base {
id: command.opaque,
data: command.encode(),
timeout,
}).catch(err => {
// TODO: not sure whether is work ?
if (err.name === 'ResponseTimeoutError') {
this.close();
}
throw err;
});
}

Expand Down
25 changes: 25 additions & 0 deletions test/channel.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const mm = require('mm');
const net = require('net');
const assert = require('assert');
const urllib = require('urllib');
const sleep = require('mz-modules/sleep');
const Channel = require('../lib/channel');
const config = require('../example/config');
const RequestCode = require('../lib/protocol/request_code');
Expand Down Expand Up @@ -91,6 +93,29 @@ describe('test/channel.test.js', function() {
await channel.ready();
});

it('should close if timeout', async () => {
const server = net.createServer();
server.listen(9876);
await sleep(100);

const channel = new Channel('127.0.0.1:9876');
await assert.rejects(async () => {
await channel.invoke(new RemotingCommand({
code: RequestCode.GET_ROUTEINTO_BY_TOPIC,
customHeader: {
topic: TOPIC,
},
}), 5000);
}, {
name: 'ResponseTimeoutError',
});
await sleep(100);

assert(!channel._socket);

server.close();
});

// it('should emit error if connect failed', function(done) {
// done = pedding(done, 2);
// const channel = new Channel('100.100.100.100:9876');
Expand Down

0 comments on commit d477806

Please sign in to comment.