This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 27697489ae5e82caca5aa47d623a1ad737434f41 Author: Claus Ibsen <[email protected]> AuthorDate: Wed Jun 25 21:12:53 2025 +0200 CAMEL-22201: The clock should seperate elapsed and wall-clock. This allows exchange.getCreated to be accurate and has same behaviour as older Camel releases. --- .../apache/camel/support/MonotonicClockTest.java} | 35 +++++++++++----------- .../org/apache/camel/support/MonotonicClock.java | 6 ++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java b/core/camel-core/src/test/java/org/apache/camel/support/MonotonicClockTest.java similarity index 57% copy from core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java copy to core/camel-core/src/test/java/org/apache/camel/support/MonotonicClockTest.java index 7e7ab0573be..0a71c39d6f6 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java +++ b/core/camel-core/src/test/java/org/apache/camel/support/MonotonicClockTest.java @@ -16,27 +16,26 @@ */ package org.apache.camel.support; -import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; -import org.apache.camel.clock.Clock; +public class MonotonicClockTest { -/** - * A clock that increases monotonically (i.e.: does not go back in time) - */ -public final class MonotonicClock implements Clock { - private final long createdNano; - - public MonotonicClock() { - this.createdNano = System.nanoTime(); - } + @Test + public void testElapsed() throws Exception { + MonotonicClock clock = new MonotonicClock(); + long e = clock.elapsed(); + long c = clock.getCreated(); + Assertions.assertNotEquals(e, c); - @Override - public long elapsed() { - return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - createdNano); - } + // elapse a tiny bit of time + Thread.sleep(2); - @Override - public long getCreated() { - return System.currentTimeMillis() - elapsed(); + long e2 = clock.elapsed(); + long c2 = clock.getCreated(); + Assertions.assertNotEquals(e2, c2); + Assertions.assertNotEquals(e, e2); + Assertions.assertTrue(e2 > e); + Assertions.assertEquals(c, c2); } } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java b/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java index 7e7ab0573be..46ab3772368 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/MonotonicClock.java @@ -21,12 +21,14 @@ import java.util.concurrent.TimeUnit; import org.apache.camel.clock.Clock; /** - * A clock that increases monotonically (i.e.: does not go back in time) + * A clock that increases monotonically (i.e.: does not go back in time) for precise elapsed calculations. */ public final class MonotonicClock implements Clock { + private final long created; private final long createdNano; public MonotonicClock() { + this.created = System.currentTimeMillis(); this.createdNano = System.nanoTime(); } @@ -37,6 +39,6 @@ public final class MonotonicClock implements Clock { @Override public long getCreated() { - return System.currentTimeMillis() - elapsed(); + return created; } }
