From a189bf9eee0b371c661f30b7db3fcea66ab7e06e Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Mon, 15 Aug 2022 14:43:42 +0200 Subject: [PATCH] Attempt to fix intermittent failure in SimpleContextPropagationTest caused by possible race condition --- .../java/io/quarkus/context/test/RequestBean.java | 12 ++++++++++++ .../context/test/SimpleContextPropagationTest.java | 10 ++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/RequestBean.java b/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/RequestBean.java index 3e8816a86b316..44d9b29128207 100644 --- a/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/RequestBean.java +++ b/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/RequestBean.java @@ -1,5 +1,7 @@ package io.quarkus.context.test; +import java.util.concurrent.CountDownLatch; + import javax.annotation.PreDestroy; import javax.enterprise.context.RequestScoped; @@ -7,6 +9,7 @@ public class RequestBean { public static volatile int DESTROY_INVOKED = 0; + public static volatile CountDownLatch LATCH; public String callMe() { return "Hello " + System.identityHashCode(this); @@ -15,5 +18,14 @@ public String callMe() { @PreDestroy public void destroy() { DESTROY_INVOKED++; + // bean is also used in tests where there is no latch + if (LATCH != null) { + LATCH.countDown(); + } + } + + public static void initState() { + LATCH = new CountDownLatch(2); + DESTROY_INVOKED = 0; } } diff --git a/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/SimpleContextPropagationTest.java b/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/SimpleContextPropagationTest.java index 3915977314d8a..57042266b27ed 100644 --- a/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/SimpleContextPropagationTest.java +++ b/integration-tests/smallrye-context-propagation/src/test/java/io/quarkus/context/test/SimpleContextPropagationTest.java @@ -75,20 +75,22 @@ public void testArcTCContextPropagation() { } @Test() - public void testArcMEContextPropagationDisabled() { + public void testArcMEContextPropagationDisabled() throws InterruptedException { // reset state - RequestBean.DESTROY_INVOKED = 0; + RequestBean.initState(); RestAssured.when().get("/context/noarc").then() .statusCode(Response.Status.OK.getStatusCode()); + Assertions.assertTrue(RequestBean.LATCH.await(3, TimeUnit.SECONDS)); Assertions.assertEquals(2, RequestBean.DESTROY_INVOKED); } @Test() - public void testArcTCContextPropagationDisabled() { + public void testArcTCContextPropagationDisabled() throws InterruptedException { // reset state - RequestBean.DESTROY_INVOKED = 0; + RequestBean.initState(); RestAssured.when().get("/context/noarc-tc").then() .statusCode(Response.Status.OK.getStatusCode()); + Assertions.assertTrue(RequestBean.LATCH.await(3, TimeUnit.SECONDS)); Assertions.assertEquals(2, RequestBean.DESTROY_INVOKED); }