test(sio-client): reduce test suite duration

This commit is contained in:
Damien Arrachequesne
2024-09-18 07:30:43 +02:00
parent fcbecd4f46
commit 09f573cad8
14 changed files with 58 additions and 43 deletions

View File

@@ -53,7 +53,7 @@
"scripts": { "scripts": {
"compile": "rimraf ./build && tsc && tsc -p tsconfig.esm.json && ./postcompile.sh", "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": "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:browser": "ts-node test/browser-runner.ts",
"test:types": "tsd", "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", "build": "rollup -c support/rollup.config.umd.js && rollup -c support/rollup.config.esm.js && rollup -c support/rollup.config.umd.msgpack.js",

View File

@@ -1,4 +1,4 @@
import expect from "expect.js"; import expect = require("expect.js");
import { io } from ".."; import { io } from "..";
import { wrap, BASE_URL, success } from "./support/util"; import { wrap, BASE_URL, success } from "./support/util";
@@ -7,6 +7,7 @@ describe("connection state recovery", () => {
return wrap((done) => { return wrap((done) => {
const socket = io(BASE_URL, { const socket = io(BASE_URL, {
forceNew: true, forceNew: true,
reconnectionDelay: 10,
}); });
expect(socket.recovered).to.eql(false); expect(socket.recovered).to.eql(false);

View File

@@ -1,4 +1,4 @@
import expect from "expect.js"; import expect = require("expect.js");
import { io, Manager, ManagerOptions } from ".."; import { io, Manager, ManagerOptions } from "..";
import hasCORS from "has-cors"; import hasCORS from "has-cors";
import { install } from "@sinonjs/fake-timers"; import { install } from "@sinonjs/fake-timers";
@@ -158,7 +158,7 @@ describe("connection", () => {
it("should reconnect by default", () => { it("should reconnect by default", () => {
return wrap((done) => { 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.io.on("reconnect", () => {
socket.disconnect(); socket.disconnect();
done(); done();
@@ -166,7 +166,7 @@ describe("connection", () => {
setTimeout(() => { setTimeout(() => {
socket.io.engine.close(); socket.io.engine.close();
}, 500); }, 100);
}); });
}); });
@@ -189,7 +189,7 @@ describe("connection", () => {
it("should reconnect automatically after reconnecting manually", () => { it("should reconnect automatically after reconnecting manually", () => {
return wrap((done) => { return wrap((done) => {
const socket = io(BASE_URL, { forceNew: true }); const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 10 });
socket socket
.once("connect", () => { .once("connect", () => {
socket.disconnect(); socket.disconnect();
@@ -202,7 +202,7 @@ describe("connection", () => {
socket.connect(); socket.connect();
setTimeout(() => { setTimeout(() => {
socket.io.engine.close(); socket.io.engine.close();
}, 500); }, 100);
}); });
}); });
}); });
@@ -281,13 +281,13 @@ describe("connection", () => {
}); });
socket.io.once("error", () => { socket.io.once("error", () => {
socket.io.on("reconnect_attempt", () => { socket.io.on("reconnect_attempt", () => {
expect().fail(); done(new Error("should not happen"));
}); });
socket.disconnect(); socket.disconnect();
// set a timeout to let reconnection possibly fire // set a timeout to let reconnection possibly fire
setTimeout(() => { setTimeout(() => {
done(); done();
}, 500); }, 100);
}); });
}); });
}); });
@@ -301,13 +301,13 @@ describe("connection", () => {
}); });
socket.io.once("reconnect_attempt", () => { socket.io.once("reconnect_attempt", () => {
socket.io.on("reconnect_attempt", () => { socket.io.on("reconnect_attempt", () => {
expect().fail(); done(new Error("should not happen"));
}); });
socket.disconnect(); socket.disconnect();
// set a timeout to let reconnection possibly fire // set a timeout to let reconnection possibly fire
setTimeout(() => { setTimeout(() => {
done(); done();
}, 500); }, 100);
}); });
}); });
}); });
@@ -332,27 +332,29 @@ describe("connection", () => {
it("should stop reconnecting on a socket and keep to reconnect on another", () => { it("should stop reconnecting on a socket and keep to reconnect on another", () => {
return wrap((done) => { return wrap((done) => {
const manager = new Manager(BASE_URL); const manager = new Manager(BASE_URL, {
reconnectionDelay: 10,
});
const socket1 = manager.socket("/"); const socket1 = manager.socket("/");
const socket2 = manager.socket("/asd"); const socket2 = manager.socket("/asd");
manager.on("reconnect_attempt", () => { manager.on("reconnect_attempt", () => {
socket1.on("connect", () => { socket1.on("connect", () => {
expect().fail(); done(new Error("should not happen"));
}); });
socket2.on("connect", () => { socket2.on("connect", () => {
setTimeout(() => { setTimeout(() => {
socket2.disconnect(); socket2.disconnect();
manager._close(); manager._close();
done(); done();
}, 500); }, 50);
}); });
socket1.disconnect(); socket1.disconnect();
}); });
setTimeout(() => { setTimeout(() => {
manager.engine.close(); manager.engine.close();
}, 1000); }, 100);
}); });
}); });
@@ -441,20 +443,19 @@ describe("connection", () => {
reconnection: true, reconnection: true,
reconnectionDelay: 10, 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", () => { socket.on("connect", () => {
// set a timeout to let reconnection possibly fire // set a timeout to let reconnection possibly fire
setTimeout(() => { setTimeout(() => {
socket.close(); socket.close();
manager._close(); manager._close();
done(); done();
}, 1000); }, 100);
}); });
}); });
}); });
@@ -549,12 +550,12 @@ describe("connection", () => {
return wrap((done) => { return wrap((done) => {
const manager = new Manager("http://localhost:9823", { const manager = new Manager("http://localhost:9823", {
reconnection: false, 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", () => { manager.on("error", () => {
// set a timeout to let reconnection possibly fire // set a timeout to let reconnection possibly fire
@@ -562,7 +563,7 @@ describe("connection", () => {
socket.disconnect(); socket.disconnect();
manager._close(); manager._close();
done(); done();
}, 1000); }, 100);
}); });
var socket = manager.socket("/invalid"); var socket = manager.socket("/invalid");
@@ -574,6 +575,7 @@ describe("connection", () => {
const manager = new Manager("http://localhost:9823", { const manager = new Manager("http://localhost:9823", {
reconnection: true, reconnection: true,
reconnectionAttempts: 2, reconnectionAttempts: 2,
reconnectionDelay: 10,
}); });
let delay = Math.floor( let delay = Math.floor(
manager.reconnectionDelay() * manager.randomizationFactor() * 0.5, manager.reconnectionDelay() * manager.randomizationFactor() * 0.5,

View File

@@ -5,4 +5,4 @@ const socket = io("http://localhost:3211", {
setTimeout(() => { setTimeout(() => {
console.log("process should not exit"); console.log("process should not exit");
}, 500); }, 50);

View File

@@ -9,4 +9,4 @@ socket.on("open", () => {
setTimeout(() => { setTimeout(() => {
console.log("process should exit now"); console.log("process should exit now");
}, 500); }, 50);

View File

@@ -10,4 +10,4 @@ socket.on("open", () => {
setTimeout(() => { setTimeout(() => {
console.log("process should exit now"); console.log("process should exit now");
}, 500); }, 50);

View File

@@ -10,4 +10,4 @@ socket.on("open", () => {
setTimeout(() => { setTimeout(() => {
console.log("process should exit now"); console.log("process should exit now");
}, 500); }, 50);

View File

@@ -9,4 +9,4 @@ socket.on("open", () => {
setTimeout(() => { setTimeout(() => {
console.log("process should exit now"); console.log("process should exit now");
}, 500); }, 50);

View File

@@ -34,6 +34,6 @@ describe("autoUnref option", function () {
setTimeout(() => { setTimeout(() => {
process.kill(); process.kill();
done(); done();
}, 1000); }, 100);
}); });
}); });

View File

@@ -1,4 +1,4 @@
import expect from "expect.js"; import expect = require("expect.js");
import { io } from ".."; import { io } from "..";
import { wrap, BASE_URL, success } from "./support/util"; import { wrap, BASE_URL, success } from "./support/util";

View File

@@ -1,4 +1,4 @@
import expect from "expect.js"; import expect = require("expect.js");
import { io } from ".."; import { io } from "..";
import { wrap, BASE_URL, success } from "./support/util"; import { wrap, BASE_URL, success } from "./support/util";
@@ -120,7 +120,7 @@ describe("socket", () => {
it("should change socket.id upon reconnection", () => { it("should change socket.id upon reconnection", () => {
return wrap((done) => { return wrap((done) => {
const socket = io(BASE_URL, { forceNew: true }); const socket = io(BASE_URL, { forceNew: true, reconnectionDelay: 10 });
socket.on("connect", () => { socket.on("connect", () => {
const id = socket.id; const id = socket.id;
@@ -328,7 +328,7 @@ describe("socket", () => {
setTimeout(() => { setTimeout(() => {
expect(count).to.eql(1); expect(count).to.eql(1);
success(done, socket); success(done, socket);
}, 200); }, 100);
}); });
}); });
@@ -630,7 +630,7 @@ describe("socket", () => {
try { try {
await socket.timeout(50).emitWithAck("unknown"); await socket.timeout(50).emitWithAck("unknown");
expect().fail(); done(new Error("should not happen"));
} catch (e) { } catch (e) {
success(done, socket); success(done, socket);
} }
@@ -646,7 +646,7 @@ describe("socket", () => {
expect(value).to.be(42); expect(value).to.be(42);
success(done, socket); success(done, socket);
} catch (e) { } catch (e) {
expect().fail(); done(new Error("should not happen"));
} }
}); });
}); });

View File

@@ -1,5 +1,5 @@
import { Server } from "socket.io"; import { Server } from "socket.io";
import expect from "expect.js"; import expect = require("expect.js");
export function createServer() { export function createServer() {
const server = new Server(3210, { const server = new Server(3210, {

View File

@@ -11,7 +11,19 @@
* @param fn * @param fn
*/ */
export function wrap(fn: (done: (err?: Error) => void) => void) { export function wrap(fn: (done: (err?: Error) => void) => void) {
return new Promise<Error>((resolve) => fn(resolve)); let once = true;
return new Promise<void>((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) { export function success(done, socket) {

View File

@@ -1,5 +1,5 @@
import { url } from "../build/cjs/url"; import { url } from "../build/cjs/url";
import expect from "expect.js"; import expect = require("expect.js");
const loc: any = {}; const loc: any = {};