From 09f573cad83c2da33ade0afd0ee3c6d41e1d60c3 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 18 Sep 2024 07:30:43 +0200 Subject: [PATCH] test(sio-client): reduce test suite duration --- packages/socket.io-client/package.json | 2 +- .../test/connection-state-recovery.ts | 3 +- packages/socket.io-client/test/connection.ts | 54 ++++++++++--------- .../test/fixtures/no-unref.ts | 2 +- .../fixtures/unref-during-reconnection.ts | 2 +- .../test/fixtures/unref-polling-only.ts | 2 +- .../test/fixtures/unref-websocket-only.ts | 2 +- .../socket.io-client/test/fixtures/unref.ts | 2 +- packages/socket.io-client/test/node.ts | 2 +- packages/socket.io-client/test/retry.ts | 2 +- packages/socket.io-client/test/socket.ts | 10 ++-- .../socket.io-client/test/support/server.ts | 2 +- .../socket.io-client/test/support/util.ts | 14 ++++- packages/socket.io-client/test/url.ts | 2 +- 14 files changed, 58 insertions(+), 43 deletions(-) diff --git a/packages/socket.io-client/package.json b/packages/socket.io-client/package.json index f5d8ed48..6cde908f 100644 --- a/packages/socket.io-client/package.json +++ b/packages/socket.io-client/package.json @@ -53,7 +53,7 @@ "scripts": { "compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh", "test": "npm run format:check && npm run compile && if test \"$BROWSERS\" = \"1\" ; then npm run test:browser; else npm run test:node; fi", - "test:node": "mocha --require ts-node/register --reporter dot --require test/support/hooks.ts --timeout 5000 --exit test/index.ts", + "test:node": "mocha --require ts-node/register --require test/support/hooks.ts --exit test/index.ts", "test:browser": "ts-node test/browser-runner.ts", "test:types": "tsd", "build": "rollup -c support/rollup.config.umd.js && rollup -c support/rollup.config.esm.js && rollup -c support/rollup.config.umd.msgpack.js", diff --git a/packages/socket.io-client/test/connection-state-recovery.ts b/packages/socket.io-client/test/connection-state-recovery.ts index 3567b0de..42c826b0 100644 --- a/packages/socket.io-client/test/connection-state-recovery.ts +++ b/packages/socket.io-client/test/connection-state-recovery.ts @@ -1,4 +1,4 @@ -import expect from "expect.js"; +import expect = require("expect.js"); import { io } from ".."; import { wrap, BASE_URL, success } from "./support/util"; @@ -7,6 +7,7 @@ describe("connection state recovery", () => { return wrap((done) => { const socket = io(BASE_URL, { forceNew: true, + reconnectionDelay: 10, }); expect(socket.recovered).to.eql(false); diff --git a/packages/socket.io-client/test/connection.ts b/packages/socket.io-client/test/connection.ts index b8c90b79..9a922e95 100644 --- a/packages/socket.io-client/test/connection.ts +++ b/packages/socket.io-client/test/connection.ts @@ -1,4 +1,4 @@ -import expect from "expect.js"; +import expect = require("expect.js"); import { io, Manager, ManagerOptions } from ".."; import hasCORS from "has-cors"; import { install } from "@sinonjs/fake-timers"; @@ -158,7 +158,7 @@ describe("connection", () => { it("should reconnect by default", () => { return wrap((done) => { - const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 0 }); + const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 10 }); socket.io.on("reconnect", () => { socket.disconnect(); done(); @@ -166,7 +166,7 @@ describe("connection", () => { setTimeout(() => { socket.io.engine.close(); - }, 500); + }, 100); }); }); @@ -189,7 +189,7 @@ describe("connection", () => { it("should reconnect automatically after reconnecting manually", () => { return wrap((done) => { - const socket = io(BASE_URL, { forceNew: true }); + const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 10 }); socket .once("connect", () => { socket.disconnect(); @@ -202,7 +202,7 @@ describe("connection", () => { socket.connect(); setTimeout(() => { socket.io.engine.close(); - }, 500); + }, 100); }); }); }); @@ -281,13 +281,13 @@ describe("connection", () => { }); socket.io.once("error", () => { socket.io.on("reconnect_attempt", () => { - expect().fail(); + done(new Error("should not happen")); }); socket.disconnect(); // set a timeout to let reconnection possibly fire setTimeout(() => { done(); - }, 500); + }, 100); }); }); }); @@ -301,13 +301,13 @@ describe("connection", () => { }); socket.io.once("reconnect_attempt", () => { socket.io.on("reconnect_attempt", () => { - expect().fail(); + done(new Error("should not happen")); }); socket.disconnect(); // set a timeout to let reconnection possibly fire setTimeout(() => { done(); - }, 500); + }, 100); }); }); }); @@ -332,27 +332,29 @@ describe("connection", () => { it("should stop reconnecting on a socket and keep to reconnect on another", () => { return wrap((done) => { - const manager = new Manager(BASE_URL); + const manager = new Manager(BASE_URL, { + reconnectionDelay: 10, + }); const socket1 = manager.socket("/"); const socket2 = manager.socket("/asd"); manager.on("reconnect_attempt", () => { socket1.on("connect", () => { - expect().fail(); + done(new Error("should not happen")); }); socket2.on("connect", () => { setTimeout(() => { socket2.disconnect(); manager._close(); done(); - }, 500); + }, 50); }); socket1.disconnect(); }); setTimeout(() => { manager.engine.close(); - }, 1000); + }, 100); }); }); @@ -441,20 +443,19 @@ describe("connection", () => { reconnection: true, reconnectionDelay: 10, }); - const cb = () => { - socket.close(); - expect().fail(); - }; - manager.on("reconnect_attempt", cb); - var socket = manager.socket("/valid"); + manager.on("reconnect_attempt", () => { + done(new Error("should not happen")); + }); + + const socket = manager.socket("/valid"); socket.on("connect", () => { // set a timeout to let reconnection possibly fire setTimeout(() => { socket.close(); manager._close(); done(); - }, 1000); + }, 100); }); }); }); @@ -549,12 +550,12 @@ describe("connection", () => { return wrap((done) => { const manager = new Manager("http://localhost:9823", { reconnection: false, + reconnectionDelay: 10, + }); + + manager.on("reconnect_attempt", () => { + done(new Error("should not happen")); }); - const cb = () => { - socket.close(); - expect().fail(); - }; - manager.on("reconnect_attempt", cb); manager.on("error", () => { // set a timeout to let reconnection possibly fire @@ -562,7 +563,7 @@ describe("connection", () => { socket.disconnect(); manager._close(); done(); - }, 1000); + }, 100); }); var socket = manager.socket("/invalid"); @@ -574,6 +575,7 @@ describe("connection", () => { const manager = new Manager("http://localhost:9823", { reconnection: true, reconnectionAttempts: 2, + reconnectionDelay: 10, }); let delay = Math.floor( manager.reconnectionDelay() * manager.randomizationFactor() * 0.5, diff --git a/packages/socket.io-client/test/fixtures/no-unref.ts b/packages/socket.io-client/test/fixtures/no-unref.ts index a96c4e1a..5e0db6f4 100644 --- a/packages/socket.io-client/test/fixtures/no-unref.ts +++ b/packages/socket.io-client/test/fixtures/no-unref.ts @@ -5,4 +5,4 @@ const socket = io("http://localhost:3211", { setTimeout(() => { console.log("process should not exit"); -}, 500); +}, 50); diff --git a/packages/socket.io-client/test/fixtures/unref-during-reconnection.ts b/packages/socket.io-client/test/fixtures/unref-during-reconnection.ts index af585e8d..8bcc4448 100644 --- a/packages/socket.io-client/test/fixtures/unref-during-reconnection.ts +++ b/packages/socket.io-client/test/fixtures/unref-during-reconnection.ts @@ -9,4 +9,4 @@ socket.on("open", () => { setTimeout(() => { console.log("process should exit now"); -}, 500); +}, 50); diff --git a/packages/socket.io-client/test/fixtures/unref-polling-only.ts b/packages/socket.io-client/test/fixtures/unref-polling-only.ts index a3ca9e36..c3d256bb 100644 --- a/packages/socket.io-client/test/fixtures/unref-polling-only.ts +++ b/packages/socket.io-client/test/fixtures/unref-polling-only.ts @@ -10,4 +10,4 @@ socket.on("open", () => { setTimeout(() => { console.log("process should exit now"); -}, 500); +}, 50); diff --git a/packages/socket.io-client/test/fixtures/unref-websocket-only.ts b/packages/socket.io-client/test/fixtures/unref-websocket-only.ts index 240b7c99..ff3052dc 100644 --- a/packages/socket.io-client/test/fixtures/unref-websocket-only.ts +++ b/packages/socket.io-client/test/fixtures/unref-websocket-only.ts @@ -10,4 +10,4 @@ socket.on("open", () => { setTimeout(() => { console.log("process should exit now"); -}, 500); +}, 50); diff --git a/packages/socket.io-client/test/fixtures/unref.ts b/packages/socket.io-client/test/fixtures/unref.ts index 63128ecd..d5f0161d 100644 --- a/packages/socket.io-client/test/fixtures/unref.ts +++ b/packages/socket.io-client/test/fixtures/unref.ts @@ -9,4 +9,4 @@ socket.on("open", () => { setTimeout(() => { console.log("process should exit now"); -}, 500); +}, 50); diff --git a/packages/socket.io-client/test/node.ts b/packages/socket.io-client/test/node.ts index 6e0b2575..b1b8112f 100644 --- a/packages/socket.io-client/test/node.ts +++ b/packages/socket.io-client/test/node.ts @@ -34,6 +34,6 @@ describe("autoUnref option", function () { setTimeout(() => { process.kill(); done(); - }, 1000); + }, 100); }); }); diff --git a/packages/socket.io-client/test/retry.ts b/packages/socket.io-client/test/retry.ts index bf791925..9d81f01e 100644 --- a/packages/socket.io-client/test/retry.ts +++ b/packages/socket.io-client/test/retry.ts @@ -1,4 +1,4 @@ -import expect from "expect.js"; +import expect = require("expect.js"); import { io } from ".."; import { wrap, BASE_URL, success } from "./support/util"; diff --git a/packages/socket.io-client/test/socket.ts b/packages/socket.io-client/test/socket.ts index a37331a5..2f067b1e 100644 --- a/packages/socket.io-client/test/socket.ts +++ b/packages/socket.io-client/test/socket.ts @@ -1,4 +1,4 @@ -import expect from "expect.js"; +import expect = require("expect.js"); import { io } from ".."; import { wrap, BASE_URL, success } from "./support/util"; @@ -120,7 +120,7 @@ describe("socket", () => { it("should change socket.id upon reconnection", () => { return wrap((done) => { - const socket = io(BASE_URL, { forceNew: true }); + const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 10 }); socket.on("connect", () => { const id = socket.id; @@ -328,7 +328,7 @@ describe("socket", () => { setTimeout(() => { expect(count).to.eql(1); success(done, socket); - }, 200); + }, 100); }); }); @@ -630,7 +630,7 @@ describe("socket", () => { try { await socket.timeout(50).emitWithAck("unknown"); - expect().fail(); + done(new Error("should not happen")); } catch (e) { success(done, socket); } @@ -646,7 +646,7 @@ describe("socket", () => { expect(value).to.be(42); success(done, socket); } catch (e) { - expect().fail(); + done(new Error("should not happen")); } }); }); diff --git a/packages/socket.io-client/test/support/server.ts b/packages/socket.io-client/test/support/server.ts index c582a5df..3003a6fe 100644 --- a/packages/socket.io-client/test/support/server.ts +++ b/packages/socket.io-client/test/support/server.ts @@ -1,5 +1,5 @@ import { Server } from "socket.io"; -import expect from "expect.js"; +import expect = require("expect.js"); export function createServer() { const server = new Server(3210, { diff --git a/packages/socket.io-client/test/support/util.ts b/packages/socket.io-client/test/support/util.ts index 0c514a83..25869743 100644 --- a/packages/socket.io-client/test/support/util.ts +++ b/packages/socket.io-client/test/support/util.ts @@ -11,7 +11,19 @@ * @param fn */ export function wrap(fn: (done: (err?: Error) => void) => void) { - return new Promise((resolve) => fn(resolve)); + let once = true; + return new Promise((resolve, reject) => { + fn((err) => { + if (!once) { + throw "done() was called multiple times"; + } else if (err) { + reject(err); + } else { + once = false; + resolve(); + } + }); + }); } export function success(done, socket) { diff --git a/packages/socket.io-client/test/url.ts b/packages/socket.io-client/test/url.ts index f595535d..03df463d 100644 --- a/packages/socket.io-client/test/url.ts +++ b/packages/socket.io-client/test/url.ts @@ -1,5 +1,5 @@ import { url } from "../build/cjs/url"; -import expect from "expect.js"; +import expect = require("expect.js"); const loc: any = {};