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": {
"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",

View File

@@ -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);

View File

@@ -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,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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";

View File

@@ -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"));
}
});
});

View File

@@ -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, {

View File

@@ -11,7 +11,19 @@
* @param fn
*/
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) {

View File

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