net.jodah.failsafe.RetryPolicy Java Examples

The following examples show how to use net.jodah.failsafe.RetryPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleCloudStorageSinkConfigurationTest.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryConfiguration() {
    final ValidatedConfiguration vc = new ValidatedConfiguration(() -> ConfigFactory.parseResources(
        "gcs-sink.conf"));

    // Check that we generate the retry policy that matches the settings.
    final GoogleCloudStorageRetryConfiguration retrySettings = vc.configuration().getSinkConfiguration("gcs", GoogleCloudStorageSinkConfiguration.class).retrySettings;
    final RetryPolicy<?> retryPolicy = retrySettings.createRetryPolicy();

    assertEquals(8, retryPolicy.getMaxRetries());
    assertEquals(Duration.of(138, SECONDS), retryPolicy.getMaxDuration());
    assertEquals(Duration.of(19, SECONDS), retryPolicy.getDelay());
    assertEquals(2.2, retryPolicy.getDelayFactor(), DEFAULT_DELTA);
    assertEquals(Duration.of(25, SECONDS), retryPolicy.getMaxDelay());
    assertEquals(1925, retryPolicy.getJitter().toMillis(), DEFAULT_DELTA);
}
 
Example #2
Source File: Issue52Test.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public void shouldCancelExecutionViaCompletableFuture() throws Throwable {
  ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
  AtomicInteger counter = new AtomicInteger();
  CompletableFuture<String> proxyFuture = Failsafe.with(new RetryPolicy<>().withDelay(Duration.ofMillis(10)))
      .with(scheduler)
      .getStageAsync(exec -> {
        Thread.sleep(100);
        counter.incrementAndGet();
        CompletableFuture<String> result = new CompletableFuture<>();
        result.completeExceptionally(new RuntimeException());
        return result;
      });

  assertTrue(proxyFuture.cancel(true));
  int count = counter.get();

  assertTrue(proxyFuture.isCancelled());
  Asserts.assertThrows(proxyFuture::get, CancellationException.class);

  // Assert that execution has actually stopped
  Thread.sleep(20);
  assertEquals(count, counter.get());
}
 
Example #3
Source File: Issue5Test.java    From failsafe with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that a failure is handled as expected by a listener registered via whenFailure.
 */
public void test() throws Throwable {
  Waiter waiter = new Waiter();
  RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withDelay(Duration.ofMillis(100))
      .withMaxDuration(Duration.ofSeconds(2))
      .withMaxRetries(3)
      .handleResult(null);

  ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
  Failsafe.with(retryPolicy).with(executor).onFailure(e -> {
    waiter.assertNull(e.getResult());
    waiter.assertNull(e.getFailure());
    waiter.resume();
  }).getAsync(() -> null);

  waiter.await(1000);
}
 
Example #4
Source File: SelectedPortWaitStrategy.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
protected void waitUntilReady()
{
    Callable<Boolean> internalCheck = new InternalCommandPortListeningCheck(waitStrategyTarget, exposedPorts);

    Set<Integer> externalPorts = exposedPorts.stream()
            .map(waitStrategyTarget::getMappedPort)
            .collect(toImmutableSet());
    Callable<Boolean> externalCheck = new ExternalPortListeningCheck(waitStrategyTarget, externalPorts);

    Failsafe.with(new RetryPolicy<>()
            .withMaxDuration(startupTimeout)
            .withMaxAttempts(Integer.MAX_VALUE) // limited by MaxDuration
            .abortOn(e -> getExitCode().isPresent()))
            .run(() -> {
                if (!getRateLimiter().getWhenReady(() -> internalCheck.call() && externalCheck.call())) {
                    // We say "timed out" immediately. Failsafe will propagate this only when timeout reached.
                    throw new ContainerLaunchException(format(
                            "Timed out waiting for container port to open (%s ports: %s should be listening)",
                            waitStrategyTarget.getContainerIpAddress(),
                            exposedPorts));
                }
            });
}
 
Example #5
Source File: Issue55Test.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public void shouldOnlyFallbackOnFailure() throws Throwable {
  ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

  AtomicInteger counter = new AtomicInteger();
  Failsafe.with(Fallback.of(counter::incrementAndGet), new RetryPolicy<>()).with(executor).getAsync(() -> null);

  Thread.sleep(100);
  assertEquals(counter.get(), 0);

  Failsafe.with(Fallback.of(counter::incrementAndGet), new RetryPolicy<>().withMaxRetries(1))
      .with(executor)
      .runAsync(() -> {
        throw new RuntimeException();
      });

  Thread.sleep(100);
  assertEquals(counter.get(), 1);
}
 
