Skip to content

Commit

Permalink
Try to avoid intermittent test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesagnew committed Jan 19, 2019
1 parent cd1e0e8 commit b878925
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -431,6 +432,20 @@ public static void waitForSize(int theTarget, List<?> theList) {
}
}

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);
}
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"));
}

Expand Down

0 comments on commit b878925

Please sign in to comment.