Skip to content

Commit

Permalink
doc(#88): add Timeout javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
zero88 committed Dec 14, 2023
1 parent 3d0f065 commit f237ae8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/src/main/java/io/github/zero88/schedulerx/TimeoutBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import io.vertx.core.Promise;
import io.vertx.core.Vertx;

/**
* Represents for a helper to create a timeout block.
*
* @since 2.0.0
*/
public final class TimeoutBlock {

private final Vertx vertx;
Expand All @@ -20,6 +25,13 @@ public TimeoutBlock(Vertx vertx, Duration timeout) {
this.timeout = timeout;
}

/**
* Wrap a given promise with the definition timeout
*
* @param promise promise
* @param <T> type of promise result
* @return a promise. if timeout, the promise fails by {@link TimeoutException}
*/
public <T> Promise<T> wrap(@NotNull Promise<T> promise) {
if (timeout.isNegative() || timeout.isZero()) {
return promise;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.zero88.schedulerx;

import java.time.Duration;
import java.util.Objects;
import java.util.function.BinaryOperator;

import org.jetbrains.annotations.NotNull;
Expand All @@ -10,6 +11,11 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
* Represents for holder to keep timeout configuration.
*
* @since 2.0.0
*/
public final class TimeoutPolicy {

private final Duration evaluationTimeout;
Expand All @@ -20,14 +26,30 @@ private TimeoutPolicy(Duration evaluationTimeout, Duration executionTimeout) {
this.executionTimeout = executionTimeout;
}

/**
* Create the default timeout policy
*/
public static TimeoutPolicy byDefault() {
return create(null, null);
}

/**
* Create timeout policy with execution timeout
*
* @param executionTimeout given execution timeout
* @return timeout policy
*/
public static TimeoutPolicy create(@NotNull Duration executionTimeout) {
return create(null, executionTimeout);
}

/**
* Create timeout policy with execution timeout
*
* @param evaluationTimeout given evaluation timeout
* @param executionTimeout given execution timeout
* @return timeout policy
*/
@JsonCreator
public static TimeoutPolicy create(@JsonProperty("evaluationTimeout") @Nullable Duration evaluationTimeout,
@JsonProperty("executionTimeout") @Nullable Duration executionTimeout) {
Expand All @@ -42,7 +64,7 @@ public static TimeoutPolicy create(@JsonProperty("evaluationTimeout") @Nullable
}

/**
* Declares the evaluation timeout. Default is {@link DefaultOptions#maxEvaluationTimeout}
* Declares the trigger evaluation timeout. Default is {@link DefaultOptions#maxEvaluationTimeout}
*
* @return the evaluation timeout
* @since 2.0.0
Expand All @@ -59,4 +81,24 @@ public static TimeoutPolicy create(@JsonProperty("evaluationTimeout") @Nullable
@JsonGetter
public @NotNull Duration executionTimeout() { return executionTimeout; }

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

TimeoutPolicy that = (TimeoutPolicy) o;

return Objects.equals(evaluationTimeout, that.evaluationTimeout) &&
Objects.equals(executionTimeout, that.executionTimeout);
}

@Override
public int hashCode() {
int result = evaluationTimeout != null ? evaluationTimeout.hashCode() : 0;
result = 31 * result + (executionTimeout != null ? executionTimeout.hashCode() : 0);
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void test_serialize_deserialize() throws JsonProcessingException {
final TimeoutPolicy deserialized = mapper.readerFor(TimeoutPolicy.class).readValue(expected);
Assertions.assertEquals(timeoutPolicy.evaluationTimeout(), deserialized.evaluationTimeout());
Assertions.assertEquals(timeoutPolicy.executionTimeout(), deserialized.executionTimeout());
Assertions.assertEquals(timeoutPolicy, deserialized);
}

@Test
Expand Down

0 comments on commit f237ae8

Please sign in to comment.