Repository: kafka Updated Branches: refs/heads/0.9.0 6aeaa1eb5 -> 836dd705d
KAFKA-2718: Avoid reusing temporary directories in core unit tests Retry to find new directory and cleanup on exit. Author: Rajini Sivaram <[email protected]> Reviewers: Ismael Juma, Guozhang Wang Closes #399 from rajinisivaram/KAFKA-2718 (cherry picked from commit 0d68eb73f3af8d0e8e32a1e61d8a569c615c3c8c) Signed-off-by: Guozhang Wang <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/kafka/repo Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/836dd705 Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/836dd705 Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/836dd705 Branch: refs/heads/0.9.0 Commit: 836dd705d16d1d6e01288529dff1b232b62d9f52 Parents: 6aeaa1e Author: Rajini Sivaram <[email protected]> Authored: Fri Nov 20 15:36:59 2015 -0800 Committer: Guozhang Wang <[email protected]> Committed: Fri Nov 20 15:37:29 2015 -0800 ---------------------------------------------------------------------- .../test/scala/unit/kafka/utils/TestUtils.scala | 27 ++++++++++---------- 1 file changed, 14 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kafka/blob/836dd705/core/src/test/scala/unit/kafka/utils/TestUtils.scala ---------------------------------------------------------------------- diff --git a/core/src/test/scala/unit/kafka/utils/TestUtils.scala b/core/src/test/scala/unit/kafka/utils/TestUtils.scala index 4059512..2f734f6 100755 --- a/core/src/test/scala/unit/kafka/utils/TestUtils.scala +++ b/core/src/test/scala/unit/kafka/utils/TestUtils.scala @@ -85,17 +85,7 @@ object TestUtils extends Logging { * Create a temporary directory */ def tempDir(): File = { - val f = new File(IoTmpDir, "kafka-" + random.nextInt(1000000)) - f.mkdirs() - f.deleteOnExit() - - Runtime.getRuntime().addShutdownHook(new Thread() { - override def run() = { - CoreUtils.rm(f) - } - }) - - f + tempRelativeDir(IoTmpDir) } def tempTopic(): String = "testTopic" + random.nextInt(1000000) @@ -104,12 +94,23 @@ object TestUtils extends Logging { * Create a temporary relative directory */ def tempRelativeDir(parent: String): File = { - val f = new File(parent, "kafka-" + random.nextInt(1000000)) - f.mkdirs() + new File(parent).mkdirs() + val attempts = 1000 + val f = Iterator.continually(new File(parent, "kafka-" + random.nextInt(1000000))) + .take(attempts).find(_.mkdir()) + .getOrElse(sys.error(s"Failed to create directory after $attempts attempts")) f.deleteOnExit() + + Runtime.getRuntime().addShutdownHook(new Thread() { + override def run() = { + CoreUtils.rm(f) + } + }) + f } + /** * Create a temporary file */
