Search in sources :

Example 1 with WorkflowTest

use of com.uber.cadence.workflow.WorkflowTest 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)

Aggregations

RetryOptions (com.uber.cadence.common.RetryOptions)1 WorkflowTest (com.uber.cadence.workflow.WorkflowTest)1 Test (org.junit.Test)1