Add test for #794

This commit is contained in:
James Agnew 2017-11-20 12:01:24 -05:00
parent a87323fa08
commit 60b6c3745b
2 changed files with 116 additions and 66 deletions

View File

@ -30,8 +30,10 @@ import org.slf4j.LoggerFactory;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicInteger;
import static org.apache.commons.lang3.StringUtils.defaultString; import static org.apache.commons.lang3.StringUtils.defaultString;
@ -123,6 +125,27 @@ public class TestUtil {
ourLog.info("Tests are using time zone: {}", TimeZone.getDefault().getID()); ourLog.info("Tests are using time zone: {}", TimeZone.getDefault().getID());
} }
/**
* <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b>
* <p>
* Wait for an atomicinteger to hit a given site and fail if it never does
*/
public static void waitForSize(int theTarget, AtomicInteger theInteger) {
long start = System.currentTimeMillis();
while (theInteger.get() != theTarget && (System.currentTimeMillis() - start) <= 15000) {
try {
Thread.sleep(50);
} catch (InterruptedException theE) {
throw new Error(theE);
}
}
if ((System.currentTimeMillis() - start) >= 15000) {
throw new IllegalStateException("Size " + theInteger.get() + " is != target " + theTarget);
}
}
/** /**
* <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b> * <b>THIS IS FOR UNIT TESTS ONLY - DO NOT CALL THIS METHOD FROM USER CODE</b>
* <p> * <p>

View File

@ -1,13 +1,8 @@
package ca.uhn.fhir.context; package ca.uhn.fhir.context;
import static org.junit.Assert.*; import ca.uhn.fhir.rest.client.MyPatientWithExtensions;
import ca.uhn.fhir.util.OperationOutcomeUtil;
import java.util.ArrayList; import ca.uhn.fhir.util.TestUtil;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender;
import org.hl7.fhir.dstu3.model.Patient; import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Reference; import org.hl7.fhir.dstu3.model.Reference;
@ -15,19 +10,21 @@ import org.hl7.fhir.dstu3.model.StructureDefinition;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Test; import org.junit.Test;
import ca.uhn.fhir.rest.client.MyPatientWithExtensions; import java.util.ArrayList;
import ca.uhn.fhir.util.TestUtil; import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.*;
public class FhirContextDstu3Test { public class FhirContextDstu3Test {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirContextDstu3Test.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirContextDstu3Test.class);
private static FhirContext ourCtx = FhirContext.forDstu3(); private static FhirContext ourCtx = FhirContext.forDstu3();
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Test @Test
public void testAutoDetectVersion() { public void testAutoDetectVersion() {
@ -35,6 +32,17 @@ public class FhirContextDstu3Test {
assertEquals(FhirVersionEnum.DSTU3, ctx.getVersion().getVersion()); assertEquals(FhirVersionEnum.DSTU3, ctx.getVersion().getVersion());
} }
@Test
public void testCustomTypeDoesntBecomeDefault() {
FhirContext ctx = FhirContext.forDstu3();
MyPatientWithExtensions pt = new MyPatientWithExtensions();
pt.addName().addGiven("FOO");
ctx.newXmlParser().encodeResourceToString(pt);
assertEquals(Patient.class, ctx.getResourceDefinition("Patient").getImplementingClass());
}
/** /**
* See #344 * See #344
*/ */
@ -57,14 +65,66 @@ public class FhirContextDstu3Test {
} }
@Test @Test
public void testCustomTypeDoesntBecomeDefault() { public void testInitialisationThreadSafety() {
FhirContext ctx = FhirContext.forDstu3(); final FhirContext ctx = FhirContext.forDstu3();
MyPatientWithExtensions pt = new MyPatientWithExtensions(); final int numThreads = 40;
pt.addName().addGiven("FOO"); final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
ctx.newXmlParser().encodeResourceToString(pt); final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
try {
final CountDownLatch threadsReady = new CountDownLatch(numThreads);
final CountDownLatch threadsFinished = new CountDownLatch(numThreads);
for (int i = 0; i < numThreads; i++) {
threadPool.submit(
new Runnable() {
public void run() {
threadsReady.countDown();
try {
threadsReady.await();
RuntimeResourceDefinition def = ctx.getResourceDefinition("patient");
ourLog.info(def.toString());
assertNotNull(def);
} catch (final Exception e) {
exceptions.add(e);
}
threadsFinished.countDown();
}
}
);
}
threadsFinished.await();
} catch (final InterruptedException e) {
exceptions.add(e);
} finally {
threadPool.shutdownNow();
}
assertTrue("failed with exception(s): " + exceptions, exceptions.isEmpty());
}
/**
* See #794
*/
@Test
public void testInitializeThreadSafety2() throws InterruptedException {
final FhirContext dstu3FhirContext = FhirContext.forDstu3();
final AtomicInteger count = new AtomicInteger();
for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
OperationOutcomeUtil.newInstance(dstu3FhirContext);
ourLog.info("Have finished {}", count.incrementAndGet());
}
}).start();
}
TestUtil.waitForSize(10, count);
assertEquals(Patient.class, ctx.getResourceDefinition("Patient").getImplementingClass());
} }
@Test @Test
@ -87,43 +147,10 @@ public class FhirContextDstu3Test {
assertEquals(null, genderChild.getBoundEnumType()); assertEquals(null, genderChild.getBoundEnumType());
} }
@Test @AfterClass
public void testInitialisationThreadSafety() { public static void afterClassClearContext() {
final FhirContext ctx = FhirContext.forDstu3(); TestUtil.clearAllStaticFieldsForUnitTest();
}
final int numThreads = 40;
final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
try {
final CountDownLatch threadsReady = new CountDownLatch(numThreads);
final CountDownLatch threadsFinished = new CountDownLatch(numThreads);
for (int i = 0; i < numThreads; i++) {
threadPool.submit(
new Runnable() {
public void run() {
threadsReady.countDown();
try {
threadsReady.await();
RuntimeResourceDefinition def = ctx.getResourceDefinition("patient");
ourLog.info(def.toString());
assertNotNull(def);
} catch(final Exception e) {
exceptions.add(e);
}
threadsFinished.countDown();
}
}
);
}
threadsFinished.await();
} catch(final InterruptedException e) {
exceptions.add(e);
} finally {
threadPool.shutdownNow();
}
assertTrue("failed with exception(s): " + exceptions, exceptions.isEmpty());
}
} }