Example #6
Source File: Issue190Test.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public void test() throws Throwable {
  RetryPolicy<Object> policy = new RetryPolicy<>().withMaxRetries(5);
  AtomicInteger failureEvents = new AtomicInteger();
  AtomicInteger successEvents = new AtomicInteger();
  Waiter waiter = new Waiter();

  Failsafe.with(policy).onFailure(e -> {
    failureEvents.incrementAndGet();
    waiter.resume();
  }).onSuccess(e -> {
    successEvents.incrementAndGet();
    waiter.resume();
  }).getAsyncExecution(execution -> Testing.futureResult(executor, true).whenComplete((result, failure) -> {
    execution.complete(result);
  })).get();

  waiter.await(1000);
  Assert.assertEquals(failureEvents.get(), 0);
  Assert.assertEquals(successEvents.get(), 1);
}
 
Example #7
Source File: Issue9Test.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public void test() throws Throwable {
  // Given - Fail twice then succeed
  AtomicInteger retryCounter = new AtomicInteger();
  Service service = mock(Service.class);
  when(service.connect()).thenThrow(failures(2, new IllegalStateException())).thenReturn(true);
  ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
  Waiter waiter = new Waiter();

  // When
  AtomicInteger successCounter = new AtomicInteger();
  Future<Boolean> future = Failsafe.with(new RetryPolicy<Boolean>().withMaxRetries(2)
      .onRetry(e -> retryCounter.incrementAndGet()))
      .with(executor)
      .onSuccess(p -> {
        successCounter.incrementAndGet();
        waiter.resume();
      })
      .getAsync(service::connect);

  // Then
  waiter.await(1000);
  verify(service, times(3)).connect();
  assertTrue(future.get());
  assertEquals(retryCounter.get(), 2);
  assertEquals(successCounter.get(), 1);
}
 
Example #8
Source File: NettyExample.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Throwable {
  EventLoopGroup group = new NioEventLoopGroup();
  Bootstrap bootstrap = createBootstrap(group);
  RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withDelay(Duration.ofSeconds(1));

  Failsafe.with(retryPolicy).with(group).runAsyncExecution(
      execution -> bootstrap.connect(HOST, PORT).addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
          System.out.println("Connected!");
          try {
            channelFuture.sync();
            channelFuture.channel().closeFuture().sync();
          } catch (Exception ignore) {
            group.shutdownGracefully();
          }
        } else if (!execution.retryOn(channelFuture.cause()))
          System.out.println("Connection attempts failed");
      }));

  Thread.sleep(5000);
}
 
Example #9
Source File: RetryLoopExample.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Throwable {
  RetryPolicy retryPolicy = new RetryPolicy<>().handle(IllegalStateException.class).withBackoff(10, 40,
      ChronoUnit.MILLIS);
  Execution execution = new Execution(retryPolicy);

  while (!execution.isComplete()) {
    try {
      execution.complete(list.size());
    } catch (IllegalStateException e) {
      execution.recordFailure(e);

      // Wait before retrying
      Thread.sleep(execution.getWaitTime().toMillis());
    }
  }

  assertEquals(execution.getLastResult(), Integer.valueOf(5));
  assertEquals(execution.getAttemptCount(), 3);
}
 
Example #10
Source File: RxJavaExample.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) {
  AtomicInteger failures = new AtomicInteger();
  RetryPolicy retryPolicy = new RetryPolicy().withDelay(Duration.ofSeconds(1));

  Observable.create((Subscriber<? super String> s) -> {
    // Fail 3 times then succeed
    if (failures.getAndIncrement() < 3)
      s.onError(new RuntimeException());
    else
      System.out.println("Subscriber completed successfully");
  }).retryWhen(attempts -> {
    Execution execution = new Execution(retryPolicy);
    return attempts.flatMap(failure -> {
      System.out.println("Failure detected");
      if (execution.canRetryOn(failure))
        return Observable.timer(execution.getWaitTime().toNanos(), TimeUnit.NANOSECONDS);
      else
        return Observable.<Long>error(failure);
    });
  }).toBlocking().forEach(System.out::println);
}
 
