Repository: activemq Updated Branches: refs/heads/master e39db5693 -> b79fcd0a7
AMQ-7017 - Prevent ArithmeticException in ProducerBrokerExchange Check for zero to prevent divide by zero error inside getPercentageBlocked() method Thank you to Matthew Stratton for the patch Project: http://git-wip-us.apache.org/repos/asf/activemq/repo Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/b79fcd0a Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/b79fcd0a Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/b79fcd0a Branch: refs/heads/master Commit: b79fcd0a768e5c7d45a7f68febf447d8379cbaf4 Parents: e39db56 Author: Christopher L. Shannon (cshannon) <[email protected]> Authored: Fri Jul 27 07:40:13 2018 -0400 Committer: Christopher L. Shannon (cshannon) <[email protected]> Committed: Fri Jul 27 07:40:49 2018 -0400 ---------------------------------------------------------------------- .../activemq/broker/ProducerBrokerExchange.java | 10 +++--- .../broker/ProducerBrokerExchangeTest.java | 38 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/activemq/blob/b79fcd0a/activemq-broker/src/main/java/org/apache/activemq/broker/ProducerBrokerExchange.java ---------------------------------------------------------------------- diff --git a/activemq-broker/src/main/java/org/apache/activemq/broker/ProducerBrokerExchange.java b/activemq-broker/src/main/java/org/apache/activemq/broker/ProducerBrokerExchange.java index 26652de..99b766b 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/broker/ProducerBrokerExchange.java +++ b/activemq-broker/src/main/java/org/apache/activemq/broker/ProducerBrokerExchange.java @@ -16,10 +16,6 @@ */ package org.apache.activemq.broker; -import java.io.IOException; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicLong; - import org.apache.activemq.broker.region.Destination; import org.apache.activemq.broker.region.Region; import org.apache.activemq.command.Message; @@ -28,6 +24,10 @@ import org.apache.activemq.state.ProducerState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + /** * Holds internal state in the broker for a MessageProducer */ @@ -213,7 +213,7 @@ public class ProducerBrokerExchange { } public int getPercentageBlocked() { - double value = flowControlInfo.getSendsBlocked() / flowControlInfo.getTotalSends(); + double value = flowControlInfo.getTotalSends() == 0 ? 0 : flowControlInfo.getSendsBlocked() / flowControlInfo.getTotalSends(); return (int) value * 100; } http://git-wip-us.apache.org/repos/asf/activemq/blob/b79fcd0a/activemq-broker/src/test/java/org/apache/activemq/broker/ProducerBrokerExchangeTest.java ---------------------------------------------------------------------- diff --git a/activemq-broker/src/test/java/org/apache/activemq/broker/ProducerBrokerExchangeTest.java b/activemq-broker/src/test/java/org/apache/activemq/broker/ProducerBrokerExchangeTest.java new file mode 100644 index 0000000..98b0ddb --- /dev/null +++ b/activemq-broker/src/test/java/org/apache/activemq/broker/ProducerBrokerExchangeTest.java @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ProducerBrokerExchangeTest { + + @Test + public void testGetPercentageBlockedHandlesDivideByZero(){ + ProducerBrokerExchange producerBrokerExchange = new ProducerBrokerExchange(); + producerBrokerExchange.getPercentageBlocked(); + } + + @Test + public void testGetPercentageBlockedNonZero(){ + ProducerBrokerExchange producerBrokerExchange = new ProducerBrokerExchange(); + producerBrokerExchange.blockingOnFlowControl(true); + producerBrokerExchange.incrementSend(); + assertEquals(100.0, producerBrokerExchange.getPercentageBlocked(), 0); + } +}
