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


##########
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:
   The idea (at least with calls to terminate the JVM) is that we can add a 
layer in between the components under test and the exit API provided by the 
SDK, and then alter that layer during testing to prevent the actual API from 
being accessed.
   
   One example is in the embedded Connect clusters we use for our integration 
tests: 
https://github.com/apache/kafka/blob/dbf5826cd545820652ed6d136727aa80a03f4f54/connect/runtime/src/test/java/org/apache/kafka/connect/util/clusters/EmbeddedConnectCluster.java#L111-L120
   
   With shutdown hooks, it's mostly hypothetical at this point since AFAICT 
there are no actual invocations of `Exit::setShutdownHookAdder` in the code 
base, but I believe the intention would be similar. You'd intercept attempts by 
components under test to add shutdown hooks, both to prevent those hooks from 
actually being added (in other words, to prevent 
`Runtime.getRuntime().addShutdownHook` from being called), and possibly to make 
assertions on those hooks.
   
   We don't want to allow for the shutdown hooks we added in this PR to be 
intercepted, right? If so, we might consider invoking 
`Runtime.getRuntime().addShutdownHook` directly here.



-- 
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-unsubscr...@kafka.apache.org

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

Reply via email to