Example #11
Source File: Java8Example.java    From failsafe with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
public static void main(String... args) {
  ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
  RetryPolicy<Object> retryPolicy = new RetryPolicy<>();

  // Create a retryable functional interface
  Function<String, String> bar = value -> Failsafe.with(retryPolicy).get(() -> value + "bar");

  // Create a retryable Stream operation
  Failsafe.with(retryPolicy).get(() -> Stream.of("foo")
      .map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar"))
      .collect(Collectors.toList()));

  // Create a individual retryable Stream operation
  Stream.of("foo").map(value -> Failsafe.with(retryPolicy).get(() -> value + "bar")).forEach(System.out::println);

  // Create a retryable CompletableFuture
  Failsafe.with(retryPolicy).with(executor).getStageAsync(() -> CompletableFuture.supplyAsync(() -> "foo")
      .thenApplyAsync(value -> value + "bar")
      .thenAccept(System.out::println));

  // Create an individual retryable CompletableFuture stages
  CompletableFuture.supplyAsync(() -> Failsafe.with(retryPolicy).get(() -> "foo"))
      .thenApplyAsync(value -> Failsafe.with(retryPolicy).get(() -> value + "bar"))
      .thenAccept(System.out::println);
}
 
Example #12
Source File: DelayableRetryPolicyTest.java    From failsafe with Apache License 2.0 6 votes vote down vote up
public void shouldDelayOnMatchingFailureType() {
  AtomicInteger delays = new AtomicInteger(0);
  RetryPolicy<Integer> retryPolicy = new RetryPolicy<Integer>()
      .handle(UncheckedExpectedException.class)
      .withMaxRetries(4)
      .withDelayOn((r, f, c) -> {
        delays.incrementAndGet(); // side-effect for test purposes
        return Duration.ofNanos(1);
      }, DelayException.class);

  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");
}
 
Example #13
Source File: FailsafeAdapter.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * Obtain an async failsafe retryer instance with the specified policy, metrics, and executor service.
 * @param retryPolicy retry policy
 * @param metrics retry metrics
 * @param retryExec executor service for scheduling async retries
 * @return {@link net.jodah.failsafe.AsyncFailsafe} instance
 */
static AsyncFailsafe<?> failsafe(RetryPolicy retryPolicy, RetryMetrics metrics, ScheduledExecutorService retryExec) {
  long startMs = System.currentTimeMillis();
  return Failsafe.with(retryPolicy).with(retryExec)
      .onRetry(e -> metrics.retryCount.inc())
      .onRetriesExceeded(e -> {
        metrics.retryTimer.update(System.currentTimeMillis() - startMs);
        metrics.permFailureCount.inc();
      })
      .onSuccess((e, ctx) -> {
        if (ctx.getExecutions() > 1) {
          metrics.retryTimer.update(System.currentTimeMillis() - startMs);
        } else {
          metrics.successCount.inc();
        }
      });
}
 
Example #14
Source File: FailsafePluginRetriesTest.java    From riptide with MIT License 6 votes vote down vote up
@Test
void shouldRetryCustomDetectedIdempotentRequest() {
    final Http unit = Http.builder()
            .executor(newCachedThreadPool())
            .requestFactory(new ApacheClientHttpRequestFactory(client))
            .baseUrl(driver.getBaseUrl())
            .converter(createJsonConverter())
            .plugin(new FailsafePlugin().withPolicy(
                    new RetryRequestPolicy(
                            new RetryPolicy<ClientHttpResponse>()
                                    .withDelay(Duration.ofMillis(500))
                                    .withMaxRetries(1))
                            .withPredicate(arguments ->
                                    arguments.getHeaders().getOrDefault("Idempotent",
                                            emptyList()).contains(
                                            "true"))))
            .build();

    driver.addExpectation(onRequestTo("/foo").withMethod(POST), giveEmptyResponse().after(800, MILLISECONDS));
    driver.addExpectation(onRequestTo("/foo").withMethod(POST), giveEmptyResponse());

    unit.post("/foo")
            .header("Idempotent", "true")
            .call(pass())
            .join();
}
 
