fix: tcp client memory leaks #10286

This commit is contained in:
regevbr
2022-09-16 17:40:06 +03:00
parent 7db5638d3f
commit a439b14c4d
2 changed files with 24 additions and 1 deletions

View File

@@ -108,6 +108,13 @@ export class ClientTCP extends ClientProxy {
this.isConnected = false;
this.socket = null;
this.connection = undefined;
if (this.routingMap.size > 0) {
const err = new Error('connection to server closed');
for (const callback of this.routingMap.values()) {
callback({ err });
}
this.routingMap.clear();
}
}
protected publish(

View File

@@ -154,9 +154,15 @@ describe('ClientTCP', () => {
});
});
describe('close', () => {
let routingMap;
let callback;
beforeEach(() => {
(client as any).socket = socket;
routingMap = new Map<string, Function>();
callback = sinon.spy();
routingMap.set('some id', callback)(client as any).socket = socket;
(client as any).isConnected = true;
(client as any).routingMap = routingMap;
client.close();
});
it('should end() socket', () => {
@@ -168,6 +174,16 @@ describe('ClientTCP', () => {
it('should set "socket" to null', () => {
expect((client as any).socket).to.be.null;
});
it('should clear out the routing map', () => {
expect((client as any).routingMap.size).to.be.eq(0);
});
it('should call callbacks', () => {
expect(
callback.calledWith({
err: new Error('connection to server closed'),
}),
).to.be.true;
});
});
describe('bindEvents', () => {
it('should bind error event handler', () => {