mock-socket icon indicating copy to clipboard operation
mock-socket copied to clipboard

Is it possible to mock connection timeout ?

Open hakib opened this issue 9 years ago • 4 comments

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.

hakib avatar Oct 05 '15 13:10 hakib

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

thoov avatar Oct 07 '15 17:10 thoov

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

hakib avatar Oct 07 '15 18:10 hakib

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

loispostula avatar Dec 05 '17 10:12 loispostula

Any news on this?

marcospassos avatar Feb 17 '20 19:02 marcospassos