This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 50cf59774b53 CAMEL-26471: camel-util - StopWatch.taken: convert to 
millis without a Duration (#28815)
50cf59774b53 is described below

commit 50cf59774b536ba0878f3d01b80807246bc74f29
Author: Adriano Machado <[email protected]>
AuthorDate: Mon Nov 2 05:52:03 2026 -0500

    CAMEL-26471: camel-util - StopWatch.taken: convert to millis without a 
Duration (#28815)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../main/java/org/apache/camel/util/StopWatch.java |  4 +--
 .../java/org/apache/camel/util/StopWatchTest.java  | 36 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/core/camel-util/src/main/java/org/apache/camel/util/StopWatch.java 
b/core/camel-util/src/main/java/org/apache/camel/util/StopWatch.java
index 9e92ac81b0c2..cb46a16ad35b 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StopWatch.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StopWatch.java
@@ -16,7 +16,7 @@
  */
 package org.apache.camel.util;
 
-import java.time.Duration;
+import java.util.concurrent.TimeUnit;
 
 /**
  * A very simple stop watch.
@@ -67,7 +67,7 @@ public final class StopWatch {
     public long taken() {
         if (start > 0) {
             long delta = TimeSource.nanoTime() - start;
-            return Duration.ofNanos(delta).toMillis();
+            return TimeUnit.NANOSECONDS.toMillis(delta);
         }
         return 0;
     }
diff --git 
a/core/camel-util/src/test/java/org/apache/camel/util/StopWatchTest.java 
b/core/camel-util/src/test/java/org/apache/camel/util/StopWatchTest.java
index 1e9c15e6d73a..e9b002ac9374 100644
--- a/core/camel-util/src/test/java/org/apache/camel/util/StopWatchTest.java
+++ b/core/camel-util/src/test/java/org/apache/camel/util/StopWatchTest.java
@@ -72,4 +72,40 @@ class StopWatchTest {
         assertTrue(taken < 1500, "Elapsed time should be smaller than 1500 ms 
but was " + taken);
     }
 
+    @Test
+    void testTakenTruncatesNanosToMillis() {
+        // task timings are compared with timeouts, so a partial milli must 
not be rounded up
+        long[] nanos = { 1_000_000_000L };
+        TimeSource.Provider previous = TimeSource.install(new 
TimeSource.Provider() {
+            @Override
+            public long currentTimeMillis() {
+                return 0;
+            }
+
+            @Override
+            public long nanoTime() {
+                return nanos[0];
+            }
+
+            @Override
+            public void sleep(long millis) {
+                // not used
+            }
+        });
+        try {
+            StopWatch stopWatch = new StopWatch();
+
+            nanos[0] += 1_999_999;
+            assertEquals(1, stopWatch.taken());
+
+            nanos[0] += 1;
+            assertEquals(2, stopWatch.taken());
+
+            // a clock going backwards truncates toward zero too
+            nanos[0] -= 3_999_999;
+            assertEquals(-1, stopWatch.taken());
+        } finally {
+            TimeSource.install(previous);
+        }
+    }
 }

Reply via email to