You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/11/16 14:04:17 UTC

[GitHub] [kafka] divijvaidya commented on a diff in pull request #12233: MINOR: Clean up tmp files created by tests

divijvaidya commented on code in PR #12233:
URL: https://github.com/apache/kafka/pull/12233#discussion_r1024041485


##########
clients/src/test/java/org/apache/kafka/test/TestUtils.java:
##########
@@ -142,26 +142,40 @@ public static String randomString(final int len) {
     }
 
     /**
-     * Create an empty file in the default temporary-file directory, using `kafka` as the prefix and `tmp` as the
-     * suffix to generate its name.
+     * Create an empty file in the default temporary-file directory, using the given prefix and suffix
+     * to generate its name.
+     * @throws IOException
      */
-    public static File tempFile() throws IOException {
-        final File file = File.createTempFile("kafka", ".tmp");
+    public static File tempFile(final String prefix, final String suffix) throws IOException {
+        final File file = Files.createTempFile(prefix, suffix).toFile();
         file.deleteOnExit();
 
+        Exit.addShutdownHook("delete-temp-file-shutdown-hook", () -> {

Review Comment:
   From what I understand, we try to use `Exit.scala` instead of `Runtime.getRuntime()` so that Tests may provide their own implementation of shutdown hook adder. This is mentioned here: https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/utils/Exit.scala#L22 
   
   I didn't understand the scenario where this hook will be disabled! Could you please explain with an example?
   
   If a test would override the shutdown hook behaviour, then I would assume it will add a new shutdown hook? or perhaps, add a new `ShutdownHookAdder` where it may play with the order of the hooks? In all such scenarios, this cleanup hook will still be executed as long it is registered correctly. 
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org