Fix test failures on Windows

Since the move to JUnit 5, a number of tests were failing on Windows.
The majority were failing due to open file handles preventing the
clean up of the tests' temporary directory. This commit addresses
these failures by updating the tests to close JarFiles, InputStreams,
OutputStreams etc.

A change has also been made to CachingOperationInvokerTests to make
a flakey test more robust. Due to System.currentTimeMillis() being
less precise on Windows than it is on *nix platforms, the test could
fail as it would not sleep for long enough for the TTL period to have
expired.
This commit is contained in:
Andy Wilkinson
2019-06-10 09:24:06 +01:00
parent c56fbf8c3d
commit cffc870fd6
22 changed files with 435 additions and 209 deletions

View File

@@ -114,7 +114,10 @@ class CachingOperationInvokerTests {
given(target.invoke(context)).willReturn(new Object());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 50L);
invoker.invoke(context);
Thread.sleep(55);
long expired = System.currentTimeMillis() + 50;
while (System.currentTimeMillis() < expired) {
Thread.sleep(10);
}
invoker.invoke(context);
verify(target, times(2)).invoke(context);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.actuate.logging;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -70,7 +71,7 @@ class LogFileWebEndpointTests {
this.environment.setProperty("logging.file.name", this.logFile.getAbsolutePath());
Resource resource = this.endpoint.logFile();
assertThat(resource).isNotNull();
assertThat(StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("--TEST--");
assertThat(contentOf(resource)).isEqualTo("--TEST--");
}
@Test
@@ -79,7 +80,7 @@ class LogFileWebEndpointTests {
this.environment.setProperty("logging.file", this.logFile.getAbsolutePath());
Resource resource = this.endpoint.logFile();
assertThat(resource).isNotNull();
assertThat(StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("--TEST--");
assertThat(contentOf(resource)).isEqualTo("--TEST--");
}
@Test
@@ -87,7 +88,13 @@ class LogFileWebEndpointTests {
LogFileWebEndpoint endpoint = new LogFileWebEndpoint(this.environment, this.logFile);
Resource resource = endpoint.logFile();
assertThat(resource).isNotNull();
assertThat(StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("--TEST--");
assertThat(contentOf(resource)).isEqualTo("--TEST--");
}
private String contentOf(Resource resource) throws IOException {
try (InputStream input = resource.getInputStream()) {
return StreamUtils.copyToString(input, StandardCharsets.UTF_8);
}
}
}