diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java index eb040fcba847..3ef8a9540964 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java @@ -52,6 +52,7 @@ import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Supplier; import java.util.stream.Collectors; import static ca.uhn.fhir.util.TestUtil.randomizeLocale; @@ -431,6 +432,20 @@ public static void waitForSize(int theTarget, List theList) { } } + public static void waitForTrue(Supplier theList) { + StopWatch sw = new StopWatch(); + while (!theList.get() && sw.getMillis() <= 16000) { + try { + Thread.sleep(50); + } catch (InterruptedException theE) { + throw new Error(theE); + } + } + if (sw.getMillis() >= 16000) { + fail("Waited " + sw.toString() + " and is still false"); + } + } + public static void waitForSize(int theTarget, Callable theCallable) throws Exception { waitForSize(theTarget, 10000, theCallable); } diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java index d64876b428d3..202c715e4729 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/subscription/resthook/RestHookWithInterceptorR4Test.java @@ -23,7 +23,8 @@ import org.springframework.test.context.ContextConfiguration; import static org.hamcrest.Matchers.hasItem; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; /** * Test the rest-hook subscriptions @@ -74,8 +75,9 @@ public void testBeforeRestHookDelivery_ModifyResourceId() throws Exception { waitForSize(1, ourUpdatedObservations); assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); assertEquals("Observation/A", ourUpdatedObservations.get(0).getId()); - assertTrue(ourHitBeforeRestHookDelivery); - assertTrue(ourHitAfterRestHookDelivery); + // TODO: JA a latch would be even better but we'd need to allow customizable orders since the ad-hoc ones run first + waitForTrue(() -> ourHitBeforeRestHookDelivery); + waitForTrue(() -> ourHitAfterRestHookDelivery); } @Test @@ -90,8 +92,9 @@ public void testBeforeRestHookDelivery_AddHeader() throws Exception { waitForSize(0, ourCreatedObservations); waitForSize(1, ourUpdatedObservations); assertEquals(Constants.CT_FHIR_JSON_NEW, ourContentTypes.get(0)); - assertTrue(ourHitBeforeRestHookDelivery); - assertTrue(ourHitAfterRestHookDelivery); + // TODO: JA a latch would be even better but we'd need to allow customizable orders since the ad-hoc ones run first + waitForTrue(() -> ourHitBeforeRestHookDelivery); + waitForTrue(() -> ourHitAfterRestHookDelivery); assertThat(ourHeaders, hasItem("X-Foo: Bar")); }