Skip to content

Commit ca287be

Browse files
authored
Clear log directories on EmbeddedKafka.stop() or end of withRunningKafka method. (#59)
1 parent b017848 commit ca287be

File tree

1 file changed

+47
-22
lines changed

1 file changed

+47
-22
lines changed

embedded-kafka/src/main/scala/net/manub/embeddedkafka/EmbeddedKafka.scala

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,14 @@ import kafka.admin.AdminUtils
88
import kafka.server.{KafkaConfig, KafkaServer}
99
import kafka.utils.ZkUtils
1010
import org.apache.kafka.clients.consumer.KafkaConsumer
11-
import org.apache.kafka.clients.producer.{
12-
KafkaProducer,
13-
ProducerConfig,
14-
ProducerRecord
15-
}
11+
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
1612
import org.apache.kafka.common.KafkaException
17-
import org.apache.kafka.common.serialization.{
18-
Deserializer,
19-
Serializer,
20-
StringDeserializer,
21-
StringSerializer
22-
}
13+
import org.apache.kafka.common.serialization.{Deserializer, Serializer, StringDeserializer, StringSerializer}
2314
import org.apache.zookeeper.server.{ServerCnxnFactory, ZooKeeperServer}
2415
import org.scalatest.Suite
2516

2617
import scala.collection.JavaConversions.mapAsJavaMap
18+
import scala.collection.mutable
2719
import scala.concurrent.duration._
2820
import scala.concurrent.{ExecutionContext, TimeoutException}
2921
import scala.language.{higherKinds, postfixOps}
@@ -37,40 +29,67 @@ object EmbeddedKafka extends EmbeddedKafkaSupport {
3729

3830
private[this] var factory: Option[ServerCnxnFactory] = None
3931
private[this] var broker: Option[KafkaServer] = None
32+
private[this] val logsDirs = mutable.Buffer.empty[Directory]
4033

4134
/**
42-
* Starts a ZooKeeper instance and a Kafka broker in memory.
35+
* Starts a ZooKeeper instance and a Kafka broker in memory, using temporary directories for storing logs.
36+
* The log directories will be cleaned after calling the [[stop()]] method or on JVM exit, whichever happens earlier.
4337
*
4438
* @param config an implicit [[EmbeddedKafkaConfig]]
4539
*/
4640
def start()(implicit config: EmbeddedKafkaConfig): Unit = {
47-
factory = Option(startZooKeeper(config.zooKeeperPort))
48-
broker = Option(startKafka(config))
41+
val zkLogsDir = Directory.makeTemp("zookeeper-logs")
42+
val kafkaLogsDir = Directory.makeTemp("kafka-logs")
43+
44+
factory = Option(startZooKeeper(config.zooKeeperPort, zkLogsDir))
45+
broker = Option(startKafka(config, kafkaLogsDir))
46+
47+
logsDirs ++= Seq(zkLogsDir, kafkaLogsDir)
4948
}
5049

50+
/**
51+
* Starts a Zookeeper instance in memory, storing logs in a specific location.
52+
*
53+
* @param zkLogsDir the path for the Zookeeper logs
54+
* @param config an implicit [[EmbeddedKafkaConfig]]
55+
*/
5156
def startZooKeeper(zkLogsDir: Directory)(
5257
implicit config: EmbeddedKafkaConfig): Unit = {
5358
factory = Option(startZooKeeper(config.zooKeeperPort, zkLogsDir))
5459
}
5560

61+
/**
62+
* Starts a Kafka broker in memory, storing logs in a specific location.
63+
*
64+
* @param kafkaLogDir the path for the Kafka logs
65+
* @param config an implicit [[EmbeddedKafkaConfig]]
66+
*/
5667
def startKafka(kafkaLogDir: Directory)(
5768
implicit config: EmbeddedKafkaConfig): Unit = {
5869
broker = Option(startKafka(config, kafkaLogDir))
5970
}
6071

6172
/**
62-
* Stops the in memory ZooKeeper instance and Kafka broker.
73+
* Stops the in memory ZooKeeper instance and Kafka broker, and deletes the log directories.
6374
*/
6475
def stop(): Unit = {
6576
stopKafka()
6677
stopZooKeeper()
78+
logsDirs.foreach(_.deleteRecursively())
79+
logsDirs.clear()
6780
}
6881

82+
/**
83+
* Stops the in memory Zookeeper instance, preserving the logs directory.
84+
*/
6985
def stopZooKeeper(): Unit = {
7086
factory.foreach(_.shutdown())
7187
factory = None
7288
}
7389

90+
/**
91+
* Stops the in memory Kafka instance, preserving the logs directory.
92+
*/
7493
def stopKafka(): Unit = {
7594
broker.foreach { b =>
7695
b.shutdown()
@@ -102,15 +121,23 @@ sealed trait EmbeddedKafkaSupport {
102121
*/
103122
def withRunningKafka(body: => Any)(implicit config: EmbeddedKafkaConfig): Any = {
104123

105-
val factory = startZooKeeper(config.zooKeeperPort)
106-
val broker = startKafka(config)
124+
def cleanLogs(directories: Directory*): Unit = {
125+
directories.foreach(_.deleteRecursively())
126+
}
127+
128+
val zkLogsDir = Directory.makeTemp("zookeeper-logs")
129+
val kafkaLogsDir = Directory.makeTemp("kafka")
130+
131+
val factory = startZooKeeper(config.zooKeeperPort, zkLogsDir)
132+
val broker = startKafka(config, kafkaLogsDir)
107133

108134
try {
109135
body
110136
} finally {
111137
broker.shutdown()
112138
broker.awaitShutdown()
113139
factory.shutdown()
140+
cleanLogs(zkLogsDir, kafkaLogsDir)
114141
}
115142
}
116143

@@ -258,8 +285,7 @@ sealed trait EmbeddedKafkaSupport {
258285
}
259286

260287
def startZooKeeper(zooKeeperPort: Int,
261-
zkLogsDir: Directory = Directory.makeTemp(
262-
"zookeeper-logs")): ServerCnxnFactory = {
288+
zkLogsDir: Directory): ServerCnxnFactory = {
263289
val tickTime = 2000
264290

265291
val zkServer = new ZooKeeperServer(zkLogsDir.toFile.jfile,
@@ -272,9 +298,8 @@ sealed trait EmbeddedKafkaSupport {
272298
factory
273299
}
274300

275-
def startKafka(
276-
config: EmbeddedKafkaConfig,
277-
kafkaLogDir: Directory = Directory.makeTemp("kafka")): KafkaServer = {
301+
def startKafka(config: EmbeddedKafkaConfig,
302+
kafkaLogDir: Directory): KafkaServer = {
278303
val zkAddress = s"localhost:${config.zooKeeperPort}"
279304

280305
val properties: Properties = new Properties

0 commit comments

Comments
 (0)