You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Claus Ibsen (Jira)" <ji...@apache.org> on 2022/01/14 06:41:00 UTC
[jira] [Resolved] (CAMEL-17486) camel-core - ThrottlePermit compareTo cast to int causes issues
[ https://issues.apache.org/jira/browse/CAMEL-17486?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Claus Ibsen resolved CAMEL-17486.
---------------------------------
Resolution: Fixed
> camel-core - ThrottlePermit compareTo cast to int causes issues
> ---------------------------------------------------------------
>
> Key: CAMEL-17486
> URL: https://issues.apache.org/jira/browse/CAMEL-17486
> Project: Camel
> Issue Type: Bug
> Components: camel-core
> Reporter: MARC BOULANGER
> Assignee: Claus Ibsen
> Priority: Minor
> Fix For: 3.15.0, 3.14.1, 3.11.6
>
>
> When a Throttler has not been used for longer than Integer#MAX_VALUE, casting the result of the ThrottlePermit comparison to an int causes a bit overflow.
> Using a value comparison prevents this issue.
>
> Current :
> {code:java}
> private static class ThrottlePermit implements Delayed {
> @Override
> public int compareTo(final Delayed o) {
> return (int) (getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
> }
> }
> {code}
> Proposal :
> {code:java}
> private static class ThrottlePermit implements Delayed {
> @Override
> public int compareTo(final Delayed o) {
> return Long.compare(getDelay(TimeUnit.MILLISECONDS), o.getDelay(TimeUnit.MILLISECONDS));
> }
> }
> {code}
> Unit Test :
> {code:java}
> public class ThrottlerPermitTest {
> /**
> * 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 a bit overflow.
> * Using a value comparison prevents this issue.
> **/
> @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 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));
> }
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)