From d47780628ef18be8fda5922c63c71b0bacc4c75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?z=C5=8Dng=20y=C7=94?= Date: Fri, 10 May 2019 15:45:50 +0800 Subject: [PATCH] fix: close socket while pulling message timeout (#80) --- lib/channel.js | 6 ++++++ test/channel.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/channel.js b/lib/channel.js index 89230af..8a7ff3a 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -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; }); } diff --git a/test/channel.test.js b/test/channel.test.js index 21f61bb..6761c67 100644 --- a/test/channel.test.js +++ b/test/channel.test.js @@ -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'); @@ -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');