You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/12/19 17:23:04 UTC
(camel) branch main updated: CAMEL-20225: use a StopWatch for computing duration (#12491)
This is an automated email from the ASF dual-hosted git repository.
orpiske 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 25d61fe4c60 CAMEL-20225: use a StopWatch for computing duration (#12491)
25d61fe4c60 is described below
commit 25d61fe4c606dcad374b31e4ad445ee4c986daaa
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Tue Dec 19 14:22:57 2023 -0300
CAMEL-20225: use a StopWatch for computing duration (#12491)
---
.../ConcurrentConsumerLoadManualTest.java | 11 +++++------
.../ConcurrentJaxbDataFormatSchemaValidationTest.java | 9 +++++----
.../camel/example/DataFormatConcurrentTest.java | 13 +++++++------
.../jms/issues/JmsConcurrentConsumersTest.java | 7 ++++---
.../jms/tuning/PerformanceRoutePojoTest.java | 7 ++++---
.../component/jms/tuning/PerformanceRouteTest.java | 7 ++++---
.../jt400/Jt400DataQueueConsumerManualTest.java | 5 +++--
.../kubernetes/cluster/utils/LeaderRecorder.java | 5 +++--
.../component/mina/MinaProducerShutdownManualIT.java | 7 ++++---
.../org/apache/camel/component/mock/MockEndpoint.java | 10 +++++-----
.../mybatis/MyBatisPollingDelayRouteTest.java | 5 +++--
.../reactive/streams/BackpressureSubscriberTest.java | 19 ++++++++++---------
.../internal/client/AbstractClientBaseTest.java | 9 +++++----
.../spring/interceptor/DelayerInterceptorTest.java | 5 +++--
.../camel/component/stream/StreamDelayTest.java | 7 ++++---
.../main/java/org/apache/camel/main/MainHelper.java | 8 +++++---
.../src/main/java/org/apache/camel/maven/RunMojo.java | 5 +++--
17 files changed, 77 insertions(+), 62 deletions(-)
diff --git a/components/camel-ironmq/src/test/java/org/apache/camel/component/ironmq/integrationtest/ConcurrentConsumerLoadManualTest.java b/components/camel-ironmq/src/test/java/org/apache/camel/component/ironmq/integrationtest/ConcurrentConsumerLoadManualTest.java
index e7272b37048..f558e35c070 100644
--- a/components/camel-ironmq/src/test/java/org/apache/camel/component/ironmq/integrationtest/ConcurrentConsumerLoadManualTest.java
+++ b/components/camel-ironmq/src/test/java/org/apache/camel/component/ironmq/integrationtest/ConcurrentConsumerLoadManualTest.java
@@ -23,6 +23,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.ironmq.IronMQConstants;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -54,7 +55,7 @@ public class ConcurrentConsumerLoadManualTest extends CamelTestSupport {
public void prepareQueue() throws InterruptedException {
// make sure the queue is empty before test
template.sendBodyAndHeader(ironMQEndpoint, null, IronMQConstants.OPERATION, IronMQConstants.CLEARQUEUE);
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
int noOfBlocks = 0;
ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= NO_OF_MESSAGES; i++) {
@@ -72,8 +73,7 @@ public class ConcurrentConsumerLoadManualTest extends CamelTestSupport {
LOGGER.info("Waiting for queue to fill up. Current size is " + mockEndpoint.getReceivedCounter() * 100);
Thread.sleep(1000);
}
- long delta = System.currentTimeMillis() - start;
- int seconds = (int) delta / 1000;
+ int seconds = (int) watch.taken() / 1000;
int msgPrSec = NO_OF_MESSAGES / seconds;
LOGGER.info("IronMQPerformanceTest: Took: " + seconds + " seconds to produce " + NO_OF_MESSAGES + " messages. Which is "
+ msgPrSec + " messages pr. second");
@@ -81,13 +81,12 @@ public class ConcurrentConsumerLoadManualTest extends CamelTestSupport {
@Test
public void testConcurrentConsumers() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
context.getRouteController().startRoute("iron");
MockEndpoint endpoint = getMockEndpoint("mock:result");
endpoint.expectedMessageCount(NO_OF_MESSAGES);
MockEndpoint.assertIsSatisfied(context, 4, TimeUnit.MINUTES);
- long delta = System.currentTimeMillis() - start;
- int seconds = (int) delta / 1000;
+ int seconds = (int) watch.taken() / 1000;
int msgPrSec = NO_OF_MESSAGES / seconds;
LOGGER.info("IronmqPerformanceTest: Took: " + seconds + " seconds to consume " + NO_OF_MESSAGES + " messages. Which is "
+ msgPrSec + " messages pr. second");
diff --git a/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/ConcurrentJaxbDataFormatSchemaValidationTest.java b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/ConcurrentJaxbDataFormatSchemaValidationTest.java
index fa4abf0ed0f..432355c0c2e 100644
--- a/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/ConcurrentJaxbDataFormatSchemaValidationTest.java
+++ b/components/camel-jaxb/src/test/java/org/apache/camel/converter/jaxb/ConcurrentJaxbDataFormatSchemaValidationTest.java
@@ -24,6 +24,7 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.converter.jaxb.address.Address;
import org.apache.camel.converter.jaxb.person.Person;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,13 +57,13 @@ public class ConcurrentJaxbDataFormatSchemaValidationTest extends CamelTestSuppo
person.setAge(Integer.valueOf(36));
person.setAddress(address);
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
for (int i = 0; i < testCount; i++) {
template.sendBody("seda:marshall", person);
}
MockEndpoint.assertIsSatisfied(context);
- LOG.info("Validation of {} messages took {} ms", testCount, System.currentTimeMillis() - start);
+ LOG.info("Validation of {} messages took {} ms", testCount, watch.taken());
String payload = mockMarshall.getExchanges().get(0).getIn().getBody(String.class);
LOG.info(payload);
@@ -94,13 +95,13 @@ public class ConcurrentJaxbDataFormatSchemaValidationTest extends CamelTestSuppo
.append("</person>")
.toString();
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
for (int i = 0; i < testCount; i++) {
template.sendBody("seda:unmarshall", xml);
}
MockEndpoint.assertIsSatisfied(context, 20, TimeUnit.SECONDS);
- LOG.info("Validation of {} messages took {} ms", testCount, System.currentTimeMillis() - start);
+ LOG.info("Validation of {} messages took {} ms", testCount, watch.taken());
Person person = mockUnmarshall.getExchanges().get(0).getIn().getBody(Person.class);
diff --git a/components/camel-jaxb/src/test/java/org/apache/camel/example/DataFormatConcurrentTest.java b/components/camel-jaxb/src/test/java/org/apache/camel/example/DataFormatConcurrentTest.java
index 8253a42f55c..f05f4984318 100644
--- a/components/camel-jaxb/src/test/java/org/apache/camel/example/DataFormatConcurrentTest.java
+++ b/components/camel-jaxb/src/test/java/org/apache/camel/example/DataFormatConcurrentTest.java
@@ -30,6 +30,7 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.spi.DataFormat;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -167,7 +168,7 @@ public class DataFormatConcurrentTest extends CamelTestSupport {
final ByteArrayInputStream[] payloads = createPayloads(testCycleCount);
ExecutorService pool = Executors.newFixedThreadPool(20);
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
for (int i = 0; i < payloads.length; i++) {
final int finalI = i;
pool.execute(new Runnable() {
@@ -178,10 +179,10 @@ public class DataFormatConcurrentTest extends CamelTestSupport {
}
latch.await();
- long end = System.currentTimeMillis();
+ long duration = watch.taken();
LOG.info("Sending {} messages to {} took {} ms",
- new Object[] { payloads.length, template.getDefaultEndpoint().getEndpointUri(), end - start });
+ payloads.length, template.getDefaultEndpoint().getEndpointUri(), duration);
}
public void marshal(final CountDownLatch latch) throws Exception {
@@ -193,7 +194,7 @@ public class DataFormatConcurrentTest extends CamelTestSupport {
final Foo[] payloads = createFoo(testCycleCount);
ExecutorService pool = Executors.newFixedThreadPool(20);
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
for (int i = 0; i < payloads.length; i++) {
final int finalI = i;
pool.execute(new Runnable() {
@@ -204,10 +205,10 @@ public class DataFormatConcurrentTest extends CamelTestSupport {
}
latch.await();
- long end = System.currentTimeMillis();
+ long duration = watch.taken();
LOG.info("Sending {} messages to {} took {} ms",
- new Object[] { payloads.length, template.getDefaultEndpoint().getEndpointUri(), end - start });
+ payloads.length, template.getDefaultEndpoint().getEndpointUri(), duration);
}
/**
diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsConcurrentConsumersTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsConcurrentConsumersTest.java
index fb52866814b..7d6ab376645 100644
--- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsConcurrentConsumersTest.java
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/issues/JmsConcurrentConsumersTest.java
@@ -27,6 +27,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.AbstractJMSTest;
import org.apache.camel.test.infra.core.CamelContextExtension;
import org.apache.camel.test.infra.core.DefaultCamelContextExtension;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@@ -69,14 +70,14 @@ public class JmsConcurrentConsumersTest extends AbstractJMSTest {
});
}
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
// wait for test completion, timeout after 30 sec to let other unit test run to not wait forever
assertTrue(latch.await(30000L, TimeUnit.MILLISECONDS));
assertEquals(0, latch.getCount(), "Latch should be zero");
- long delta = System.currentTimeMillis() - start;
- assertTrue(delta < 20000L, "Should be faster than 20000 millis, took " + delta + " millis");
+ long duration = watch.taken();
+ assertTrue(duration < 20000L, "Should be faster than 20000 millis, took " + duration + " millis");
executor.shutdown();
}
diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRoutePojoTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRoutePojoTest.java
index 76fade5e15c..27fee0e5c9e 100644
--- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRoutePojoTest.java
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRoutePojoTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.jms.tuning;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.junit5.CamelSpringTestSupport;
+import org.apache.camel.util.StopWatch;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -37,7 +38,7 @@ public class PerformanceRoutePojoTest extends CamelSpringTestSupport {
@Test
public void testPojoPerformance() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
int size = 200;
getMockEndpoint("mock:audit").expectedMessageCount(size);
@@ -60,8 +61,8 @@ public class PerformanceRoutePojoTest extends CamelSpringTestSupport {
MockEndpoint.assertIsSatisfied(context);
- long delta = System.currentTimeMillis() - start;
- LOG.info("RoutePerformancePojoTest: Sent: {} Took: {} ms", size, delta);
+ long duration = watch.taken();
+ LOG.info("RoutePerformancePojoTest: Sent: {} Took: {} ms", size, duration);
}
}
diff --git a/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRouteTest.java b/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRouteTest.java
index 7c67bea6dd8..c138b3da53f 100644
--- a/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRouteTest.java
+++ b/components/camel-jms/src/test/java/org/apache/camel/component/jms/tuning/PerformanceRouteTest.java
@@ -24,6 +24,7 @@ import org.apache.camel.component.jms.AbstractJMSTest;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.infra.core.CamelContextExtension;
import org.apache.camel.test.infra.core.DefaultCamelContextExtension;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Order;
@@ -49,7 +50,7 @@ public class PerformanceRouteTest extends AbstractJMSTest {
public void testPerformance() throws Exception {
assumeTrue(canRunOnThisPlatform(), "Test is not intended for this platform");
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
int size = 200;
getMockEndpoint("mock:audit").expectedMessageCount(size);
@@ -72,8 +73,8 @@ public class PerformanceRouteTest extends AbstractJMSTest {
MockEndpoint.assertIsSatisfied(context);
- long delta = System.currentTimeMillis() - start;
- LOG.info("RoutePerformanceTest: Sent: {} Took: {} ms", size, delta);
+ long duration = watch.taken();
+ LOG.info("RoutePerformanceTest: Sent: {} Took: {} ms", size, duration);
}
private boolean canRunOnThisPlatform() {
diff --git a/components/camel-jt400/src/test/java/org/apache/camel/component/jt400/Jt400DataQueueConsumerManualTest.java b/components/camel-jt400/src/test/java/org/apache/camel/component/jt400/Jt400DataQueueConsumerManualTest.java
index 851d7ad882e..7174bac6a92 100644
--- a/components/camel-jt400/src/test/java/org/apache/camel/component/jt400/Jt400DataQueueConsumerManualTest.java
+++ b/components/camel-jt400/src/test/java/org/apache/camel/component/jt400/Jt400DataQueueConsumerManualTest.java
@@ -22,6 +22,7 @@ import java.util.Properties;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
@@ -105,9 +106,9 @@ public class Jt400DataQueueConsumerManualTest {
}
}).start();
- final long startTime = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
while (!receiveFlag) {
- if ((System.currentTimeMillis() - startTime) > BLOCKING_THRESHOLD) {
+ if ((watch.taken()) > BLOCKING_THRESHOLD) {
/* Passed test. */
return;
}
diff --git a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LeaderRecorder.java b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LeaderRecorder.java
index f2330d6be33..ce0c2d6ca6d 100644
--- a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LeaderRecorder.java
+++ b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/cluster/utils/LeaderRecorder.java
@@ -29,6 +29,7 @@ import org.apache.camel.RuntimeCamelException;
import org.apache.camel.cluster.CamelClusterEventListener;
import org.apache.camel.cluster.CamelClusterMember;
import org.apache.camel.cluster.CamelClusterView;
+import org.apache.camel.util.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -68,9 +69,9 @@ public class LeaderRecorder implements CamelClusterEventListener.Leadership {
}
public void waitForLeader(Predicate<String> as, long time, TimeUnit unit) {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
while (!as.test(getCurrentLeader())) {
- assertFalse(System.currentTimeMillis() - start > TimeUnit.MILLISECONDS.convert(time, unit),
+ assertFalse(watch.taken() > TimeUnit.MILLISECONDS.convert(time, unit),
"Timeout while waiting for condition");
doWait(50);
}
diff --git a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaProducerShutdownManualIT.java b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaProducerShutdownManualIT.java
index 5b5fd2a04f6..edbc61d59db 100644
--- a/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaProducerShutdownManualIT.java
+++ b/components/camel-mina/src/test/java/org/apache/camel/component/mina/MinaProducerShutdownManualIT.java
@@ -22,6 +22,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.Producer;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
@@ -37,7 +38,7 @@ public class MinaProducerShutdownManualIT {
private static final Logger LOG = LoggerFactory.getLogger(MinaProducerShutdownManualIT.class);
private static final String URI = "mina:tcp://localhost:6321?textline=true&sync=false";
- private long start;
+ private StopWatch stopWatch;
private CamelContext context;
public static void main(String[] args) throws Exception {
@@ -51,7 +52,7 @@ public class MinaProducerShutdownManualIT {
Thread hook = new AssertShutdownHook();
Runtime.getRuntime().addShutdownHook(hook);
- start = System.currentTimeMillis();
+ stopWatch = new StopWatch();
context = new DefaultCamelContext();
context.addRoutes(createRouteBuilder());
@@ -66,7 +67,7 @@ public class MinaProducerShutdownManualIT {
@Override
public void run() {
- long diff = System.currentTimeMillis() - start;
+ long diff = stopWatch.taken();
if (diff > 5000) {
LOG.error("ERROR: MinaProducer should be able to shutdown within 5000 millis: time={}", diff);
}
diff --git a/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java b/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
index 3d8d84db169..9648ea56f42 100644
--- a/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
+++ b/components/camel-mock/src/main/java/org/apache/camel/component/mock/MockEndpoint.java
@@ -166,16 +166,15 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
}
public static void assertWait(long timeout, TimeUnit unit, MockEndpoint... endpoints) throws InterruptedException {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
long left = unit.toMillis(timeout);
- long end = start + left;
for (MockEndpoint endpoint : endpoints) {
if (!endpoint.await(left, TimeUnit.MILLISECONDS)) {
throw new AssertionError(
"Timeout waiting for endpoints to receive enough messages. " + endpoint.getEndpointUri()
+ " timed out.");
}
- left = end - System.currentTimeMillis();
+ left = left - watch.taken();
if (left <= 0) {
left = 0;
}
@@ -976,7 +975,8 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
expects(() -> {
// wait at most 5 seconds for the file to exists
- final long timeout = System.currentTimeMillis() + 5000;
+ final long timeout = 5000;
+ final StopWatch watch = new StopWatch();
boolean stop = false;
while (!stop && !file.exists()) {
@@ -985,7 +985,7 @@ public class MockEndpoint extends DefaultEndpoint implements BrowsableEndpoint,
} catch (InterruptedException e) {
// ignore
}
- stop = System.currentTimeMillis() > timeout;
+ stop = watch.taken() > timeout;
}
assertTrue("The file should exists: " + name, file.exists());
diff --git a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisPollingDelayRouteTest.java b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisPollingDelayRouteTest.java
index 67b570ce525..50cffc763b8 100644
--- a/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisPollingDelayRouteTest.java
+++ b/components/camel-mybatis/src/test/java/org/apache/camel/component/mybatis/MyBatisPollingDelayRouteTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.mybatis;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -26,12 +27,12 @@ public class MyBatisPollingDelayRouteTest extends MyBatisTestSupport {
@Test
public void testSendAccountBean() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch stopWatch = new StopWatch();
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMinimumMessageCount(2);
MockEndpoint.assertIsSatisfied(context);
- long delta = System.currentTimeMillis() - start;
+ long delta = stopWatch.taken();
assertTrue(delta < 7000, "Should not take that long: " + delta);
}
diff --git a/components/camel-reactive-streams/src/test/java/org/apache/camel/component/reactive/streams/BackpressureSubscriberTest.java b/components/camel-reactive-streams/src/test/java/org/apache/camel/component/reactive/streams/BackpressureSubscriberTest.java
index d3bff071d04..59113881780 100644
--- a/components/camel-reactive-streams/src/test/java/org/apache/camel/component/reactive/streams/BackpressureSubscriberTest.java
+++ b/components/camel-reactive-streams/src/test/java/org/apache/camel/component/reactive/streams/BackpressureSubscriberTest.java
@@ -23,6 +23,7 @@ import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.component.reactive.streams.api.CamelReactiveStreams;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -36,7 +37,7 @@ public class BackpressureSubscriberTest extends BaseReactiveTest {
@Test
public void testBackpressure() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
Observable.range(0, 10)
.toFlowable(BackpressureStrategy.BUFFER)
.subscribe(CamelReactiveStreams.get(context).streamSubscriber("slowNumbers", Integer.class));
@@ -44,17 +45,17 @@ public class BackpressureSubscriberTest extends BaseReactiveTest {
MockEndpoint endpoint = getMockEndpoint("mock:endpoint");
endpoint.expectedMessageCount(10);
endpoint.assertIsSatisfied();
- long end = System.currentTimeMillis();
+ long duration = watch.taken();
// Maximum one inflight exchange, even if multiple consumer threads are present
// Must take at least 50 * 10 = 500ms
- assertTrue(end - start >= 500, "Exchange completed too early");
+ assertTrue(duration >= 500, "Exchange completed too early");
}
@Test
public void testSlowerBackpressure() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
Observable.range(0, 2)
.toFlowable(BackpressureStrategy.BUFFER)
.subscribe(CamelReactiveStreams.get(context).streamSubscriber("slowerNumbers", Integer.class));
@@ -62,28 +63,28 @@ public class BackpressureSubscriberTest extends BaseReactiveTest {
MockEndpoint endpoint = getMockEndpoint("mock:endpoint");
endpoint.expectedMessageCount(2);
endpoint.assertIsSatisfied();
- long end = System.currentTimeMillis();
+ long duration = watch.taken();
// Maximum one inflight exchange, even if multiple consumer threads are present
// Must take at least 300 * 2 = 600ms
- assertTrue(end - start >= 600, "Exchange completed too early");
+ assertTrue(duration >= 600, "Exchange completed too early");
}
@Test
public void testParallelSlowBackpressure() throws Exception {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
Flowable.range(0, 40)
.subscribe(CamelReactiveStreams.get(context).streamSubscriber("parallelSlowNumbers", Integer.class));
MockEndpoint endpoint = getMockEndpoint("mock:endpoint");
endpoint.expectedMessageCount(40);
endpoint.assertIsSatisfied();
- long end = System.currentTimeMillis();
+ long duration = watch.taken();
// Maximum 5 inflight exchanges
// Must take at least 100 * (40 / 5) = 800ms
- assertTrue(end - start >= 800, "Exchange completed too early");
+ assertTrue(duration >= 800, "Exchange completed too early");
}
@Override
diff --git a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBaseTest.java b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBaseTest.java
index 0975eac17c3..ce1be5a2945 100644
--- a/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBaseTest.java
+++ b/components/camel-salesforce/camel-salesforce-component/src/test/java/org/apache/camel/component/salesforce/internal/client/AbstractClientBaseTest.java
@@ -26,6 +26,7 @@ import org.apache.camel.component.salesforce.internal.SalesforceSession;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.DefaultExchange;
import org.apache.camel.support.DefaultMessage;
+import org.apache.camel.util.StopWatch;
import org.eclipse.jetty.client.Request;
import org.eclipse.jetty.client.Response;
import org.eclipse.jetty.client.Result;
@@ -155,11 +156,11 @@ public class AbstractClientBaseTest {
// completes the request
listener.getValue().onComplete(result);
- final long stopStartTime = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
// should not wait
client.stop();
- final long elapsed = System.currentTimeMillis() - stopStartTime;
+ final long elapsed = watch.taken();
assertTrue(elapsed < 10);
}
@@ -170,11 +171,11 @@ public class AbstractClientBaseTest {
// the request never completes
- final long stopStartTime = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
// will wait for 1 second
client.stop();
- final long elapsed = System.currentTimeMillis() - stopStartTime;
+ final long elapsed = watch.taken();
assertTrue(elapsed > 900 && elapsed < 1100);
}
diff --git a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/DelayerInterceptorTest.java b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/DelayerInterceptorTest.java
index 2073b97eaa0..d472ad90c9d 100644
--- a/components/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/DelayerInterceptorTest.java
+++ b/components/camel-spring-xml/src/test/java/org/apache/camel/spring/interceptor/DelayerInterceptorTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.spring.interceptor;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.spring.SpringTestSupport;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -40,11 +41,11 @@ public class DelayerInterceptorTest extends SpringTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(10);
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
for (int i = 0; i < 10; i++) {
template.sendBody("direct:start", "Message #" + i);
}
- long delta = System.currentTimeMillis() - start;
+ long delta = watch.taken();
assertMockEndpointsSatisfied();
diff --git a/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamDelayTest.java b/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamDelayTest.java
index fc56ef0cac9..3015316a7a2 100644
--- a/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamDelayTest.java
+++ b/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamDelayTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.stream;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -29,10 +30,10 @@ public class StreamDelayTest extends CamelTestSupport {
@Test
public void testStringContent() {
- long start = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
template.sendBody("direct:in", "Hello Text World\n");
- long delta = System.currentTimeMillis() - start;
- assertTrue(delta > 1900 && delta < 3000, "Delay should be around 2 sec: " + delta);
+ long duration = watch.taken();
+ assertTrue(duration > 1900 && duration < 3000, "Delay should be around 2 sec: " + duration);
}
@Override
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
index b9573e4cdd0..ee5687b64dc 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java
@@ -42,6 +42,7 @@ import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.OrderedLocationProperties;
import org.apache.camel.util.OrderedProperties;
+import org.apache.camel.util.StopWatch;
import org.apache.camel.util.StringHelper;
import org.apache.camel.util.TimeUtils;
import org.slf4j.Logger;
@@ -51,13 +52,14 @@ public final class MainHelper {
private static final Logger LOG = LoggerFactory.getLogger(MainHelper.class);
private final String version;
- private final long startDate;
+ private final StopWatch stopWatch;
private final Set<String> componentEnvNames = new HashSet<>();
private final Set<String> dataformatEnvNames = new HashSet<>();
private final Set<String> languageEnvNames = new HashSet<>();
public MainHelper() {
- startDate = System.currentTimeMillis();
+ stopWatch = new StopWatch();
+
try {
InputStream is = MainHelper.class.getResourceAsStream("/org/apache/camel/main/components.properties");
loadLines(is, componentEnvNames, s -> "CAMEL_COMPONENT_" + s.toUpperCase(Locale.US).replace('-', '_'));
@@ -82,7 +84,7 @@ public final class MainHelper {
}
public String getUptime() {
- long delta = System.currentTimeMillis() - startDate;
+ long delta = stopWatch.taken();
if (delta == 0) {
return "";
}
diff --git a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
index 639e676aedb..76f6b986f5c 100644
--- a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
+++ b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/RunMojo.java
@@ -32,6 +32,7 @@ import java.util.Set;
import org.apache.camel.util.CastUtils;
import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.StopWatch;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
@@ -519,7 +520,7 @@ public class RunMojo extends AbstractExecMojo {
}
private void terminateThreads(ThreadGroup threadGroup) {
- long startTime = System.currentTimeMillis();
+ StopWatch watch = new StopWatch();
Set<Thread> uncooperativeThreads = new HashSet<>(); // these were not responsive
// to interruption
for (Collection<Thread> threads = getActiveThreads(threadGroup);
@@ -544,7 +545,7 @@ public class RunMojo extends AbstractExecMojo {
joinThread(thread, 0); // waits until not alive; no timeout
continue;
}
- long timeout = daemonThreadJoinTimeout - (System.currentTimeMillis() - startTime);
+ long timeout = daemonThreadJoinTimeout - watch.taken();
if (timeout > 0) {
joinThread(thread, timeout);
}