Skip to content

Commit 7f8ca7d

Browse files
authored
[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
1 parent 1c44eac commit 7f8ca7d

File tree

8 files changed

+65
-25
lines changed

8 files changed

+65
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 3.1.2 (11/08/2019)
6+
- [ISSUE-36](https://github.com/salesforce/kafka-junit/issues/36) Temporary directories should now be cleaned up properly on JVM shutdown.
7+
58
## 3.1.1 (03/22/2019)
69
- 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.
710

kafka-junit-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<artifactId>kafka-junit</artifactId>
77
<groupId>com.salesforce.kafka.test</groupId>
8-
<version>3.1.1</version>
8+
<version>3.1.2</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

1212
<artifactId>kafka-junit-core</artifactId>
13-
<version>3.1.1</version>
13+
<version>3.1.2</version>
1414

1515
<!-- defined properties -->
1616
<properties>

kafka-junit-core/src/main/java/com/salesforce/kafka/test/Utils.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727

2828
import java.io.File;
2929
import java.io.IOException;
30+
import java.nio.file.FileVisitResult;
3031
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.nio.file.SimpleFileVisitor;
34+
import java.nio.file.attribute.BasicFileAttributes;
3135

3236
/**
3337
* Collection of Utilities.
@@ -39,16 +43,49 @@ class Utils {
3943
*/
4044
static File createTempDirectory() {
4145
// Create temp path to store logs
42-
final File logDir;
46+
final Path logDir;
4347
try {
44-
logDir = Files.createTempDirectory("kafka-unit").toFile();
45-
} catch (IOException e) {
48+
logDir = Files.createTempDirectory("kafka-unit");
49+
} catch (final IOException e) {
4650
throw new RuntimeException(e);
4751
}
4852

49-
// Ensure its removed on termination.
50-
logDir.deleteOnExit();
53+
// Ensure its removed on termination by recursively removing all files under the temp directory.
54+
Utils.recursiveDeleteOnShutdownHook(logDir);
5155

52-
return logDir;
56+
// Return as a File
57+
return logDir.toFile();
58+
}
59+
60+
/**
61+
* Registers a shutdown hook to recursively cleanup/delete a directory and all of it's contents.
62+
* @param path the Path to remove.
63+
*/
64+
private static void recursiveDeleteOnShutdownHook(final Path path) {
65+
Runtime.getRuntime().addShutdownHook(new Thread(
66+
() -> {
67+
try {
68+
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
69+
@Override
70+
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
71+
Files.delete(file);
72+
return FileVisitResult.CONTINUE;
73+
}
74+
75+
@Override
76+
public FileVisitResult postVisitDirectory(final Path dir, final IOException exception) throws IOException {
77+
if (exception == null) {
78+
Files.delete(dir);
79+
return FileVisitResult.CONTINUE;
80+
}
81+
// directory iteration failed
82+
throw exception;
83+
}
84+
});
85+
} catch (final IOException exception) {
86+
throw new RuntimeException("Failed to delete " + path, exception);
87+
}
88+
}
89+
));
5390
}
5491
}

kafka-junit4/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need
2020
<dependency>
2121
<groupId>com.salesforce.kafka.test</groupId>
2222
<artifactId>kafka-junit4</artifactId>
23-
<version>3.1.1</version>
23+
<version>3.1.2</version>
2424
<scope>test</scope>
2525
</dependency>
2626
```
@@ -32,7 +32,7 @@ Include this library in your project's POM with test scope. **You'll also need
3232
<dependency>
3333
<groupId>com.salesforce.kafka.test</groupId>
3434
<artifactId>kafka-junit4</artifactId>
35-
<version>3.1.1</version>
35+
<version>3.1.2</version>
3636
<scope>test</scope>
3737
</dependency>
3838

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

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

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

kafka-junit4/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
<parent>
3333
<artifactId>kafka-junit</artifactId>
3434
<groupId>com.salesforce.kafka.test</groupId>
35-
<version>3.1.1</version>
35+
<version>3.1.2</version>
3636
</parent>
3737
<modelVersion>4.0.0</modelVersion>
3838

3939
<!-- Module Definition & Version -->
4040
<artifactId>kafka-junit4</artifactId>
41-
<version>3.1.1</version>
41+
<version>3.1.2</version>
4242

4343
<!-- defined properties -->
4444
<properties>
@@ -50,7 +50,7 @@
5050
<dependency>
5151
<groupId>com.salesforce.kafka.test</groupId>
5252
<artifactId>kafka-junit-core</artifactId>
53-
<version>3.1.1</version>
53+
<version>3.1.2</version>
5454
</dependency>
5555

5656
<!-- JUnit is Required -->

kafka-junit5/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Include this library in your project's POM with test scope. **You'll also need
2020
<dependency>
2121
<groupId>com.salesforce.kafka.test</groupId>
2222
<artifactId>kafka-junit5</artifactId>
23-
<version>3.1.1</version>
23+
<version>3.1.2</version>
2424
<scope>test</scope>
2525
</dependency>
2626
```
@@ -31,7 +31,7 @@ Include this library in your project's POM with test scope. **You'll also need
3131
<dependency>
3232
<groupId>com.salesforce.kafka.test</groupId>
3333
<artifactId>kafka-junit5</artifactId>
34-
<version>3.1.1</version>
34+
<version>3.1.2</version>
3535
<scope>test</scope>
3636
</dependency>
3737

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

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

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

kafka-junit5/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
<parent>
3232
<artifactId>kafka-junit</artifactId>
3333
<groupId>com.salesforce.kafka.test</groupId>
34-
<version>3.1.1</version>
34+
<version>3.1.2</version>
3535
</parent>
3636
<modelVersion>4.0.0</modelVersion>
3737

3838
<artifactId>kafka-junit5</artifactId>
39-
<version>3.1.1</version>
39+
<version>3.1.2</version>
4040

4141
<!-- defined properties -->
4242
<properties>
@@ -48,7 +48,7 @@
4848
<dependency>
4949
<groupId>com.salesforce.kafka.test</groupId>
5050
<artifactId>kafka-junit-core</artifactId>
51-
<version>3.1.1</version>
51+
<version>3.1.2</version>
5252
</dependency>
5353

5454
<!-- JUnit is Required -->

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
<groupId>com.salesforce.kafka.test</groupId>
3434
<artifactId>kafka-junit</artifactId>
35-
<version>3.1.1</version>
35+
<version>3.1.2</version>
3636

3737
<!-- Submodules -->
3838
<modules>

0 commit comments

Comments
 (0)