From 3927dca3e06b76524d3ec25a142241279b37111f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 17 Jan 2017 11:48:57 +0000 Subject: [PATCH] Tolerate files being briefly left open by FileWritingMessageHandler When the context is closed, FileWritingMessageHandler is stopped and it closes its output files. However, it appears to do so in a manner which means that they may be closed after the call to close the context is completed. This causes problems on Windows as files that are still open cannot be deleted. This commit adds a workaround to SampleIntegrationApplicationTests so that it makes up to 10 attempts each 0.5s apart to clean up the input and output directories. --- .../SampleIntegrationApplicationTests.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-integration/src/test/java/sample/integration/consumer/SampleIntegrationApplicationTests.java b/spring-boot-samples/spring-boot-sample-integration/src/test/java/sample/integration/consumer/SampleIntegrationApplicationTests.java index ac013b438a..1133adeb69 100644 --- a/spring-boot-samples/spring-boot-sample-integration/src/test/java/sample/integration/consumer/SampleIntegrationApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-integration/src/test/java/sample/integration/consumer/SampleIntegrationApplicationTests.java @@ -50,9 +50,21 @@ public class SampleIntegrationApplicationTests { private ConfigurableApplicationContext context; @Before - public void deleteOutput() { - FileSystemUtils.deleteRecursively(new File("target/input")); - FileSystemUtils.deleteRecursively(new File("target/output")); + public void deleteInputAndOutput() throws InterruptedException { + deleteIfExists(new File("target/input")); + deleteIfExists(new File("target/output")); + } + + private void deleteIfExists(File directory) throws InterruptedException { + if (directory.exists()) { + for (int i = 0; i < 10; i++) { + if (FileSystemUtils.deleteRecursively(directory)) { + return; + } + Thread.sleep(100); + } + throw new IllegalStateException("Failed to delete '" + directory + "'"); + } } @After