Try to avoid intermittent test failure

This commit is contained in:
jamesagnew 2019-01-19 18:44:09 -05:00
parent cd1e0e881c
commit b878925884
2 changed files with 23 additions and 5 deletions

View File

@ -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<Boolean> 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<Number> theCallable) throws Exception {
waitForSize(theTarget, 10000, theCallable);
}

View File

@ -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"));
}