fix(s3): stop S3InternalErrorIntegrationTest racing background callers of the mock (#3887)

Mockito stubbing with when(...).thenThrow(...) is not safe against concurrent
invocations on the same mock. The mocked S3Service is shared with background
writers (CloudTrail delivery, flow logs, Firehose, ...), so a call from one of
them mid-stubbing attached the answer to the wrong method and listBuckets()
kept returning an empty list, rendering a 200 instead of the expected error.

Bake the failure into the mock's default answer at creation time and install
it with QuarkusMock, so no post-construction stubbing is needed.

Fixes #3833

Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
Matheus André authored and GitHub committed 2026-09-18 22:53:43 -04:00
1 parent fbc0d4d3f2
commit 9743eedda1
1 file changed
+19 -6
@@ -1,31 +1,44 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusMock;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.mockito.Mockito.when;
/**
* S3 routes must never surface Quarkus's plain-text 500 page: SDK REST-XML parsers choke on
* it (issue #1664 — a lazily-instantiated S3Service whose constructor throws turned every S3
* call into an opaque 500). Unhandled throwables map to AWS's InternalError XML contract.
*
* <p>The failure is baked into the mock's default answer instead of stubbed afterwards with
* {@code when(...).thenThrow(...)}. The mocked S3Service is shared with background writers
* (CloudTrail delivery, flow logs, Firehose, ...) that may call it mid-stubbing; Mockito then
* attaches the answer to their invocation and {@code listBuckets()} keeps returning an empty
* list, rendering a 200 instead of the expected error (issue #3833).
*/
@QuarkusTest
class S3InternalErrorIntegrationTest {
@InjectMock
S3Service s3Service;
private static void failListBucketsWith(RuntimeException failure) {
S3Service failing = Mockito.mock(S3Service.class, invocation -> {
if ("listBuckets".equals(invocation.getMethod().getName())) {
throw failure;
}
return Mockito.RETURNS_DEFAULTS.answer(invocation);
});
QuarkusMock.installMockForType(failing, S3Service.class);
}
@Test
void unhandledThrowableRendersInternalErrorXml() {
when(s3Service.listBuckets()).thenThrow(new RuntimeException("simulated bean failure"));
failListBucketsWith(new RuntimeException("simulated bean failure"));
given()
.when()
@@ -40,7 +53,7 @@ class S3InternalErrorIntegrationTest {
@Test
void awsExceptionStillRendersItsOwnXmlError() {
when(s3Service.listBuckets()).thenThrow(new AwsException("AccessDenied", "Access Denied", 403));
failListBucketsWith(new AwsException("AccessDenied", "Access Denied", 403));
given()
.when()