Skip to content

Commit 40013b5

Browse files
Guillaume Lamirandguillaumelamirand
authored andcommitted
fix: properly handle fire and forget in V4
1 parent 6c4531d commit 40013b5

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<gravitee-reactor-message.version>3.0.0</gravitee-reactor-message.version>
4747
<gravitee-entrypoint-sse.version>4.1.0</gravitee-entrypoint-sse.version>
4848

49+
<awaitility.version>4.2.1</awaitility.version>
50+
4951
<maven-assembly-plugin.version>3.7.1</maven-assembly-plugin.version>
5052
<json-schema-generator-maven-plugin.version>1.3.0</json-schema-generator-maven-plugin.version>
5153
<json-schema-generator-maven-plugin.outputDirectory>${project.build.directory}/schemas</json-schema-generator-maven-plugin.outputDirectory>
@@ -187,6 +189,12 @@
187189
<artifactId>assertj-core</artifactId>
188190
<scope>test</scope>
189191
</dependency>
192+
<dependency>
193+
<groupId>org.awaitility</groupId>
194+
<artifactId>awaitility</artifactId>
195+
<version>${awaitility.version}</version>
196+
<scope>test</scope>
197+
</dependency>
190198
</dependencies>
191199

192200
<build>

src/main/java/io/gravitee/policy/callout/CalloutHttpPolicy.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public Completable onResponse(HttpExecutionContext ctx) {
6666
}
6767