Example #15
Source File: LoggingRetryListenerTest.java    From riptide with MIT License 6 votes vote down vote up
@Test
void shouldNotLogResults() {
    final AtomicBoolean success = new AtomicBoolean(false);

    final RequestArguments arguments = RequestArguments.create();

    Failsafe.with(new RetryPolicy<ClientHttpResponse>()
            .withMaxRetries(3)
            .handleResultIf(Objects::isNull)
            .onRetry(new RetryListenerAdapter(unit, arguments)))
            .get(() -> {
                if (!success.getAndSet(true)) {
                    return null;
                }

                return mock(ClientHttpResponse.class);
            });

    verifyNoMoreInteractions(logger);
}
 
Example #16
Source File: CompositeRetryListenerTest.java    From riptide with MIT License 6 votes vote down vote up
@Test
void shouldPropagateRetryToEveryListener() {
    final AtomicBoolean success = new AtomicBoolean(false);

    final RequestArguments arguments = RequestArguments.create();
    final IllegalStateException exception = new IllegalStateException();

    Failsafe.with(new RetryPolicy<ClientHttpResponse>()
            .withMaxRetries(3)
            .onRetry(new RetryRequestPolicy.RetryListenerAdapter(unit, arguments)))
            .run(() -> {
                if (!success.getAndSet(true)) {
                    throw exception;
                }
            });

    verify(first).onRetry(eq(arguments), argThat(hasFeature(ExecutionAttemptedEvent::getLastResult, nullValue())));
    verify(first).onRetry(eq(arguments), argThat(hasFeature(ExecutionAttemptedEvent::getLastFailure, notNullValue())));
    verify(second).onRetry(eq(arguments), argThat(hasFeature(ExecutionAttemptedEvent::getLastResult, nullValue())));
    verify(second).onRetry(eq(arguments), argThat(hasFeature(ExecutionAttemptedEvent::getLastFailure, notNullValue())));
}
 
Example #17
Source File: TestTableRetryPolicy.java    From samza with Apache License 2.0 6 votes vote down vote up
@Test
public void testFixedRetry() {
  TableRetryPolicy retryPolicy = new TableRetryPolicy();
  retryPolicy.withFixedBackoff(Duration.ofMillis(1000));
  retryPolicy.withJitter(Duration.ofMillis(100));
  retryPolicy.withStopAfterAttempts(4);
  Assert.assertEquals(TableRetryPolicy.BackoffType.FIXED, retryPolicy.getBackoffType());
  RetryPolicy fsRetry = FailsafeAdapter.valueOf(retryPolicy);
  Assert.assertEquals(1000, fsRetry.getDelay().toMillis());
  Assert.assertEquals(100, fsRetry.getJitter().toMillis());
  Assert.assertEquals(4, fsRetry.getMaxRetries());
  Assert.assertNotNull(retryPolicy.getRetryPredicate());
  Assert.assertEquals("{\"sleepTime\":{\"seconds\":1,\"nanos\":0},\"exponentialFactor\":0.0,"
      + "\"jitter\":{\"seconds\":0,\"nanos\":100000000},\"maxAttempts\":4,\"backoffType\":\"FIXED\","
      + "\"retryPredicate\":{}}", retryPolicy.toConfig(null, null).get("TableRetryPolicy"));
}
 
Example #18
Source File: TestTableRetryPolicy.java    From samza with Apache License 2.0 6 votes vote down vote up
@Test
public void testExponentialRetry() {
  TableRetryPolicy retryPolicy = new TableRetryPolicy();
  retryPolicy.withExponentialBackoff(Duration.ofMillis(1000), Duration.ofMillis(2000), 1.5);
  retryPolicy.withJitter(Duration.ofMillis(100));
  Assert.assertEquals(TableRetryPolicy.BackoffType.EXPONENTIAL, retryPolicy.getBackoffType());
  RetryPolicy fsRetry = FailsafeAdapter.valueOf(retryPolicy);
  Assert.assertEquals(1000, fsRetry.getDelay().toMillis());
  Assert.assertEquals(2000, fsRetry.getMaxDelay().toMillis());
  Assert.assertEquals(1.5, fsRetry.getDelayFactor(), 0.001);
  Assert.assertEquals(100, fsRetry.getJitter().toMillis());
  Assert.assertEquals("{\"sleepTime\":{\"seconds\":1,\"nanos\":0},\"exponentialFactor\":1.5,"
          + "\"exponentialMaxSleep\":{\"seconds\":2,\"nanos\":0},\"jitter\":{\"seconds\":0,\"nanos\":100000000},"
          + "\"backoffType\":\"EXPONENTIAL\",\"retryPredicate\":{}}",
      retryPolicy.toConfig(null, null).get("TableRetryPolicy"));
}
 
