Use AutoClosables with try-with-resources

Closes gh-33538
This commit is contained in:
Moritz Halbritter
2022-12-16 15:43:15 +01:00
parent 725337f976
commit f36e2ecb7b
16 changed files with 121 additions and 77 deletions

View File

@@ -139,9 +139,10 @@ class HttpTunnelConnectionTests {
@Test
void connectFailureLogsWarning(CapturedOutput output) throws Exception {
this.requestFactory.willRespond(new ConnectException());
TunnelChannel tunnel = openTunnel(true);
assertThat(tunnel.isOpen()).isFalse();
assertThat(output).contains("Failed to connect to remote application at http://localhost:12345");
try (TunnelChannel tunnel = openTunnel(true)) {
assertThat(tunnel.isOpen()).isFalse();
assertThat(output).contains("Failed to connect to remote application at http://localhost:12345");
}
}
private void write(TunnelChannel channel, String string) throws IOException {

View File

@@ -53,34 +53,37 @@ class SocketTargetServerConnectionTests {
void readData() throws Exception {
this.server.willSend("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.allocate(5);
channel.read(buffer);
assertThat(buffer.array()).isEqualTo("hello".getBytes());
try (ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT)) {
ByteBuffer buffer = ByteBuffer.allocate(5);
channel.read(buffer);
assertThat(buffer.array()).isEqualTo("hello".getBytes());
}
}
@Test
void writeData() throws Exception {
this.server.expect("hello".getBytes());
this.server.start();
ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT);
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
try (ByteChannel channel = this.connection.open(DEFAULT_TIMEOUT)) {
ByteBuffer buffer = ByteBuffer.wrap("hello".getBytes());
channel.write(buffer);
this.server.closeAndVerify();
}
}
@Test
void timeout() throws Exception {
this.server.delay(1000);
this.server.start();
ByteChannel channel = this.connection.open(10);
long startTime = System.currentTimeMillis();
assertThatExceptionOfType(SocketTimeoutException.class).isThrownBy(() -> channel.read(ByteBuffer.allocate(5)))
.satisfies((ex) -> {
long runTime = System.currentTimeMillis() - startTime;
assertThat(runTime).isGreaterThanOrEqualTo(10L);
assertThat(runTime).isLessThan(10000L);
});
try (ByteChannel channel = this.connection.open(10)) {
long startTime = System.currentTimeMillis();
assertThatExceptionOfType(SocketTimeoutException.class)
.isThrownBy(() -> channel.read(ByteBuffer.allocate(5))).satisfies((ex) -> {
long runTime = System.currentTimeMillis() - startTime;
assertThat(runTime).isGreaterThanOrEqualTo(10L);
assertThat(runTime).isLessThan(10000L);
});
}
}
static class MockServer {