From 9743eedda145a355cc56269dd95641f65f190696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Andr=C3=A9?= <92062874+matheusandre1@users.noreply.github.com> Date: Fri, 18 Sep 2026 23:53:43 -0300 Subject: [PATCH] fix(s3): stop S3InternalErrorIntegrationTest racing background callers of the mock (#3887) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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é --- .../s3/S3InternalErrorIntegrationTest.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3InternalErrorIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3InternalErrorIntegrationTest.java index 0b9df6e503..ac17588df0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3InternalErrorIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3InternalErrorIntegrationTest.java @@ -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. + * + *

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()