Force refused/timed out connection, SocketChannel with Selector
Posted on 2009-12-29
I have an application which needs to behave a certain way when a connection made with a SocketChannel is refused or times out. It behaves as expected when connecting to the production host, but I am trying to write a self-contained test for it in JUnit and am having trouble getting the refusal or the timeout to occur when I attempt to connect to a ServerSocket established locally.
When it times out (using setSoTimeout(100)) talking to the remote host or the remote host refuses the connection, calling finishConnect() on the selected key will raise a ConnectException, which I can then catch and deal with appropriately. But when attempting to connect to an unbound port locally in the JUnit test, it neither times out nor receives a connection refusal. Instead it loops forever, never selecting the key for the connection attempt. Same story if I bind() the port to a ServerSocket but never accept().
Unfortunately the work is covered by an NDA, so I can't post code. But just at first glance, is there something the remote host is configured to do, that I need to do to refuse the connection or make the connection time out locally?