Repository: activemq
Updated Branches:
  refs/heads/activemq-5.15.x 887db9e2f -> e3f76e169


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

(cherry picked from commit b79fcd0a768e5c7d45a7f68febf447d8379cbaf4)


Project: http://git-wip-us.apache.org/repos/asf/activemq/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq/commit/e3f76e16
Tree: http://git-wip-us.apache.org/repos/asf/activemq/tree/e3f76e16
Diff: http://git-wip-us.apache.org/repos/asf/activemq/diff/e3f76e16

Branch: refs/heads/activemq-5.15.x
Commit: e3f76e169325f4741c4ddf0c38c10ca65fa1c385
Parents: 887db9e
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:41:22 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/e3f76e16/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/e3f76e16/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);
+    }
+}

Reply via email to