From c51eaad9745d36d737b30d645fa1db753ae87246 Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Fri, 11 Aug 2017 13:49:44 -0700 Subject: [PATCH] Fix #657: Use exceptions instead of assert() to do runtime parameter validations (#673) --- pulsar-client-cpp/lib/ProducerConfiguration.cc | 8 ++++++-- pulsar-client-cpp/tests/BatchMessageTest.cc | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/pulsar-client-cpp/lib/ProducerConfiguration.cc b/pulsar-client-cpp/lib/ProducerConfiguration.cc index aead635e1c58a..27952ccec5cd8 100644 --- a/pulsar-client-cpp/lib/ProducerConfiguration.cc +++ b/pulsar-client-cpp/lib/ProducerConfiguration.cc @@ -55,7 +55,9 @@ CompressionType ProducerConfiguration::getCompressionType() const { } ProducerConfiguration& ProducerConfiguration::setMaxPendingMessages(int maxPendingMessages) { - assert(maxPendingMessages > 0); + if (maxPendingMessages <= 0) { + throw "maxPendingMessages needs to be greater than 0"; + } impl_->maxPendingMessages = maxPendingMessages; return *this; } @@ -104,7 +106,9 @@ const bool& ProducerConfiguration::getBatchingEnabled() const { ProducerConfiguration& ProducerConfiguration::setBatchingMaxMessages( const unsigned int& batchingMaxMessages) { - assert(batchingMaxMessages > 1); + if (batchingMaxMessages <= 1) { + throw "batchingMaxMessages needs to be greater than 1"; + } impl_->batchingMaxMessages = batchingMaxMessages; return *this; } diff --git a/pulsar-client-cpp/tests/BatchMessageTest.cc b/pulsar-client-cpp/tests/BatchMessageTest.cc index dd778fbf2b321..a138e09301b2f 100644 --- a/pulsar-client-cpp/tests/BatchMessageTest.cc +++ b/pulsar-client-cpp/tests/BatchMessageTest.cc @@ -72,7 +72,12 @@ static void sendCallBackExpectingErrors(Result r, const Message& msg) { TEST(BatchMessageTest, testProducerConfig) { ProducerConfiguration conf; - ASSERT_DEATH(conf.setBatchingMaxMessages(1), ""); + try { + conf.setBatchingMaxMessages(1); + FAIL(); + } catch (const char* ex) { + // Ok + } } TEST(BatchMessageTest, testProducerTimeout) { @@ -301,7 +306,7 @@ TEST(BatchMessageTest, testSmallReceiverQueueSize) { LOG_DEBUG("Received Message with [ content - " << receivedMsg.getDataAsString() << "] [ messageID = " << receivedMsg.getMessageId() << "]"); ASSERT_EQ(receivedMsg.getProperty("msgIndex"), boost::lexical_cast(i)); ASSERT_EQ(expectedMessageContent, receivedMsg.getDataAsString()); - ASSERT_EQ(ResultOk, consumer.acknowledge(receivedMsg)); + ASSERT_EQ(ResultOk, consumer.acknowledge(receivedMsg)); } ConsumerStatsImplPtr consumerStatsImplPtr = PulsarFriend::getConsumerStatsPtr(consumer);