TCPConn - Simulate socket error 104 "connection reset by peer"

2,437 views
Skip to first unread message

Willow

unread,
Aug 22, 2012, 7:25:58 PM8/22/12
to golan...@googlegroups.com
How do I close a TCPConn so the other end receives error 104: connection reset by peer?  I'm trying to simulate it for a test case in my application because it happens from time to time instead of an io.EOF error when the connection is closed.

Kyle Lemons

unread,
Aug 22, 2012, 8:08:35 PM8/22/12
to Willow, golan...@googlegroups.com
You can use a mocked out implementation of net.Conn that returns the same error.  If you're actually using a live on-the-wire TCPConn in your test (which I don't usually recommend, especially for unit tests), I know of no way to deliberately murder a TCP stream in that way.

brainman

unread,
Aug 22, 2012, 8:37:31 PM8/22/12
to golan...@googlegroups.com
On Thursday, 23 August 2012 09:25:58 UTC+10, Willow wrote:
...

It is easy enough to change net package to return this error when you need it to (as Kyle suggested). Alternatively you could try fiddle with socket options to make closesocket do it for you. If you are on Windows, here is http://goo.gl/MyfKX for some explanation of what you might need to do. It is SO_LINGER option you are after. I suspect, other OS will be similar.

Alex

Willow

unread,
Aug 22, 2012, 9:13:52 PM8/22/12
to golan...@googlegroups.com, Willow
Darn, I had my hopes up of being a TCP murderer.  I've been planning on mocking my net.Conn and just putting it off, guess this gives me a good reason to go ahead and get it done with.

Looking into what brainman suggested appears to be more work and less reliable.

Thanks,
Willow

Kyle Lemons

unread,
Aug 22, 2012, 9:35:23 PM8/22/12
to Willow, golan...@googlegroups.com
It requires modifying the stdlib.  If you're doing an integration test, you can probably create a trivial-ish C program which accepts a connection, writes some data, and then does an RST, which (I believe) will be interpreted as connection reset by peer, but that is probably more work than mocking net.Conn :).
 
Thanks,
Willow

Reply all
Reply to author
Forward
0 new messages