-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experimental Websocket readyState
returned as a string
#79
Comments
@dougw-bc can you please provide an exampe of this as it seems like the implementaiton is with integers and that has been the case for the last 2 years at least. Are you sure you are not runnign a particularly old version of k6. |
I noticed this when trying to test an Elixir Phoenix Channel websocket server. Here is the code that was not functioning as expected on k6:
UPDATE: - this still behaves oddly, but not because of the websocket.readyState return value, it is something to do with the switch statement Here is a reproduction of the issue with a small script: import { WebSocket } from "k6/experimental/websockets";
export const options = {
vus: 1,
iterations: 1,
};
// this just hids the default it seems - maybe with any integer?
function connectionState(state) {
switch (state) {
case 0:
return "connecting";
case 1:
return "open";
case 2:
return "closing";
case 3:
return "closed";
default:
return "uh oh";
}
}
export default async () => {
const url = "wss://test-api.k6.io/ws/crocochat/publicRoom/";
const socket = new WebSocket(url);
socket.onopen = () => {
console.log("connected! ", connectionState(socket.readyState));
};
socket.onclose = () => {
console.log("closed! ", connectionState(socket.readyState));
};
console.log(connectionState(socket.readyState));
const checkInterval = setInterval(() => {
console.log(connectionState(socket.readyState));
}, 250);
setTimeout(function () {
console.log(`Closing the socket`);
socket.close();
clearInterval(checkInterval);
}, 3000);
}; This is on the following version: My logs look like this:
|
After further testing - there might be something else going on with the const vals = { 0: "connecting", 1: "open", 2: "closing", 3: "closed" };
function connectionState(state) {
return vals[state] || "uh oh";
} |
for some reason - likely a bug in goja/Sobek returning a type that is uint8 under the hood (or int) doesn't actually get to be a number in js. The current fix casts the ReadyState type to uint8 so that Sobek handles it correctly. Fixes #79
Whank you for reportuing this 🙇 I managed to reproduce it and it seems to be a Sobek/goja bug where if you have type that is uint8 under the hood and return it goja/Sobek makes it into a string instead. But if you just return uint8 it works ... 🤦 I will try to go further to fix it in the goja/Sobek code, but until then I have made #80 |
for some reason - likely a bug in goja/Sobek returning a type that is uint8 under the hood (or int) doesn't actually get to be a number in js. The current fix casts the ReadyState type to uint8 so that Sobek handles it correctly. Fixes #79
Brief summary
The experimental websocket encodes its internal
readyState
as a string.https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState
In a browser this value is represented as an integer.
k6 version
v0.53.0
OS
Ubuntu, Windows 11
Docker version and image (if applicable)
N/A
Steps to reproduce the problem
Try to compare readyState to an integer, and it will not work as expected
Expected behaviour
WebSocket.readyState
should expose integers rather than string values.Actual behaviour
WebSocket.readyState
returns a String instead of IntegerThe text was updated successfully, but these errors were encountered: