thoov / mock-socket

Javascript mocking library for WebSockets and Socket.IO
MIT License
800 stars 119 forks source link

Is it possible to mock connection timeout ? #57

Open hakib opened 9 years ago

hakib commented 9 years ago

Is it possible to delay a message by X ms ? specifically the connect message in my case.

I'm trying to test timeouts in some RPC implementation using web sockets. The specific scenario i'm trying to test is the app opening a websocket connection, hitting some internal timeout, notifying the user and setting state to disconnected, then the ws onconnect is invoked in wich case i'm just closing the connection. I have similar scenarios with send as well.

I've seen that you use some "timing hacks" (as you call them) to delay messages (currently set to 4ms). Is it possible to use that some how for my use case?

Thanks.

thoov commented 9 years ago

@hakib Right now it is not possible. Let me add something to allow you to do this. This is what I have in mind:

var url = 'ws://example';
var mockServer = MockServer(url, {
  connection_delay: 10000 // 10 seconds
});

var mockSocket = new MockSocket(url);

setTimeout(function() {
  if (mockSocket.readyState === MockSocket.CONNECTING) {
    // the connection is still pending so we tell the user that the connection is taking longer
    // than expected.

    mockSocket.close();
  }
}, 3000);  // check after 3 seconds

Would this work for you?

hakib commented 9 years ago

Thanks @thoov. This looks just about right. How would you suggest setting timeout for send ?

loispostula commented 6 years ago

@thoov Is this solution present now? can't seem to make it work

marcospassos commented 4 years ago

Any news on this?