Search in sources :

Example 1 with RetryOptions

use of com.uber.cadence.common.RetryOptions in project cadence-client by uber-java.

the class RetryerTest method testInterruptedException.

@Test
public void testInterruptedException() throws InterruptedException {
    RetryOptions options = new RetryOptions.Builder().setInitialInterval(Duration.ofMillis(10)).setMaximumInterval(Duration.ofMillis(100)).setExpiration(Duration.ofSeconds(100)).setDoNotRetry(InterruptedException.class).validateBuildWithDefaults();
    long start = System.currentTimeMillis();
    try {
        Retryer.retryWithResultAsync(options, () -> {
            CompletableFuture<Void> result = new CompletableFuture<>();
            result.completeExceptionally(new InterruptedException("simulated"));
            return result;
        }).get();
        fail("unreachable");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof InterruptedException);
        assertEquals("simulated", e.getCause().getMessage());
    }
    assertTrue(System.currentTimeMillis() - start < 100000);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RetryOptions(com.uber.cadence.common.RetryOptions) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with RetryOptions

use of com.uber.cadence.common.RetryOptions in project cadence-client by uber-java.

the class DeterministicRunnerTest method testRetry.

/**
 * Async retry cannot be tested here as it relies on timer that is implemented outside of
 * Dispatcher.
 *
 * @see WorkflowTest#testAsyncRetry()
 */
@Test
public void testRetry() throws Throwable {
    RetryOptions retryOptions = new RetryOptions.Builder().setInitialInterval(Duration.ofSeconds(10)).setMaximumInterval(Duration.ofSeconds(100)).setExpiration(Duration.ofMinutes(5)).setBackoffCoefficient(2.0).build();
    DeterministicRunnerImpl d = new DeterministicRunnerImpl(threadPool, null, // clock override
    () -> currentTime, () -> {
        trace.add("started");
        Workflow.retry(retryOptions, () -> {
            trace.add("retry at " + Workflow.currentTimeMillis());
            throw new IllegalThreadStateException("simulated");
        });
        trace.add("beforeSleep");
        Workflow.sleep(60000);
        trace.add("done");
    });
    try {
        for (int i = 0; i < Duration.ofSeconds(400).toMillis(); i += 10) {
            currentTime = i;
            d.runUntilAllBlocked();
        }
        fail("failure expected");
    } catch (IllegalThreadStateException e) {
        assertEquals("simulated", e.getMessage());
    }
    int attempt = 1;
    long time = 0;
    trace.addExpected("started");
    while (time < retryOptions.getExpiration().toMillis()) {
        trace.addExpected("retry at " + time);
        long sleepMillis = (long) ((Math.pow(retryOptions.getBackoffCoefficient(), attempt - 1)) * retryOptions.getInitialInterval().toMillis());
        sleepMillis = Math.min(sleepMillis, retryOptions.getMaximumInterval().toMillis());
        attempt++;
        time += sleepMillis;
    }
}
Also used : RetryOptions(com.uber.cadence.common.RetryOptions) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Test(org.junit.Test)

Example 3 with RetryOptions

use of com.uber.cadence.common.RetryOptions in project cadence-client by uber-java.

the class ActivityOptionsTest method testOnlyAnnotationsPresent.

@Test
public void testOnlyAnnotationsPresent() throws NoSuchMethodException {
    Method method = ActivityOptionsTest.class.getMethod("activityAndRetryOptions");
    ActivityMethod a = method.getAnnotation(ActivityMethod.class);
    MethodRetry r = method.getAnnotation(MethodRetry.class);
    ActivityOptions o = new ActivityOptions.Builder().build();
    ActivityOptions merged = ActivityOptions.merge(a, r, o);
    Assert.assertEquals(a.taskList(), merged.getTaskList());
    Assert.assertEquals(a.heartbeatTimeoutSeconds(), merged.getHeartbeatTimeout().getSeconds());
    Assert.assertEquals(a.scheduleToCloseTimeoutSeconds(), merged.getScheduleToCloseTimeout().getSeconds());
    Assert.assertEquals(a.scheduleToStartTimeoutSeconds(), merged.getScheduleToStartTimeout().getSeconds());
    Assert.assertEquals(a.startToCloseTimeoutSeconds(), merged.getStartToCloseTimeout().getSeconds());
    RetryOptions rMerged = merged.getRetryOptions();
    Assert.assertEquals(r.maximumAttempts(), rMerged.getMaximumAttempts());
    Assert.assertEquals(r.minimumAttempts(), rMerged.getMinimumAttempts());
    Assert.assertEquals(r.backoffCoefficient(), rMerged.getBackoffCoefficient(), 0.0);
    Assert.assertEquals(Duration.ofSeconds(r.expirationSeconds()), rMerged.getExpiration());
    Assert.assertEquals(Duration.ofSeconds(r.initialIntervalSeconds()), rMerged.getInitialInterval());
    Assert.assertEquals(Duration.ofSeconds(r.maximumIntervalSeconds()), rMerged.getMaximumInterval());
    Assert.assertEquals(Arrays.asList(r.doNotRetry()), rMerged.getDoNotRetry());
}
Also used : RetryOptions(com.uber.cadence.common.RetryOptions) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 4 with RetryOptions

use of com.uber.cadence.common.RetryOptions in project cadence-client by uber-java.

the class RetryerTest method testExpiration.

@Test
public void testExpiration() throws InterruptedException {
    RetryOptions options = new RetryOptions.Builder().setInitialInterval(Duration.ofMillis(10)).setMaximumInterval(Duration.ofMillis(100)).setExpiration(Duration.ofMillis(500)).validateBuildWithDefaults();
    long start = System.currentTimeMillis();
    try {
        Retryer.retryWithResultAsync(options, () -> {
            throw new IllegalArgumentException("simulated");
        }).get();
        fail("unreachable");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof IllegalArgumentException);
        assertEquals("simulated", e.getCause().getMessage());
    }
    assertTrue(System.currentTimeMillis() - start > 500);
}
Also used : RetryOptions(com.uber.cadence.common.RetryOptions) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with RetryOptions

use of com.uber.cadence.common.RetryOptions in project cadence-client by uber-java.

the class RetryerTest method testExpirationFuture.

@Test
public void testExpirationFuture() throws InterruptedException {
    RetryOptions options = new RetryOptions.Builder().setInitialInterval(Duration.ofMillis(10)).setMaximumInterval(Duration.ofMillis(100)).setExpiration(Duration.ofMillis(500)).validateBuildWithDefaults();
    long start = System.currentTimeMillis();
    try {
        Retryer.retryWithResultAsync(options, () -> {
            CompletableFuture<Void> result = new CompletableFuture<>();
            result.completeExceptionally(new IllegalArgumentException("simulated"));
            return result;
        }).get();
        fail("unreachable");
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof IllegalArgumentException);
        assertEquals("simulated", e.getCause().getMessage());
    }
    assertTrue(System.currentTimeMillis() - start > 500);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RetryOptions(com.uber.cadence.common.RetryOptions) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

RetryOptions (com.uber.cadence.common.RetryOptions)5 Test (org.junit.Test)5 ExecutionException (java.util.concurrent.ExecutionException)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 WorkflowTest (com.uber.cadence.workflow.WorkflowTest)1 Method (java.lang.reflect.Method)1