6868
private Completable doCallOut(HttpExecutionContext ctx) {
69+
if (configuration.isFireAndForget()) {
70+
return Completable.fromRunnable(() -> executeCallOut(ctx).onErrorComplete().subscribe());
71+
} else {
72+
return executeCallOut(ctx);
73+
}
74+
}
75+
76+
private Completable executeCallOut(HttpExecutionContext ctx) {
6977
var templateEngine = ctx.getTemplateEngine();
7078
var vertx = ctx.getComponent(Vertx.class);
7179

src/test/java/io/gravitee/policy/callout/AbstractV4EngineTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.Map;
4141
import java.util.Set;
4242
import java.util.stream.Stream;
43+
import org.junit.jupiter.api.BeforeEach;
4344
import org.junit.jupiter.api.extension.RegisterExtension;
4445

4546
@GatewayTest
@@ -50,6 +51,11 @@ public class AbstractV4EngineTest extends AbstractPolicyTest<CalloutHttpPolicy,
5051
@RegisterExtension
5152
static WireMockExtension calloutServer = WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build();
5253

54+
@BeforeEach
55+
public void cleanStub() {
56+
calloutServer.resetAll();
57+
}
58+
5359
@Override
5460
public void configureEntrypoints(Map<String, EntrypointConnectorPlugin<?, ?>> entrypoints) {
5561
entrypoints.putIfAbsent("http-proxy", EntrypointBuilder.build("http-proxy", HttpProxyEntrypointConnectorFactory.class));

src/test/java/io/gravitee/policy/callout/CalloutHttpPolicyV4IntegrationTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
2525
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.awaitility.Awaitility.await;
2728

2829
import io.gravitee.apim.gateway.tests.sdk.annotations.DeployApi;
2930
import io.gravitee.common.http.MediaType;
@@ -62,7 +63,7 @@ void should_do_callout_and_process_response(String path, HttpClient client) {
6263
.test();
6364

6465
obs
65-
.awaitDone(30, TimeUnit.SECONDS)
66+
.awaitDone(5, TimeUnit.SECONDS)
6667
.assertComplete()
6768
.assertValue(response -> {
6869
assertThat(response).hasToString("new content: response from callout");
@@ -79,9 +80,9 @@ void should_do_callout_and_process_response(String path, HttpClient client) {
7980
{ "/apis/v4/proxy/request-callout-http-fire-and-forget.json", "/apis/v4/proxy/response-callout-http-fire-and-forget.json" }
8081
)
8182
@ValueSource(strings = { "/proxy-request-fire-and-forget", "/proxy-response-fire-and-forget" })
82-
void should_do_callout_and_do_nothing_with_callout_response(String apiEndpoint, HttpClient client) {
83+
void should_do_callout_with_fire_and_forget_and_do_nothing_with_callout_response(String apiEndpoint, HttpClient client) {
8384
wiremock.stubFor(get("/endpoint").willReturn(ok("response from backend")));
84-
calloutServer.stubFor(get("/callout").willReturn(ok("response from callout")));
85+
calloutServer.stubFor(get("/callout").willReturn(ok("response from callout").withFixedDelay(8000)));
8586

8687
var obs = client
8788
.rxRequest(HttpMethod.GET, apiEndpoint)
@@ -93,7 +94,7 @@ void should_do_callout_and_do_nothing_with_callout_response(String apiEndpoint,
9394
.test();
9495

9596
obs
96-
.awaitDone(30, TimeUnit.SECONDS)
97+
.awaitDone(5, TimeUnit.SECONDS)
9798
.assertComplete()
9899
.assertValue(response -> {
99100
assertThat(response).hasToString("new content");
@@ -102,7 +103,12 @@ void should_do_callout_and_do_nothing_with_callout_response(String apiEndpoint,
102103
.assertNoErrors();
103104

104105
wiremock.verify(getRequestedFor(urlPathEqualTo("/endpoint")));
105-
calloutServer.verify(getRequestedFor(urlPathEqualTo("/callout")).withHeader("X-Callout", equalTo("calloutHeader")));
106+
107+
await()
108+
.atMost(10, TimeUnit.SECONDS)
109+
.untilAsserted(() ->
110+
calloutServer.verify(getRequestedFor(urlPathEqualTo("/callout")).withHeader("X-Callout", equalTo("calloutHeader")))
111+
);
106112
}
107113

108114
@ParameterizedTest
@@ -130,7 +136,7 @@ void should_process_el_before_callout(String apiEndpoint, HttpClient client) {
130136
.test();
131137

132138
obs
133-
.awaitDone(30, TimeUnit.SECONDS)
139+
.awaitDone(5, TimeUnit.SECONDS)
134140
.assertComplete()
135141
.assertValue(response -> {
136142
assertThat(response).hasToString("new content");
@@ -164,7 +170,7 @@ void should_do_callout_and_interrupt_when_callout_fails(String apiEndpoint, Http
164170
.test();
165171

166172
obs
167-
.awaitDone(30, TimeUnit.SECONDS)
173+
.awaitDone(5, TimeUnit.SECONDS)
168174
.assertComplete()
169175
.assertValue(response -> {
170176
assertThat(response).hasToString("Error content with status 502");

src/test/java/io/gravitee/policy/callout/CalloutHttpPolicyV4Test.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static io.gravitee.policy.v3.callout.CalloutHttpPolicyV3.CALLOUT_EXIT_ON_ERROR;
2626
import static io.gravitee.policy.v3.callout.CalloutHttpPolicyV3.CALLOUT_HTTP_ERROR;
2727
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.awaitility.Awaitility.await;
2829
import static org.mockito.Mockito.mock;
2930
import static org.mockito.Mockito.verify;
3031
import static test.RequestBuilder.aRequest;
@@ -341,11 +342,10 @@ void should_call_and_do_nothing_when_fire_and_forget_defined(boolean exitOnError
341342
)
342343
.onRequest(ctx)
343344
.test()
344-
.awaitDone(30, TimeUnit.SECONDS)
345345
.assertComplete();
346346

347347
assertThat(ctx.getAttributes()).isEmpty();
348-
wiremock.verify(getRequestedFor(urlPathEqualTo("/")));
348+
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> wiremock.verify(getRequestedFor(urlPathEqualTo("/"))));
349349
}
350350

351351
@Test
@@ -662,11 +662,10 @@ void should_call_and_do_nothing_when_fire_and_forget_defined(boolean exitOnError
662662
)
663663
.onResponse(ctx)
664664
.test()
665-
.awaitDone(30, TimeUnit.SECONDS)
666665
.assertComplete();
667666

668667
assertThat(ctx.getAttributes()).isEmpty();
669-
wiremock.verify(getRequestedFor(urlPathEqualTo("/")));
668+
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> wiremock.verify(getRequestedFor(urlPathEqualTo("/"))));
670669
}
671670

672671
@Test

src/test/resources/apis/v4/proxy/response-callout-http-el-support.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
}
5858
],
5959
"body": "{#request.headers['X-Body'][0]}",
60-
"fireAndForget": true
60+
"fireAndForget": false
6161
}
6262
},
6363
{

0 commit comments

Comments
 (0)