Add a test
This commit is contained in:
parent
ce720f5601
commit
ff85503acb
|
@ -89,7 +89,7 @@ public class SearchParamPresenceSvcImpl implements ISearchParamPresenceSvc {
|
|||
searchParam = new SearchParam();
|
||||
searchParam.setResourceName(resourceType);
|
||||
searchParam.setParamName(paramName);
|
||||
searchParam = mySearchParamDao.saveAndFlush(searchParam);
|
||||
searchParam = mySearchParamDao.save(searchParam);
|
||||
ourLog.info("Added search param {} with pid {}", paramName, searchParam.getId());
|
||||
// Don't add the newly saved entity to the map in case the save fails
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import ca.uhn.fhir.jpa.entity.ResourceTable;
|
|||
import ca.uhn.fhir.jpa.entity.ResourceTag;
|
||||
import ca.uhn.fhir.jpa.entity.TagTypeEnum;
|
||||
import ca.uhn.fhir.jpa.provider.SystemProviderDstu2Test;
|
||||
import ca.uhn.fhir.jpa.util.StopWatch;
|
||||
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
|
||||
import ca.uhn.fhir.model.primitive.IdDt;
|
||||
import ca.uhn.fhir.rest.api.Constants;
|
||||
|
@ -37,9 +38,12 @@ import java.io.InputStream;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -62,43 +66,6 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
|
|||
myDaoConfig.setReuseCachedSearchResultsForMillis(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionWhichFailsPersistsNothing() {
|
||||
|
||||
// Run a transaction which points to that practitioner
|
||||
// in a field that isn't allowed to refer to a practitioner
|
||||
Bundle input = new Bundle();
|
||||
input.setType(BundleType.TRANSACTION);
|
||||
|
||||
Patient pt = new Patient();
|
||||
pt.setId("PT");
|
||||
pt.setActive(true);
|
||||
pt.addName().setFamily("FAMILY");
|
||||
input.addEntry()
|
||||
.setResource(pt)
|
||||
.getRequest().setMethod(HTTPVerb.PUT).setUrl("Patient/PT");
|
||||
|
||||
Observation obs = new Observation();
|
||||
obs.setId("OBS");
|
||||
obs.getCode().addCoding().setSystem("foo").setCode("bar");
|
||||
obs.addPerformer().setReference("Practicioner/AAAAA");
|
||||
input.addEntry()
|
||||
.setResource(obs)
|
||||
.getRequest().setMethod(HTTPVerb.PUT).setUrl("Observation/OBS");
|
||||
|
||||
try {
|
||||
mySystemDao.transaction(mySrd, input);
|
||||
fail();
|
||||
} catch (UnprocessableEntityException e) {
|
||||
assertThat(e.getMessage(), containsString("Resource type 'Practicioner' is not valid for this path"));
|
||||
}
|
||||
|
||||
assertThat(myResourceTableDao.findAll(), empty());
|
||||
assertThat(myResourceIndexedSearchParamStringDao.findAll(), empty());
|
||||
|
||||
}
|
||||
|
||||
|
||||
private Bundle createInputTransactionWithPlaceholderIdInMatchUrl(HTTPVerb theVerb) {
|
||||
|
||||
Patient pat = new Patient();
|
||||
|
@ -209,6 +176,11 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
|
|||
return null;
|
||||
}
|
||||
|
||||
private Bundle loadBundle(String theFileName) throws IOException {
|
||||
String req = IOUtils.toString(FhirSystemDaoDstu3Test.class.getResourceAsStream(theFileName), StandardCharsets.UTF_8);
|
||||
return myFhirCtx.newXmlParser().parseResource(Bundle.class, req);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBatchCreateWithBadRead() {
|
||||
Bundle request = new Bundle();
|
||||
|
@ -1222,8 +1194,7 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
|
|||
|
||||
@Test
|
||||
public void testTransactionCreateWithPutUsingUrl2() throws Exception {
|
||||
String req = IOUtils.toString(FhirSystemDaoDstu3Test.class.getResourceAsStream("/bundle-dstu3.xml"), StandardCharsets.UTF_8);
|
||||
Bundle request = myFhirCtx.newXmlParser().parseResource(Bundle.class, req);
|
||||
Bundle request = loadBundle("/bundle-dstu3.xml");
|
||||
mySystemDao.transaction(mySrd, request);
|
||||
}
|
||||
|
||||
|
@ -2147,6 +2118,42 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionWhichFailsPersistsNothing() {
|
||||
|
||||
// Run a transaction which points to that practitioner
|
||||
// in a field that isn't allowed to refer to a practitioner
|
||||
Bundle input = new Bundle();
|
||||
input.setType(BundleType.TRANSACTION);
|
||||
|
||||
Patient pt = new Patient();
|
||||
pt.setId("PT");
|
||||
pt.setActive(true);
|
||||
pt.addName().setFamily("FAMILY");
|
||||
input.addEntry()
|
||||
.setResource(pt)
|
||||
.getRequest().setMethod(HTTPVerb.PUT).setUrl("Patient/PT");
|
||||
|
||||
Observation obs = new Observation();
|
||||
obs.setId("OBS");
|
||||
obs.getCode().addCoding().setSystem("foo").setCode("bar");
|
||||
obs.addPerformer().setReference("Practicioner/AAAAA");
|
||||
input.addEntry()
|
||||
.setResource(obs)
|
||||
.getRequest().setMethod(HTTPVerb.PUT).setUrl("Observation/OBS");
|
||||
|
||||
try {
|
||||
mySystemDao.transaction(mySrd, input);
|
||||
fail();
|
||||
} catch (UnprocessableEntityException e) {
|
||||
assertThat(e.getMessage(), containsString("Resource type 'Practicioner' is not valid for this path"));
|
||||
}
|
||||
|
||||
assertThat(myResourceTableDao.findAll(), empty());
|
||||
assertThat(myResourceIndexedSearchParamStringDao.findAll(), empty());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Format changed, source isn't valid
|
||||
*/
|
||||
|
@ -2815,6 +2822,92 @@ public class FhirSystemDaoDstu3Test extends BaseJpaDstu3SystemTest {
|
|||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultipleConcurrentWritesToSameResource() throws InterruptedException {
|
||||
|
||||
ThreadPoolExecutor exec = new ThreadPoolExecutor(10, 10,
|
||||
0L, TimeUnit.MILLISECONDS,
|
||||
new LinkedBlockingQueue<Runnable>());
|
||||
|
||||
final AtomicInteger errors = new AtomicInteger();
|
||||
|
||||
List<Future> futures = new ArrayList<>();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
final Patient p = new Patient();
|
||||
p.setId("PID");
|
||||
p.setActive(true);
|
||||
p.setBirthDate(new Date());
|
||||
p.addIdentifier().setSystem("foo1");
|
||||
p.addIdentifier().setSystem("foo2");
|
||||
p.addIdentifier().setSystem("foo3");
|
||||
p.addIdentifier().setSystem("foo4");
|
||||
p.addName().setFamily("FOO" + i);
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB1");
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB2");
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB3");
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB4");
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB5");
|
||||
p.addName().addGiven("AAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
|
||||
|
||||
Organization o = new Organization();
|
||||
o.setName("ORG" + i);
|
||||
|
||||
final Bundle t = new Bundle();
|
||||
t.setType(BundleType.TRANSACTION);
|
||||
t.addEntry()
|
||||
.setResource(p)
|
||||
.getRequest()
|
||||
.setUrl("Patient/PID")
|
||||
.setMethod(HTTPVerb.PUT);
|
||||
t.addEntry()
|
||||
.setResource(o)
|
||||
.getRequest()
|
||||
.setUrl("Organization")
|
||||
.setMethod(HTTPVerb.POST);
|
||||
|
||||
if (i == 0) {
|
||||
mySystemDao.transaction(mySrd, t);
|
||||
}
|
||||
futures.add(exec.submit(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
mySystemDao.transaction(mySrd, t);
|
||||
} catch (Exception e) {
|
||||
ourLog.error("Failed to update", e);
|
||||
errors.incrementAndGet();
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
ourLog.info("Shutting down excutor");
|
||||
StopWatch sw = new StopWatch();
|
||||
for (Future next : futures) {
|
||||
while (!next.isDone()) {
|
||||
Thread.sleep(20);
|
||||
}
|
||||
}
|
||||
exec.shutdown();
|
||||
ourLog.info("Shut down excutor in {}ms", sw.getMillis());
|
||||
ourLog.info("Had {} errors", errors.get());
|
||||
|
||||
Patient currentPatient = myPatientDao.read(new IdType("Patient/PID"));
|
||||
Long currentVersion = currentPatient.getIdElement().getVersionIdPartAsLong();
|
||||
ourLog.info("Current version: {}", currentVersion);
|
||||
|
||||
IBundleProvider historyBundle = myPatientDao.history(new IdType("Patient/PID"),null,null,mySrd);
|
||||
Patient lastPatient = (Patient) historyBundle.getResources(0,1).get(0);
|
||||
Long lastVersion = lastPatient.getIdElement().getVersionIdPartAsLong();
|
||||
ourLog.info("Last version: {}", lastVersion);
|
||||
|
||||
assertEquals(currentVersion, lastVersion);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void afterClassClearContext() {
|
||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
|
|
|
@ -1,33 +1,43 @@
|
|||
package ca.uhn.fhir.jpa.stresstest;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test;
|
||||
import ca.uhn.fhir.rest.api.Constants;
|
||||
import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.hl7.fhir.dstu3.hapi.validation.FhirInstanceValidator;
|
||||
import org.hl7.fhir.dstu3.model.*;
|
||||
import org.hl7.fhir.dstu3.model.Bundle;
|
||||
import org.hl7.fhir.dstu3.model.Bundle.BundleType;
|
||||
import org.hl7.fhir.dstu3.model.Bundle.HTTPVerb;
|
||||
import org.junit.*;
|
||||
import org.hl7.fhir.dstu3.model.CodeableConcept;
|
||||
import org.hl7.fhir.dstu3.model.Coding;
|
||||
import org.hl7.fhir.dstu3.model.Patient;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import ca.uhn.fhir.jpa.provider.dstu3.BaseResourceProviderDstu3Test;
|
||||
import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StressTestDstu3Test extends BaseResourceProviderDstu3Test {
|
||||
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(StressTestDstu3Test.class);
|
||||
private RequestValidatingInterceptor myRequestValidatingInterceptor;
|
||||
|
||||
@After
|
||||
public void after() throws Exception {
|
||||
super.after();
|
||||
|
||||
ourRestServer.unregisterInterceptor(myRequestValidatingInterceptor);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
super.before();
|
||||
|
@ -38,13 +48,6 @@ public class StressTestDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
myRequestValidatingInterceptor.addValidatorModule(module);
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() throws Exception {
|
||||
super.after();
|
||||
|
||||
ourRestServer.unregisterInterceptor(myRequestValidatingInterceptor);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultithreadedSearch() throws Exception {
|
||||
|
@ -75,7 +78,6 @@ public class StressTestDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This test prevents a deadlock that was detected with a large number of
|
||||
* threads creating resources and blocking on the searchparamcache refreshing
|
||||
|
@ -142,7 +144,7 @@ public class StressTestDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
|
||||
public class BaseTask extends Thread {
|
||||
public class BaseTask extends Thread {
|
||||
protected Throwable myError;
|
||||
protected int myTaskCount = 0;
|
||||
|
||||
|
@ -226,4 +228,5 @@ public class StressTestDstu3Test extends BaseResourceProviderDstu3Test {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue