Skip to content

Commit

Permalink
test(sio-client): reduce test suite duration
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Sep 18, 2024
1 parent fcbecd4 commit 09f573c
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/socket.io-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion packages/socket.io-client/test/connection-state-recovery.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand Down
54 changes: 28 additions & 26 deletions packages/socket.io-client/test/connection.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -158,15 +158,15 @@ 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();
});

setTimeout(() => {
socket.io.engine.close();
}, 500);
}, 100);
});
});

Expand All @@ -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();
Expand All @@ -202,7 +202,7 @@ describe("connection", () => {
socket.connect();
setTimeout(() => {
socket.io.engine.close();
}, 500);
}, 100);
});
});
});
Expand Down Expand Up @@ -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);
});
});
});
Expand All @@ -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);
});
});
});
Expand All @@ -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);
});
});

Expand Down Expand Up @@ -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);
});
});
});
Expand Down Expand Up @@ -549,20 +550,20 @@ 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
setTimeout(() => {
socket.disconnect();
manager._close();
done();
}, 1000);
}, 100);
});

var socket = manager.socket("/invalid");
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/fixtures/no-unref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const socket = io("http://localhost:3211", {

setTimeout(() => {
console.log("process should not exit");
}, 500);
}, 50);
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ socket.on("open", () => {

setTimeout(() => {
console.log("process should exit now");
}, 500);
}, 50);
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ socket.on("open", () => {

setTimeout(() => {
console.log("process should exit now");
}, 500);
}, 50);
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ socket.on("open", () => {

setTimeout(() => {
console.log("process should exit now");
}, 500);
}, 50);
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/fixtures/unref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ socket.on("open", () => {

setTimeout(() => {
console.log("process should exit now");
}, 500);
}, 50);
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ describe("autoUnref option", function () {
setTimeout(() => {
process.kill();
done();
}, 1000);
}, 100);
});
});
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/retry.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
10 changes: 5 additions & 5 deletions packages/socket.io-client/test/socket.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -328,7 +328,7 @@ describe("socket", () => {
setTimeout(() => {
expect(count).to.eql(1);
success(done, socket);
}, 200);
}, 100);
});
});

Expand Down Expand Up @@ -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);
}
Expand All @@ -646,7 +646,7 @@ describe("socket", () => {
expect(value).to.be(42);
success(done, socket);
} catch (e) {
expect().fail();
done(new Error("should not happen"));
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/support/server.ts
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down
14 changes: 13 additions & 1 deletion packages/socket.io-client/test/support/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/socket.io-client/test/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { url } from "../build/cjs/url";
import expect from "expect.js";
import expect = require("expect.js");

const loc: any = {};

Expand Down

0 comments on commit 09f573c

Please sign in to comment.