Skip to content

Commit ffea749

Browse files
committed
Start the function only once in test
If a test using `WithFunction` contains multiple test method, the function will be started multiple time, wich will fail the test. Using an atomic boolean to be sure that the function is only started once fixes the issue. Fixes #44661
1 parent acb9e0f commit ffea749

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

integration-tests/google-cloud-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionTestCase.java

+11
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,15 @@ public void test() {
2121
.statusCode(200)
2222
.body(is("Hello World!"));
2323
}
24+
25+
// we do twice the test to be sure we can call multiple time the same function
26+
@Test
27+
public void test2() {
28+
// test the function using RestAssured
29+
when()
30+
.get()
31+
.then()
32+
.statusCode(200)
33+
.body(is("Hello World!"));
34+
}
2435
}

test-framework/google-cloud-functions/src/main/java/io/quarkus/google/cloud/functions/test/CloudFunctionTestResource.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.Map;
5+
import java.util.concurrent.atomic.AtomicBoolean;
56

67
import org.eclipse.microprofile.config.ConfigProvider;
78

@@ -16,6 +17,7 @@ public class CloudFunctionTestResource implements QuarkusTestResourceConfigurabl
1617
private FunctionType functionType;
1718
private String functionName;
1819
private CloudFunctionsInvoker invoker;
20+
private final AtomicBoolean started = new AtomicBoolean(false);
1921

2022
@Override
2123
public void init(WithFunction withFunction) {
@@ -30,14 +32,16 @@ public Map<String, String> start() {
3032

3133
@Override
3234
public void inject(TestInjector testInjector) {
33-
// This is a hack, we cannot start the invoker in the start() method as Quarkus is not yet initialized,
34-
// so we start it here as this method is called later (the same for reading the test port).
35-
int port = ConfigProvider.getConfig().getOptionalValue("quarkus.http.test-port", Integer.class).orElse(8081);
36-
this.invoker = new CloudFunctionsInvoker(functionType, port);
37-
try {
38-
this.invoker.start();
39-
} catch (Exception e) {
40-
throw new RuntimeException(e);
35+
if (started.compareAndSet(false, true)) {
36+
// This is a hack, we cannot start the invoker in the start() method as Quarkus is not yet initialized,
37+
// so we start it here as this method is called later (the same for reading the test port).
38+
int port = ConfigProvider.getConfig().getOptionalValue("quarkus.http.test-port", Integer.class).orElse(8081);
39+
this.invoker = new CloudFunctionsInvoker(functionType, port);
40+
try {
41+
this.invoker.start();
42+
} catch (Exception e) {
43+
throw new RuntimeException(e);
44+
}
4145
}
4246
}
4347

0 commit comments

Comments
 (0)