TCP Outbound gateway - force socket close on error receiving response; eliminate stack traces from expected exceptions on buffer overflow tests

This commit is contained in:
Gary Russell
2010-05-30 20:23:15 +00:00
parent ec81a83ef0
commit 7a139a10a7
3 changed files with 49 additions and 6 deletions

View File

@@ -86,6 +86,7 @@ public class SimpleTcpNetOutboundGateway extends
return object;
} catch (Exception e) {
this.reader = null;
this.handler.close();
throw new MessagingException(requestMessage, e);
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.ip.tcp;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.net.ServerSocket;
import java.net.Socket;
@@ -187,4 +188,49 @@ public class SimpleTcpNetOutboundGatewayTests {
assertEquals("OK", new String(bytes));
}
@Test
public void testOutboundCloseOnTimeout() throws Exception {
final int port = SocketUtils.findAvailableServerSocket();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final CountDownLatch latch3 = new CountDownLatch(1);
Thread t = new Thread(new Runnable() {
public void run() {
try {
ServerSocket ss = ServerSocketFactory.getDefault().createServerSocket(port);
latch1.countDown();
boolean first = true;
while (true) {
Socket s = ss.accept();
byte[] b = new byte[1024];
s.getInputStream().read(b);
if (!first)
s.getOutputStream().write("OK\r\n".getBytes());
first = false;
latch2.countDown();
latch3.await();
s.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}});
t.start();
latch1.await(2000, TimeUnit.MILLISECONDS);
SimpleTcpNetOutboundGateway gateway = new SimpleTcpNetOutboundGateway
("localhost", port);
gateway.setMessageFormat(MessageFormats.FORMAT_CRLF);
gateway.setClose(false);
gateway.setSoTimeout(500);
Message<String> message = MessageBuilder.withPayload("test").build();
try {
gateway.handleRequestMessage(message);
fail("Expected failure");
} catch (Exception e) { }
latch3.countDown();
latch2.await(2000, TimeUnit.MILLISECONDS);
byte[] bytes = (byte[]) gateway.handleRequestMessage(message);
assertEquals("OK", new String(bytes));
}
}

View File

@@ -186,9 +186,7 @@ public class SocketUtils {
writeByte(outputStream, 'x', true);
}
Thread.sleep(1000000000L); // wait forever, but we're a daemon
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) { }
}
});
thread.setDaemon(true);
@@ -293,9 +291,7 @@ public class SocketUtils {
writeByte(outputStream, 'x', true);
}
Thread.sleep(1000000000L); // wait forever, but we're a daemon
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) { }
}
});
thread.setDaemon(true);