Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from "./error";
import * as protocol from "./protocol";
import {PassThrough, Duplex} from "stream";
import {awaitNextTick} from "./util";

/**
* Reconnection settings for the socket
Expand Down Expand Up @@ -345,7 +344,9 @@ export class FluentSocket extends EventEmitter {
) {
if (this.state === SocketState.DISCONNECTING) {
// Try again once the socket has fully closed
await awaitNextTick();
await new Promise(resolve =>
this.once(FluentSocketEvent.CLOSE, resolve)
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I wonder why I didn't just do this in the first place 🤷 I guess I read somewhere in the node docs that it will be disconnected by the next tick? not sure where I got that from.

return await this.connect();
} else {
// noop, we're connected
Expand Down
28 changes: 28 additions & 0 deletions test/test.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../src/error";
import {awaitNextTick, awaitTimeout} from "../src/util";
import {FluentSocketEvent} from "../src/socket";
import {FluentServer} from "../src";

chai.use(chaiAsPromised);
const expect = chai.expect;
Expand Down Expand Up @@ -518,6 +519,33 @@ describe("FluentClient", () => {
sinon.assert.calledTwice(spy);
});

it("should allow multiple connects and disconnects in succession", async () => {
const server = new FluentServer();
await server.listen();

try {
const client = new FluentClient("abc", {
socket: {
port: server.port,
},
disableAutoconnect: true,
});

await client.connect();
try {
const firstEvent = client.emit("a", {event: "foo bar"});

await client.disconnect();
await client.connect();
await expect(firstEvent).to.eventually.be.fulfilled;
} finally {
await client.shutdown();
}
} finally {
await server.close();
}
});

it("should reject pending events after shutdown", async () => {
const {client, socket} = createFluentClient("test");
socket.isWritable = false;
Expand Down