Skip to content

Commit

Permalink
[ISSUE-36] Temp directories are not being cleaned up properly (#37)
Browse files Browse the repository at this point in the history
* [ISSUE-36] Temp directories are not being cleaned up properly

* bump release version to 3.1.2

* update changelog

* resolve checkstyle violations
  • Loading branch information
Crim authored Nov 8, 2019
1 parent 1c44eac commit 7f8ca7d
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions kafka-junit-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<artifactId>kafka-junit</artifactId>
<groupId>com.salesforce.kafka.test</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>kafka-junit-core</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>

<!-- defined properties -->
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Path>() {
@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);
}
}
));
}
}
10 changes: 5 additions & 5 deletions kafka-junit4/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -32,7 +32,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -58,7 +58,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -84,7 +84,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -110,7 +110,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand Down
6 changes: 3 additions & 3 deletions kafka-junit4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<parent>
<artifactId>kafka-junit</artifactId>
<groupId>com.salesforce.kafka.test</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<!-- Module Definition & Version -->
<artifactId>kafka-junit4</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>

<!-- defined properties -->
<properties>
Expand All @@ -50,7 +50,7 @@
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit-core</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</dependency>

<!-- JUnit is Required -->
Expand Down
10 changes: 5 additions & 5 deletions kafka-junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -31,7 +31,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -57,7 +57,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -83,7 +83,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand All @@ -109,7 +109,7 @@ Include this library in your project's POM with test scope. **You'll also need
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>

Expand Down
6 changes: 3 additions & 3 deletions kafka-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
<parent>
<artifactId>kafka-junit</artifactId>
<groupId>com.salesforce.kafka.test</groupId>
<version>3.1.1</version>
<version>3.1.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>kafka-junit5</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>

<!-- defined properties -->
<properties>
Expand All @@ -48,7 +48,7 @@
<dependency>
<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit-core</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>
</dependency>

<!-- JUnit is Required -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<groupId>com.salesforce.kafka.test</groupId>
<artifactId>kafka-junit</artifactId>
<version>3.1.1</version>
<version>3.1.2</version>

<!-- Submodules -->
<modules>
Expand Down

0 comments on commit 7f8ca7d

Please sign in to comment.