Example #19
Source File: GoogleCloudStorageFileManager.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
public GoogleCloudStorageFileManager(
    final int recordBufferSize,
    final Schema schema,
    final String bucket,
    final String inflightDir,
    final String publishDir,
    RetryPolicy<?> retryPolicy
) {
    try {
        this.recordBufferSize = recordBufferSize;
        this.schema = Objects.requireNonNull(schema);
        this.bucketEncoded = URLEncoder.encode(bucket, URL_ENCODING);
        this.inflightDir = Objects.requireNonNull(inflightDir);
        this.publishDir = Objects.requireNonNull(publishDir);
        this.retryPolicy = Objects.requireNonNull(retryPolicy)
                                  .handle(RetriableIOException.class);
    } catch (final UnsupportedEncodingException e) {
        // Should not happen. URL encoding the bucket and dirs is verified during
        // configuration verification.
        logger.error("Could not URL-encode bucket name.", e);
        throw new RuntimeException(e);
    }
}
 
Example #20
Source File: DockerRegistry.java    From carnotzet with Apache License 2.0 6 votes vote down vote up
private String downloadImageManifestAsString(String digest, ImageRef imageRef) {
	WebTarget registry = getRegistryWebTarget(imageRef);
	WebTarget url = registry.path("v2/{name}/blobs/{reference}")
			.resolveTemplate("name", imageRef.getImageName(), false)
			.resolveTemplate("reference", digest, false);
	log.info("Downloading image manifest from {} ...", url.getUri().toString());

	RetryPolicy<Object> retryPolicy = new RetryPolicy<>()
			.handle(WebApplicationException.class)
			.withDelay(Duration.ofSeconds(Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_RETRY_DELAY_SECONDS, "1"))))
			.withMaxRetries(Integer.parseInt(System.getProperty(CARNOTZET_MANIFEST_DOWNLOAD_RETRIES, "0")))
			.onRetry((o) -> log.info("Download attempt failed: {} : Retrying... ", o.getLastFailure().toString()))
			.onFailure((o) -> {
				log.error("Download failed: {} ", o.getFailure().toString());
				throw new IllegalStateException(o.getFailure());
			});
	String value = Failsafe.with(retryPolicy).get(() ->
			url.request("application/vnd.docker.container.image.v1+json").get(String.class)
	);

	log.info("Image manifest downloaded");
	return value;
}
 
Example #21
Source File: WebHook.java    From bouncr with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void run(T object) {
    RetryPolicy retryPolicy = method.equalsIgnoreCase("get") ? idempotent : notIdempotent;
    Failsafe.with(retryPolicy)
            .with(executor)
            .onSuccess(response -> {

            })
            .get(() -> {
                RequestBody body = RequestBody.create(JSON, mapper.writeValueAsString(object));
                Request.Builder requestBuilder;
                requestBuilder = new Request.Builder()
                        .url(url)
                        .header("content-type", "application/json")
                        .method(method, body);
                if (headers != null) {
                    headers.forEach(requestBuilder::addHeader);
                }
                return client.newCall(requestBuilder.build()).execute();
            });
}
 
Example #22
Source File: Issue36Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
@Test
public void retryListener_WithFailedResponses_ShouldBeCalled() {
  RetryPolicy<Boolean> policy = new RetryPolicy<Boolean>().handleResultIf(response -> response != null && !response)
      .handle(Exception.class)
      .withMaxRetries(3);
  AtomicInteger listenerCallbacks = new AtomicInteger();
  Failsafe.with(policy
      .onRetry(e -> listenerCallbacks.incrementAndGet()))
      .get(() -> false);
  assertEquals(listenerCallbacks.get(), 3);
}
 
Example #23
Source File: Issue36Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
@Test
public void failedAttemptListener_WithExceptions_ShouldBeCalled() {
  RetryPolicy<Boolean> policy = new RetryPolicy<Boolean>().handleResultIf(response -> response != null && !response)
      .handle(Exception.class)
      .withMaxRetries(3);
  AtomicInteger listenerCallbacks = new AtomicInteger();
  Testing.ignoreExceptions(() -> Failsafe.with(policy
        .onFailedAttempt(e -> listenerCallbacks.incrementAndGet()))
        .get(() -> {
          throw new RuntimeException();
        }));
  assertEquals(listenerCallbacks.get(), 4);
}
 
