From 7f8ca7d91f42ecff49f43582df690c9a0077ca57 Mon Sep 17 00:00:00 2001 From: Steviep Date: Fri, 8 Nov 2019 11:53:27 +0900 Subject: [PATCH] [ISSUE-36] Temp directories are not being cleaned up properly (#37) * [ISSUE-36] Temp directories are not being cleaned up properly * bump release version to 3.1.2 * update changelog * resolve checkstyle violations --- CHANGELOG.md | 3 ++ kafka-junit-core/pom.xml | 4 +- .../java/com/salesforce/kafka/test/Utils.java | 49 ++++++++++++++++--- kafka-junit4/README.md | 10 ++-- kafka-junit4/pom.xml | 6 +-- kafka-junit5/README.md | 10 ++-- kafka-junit5/pom.xml | 6 +-- pom.xml | 2 +- 8 files changed, 65 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a34274..0072ced 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 3.1.2 (11/08/2019) +- [ISSUE-36](https://github.com/salesforce/kafka-junit/issues/36) Temporary directories should now be cleaned up properly on JVM shutdown. + ## 3.1.1 (03/22/2019) - Replace internal uses of Guava with JDK-comparable methods so that if a transitive dependency on Curator resolves to a more recent version that shades Guava this library will not break. diff --git a/kafka-junit-core/pom.xml b/kafka-junit-core/pom.xml index 70f00ef..a313374 100644 --- a/kafka-junit-core/pom.xml +++ b/kafka-junit-core/pom.xml @@ -5,12 +5,12 @@ kafka-junit com.salesforce.kafka.test - 3.1.1 + 3.1.2 4.0.0 kafka-junit-core - 3.1.1 + 3.1.2 diff --git a/kafka-junit-core/src/main/java/com/salesforce/kafka/test/Utils.java b/kafka-junit-core/src/main/java/com/salesforce/kafka/test/Utils.java index 7f16b4c..2f14772 100644 --- a/kafka-junit-core/src/main/java/com/salesforce/kafka/test/Utils.java +++ b/kafka-junit-core/src/main/java/com/salesforce/kafka/test/Utils.java @@ -27,7 +27,11 @@ import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitResult; import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; /** * Collection of Utilities. @@ -39,16 +43,49 @@ class Utils { */ static File createTempDirectory() { // Create temp path to store logs - final File logDir; + final Path logDir; try { - logDir = Files.createTempDirectory("kafka-unit").toFile(); - } catch (IOException e) { + logDir = Files.createTempDirectory("kafka-unit"); + } catch (final IOException e) { throw new RuntimeException(e); } - // Ensure its removed on termination. - logDir.deleteOnExit(); + // Ensure its removed on termination by recursively removing all files under the temp directory. + Utils.recursiveDeleteOnShutdownHook(logDir); - return logDir; + // Return as a File + return logDir.toFile(); + } + + /** + * Registers a shutdown hook to recursively cleanup/delete a directory and all of it's contents. + * @param path the Path to remove. + */ + private static void recursiveDeleteOnShutdownHook(final Path path) { + Runtime.getRuntime().addShutdownHook(new Thread( + () -> { + try { + Files.walkFileTree(path, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(final Path dir, final IOException exception) throws IOException { + if (exception == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + // directory iteration failed + throw exception; + } + }); + } catch (final IOException exception) { + throw new RuntimeException("Failed to delete " + path, exception); + } + } + )); } } diff --git a/kafka-junit4/README.md b/kafka-junit4/README.md index 57d33c8..65ef331 100644 --- a/kafka-junit4/README.md +++ b/kafka-junit4/README.md @@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit4 - 3.1.1 + 3.1.2 test ``` @@ -32,7 +32,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit4 - 3.1.1 + 3.1.2 test @@ -58,7 +58,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit4 - 3.1.1 + 3.1.2 test @@ -84,7 +84,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit4 - 3.1.1 + 3.1.2 test @@ -110,7 +110,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit4 - 3.1.1 + 3.1.2 test diff --git a/kafka-junit4/pom.xml b/kafka-junit4/pom.xml index 115352f..11ca63a 100644 --- a/kafka-junit4/pom.xml +++ b/kafka-junit4/pom.xml @@ -32,13 +32,13 @@ kafka-junit com.salesforce.kafka.test - 3.1.1 + 3.1.2 4.0.0 kafka-junit4 - 3.1.1 + 3.1.2 @@ -50,7 +50,7 @@ com.salesforce.kafka.test kafka-junit-core - 3.1.1 + 3.1.2 diff --git a/kafka-junit5/README.md b/kafka-junit5/README.md index 93eb0f2..386d71c 100644 --- a/kafka-junit5/README.md +++ b/kafka-junit5/README.md @@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit5 - 3.1.1 + 3.1.2 test ``` @@ -31,7 +31,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit5 - 3.1.1 + 3.1.2 test @@ -57,7 +57,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit5 - 3.1.1 + 3.1.2 test @@ -83,7 +83,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit5 - 3.1.1 + 3.1.2 test @@ -109,7 +109,7 @@ Include this library in your project's POM with test scope. **You'll also need com.salesforce.kafka.test kafka-junit5 - 3.1.1 + 3.1.2 test diff --git a/kafka-junit5/pom.xml b/kafka-junit5/pom.xml index f1f7330..9c26d24 100644 --- a/kafka-junit5/pom.xml +++ b/kafka-junit5/pom.xml @@ -31,12 +31,12 @@ kafka-junit com.salesforce.kafka.test - 3.1.1 + 3.1.2 4.0.0 kafka-junit5 - 3.1.1 + 3.1.2 @@ -48,7 +48,7 @@ com.salesforce.kafka.test kafka-junit-core - 3.1.1 + 3.1.2 diff --git a/pom.xml b/pom.xml index 9cfcd60..d0979f8 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ com.salesforce.kafka.test kafka-junit - 3.1.1 + 3.1.2