Java Code Examples for io.github.resilience4j.retry.Retry#decorateCheckedRunnable()
The following examples show how to use
io.github.resilience4j.retry.Retry#decorateCheckedRunnable() .
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: EventPublisherTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldReturnAfterThreeAttempts() { willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld(); Retry retry = Retry.ofDefaults("id"); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); then(helloWorldService).should(times(3)).sayHelloWorld(); assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2); testSubscriber.assertValueCount(3) .assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.RETRY, RetryEvent.Type.ERROR); }
Example 2
Source File: EventPublisherTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldReturnAfterTwoAttempts() { willThrow(new HelloWorldException()).willDoNothing().given(helloWorldService) .sayHelloWorld(); Retry retry = Retry.ofDefaults("id"); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); then(helloWorldService).should(times(2)).sayHelloWorld(); assertThat(result.isSuccess()).isTrue(); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); testSubscriber.assertValueCount(2) .assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.SUCCESS); }
Example 3
Source File: EventPublisherTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldIgnoreError() { willThrow(new HelloWorldException()).willDoNothing().given(helloWorldService) .sayHelloWorld(); RetryConfig config = RetryConfig.custom() .retryOnException(t -> t instanceof IOException) .maxAttempts(3).build(); Retry retry = Retry.of("id", config); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); then(helloWorldService).should().sayHelloWorld(); assertThat(result.isFailure()).isTrue(); assertThat(sleptTime).isEqualTo(0); testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.IGNORED_ERROR); }
Example 4
Source File: RunnableRetryTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldReturnAfterOneAttemptAndIgnoreException() { willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld(); RetryConfig config = RetryConfig.custom() .retryOnException(throwable -> Match(throwable).of( Case($(Predicates.instanceOf(HelloWorldException.class)), false), Case($(), true))) .build(); Retry retry = Retry.of("id", config); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); // because the exception should be rethrown immediately then(helloWorldService).should().sayHelloWorld(); assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class); assertThat(sleptTime).isEqualTo(0); }
Example 5
Source File: RunnableRetryTest.java From resilience4j with Apache License 2.0 | 6 votes |
@Test public void shouldTakeIntoAccountBackoffFunction() { willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld(); RetryConfig config = RetryConfig .custom() .intervalFunction(IntervalFunction.of(Duration.ofMillis(500), x -> x * x)) .build(); Retry retry = Retry.of("id", config); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try.run(retryableRunnable); then(helloWorldService).should(times(3)).sayHelloWorld(); assertThat(sleptTime).isEqualTo( RetryConfig.DEFAULT_WAIT_DURATION + RetryConfig.DEFAULT_WAIT_DURATION * RetryConfig.DEFAULT_WAIT_DURATION); }
Example 6
Source File: ExampleIntegrationTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void helloPubSub_shouldRunWithFunctionsFramework() throws Throwable { String functionUrl = BASE_URL + "/helloPubsub"; // URL to your locally-running function // Initialize constants String name = UUID.randomUUID().toString(); String nameBase64 = Base64.getEncoder().encodeToString(name.getBytes(StandardCharsets.UTF_8)); String jsonStr = gson.toJson(Map.of("data", Map.of("data", nameBase64))); HttpPost postRequest = new HttpPost(URI.create(functionUrl)); postRequest.setEntity(new StringEntity(jsonStr)); // The Functions Framework Maven plugin process takes time to start up // Use resilience4j to retry the test HTTP request until the plugin responds RetryRegistry registry = RetryRegistry.of(RetryConfig.custom() .maxAttempts(8) .retryExceptions(HttpHostConnectException.class) .intervalFunction(IntervalFunction.ofExponentialBackoff(200, 2)) .build()); Retry retry = registry.retry("my"); // Perform the request-retry process CheckedRunnable retriableFunc = Retry.decorateCheckedRunnable( retry, () -> client.execute(postRequest)); retriableFunc.run(); // Get Functions Framework plugin process' stdout InputStream stdoutStream = emulatorProcess.getErrorStream(); ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream(); stdoutBytes.write(stdoutStream.readNBytes(stdoutStream.available())); // Verify desired name value is present assertThat(stdoutBytes.toString(StandardCharsets.UTF_8)).contains( String.format("Hello %s!", name)); }
Example 7
Source File: ExampleIntegrationTest.java From java-docs-samples with Apache License 2.0 | 5 votes |
@Test public void helloGcs_shouldRunWithFunctionsFramework() throws Throwable { String functionUrl = BASE_URL + "/helloGcs"; // URL to your locally-running function // Initialize constants String name = UUID.randomUUID().toString(); String jsonStr = gson.toJson(Map.of( "data", Map.of( "name", name, "resourceState", "exists", "metageneration", 1), "context", Map.of( "eventType", "google.storage.object.finalize") )); HttpPost postRequest = new HttpPost(URI.create(functionUrl)); postRequest.setEntity(new StringEntity(jsonStr)); // The Functions Framework Maven plugin process takes time to start up // Use resilience4j to retry the test HTTP request until the plugin responds RetryRegistry registry = RetryRegistry.of(RetryConfig.custom() .maxAttempts(8) .retryExceptions(HttpHostConnectException.class) .intervalFunction(IntervalFunction.ofExponentialBackoff(200, 2)) .build()); Retry retry = registry.retry("my"); // Perform the request-retry process CheckedRunnable retriableFunc = Retry.decorateCheckedRunnable( retry, () -> client.execute(postRequest)); retriableFunc.run(); // Get Functions Framework plugin process' stdout InputStream stdoutStream = emulatorProcess.getErrorStream(); ByteArrayOutputStream stdoutBytes = new ByteArrayOutputStream(); stdoutBytes.write(stdoutStream.readNBytes(stdoutStream.available())); // Verify desired name value is present assertThat(stdoutBytes.toString(StandardCharsets.UTF_8)).contains( String.format("File %s uploaded.", name)); }
Example 8
Source File: RunnableRetryTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void shouldReturnAfterThreeAttempts() { willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld(); Retry retry = Retry.ofDefaults("id"); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); then(helloWorldService).should(times(3)).sayHelloWorld(); assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class); assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION * 2); }
Example 9
Source File: RunnableRetryTest.java From resilience4j with Apache License 2.0 | 5 votes |
@Test public void shouldReturnAfterOneAttempt() { willThrow(new HelloWorldException()).given(helloWorldService).sayHelloWorld(); RetryConfig config = RetryConfig.custom().maxAttempts(1).build(); Retry retry = Retry.of("id", config); CheckedRunnable retryableRunnable = Retry .decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); Try<Void> result = Try.run(retryableRunnable); then(helloWorldService).should().sayHelloWorld(); assertThat(result.isFailure()).isTrue(); assertThat(result.failed().get()).isInstanceOf(HelloWorldException.class); assertThat(sleptTime).isEqualTo(0); }
Example 10
Source File: Decorators.java From resilience4j with Apache License 2.0 | 4 votes |
public DecorateCheckedRunnable withRetry(Retry retryContext) { runnable = Retry.decorateCheckedRunnable(retryContext, runnable); return this; }