Example #24
Source File: Issue36Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
@Test
public void retryListener_WithExceptions_ShouldBeCalled() {
  RetryPolicy<Boolean> policy = new RetryPolicy<Boolean>().handleResultIf(response -> response != null && !response)
      .handle(Exception.class)
      .withMaxRetries(3);
  AtomicInteger listenerCallbacks = new AtomicInteger();
  Testing.ignoreExceptions(() -> Failsafe.with(policy
        .onRetry(e -> listenerCallbacks.incrementAndGet()))
        .get(() -> {
          throw new RuntimeException();
        }));
  assertEquals(listenerCallbacks.get(), 3);
}
 
Example #25
Source File: Issue52Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = CancellationException.class)
public void shouldCancelExecutionViaFuture() throws Throwable {
  ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
  Future<Object> proxyFuture = Failsafe.with(new RetryPolicy<>().withDelay(Duration.ofMillis(10)))
      .with(scheduler)
      .getAsync(exec -> {
        throw new IllegalStateException();
      });

  assertTrue(proxyFuture.cancel(true));
  proxyFuture.get(); // should throw CancellationException per .getAsync() javadoc.
}
 
Example #26
Source File: Issue242Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
public void shouldDelayOnExplicitRetry() throws Throwable {
  RetryPolicy<String> retryPolicy = new RetryPolicy<String>().handleResult(null)
    .withDelay(Duration.ofMillis(110))
    .withMaxAttempts(3);

  long startTime = System.currentTimeMillis();
  Failsafe.with(retryPolicy).runAsyncExecution(exec -> {
    if (!exec.complete(null, null))
      exec.retry();
  }).get();
  assertTrue(System.currentTimeMillis() - startTime > 200, "Expected delay between retries");
}
 
Example #27
Source File: GoogleCloudStorageSinkConfigurationTest.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultRetryConfigurationValid() {
    // Check that we can generate settings from our defaults.
    final RetryPolicy<?> retryPolicy =
        GoogleCloudStorageSinkConfiguration.DEFAULT_RETRY_SETTINGS.createRetryPolicy();
    assertNotNull(retryPolicy);
}
 
Example #28
Source File: GoogleCloudStorageRetryConfiguration.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
public RetryPolicy<?> createRetryPolicy() {
    final RetryPolicy<Object> policyBeforeJitter = new RetryPolicy<>()
        .withMaxRetries(maxAttempts - 1)
        .withMaxDuration(totalTimeout)
        .withBackoff(initialRetryDelay.toNanos(), maxRetryDelay.toNanos(), ChronoUnit.NANOS, retryDelayMultiplier);
    final RetryPolicy<Object> policyWithJitterFactor = jitterFactor.map(policyBeforeJitter::withJitter).orElse(policyBeforeJitter);
    return jitterDelay.map(policyWithJitterFactor::withJitter).orElse(policyWithJitterFactor);
}
 
Example #29
Source File: Issue218Test.java    From failsafe with Apache License 2.0 5 votes vote down vote up
public void test() {
  RetryPolicy<Void> retryPolicy = new RetryPolicy<Void>().withMaxAttempts(2);
  Fallback<Void> fallback = Fallback.VOID;
  Failsafe.with(fallback, retryPolicy).run(() -> {
    throw new Exception();
  });
}
 
Example #30
Source File: GoogleCloudStorageFileManager.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private static <T> T withRetry(final String method,
                               final URL url,
                               final boolean write,
                               final ImmutableMap<String, String> additionalHeaders,
                               final IOExceptions.IOFunction<HttpURLConnection, T> consumer,
                               final RetryPolicy<?> retryPolicy) {
    @SuppressWarnings("unchecked")
    final RetryPolicy<T> localPolicy = (RetryPolicy<T>) retryPolicy.copy();
    final RetryPolicy<T> listeningPolicy = localPolicy
        .onRetry(event -> logger.error("Will retry after attempt #{}/{} of call to GCS API failed: {} {}", event.getAttemptCount(), retryPolicy.getMaxRetries(), method, url, event.getLastFailure()));
    return Failsafe
        .with(listeningPolicy)
        .onFailure(event -> logger.error("Failed GCS API call after {} attempts: {} {}", event.getAttemptCount(), method, url, event.getFailure()))
        .get(() -> consumer.apply(setupUrlConnection(method, url, write, additionalHeaders)));
}