diff --git a/core/src/main/scala/kafka/tools/ConsumerPerformance.scala b/core/src/main/scala/kafka/tools/ConsumerPerformance.scala index d2cd20d42f059..a329b4aae55c2 100644 --- a/core/src/main/scala/kafka/tools/ConsumerPerformance.scala +++ b/core/src/main/scala/kafka/tools/ConsumerPerformance.scala @@ -24,6 +24,7 @@ import java.util.concurrent.atomic.AtomicLong import java.util.{Properties, Random} import com.typesafe.scalalogging.LazyLogging +import joptsimple.OptionException import kafka.utils.{CommandLineUtils, ToolsUtils} import org.apache.kafka.clients.consumer.{ConsumerRebalanceListener, KafkaConsumer} import org.apache.kafka.common.serialization.ByteArrayDeserializer @@ -255,7 +256,12 @@ object ConsumerPerformance extends LazyLogging { .ofType(classOf[Long]) .defaultsTo(10000) - options = parser.parse(args: _*) + try + options = parser.parse(args: _*) + catch { + case e: OptionException => + CommandLineUtils.printUsageAndDie(parser, e.getMessage) + } if(options.has(numThreadsOpt) || options.has(numFetchersOpt)) println("WARNING: option [threads] and [num-fetch-threads] have been deprecated and will be ignored by the test") diff --git a/core/src/test/scala/unit/kafka/tools/ConsumerPerformanceTest.scala b/core/src/test/scala/unit/kafka/tools/ConsumerPerformanceTest.scala index 530d191a42e1e..94b732d2fd57d 100644 --- a/core/src/test/scala/unit/kafka/tools/ConsumerPerformanceTest.scala +++ b/core/src/test/scala/unit/kafka/tools/ConsumerPerformanceTest.scala @@ -20,7 +20,7 @@ package kafka.tools import java.io.ByteArrayOutputStream import java.text.SimpleDateFormat -import joptsimple.OptionException +import kafka.utils.Exit import org.junit.Assert.assertEquals import org.junit.Test @@ -97,8 +97,9 @@ class ConsumerPerformanceTest { assertEquals(10, config.numMessages) } - @Test(expected = classOf[OptionException]) + @Test(expected = classOf[IllegalArgumentException]) def testConfigWithUnrecognizedOption(): Unit = { + Exit.setExitProcedure((_, message) => throw new IllegalArgumentException(message.orNull)) //Given val args: Array[String] = Array( "--broker-list", "localhost:9092", @@ -106,9 +107,12 @@ class ConsumerPerformanceTest { "--messages", "10", "--new-consumer" ) - - //When - new ConsumerPerformance.ConsumerPerfConfig(args) + try { + //When + new ConsumerPerformance.ConsumerPerfConfig(args) + } finally { + Exit.resetExitProcedure() + } } private def testHeaderMatchContent(detailed: Boolean, expectedOutputLineCount: Int, fun: () => Unit): Unit = {