Skip to content

Commit

Permalink
fix: properly report timeout errors when connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jun 20, 2023
1 parent 781d753 commit cf9a50b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/manager.ts
Expand Up @@ -354,9 +354,8 @@ export class Manager<
const timer = this.setTimeoutFn(() => {
debug("connect attempt timed out after %d", timeout);
openSubDestroy();
this.onerror(new Error("timeout"));
socket.close();
// @ts-ignore
socket.emit("error", new Error("timeout"));
}, timeout);

if (this.opts.autoUnref) {
Expand Down
30 changes: 30 additions & 0 deletions test/socket.ts
Expand Up @@ -70,6 +70,36 @@ describe("socket", () => {
});
});

it("fire a connect_error event on open timeout (polling)", () => {
return wrap((done) => {
const socket = io(BASE_URL, {
forceNew: true,
transports: ["polling"],
timeout: 5,
});
socket.on("connect_error", (err) => {
expect(err.message).to.eql("timeout");
socket.disconnect();
done();
});
});
});

it("fire a connect_error event on open timeout (websocket)", () => {
return wrap((done) => {
const socket = io(BASE_URL, {
forceNew: true,
transports: ["websocket"],
timeout: 5,
});
socket.on("connect_error", (err) => {
expect(err.message).to.eql("timeout");
socket.disconnect();
done();
});
});
});

it("doesn't fire a connect_error event when the connection is already established", () => {
return wrap((done) => {
const socket = io(BASE_URL, { forceNew: true });
Expand Down

0 comments on commit cf9a50b

Please sign in to comment.