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 2021/11/09 09:17:41 UTC
[camel] branch main updated: CAMEL-17121: fixed time+iteration
bounded support for background tasks
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 7933604 CAMEL-17121: fixed time+iteration bounded support for background tasks
7933604 is described below
commit 7933604702897ddf6c7664cfe16eec19983d48e7
Author: Otavio Rodolfo Piske <op...@redhat.com>
AuthorDate: Mon Nov 8 12:57:29 2021 +0100
CAMEL-17121: fixed time+iteration bounded support for background tasks
---
.../apache/camel/support/task/BackgroundTask.java | 12 ++
.../apache/camel/support/task/budget/Budget.java | 14 ++
.../camel/support/task/budget/IterationBudget.java | 14 --
.../task/budget/IterationTimeBoundedBudget.java | 18 +--
.../support/task/budget/TimeBoundedBudget.java | 20 +++
.../task/BackgroundIterationTimeTaskTest.java | 148 +++++++++++++++++++++
6 files changed, 198 insertions(+), 28 deletions(-)
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java b/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
index 0d9a971..0ea62b5 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/BackgroundTask.java
@@ -90,6 +90,12 @@ public class BackgroundTask implements BlockingTask {
return;
}
+ if (!budget.next()) {
+ LOG.warn("The task {} does not have more budget to continue running", name);
+
+ return;
+ }
+
if (predicate.test(payload)) {
latch.countDown();
LOG.trace("Task {} has succeeded and the current task won't be schedulable anymore: {}", name, latch.getCount());
@@ -102,6 +108,12 @@ public class BackgroundTask implements BlockingTask {
return;
}
+ if (!budget.next()) {
+ LOG.warn("The task {} does not have more budget to continue running", name);
+
+ return;
+ }
+
if (supplier.getAsBoolean()) {
latch.countDown();
LOG.trace("Task {} succeeded and the current task won't be schedulable anymore: {}", name, latch.getCount());
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/Budget.java b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/Budget.java
index fc50da5..b4ea140 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/Budget.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/Budget.java
@@ -34,4 +34,18 @@ public interface Budget {
* @return the interval, in milliseconds, for each task execution
*/
long interval();
+
+ /**
+ * Whether the task has budget to continue executing or not
+ *
+ * @return true if the task can continue or false otherwise
+ */
+ boolean canContinue();
+
+ /**
+ * Move the task to the next iteration
+ *
+ * @return true if the task can continue or false otherwise
+ */
+ boolean next();
}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationBudget.java b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationBudget.java
index d01e74e..bab5111 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationBudget.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationBudget.java
@@ -35,18 +35,4 @@ public interface IterationBudget extends Budget {
* @return the current number of iterations
*/
int iterations();
-
- /**
- * Whether the task has budget to continue executing or not
- *
- * @return true if the task can continue or false otherwise
- */
- boolean canContinue();
-
- /**
- * Move the task to the next iteration
- *
- * @return true if the task can continue or false otherwise
- */
- boolean next();
}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationTimeBoundedBudget.java b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationTimeBoundedBudget.java
index ac24b32..5c95986 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationTimeBoundedBudget.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/IterationTimeBoundedBudget.java
@@ -17,18 +17,13 @@
package org.apache.camel.support.task.budget;
-import java.time.Duration;
-import java.time.Instant;
-
public class IterationTimeBoundedBudget implements IterationBudget, TimeBudget {
private IterationBudget iterationBudget;
- private final long maxDuration;
- private final Instant startTime;
+ private TimeBoundedBudget timeBoundedBudget;;
public IterationTimeBoundedBudget(long initialDelay, long interval, int maxIterations, long maxDuration) {
iterationBudget = new IterationBoundedBudget(initialDelay, interval, maxIterations);
- this.maxDuration = maxDuration;
- this.startTime = Instant.now();
+ timeBoundedBudget = new TimeBoundedBudget(initialDelay, interval, maxDuration);
}
@Override
@@ -67,18 +62,13 @@ public class IterationTimeBoundedBudget implements IterationBudget, TimeBudget {
return false;
}
- // ... the time budget is exhausted
- if (Duration.between(startTime, Instant.now()).toMillis() >= maxDuration) {
- return false;
- }
-
// Otherwise, can continue to schedule/run the task
- return true;
+ return timeBoundedBudget.canContinue();
}
@Override
public long maxDuration() {
- return maxDuration;
+ return timeBoundedBudget.maxDuration();
}
}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/TimeBoundedBudget.java b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/TimeBoundedBudget.java
index bb01b0b..0b2a60f 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/task/budget/TimeBoundedBudget.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/task/budget/TimeBoundedBudget.java
@@ -17,15 +17,20 @@
package org.apache.camel.support.task.budget;
+import java.time.Duration;
+import java.time.Instant;
+
public class TimeBoundedBudget implements TimeBudget {
private final long initialDelay;
private final long interval;
private final long maxDuration;
+ private final Instant startTime;
TimeBoundedBudget(long initialDelay, long interval, long maxDuration) {
this.initialDelay = initialDelay;
this.interval = interval;
this.maxDuration = maxDuration;
+ this.startTime = Instant.now();
}
@Override
@@ -42,4 +47,19 @@ public class TimeBoundedBudget implements TimeBudget {
public long interval() {
return interval;
}
+
+ @Override
+ public boolean canContinue() {
+ // ... the time budget is exhausted
+ if (Duration.between(startTime, Instant.now()).toMillis() >= maxDuration) {
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public boolean next() {
+ return true;
+ }
}
diff --git a/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java
new file mode 100644
index 0000000..b4fb5d2
--- /dev/null
+++ b/core/camel-support/src/test/java/org/apache/camel/support/task/BackgroundIterationTimeTaskTest.java
@@ -0,0 +1,148 @@
+/*
+ * 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.support.task;
+
+import java.time.Duration;
+import java.util.concurrent.Executors;
+
+import org.apache.camel.support.task.budget.Budgets;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class BackgroundIterationTimeTaskTest extends TaskTestSupport {
+
+ @DisplayName("Test that the task does not run for more than the max iterations when using a supplier")
+ @Test
+ @Timeout(10)
+ void testRunNoMoreSupplier() {
+ // It should run 3 times only because that's the max iterations
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withMaxIterations(3)
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ZERO)
+ .withMaxDuration(Duration.ofSeconds(5))
+ .build())
+ .build();
+
+ boolean completed = task.run(this::booleanSupplier);
+ assertEquals(3, taskCount);
+ assertFalse(completed, "The task did not complete, the return should be false");
+ }
+
+ @DisplayName("Test that the task does not run for more than the max iterations when using a supplier and initial delay")
+ @Test
+ @Timeout(10)
+ void testRunNoMoreSupplierWithDelay() {
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ofSeconds(1))
+ .withMaxDuration(Duration.ofSeconds(5))
+ .withMaxIterations(5)
+ .build())
+ .build();
+
+ boolean completed = task.run(this::booleanSupplier);
+ assertTrue(maxIterations > taskCount, "The task execution should not exceed the max iterations");
+ assertFalse(completed, "The task did not complete, the return should be false");
+ }
+
+ @DisplayName("Test that the task does not run for more than the max iterations when using a predicate and an initial delay")
+ @Test
+ @Timeout(10)
+ void testRunNoMorePredicate() {
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ZERO)
+ .withMaxIterations(3)
+ .build())
+ .build();
+
+ boolean completed = task.run(this::taskPredicate, new Object());
+ assertEquals(3, taskCount);
+ assertFalse(completed, "The task did not complete, the return should be false");
+ }
+
+ @DisplayName("Test that the task stops running once the predicate is true")
+ @Test
+ @Timeout(10)
+ void testRunNoMorePredicateDeterministic() {
+ // It should run 3 times only
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ZERO)
+ .withMaxDuration(Duration.ofSeconds(5))
+ .withMaxIterations(4)
+ .build())
+ .build();
+
+ boolean completed = task.run(this::taskPredicateWithDeterministicStop, Integer.valueOf(3));
+ assertEquals(3, taskCount);
+ assertTrue(completed, "The task did complete, the return should be true");
+ }
+
+ @DisplayName("Test that the task stops running once the predicate is true when the test is slow")
+ @Test
+ @Timeout(10)
+ void testRunNoMorePredicateDeterministicSlow() {
+ // It should run 5 times in 4 seconds because there is no delay
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withMaxIterations(4)
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ZERO)
+ .build())
+ .build();
+
+ boolean completed = task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3));
+ assertTrue(taskCount < maxIterations);
+ assertFalse(completed, "The task did not complete because of timeout, the return should be false");
+ }
+
+ @DisplayName("Test that the task stops running once the predicate is true when the test is slow")
+ @Test
+ @Timeout(10)
+ void testRunNoMorePredicateDeterministicSlowWithDelay() {
+ // It should run 5 times in 4 seconds because there is no delay
+ BackgroundTask task = Tasks.backgroundTask()
+ .withScheduledExecutor(Executors.newSingleThreadScheduledExecutor())
+ .withBudget(Budgets.iterationTimeBudget()
+ .withInterval(Duration.ofSeconds(1))
+ .withInitialDelay(Duration.ofSeconds(1))
+ .withMaxIterations(4)
+ .build())
+ .build();
+
+ boolean completed = task.run(this::taskPredicateWithDeterministicStopSlow, Integer.valueOf(3));
+ assertTrue(taskCount < maxIterations);
+ assertFalse(completed, "The task did not complete because of timeout, the return should be false");
+ }
+}