Skip to content

Commit 488f367

Browse files
fix: background reconnection task still running after calling disconnect (#36)
Signed-off-by: Rudren Ganatra <rudren.ganatra@kasada.io>
1 parent 8387aa0 commit 488f367

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/socket.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@ export class FluentSocket extends EventEmitter {
673673
*/
674674
public disconnect(): Promise<void> {
675675
return new Promise(resolve => {
676+
if (this.reconnectTimeoutId !== null) {
677+
clearTimeout(this.reconnectTimeoutId);
678+
this.reconnectTimeoutId = null;
679+
}
676680
if (this.socket !== null) {
677681
this.state = SocketState.DISCONNECTING;
678682
this.socket.end(resolve);

test/test.socket.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,34 @@ describe("FluentSocket", () => {
262262
});
263263
});
264264

265+
it("should not reconnect after disconnect if the remote socket is unavilable", done => {
266+
const {socket, stream, connectStub} = createFluentSocket({reconnect: {}});
267+
268+
expect((<any>socket).reconnectEnabled).to.be.true;
269+
270+
socket.connect();
271+
272+
sinon.assert.calledOnce(connectStub);
273+
274+
const spy = sinon.spy(socket, "connect");
275+
276+
socket.once(FluentSocketEvent.WRITABLE, async () => {
277+
socket.once(FluentSocketEvent.CLOSE, async () => {
278+
setImmediate(async () => {
279+
expect((<any>socket).reconnectTimeoutId).not.to.be.null;
280+
await expect(socket.disconnect()).to.eventually.be.equal(undefined);
281+
expect((<any>socket).reconnectTimeoutId).to.be.null;
282+
sinon.assert.notCalled(spy);
283+
done();
284+
});
285+
});
286+
287+
setImmediate(() => {
288+
stream.socket.emit("close");
289+
});
290+
});
291+
});
292+
265293
it("should reject writes if socket is not writable", done => {
266294
const {socket, stream, connectStub} = createFluentSocket();
267295

0 commit comments

Comments
 (0)