diff --git a/core/src/main/scala/kafka/zookeeper/ZooKeeperClient.scala b/core/src/main/scala/kafka/zookeeper/ZooKeeperClient.scala index 5cb127c3c7edf..97ec9a44c36f3 100755 --- a/core/src/main/scala/kafka/zookeeper/ZooKeeperClient.scala +++ b/core/src/main/scala/kafka/zookeeper/ZooKeeperClient.scala @@ -92,7 +92,12 @@ class ZooKeeperClient(connectString: String, metricNames += "SessionState" expiryScheduler.startup() - waitUntilConnected(connectionTimeoutMs, TimeUnit.MILLISECONDS) + try waitUntilConnected(connectionTimeoutMs, TimeUnit.MILLISECONDS) + catch { + case e: Throwable => + close() + throw e + } override def metricName(name: String, metricTags: scala.collection.Map[String, String]): MetricName = { explicitMetricName(metricGroup, metricType, name, metricTags) diff --git a/core/src/test/scala/unit/kafka/zookeeper/ZooKeeperClientTest.scala b/core/src/test/scala/unit/kafka/zookeeper/ZooKeeperClientTest.scala index fcbf699ec9614..0088c657de0a4 100644 --- a/core/src/test/scala/unit/kafka/zookeeper/ZooKeeperClientTest.scala +++ b/core/src/test/scala/unit/kafka/zookeeper/ZooKeeperClientTest.scala @@ -57,12 +57,22 @@ class ZooKeeperClientTest extends ZooKeeperTestHarness { System.clearProperty(JaasUtils.JAVA_LOGIN_CONFIG_PARAM) } - @Test(expected = classOf[ZooKeeperClientTimeoutException]) + @Test def testUnresolvableConnectString(): Unit = { - new ZooKeeperClient("some.invalid.hostname.foo.bar.local", zkSessionTimeout, connectionTimeoutMs = 10, - Int.MaxValue, time, "testMetricGroup", "testMetricType").close() + try { + new ZooKeeperClient("some.invalid.hostname.foo.bar.local", zkSessionTimeout, connectionTimeoutMs = 10, + Int.MaxValue, time, "testMetricGroup", "testMetricType") + } catch { + case e: ZooKeeperClientTimeoutException => + assertEquals("ZooKeeper client threads still running", Set.empty, runningZkSendThreads) + } } + private def runningZkSendThreads: collection.Set[String] = Thread.getAllStackTraces.keySet.asScala + .filter(_.isAlive) + .map(_.getName) + .filter(t => t.contains("SendThread()")) + @Test(expected = classOf[ZooKeeperClientTimeoutException]) def testConnectionTimeout(): Unit = { zookeeper.shutdown()