This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.14.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 0fc6c3e6e71616b26a9b85112edd0715e221909b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Jan 14 07:19:49 2022 +0100 CAMEL-17486: camel-core - ThrottlePermit compareTo cast to int causes issues. Thanks to Marc Boulanger for the patch and unit test. --- .../java/org/apache/camel/processor/Throttler.java | 2 +- .../camel/processor/ThrottlerPermitTest.java | 70 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Throttler.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Throttler.java index ccfc609..2a77afa 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/Throttler.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/Throttler.java @@ -393,7 +393,7 @@ public class Throttler extends AsyncProcessorSupport implements Traceable, IdAwa @Override public int compareTo(final Delayed o) { - return (int) (getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); + return Long.compare(getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS)); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ThrottlerPermitTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ThrottlerPermitTest.java new file mode 100644 index 0000000..cf5da58 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/ThrottlerPermitTest.java @@ -0,0 +1,70 @@ +/* + * 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.camel.processor; + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.Delayed; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * When a Throttler has not been used for longer than {@link java.lang.Integer#MAX_VALUE}, + * casting the result of the {@link ThrottlePermit} comparison to an int causes an overflow. + * Using a value comparison prevents this issue. + */ +public class ThrottlerPermitTest { + + @Test + public void testThrottlerPermitWithOldScheduledTime() { + long timeMillis = System.currentTimeMillis(); + // 30 days in the past + ThrottlePermit throttlePermitOld = new ThrottlePermit(timeMillis - 2592000000L); + // Now + ThrottlePermit throttlePermitNow = new ThrottlePermit(timeMillis); + ThrottlePermit throttlePermitNow2 = new ThrottlePermit(timeMillis); + // Future + ThrottlePermit throttlePermitFuture = new ThrottlePermit(timeMillis + 1000); + + assertEquals(-1, throttlePermitOld.compareTo(throttlePermitNow)); + assertEquals(0, throttlePermitNow.compareTo(throttlePermitNow2)); + assertEquals(1, throttlePermitFuture.compareTo(throttlePermitNow)); + } + + private static final class ThrottlePermit implements Delayed { + private volatile long scheduledTime; + + ThrottlePermit(final long delayMs) { + setDelayMs(delayMs); + } + + public void setDelayMs(final long delayMs) { + this.scheduledTime = System.currentTimeMillis() + delayMs; + } + + @Override + public long getDelay(final TimeUnit unit) { + return unit.convert(scheduledTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS); + } + + @Override + public int compareTo(final Delayed o) { + return Long.compare(getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS)); + } + } +}