Search in sources :

Example 6 with RetryPolicy

use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.

the class DelayableRetryPolicyTest method shouldDelayOnMatchingFailureType.

public void shouldDelayOnMatchingFailureType() {
    AtomicInteger delays = new AtomicInteger(0);
    RetryPolicy<Integer> retryPolicy = RetryPolicy.<Integer>builder().handle(UncheckedExpectedException.class).withMaxRetries(4).withDelayFnOn(ctx -> {
        // side-effect for test purposes
        delays.incrementAndGet();
        return Duration.ofNanos(1);
    }, DelayException.class).build();
    AtomicInteger attempts = new AtomicInteger(0);
    int result = Failsafe.with(Fallback.of(123), retryPolicy).get(() -> {
        int i = attempts.getAndIncrement();
        switch(i) {
            case 0:
            case 2:
                throw new DelayException();
            default:
                throw new UncheckedExpectedException();
        }
    });
    assertEquals(result, 123, "Fallback should be used");
    assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
    assertEquals(delays.get(), 2, "Expecting two dynamic delays matching DelayException failure");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RetryPolicy(dev.failsafe.RetryPolicy) ExecutionContext(dev.failsafe.ExecutionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Fallback(dev.failsafe.Fallback) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 7 with RetryPolicy

use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.

the class DelayableRetryPolicyTest method shouldDelayOnMatchingResult.

public void shouldDelayOnMatchingResult() {
    AtomicInteger delays = new AtomicInteger(0);
    RetryPolicy<Object> retryPolicy = RetryPolicy.builder().handleResultIf(result -> true).withMaxRetries(4).withDelayFnWhen(ctx -> {
        // side-effect for test purposes
        delays.incrementAndGet();
        return Duration.ofNanos(1);
    }, "expected").build();
    Fallback<Object> fallback = Fallback.<Object>builder(123).handleResultIf(result -> true).build();
    AtomicInteger attempts = new AtomicInteger(0);
    Object result = Failsafe.with(fallback, retryPolicy).get(() -> {
        int i = attempts.getAndIncrement();
        switch(i) {
            case 0:
            case 3:
                return "expected";
            default:
                return i;
        }
    });
    assertEquals(result, 123, "Fallback should be used");
    assertEquals(attempts.get(), 5, "Expecting five attempts (1 + 4 retries)");
    assertEquals(delays.get(), 2, "Expecting two dynamic delays matching String result");
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RetryPolicy(dev.failsafe.RetryPolicy) ExecutionContext(dev.failsafe.ExecutionContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Fallback(dev.failsafe.Fallback) Test(org.testng.annotations.Test) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 8 with RetryPolicy

use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.

the class Issue36Test method failedAttemptListener_WithExceptions_ShouldBeCalled.

@Test
public void failedAttemptListener_WithExceptions_ShouldBeCalled() {
    AtomicInteger listenerCallbacks = new AtomicInteger();
    RetryPolicy<Boolean> policy = RetryPolicy.<Boolean>builder().handleResultIf(response -> response != null && !response).handle(Exception.class).withMaxRetries(3).onFailedAttempt(e -> listenerCallbacks.incrementAndGet()).build();
    Testing.ignoreExceptions(() -> Failsafe.with(policy).get(() -> {
        throw new RuntimeException();
    }));
    assertEquals(listenerCallbacks.get(), 4);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) Assert.assertEquals(org.testng.Assert.assertEquals) Failsafe(dev.failsafe.Failsafe) Test(org.testng.annotations.Test) RetryPolicyBuilder(dev.failsafe.RetryPolicyBuilder) Testing(dev.failsafe.testing.Testing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.testng.annotations.Test)

Example 9 with RetryPolicy

use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.

the class NettyExample method main.

public static void main(String... args) throws Throwable {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = createBootstrap(group);
    RetryPolicy<Object> retryPolicy = RetryPolicy.builder().withDelay(Duration.ofSeconds(1)).onSuccess(e -> System.out.println("Success!")).onFailure(e -> System.out.println("Connection attempts failed")).build();
    Failsafe.with(retryPolicy).with(group).runAsyncExecution(execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            execution.complete();
            try {
                channelFuture.sync();
                channelFuture.channel().closeFuture().sync();
            } catch (Exception ignore) {
                group.shutdownGracefully();
            }
        } else
            execution.recordFailure(channelFuture.cause());
    }));
    Thread.sleep(5000);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) RetryPolicy(dev.failsafe.RetryPolicy) Duration(java.time.Duration) io.netty.channel(io.netty.channel) Failsafe(dev.failsafe.Failsafe) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 10 with RetryPolicy

use of dev.failsafe.RetryPolicy in project failsafe by jhalterman.

the class VertxExample method main.

/**
 * A Vert.x sender and retryable receiver example.
 */
public static void main(String... args) throws Throwable {
    // Receiver that fails 2 times then succeeds
    AtomicInteger failures = new AtomicInteger();
    vertx.eventBus().consumer("ping-address", message -> {
        if (failures.getAndIncrement() < 2)
            message.fail(1, "Failed");
        else {
            message.reply("pong!");
        }
    });
    // Retryable sender
    Failsafe.with(retryPolicy).with(scheduler).getAsyncExecution(execution -> vertx.eventBus().send("ping-address", "ping!", reply -> {
        if (reply.succeeded())
            execution.recordResult(reply.result());
        else
            execution.recordFailure(reply.cause());
    }));
    Thread.sleep(5000);
    System.exit(0);
}
Also used : RetryPolicy(dev.failsafe.RetryPolicy) DefaultScheduledFuture(dev.failsafe.spi.DefaultScheduledFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Vertx(io.vertx.core.Vertx) Message(io.vertx.core.eventbus.Message) ReplyFailure(io.vertx.core.eventbus.ReplyFailure) Failsafe(dev.failsafe.Failsafe) Scheduler(dev.failsafe.spi.Scheduler) ReplyException(io.vertx.core.eventbus.ReplyException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Failsafe (dev.failsafe.Failsafe)15 RetryPolicy (dev.failsafe.RetryPolicy)15 Test (org.testng.annotations.Test)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Testing (dev.failsafe.testing.Testing)9 Assert.assertEquals (org.testng.Assert.assertEquals)8 Duration (java.time.Duration)7 RetryPolicyBuilder (dev.failsafe.RetryPolicyBuilder)4 Executors (java.util.concurrent.Executors)4 Assert.fail (org.testng.Assert.fail)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 ExecutionException (java.util.concurrent.ExecutionException)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 TimeUnit (java.util.concurrent.TimeUnit)3 Waiter (net.jodah.concurrentunit.Waiter)3 ExecutionContext (dev.failsafe.ExecutionContext)2 Fallback (dev.failsafe.Fallback)2 Function (java.util.function.Function)2 Assert (org.testng.Assert)2 AfterClass (org.testng.annotations.AfterClass)2