From b878925884213b43287baf4a6fcef1a2e42472cf Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Sat, 19 Jan 2019 18:44:09 -0500 Subject: [PATCH] Try to avoid intermittent test failure --- .../java/ca/uhn/fhir/jpa/dao/BaseJpaTest.java | 15 +++++++++++++++ .../resthook/RestHookWithInterceptorR4Test.java | 13 ++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) 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 eb040fcba84..3ef8a954096 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.sql.SQLException; 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 abstract class BaseJpaTest { } } + 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 d64876b428d..202c715e472 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.context.annotation.Configuration; 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 class RestHookWithInterceptorR4Test extends BaseSubscriptionsR4Test { 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 class RestHookWithInterceptorR4Test extends BaseSubscriptionsR4Test { 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")); }