-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetriesWithResilience4jTest.java
83 lines (71 loc) · 3.35 KB
/
RetriesWithResilience4jTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.pchudzik.blog.examples.asynccommunication.retries;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.stubbing.Scenario;
import com.pchudzik.blog.examples.asynccommunication.Hello;
import com.pchudzik.blog.examples.asynccommunication.WireMockScenario;
import io.github.resilience4j.core.IntervalFunction;
import io.github.resilience4j.retry.RetryConfig;
import io.github.resilience4j.retry.RetryRegistry;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.Rule;
import org.junit.Test;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class RetriesWithResilience4jTest {
private static final ResponseDefinitionBuilder OK_RESPONSE = aResponse().withStatus(200).withBody("{\"message\":\"hello world\"}");
@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(8080), true);
@Test
public void resilience4j_retries_sample() throws Throwable {
WireMockScenario
.of(get(urlMatching("/hello")), wireMockRule)
.willRespondWith(
aResponse().withFault(Fault.CONNECTION_RESET_BY_PEER),
aResponse().withStatus(503).withBody("Error"),
OK_RESPONSE);
HelloWorld target = new Retrofit.Builder()
.client(new OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.SECONDS)
.readTimeout(2, TimeUnit.SECONDS)
.callTimeout(3, TimeUnit.SECONDS)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.build())
.baseUrl("http://localhost:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(HelloWorld.class);
RetryRegistry simpleRetryRegistry = RetryRegistry.of(RetryConfig.<Response<Hello>>custom()
.intervalFunction(IntervalFunction.ofExponentialBackoff(50, 1.5))
.maxAttempts(3)
.retryOnException(ex -> {
System.out.println(ex.getMessage());
return true;
})
.retryOnResult(response -> response.code() == 503)
.build());
Optional<String> value = Optional
.ofNullable(simpleRetryRegistry
.retry("hello world")
.executeCallable(() -> target.sayHello().execute())
.body())
.map(Hello::getMessage);
assertThat(value.orElse("FAIL"), equalTo("hello world"));
}
interface HelloWorld {
@GET("hello")
Call<Hello> sayHello();
}
}