socket.io-protocol
socket.io-protocol copied to clipboard
waitFor latency causes the test-suite to miss some packets
Currently, for each message check this function is called to wait for a message. However it causes a lot of failed checks because the latency of this function is quite high (because of the new event listener and the promise).
function waitFor(socket, eventType) {
return new Promise((resolve) => {
socket.addEventListener(
eventType,
(event) => {
resolve(event);
},
{ once: true }
);
});
}
It became really problematic with buffered messages. Recently I have added a flush mechanism on socketioxide to avoid flushing the websocket for each socket.io packet. Also because the engine.io connect packet is sent immediately after the websocket initialisation it may not be handled because of the same issue.
Here is an example where the waitFor function makes the test break and a hacky solution. However this happens almost everywhere. This issue can be reproductible even with the official test server with the following command to overload a bit the nodejs event loop :
seq 10 | parallel -j 10 'npm test -- -f "should connect then disconnect from a custom namespace"; echo '
it("should connect then disconnect from a custom namespace", async () => {
const socket = await initSocketIOConnection();
await waitFor(socket, "message"); // ping
// Create a waiting promise *before* sending the trigger packet "40/custom" makes the trick
const handshakeHandler = waitForPackets(socket, 2);
socket.send("40/custom");
// await waitFor(socket, "message"); // Socket.IO handshake
// await waitFor(socket, "message"); // auth packet
await handshakeHandler;
socket.send("41/custom");
socket.send('42["message","message to main namespace"]');
const { data } = await waitFor(socket, "message");
expect(data).to.eql('42["message-back","message to main namespace"]');
});
I didn't find any good solution to propose a PR. If there is no solution to this issue. I may consider rewriting the test suite in another language to avoid event loop issues. Maybe @darrachequesne you have an idea ?