Skip to content
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

Allow stubbing a response for connection timeouts #591

Closed
bmuskalla opened this issue Jan 26, 2017 · 9 comments
Closed

Allow stubbing a response for connection timeouts #591

bmuskalla opened this issue Jan 26, 2017 · 9 comments

Comments

@bmuskalla
Copy link

Using #withFixedDelay works great to test and simulate SocketTimeoutException. The same should be possible for simulating connection timeouts.

If you agree, I'm happy to contribute something like Fault#REFUSE_CONNECTION

@tomakehurst
Copy link
Member

I tried to do this in WireMock 1.x by overriding the socket accept() method in the connector and adding a Thread.sleep(), but it never worked reliably so it got removed in v2.

If you've got another way you think this could work, then it'd be great to have it back. Have you got a specific strategy in mind?

@bmuskalla
Copy link
Author

Right now, I'm using a separate socket for those tests along those lines which results in the expected behavior. Not sure yet if we can do something similar with Jetty though. I'll have a look.

private ServerSocket createBlockingSocket() throws IOException {
		ServerSocket serverSocket = new ServerSocket(0, 1);
		new Socket().connect(serverSocket.getLocalSocketAddress());
		return serverSocket;
}

@tomakehurst
Copy link
Member

Interesting. I spent quite a while messing around with the backlog parameter, and I couldn't seem to get the desired behaviour. The Javadocs mention that there's some platform-dependent behaviour in the use of the backlog parameter, so this might have been why.

If you can find a way to get this working reliably at least across the major JVM impls, then I guess it should be possible somehow to override Jetty's default socket factory and insert this behaviour.

@indicalabs
Copy link

did any body found a way to simulate connection timeout behaviour?

@kkrhdd
Copy link

kkrhdd commented Jul 10, 2018

@bmuskalla I have tried to simulate SocketTimeoutException using #withFixedDelay. I always get SocketException. Can you please help me with the stubbing?

@plum117
Copy link

plum117 commented Aug 8, 2019

Anyone can have an answer? I'm able to simulate Read timeout with fixedDelay but how can we do it for connection Timeout?

@kkrhdd
Copy link

kkrhdd commented Aug 9, 2019

Anyone can have an answer? I'm able to simulate Read timeout with fixedDelay but how can we do it for connection Timeout?

@plum117 By Just shutting down the wiremock, you will get ConnectException

@tomakehurst
Copy link
Member

It's basically impossible to reliably force connection timeouts in pure Java at the moment.

It used to be the case that you could inject a delay before calling .accept() on the socket, but that stopped working a while back, I guess due to a change in the implementation internals.

My recommendation at the moment would be to use a tool that works at the level of the network stack. iptables ... -j DROP type commands will do the trick, or if you want a level of automation over this you can use tools such as https://github.com/tomakehurst/saboteur or https://github.com/alexei-led/pumba.

@tomakehurst
Copy link
Member

BTW, shutting down WireMock won't have the same effect - when a port is not being listened on, you get a TCP RST (reset) packet back, whereas a connection timeout happens when you get nothing back from the server in the timeout window after your initial SYN packet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants