AbstractOpWithTimeout extends AbstractOp { @Override public Object attempt(final Connection connection) throws Exception { final Socket socket = connection.getSocket(); final int previousTimeoutMs = socket.getSoTimeout(); final int timeoutMs = getTimeoutMs(); final boolean changeTimeout = previousTimeoutMs != timeoutMs; if (changeTimeout) { socket.setSoTimeout(timeoutMs); } try { return super.attempt(connection); } finally { if (changeTimeout) { socket.setSoTimeout(previousTimeoutMs); } } } AbstractOpWithTimeout(int msgType, int msgParts, int timeoutMs); @Override Object attempt(final Connection connection);  }
@Test public void attemptWillSetAndResetSoTimeout() throws Exception { final AbstractOpWithTimeout mockOp = mock(AbstractTestOpWithTimeout.class, withSettings().useConstructor(0, 0, 123).defaultAnswer(CALLS_REAL_METHODS)); final Socket socket = mock(Socket.class); when(socket.getSoTimeout()).thenReturn(456); final Connection connection = mock(Connection.class); when(connection.getSocket()).thenReturn(socket); mockOp.attempt(connection); verify(socket).getSoTimeout(); verify(socket).setSoTimeout(123); verify(socket).setSoTimeout(456); verifyNoMoreInteractions(socket); }
@Test public void attemptWontSetAndResetSoTimeout() throws Exception { final AbstractOpWithTimeout mockOp = mock(AbstractTestOpWithTimeout.class, withSettings().useConstructor(0, 0, 123).defaultAnswer(CALLS_REAL_METHODS)); final Socket socket = mock(Socket.class); when(socket.getSoTimeout()).thenReturn(123); final Connection connection = mock(Connection.class); when(connection.getSocket()).thenReturn(socket); mockOp.attempt(connection); verify(socket).getSoTimeout(); verifyNoMoreInteractions(socket); }