From 3a7fba65820e5c406a52d00c7ed5b96479017f9b Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Mon, 4 Dec 2017 23:20:43 -0500 Subject: [PATCH] Fix 2 tests --- .../search/StaleSearchDeletingSvcImpl.java | 7 +- ...rResourceDaoDstu3SearchPageExpiryTest.java | 118 +++++++++++++++++- ...temProviderTransactionSearchDstu2Test.java | 2 +- 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java index 29d12b36916..f5a139b60b7 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/search/StaleSearchDeletingSvcImpl.java @@ -27,6 +27,7 @@ import ca.uhn.fhir.jpa.dao.data.ISearchResultDao; import ca.uhn.fhir.jpa.entity.Search; import com.google.common.annotations.VisibleForTesting; import org.apache.commons.lang3.time.DateUtils; +import org.hl7.fhir.dstu3.model.InstantType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Slice; @@ -67,7 +68,7 @@ public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc { private void deleteSearch(final Long theSearchPid) { Search searchToDelete = mySearchDao.findOne(theSearchPid); if (searchToDelete != null) { - ourLog.info("Deleting search {}/{} - Created[{}] -- Last returned[{}]", searchToDelete.getId(), searchToDelete.getUuid(), searchToDelete.getCreated(), searchToDelete.getSearchLastReturned()); + ourLog.info("Deleting search {}/{} - Created[{}] -- Last returned[{}]", searchToDelete.getId(), searchToDelete.getUuid(), new InstantType(searchToDelete.getCreated()), new InstantType(searchToDelete.getSearchLastReturned())); mySearchIncludeDao.deleteForSearch(searchToDelete.getId()); mySearchResultDao.deleteForSearch(searchToDelete.getId()); mySearchDao.delete(searchToDelete); @@ -84,6 +85,10 @@ public class StaleSearchDeletingSvcImpl implements IStaleSearchDeletingSvc { } final Date cutoff = new Date((now() - cutoffMillis) - myCutoffSlack); + if (ourNowForUnitTests != null) { + ourLog.info("Searching for searches which are before {} - now is {}", new InstantType(cutoff), new InstantType(new Date(now()))); + } + ourLog.debug("Searching for searches which are before {}", cutoff); TransactionTemplate tt = new TransactionTemplate(myTransactionManager); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java index f70f9a81f3d..9585a709832 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoDstu3SearchPageExpiryTest.java @@ -79,6 +79,120 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test { sleepAtLeast(250); + final String searchUuid2; + { + SearchParameterMap params = new SearchParameterMap(); + params.add(Patient.SP_FAMILY, new StringParam("EXPIRE")); + final IBundleProvider bundleProvider = myPatientDao.search(params); + assertThat(toUnqualifiedVersionlessIds(bundleProvider), containsInAnyOrder(pid1, pid2)); + searchUuid2 = bundleProvider.getUuid(); + Validate.notBlank(searchUuid2); + } + assertEquals(searchUuid1, searchUuid2); + + sleepAtLeast(500); + + // We're now past 500ms so we shouldn't reuse the search + + final String searchUuid3; + { + SearchParameterMap params = new SearchParameterMap(); + params.add(Patient.SP_FAMILY, new StringParam("EXPIRE")); + final IBundleProvider bundleProvider = myPatientDao.search(params); + assertThat(toUnqualifiedVersionlessIds(bundleProvider), containsInAnyOrder(pid1, pid2)); + searchUuid3 = bundleProvider.getUuid(); + Validate.notBlank(searchUuid3); + } + assertNotEquals(searchUuid1, searchUuid3); + + // Search just got used so it shouldn't be deleted + + myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); + final AtomicLong search3timestamp = new AtomicLong(); + newTxTemplate().execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus theArg0) { + Search search3 = mySearchEntityDao.findByUuid(searchUuid3); + assertNotNull(search3); + Search search2 = mySearchEntityDao.findByUuid(searchUuid2); + assertNotNull(search2); + search3timestamp.set(search2.getSearchLastReturned().getTime()); + } + }); + + StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 800); + + myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); + newTxTemplate().execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus theArg0) { + assertNotNull(mySearchEntityDao.findByUuid(searchUuid3)); + } + }); + newTxTemplate().execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus theArg0) { + assertNotNull(mySearchEntityDao.findByUuid(searchUuid1)); + } + }); + + StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 1100); + + myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); + newTxTemplate().execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus theArg0) { + assertNull("Search 1 still exists", mySearchEntityDao.findByUuid(searchUuid1)); + assertNotNull("Search 3 still exists", mySearchEntityDao.findByUuid(searchUuid3)); + } + }); + + StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 1600); + + myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); + newTxTemplate().execute(new TransactionCallbackWithoutResult() { + @Override + protected void doInTransactionWithoutResult(TransactionStatus theArg0) { + assertNull("Search 1 still exists", mySearchEntityDao.findByUuid(searchUuid1)); + assertNull("Search 3 still exists", mySearchEntityDao.findByUuid(searchUuid3)); + } + }); + + } + + @Test + public void testExpirePagesAfterSingleUse2() throws Exception { + IIdType pid1; + IIdType pid2; + { + Patient patient = new Patient(); + patient.addName().setFamily("EXPIRE"); + pid1 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless(); + } + Thread.sleep(10); + { + Patient patient = new Patient(); + patient.addName().setFamily("EXPIRE"); + pid2 = myPatientDao.create(patient, mySrd).getId().toUnqualifiedVersionless(); + } + Thread.sleep(10); + + myDaoConfig.setExpireSearchResultsAfterMillis(1000L); + myDaoConfig.setReuseCachedSearchResultsForMillis(500L); + long start = System.currentTimeMillis(); + + final String searchUuid1; + { + SearchParameterMap params = new SearchParameterMap(); + params.add(Patient.SP_FAMILY, new StringParam("EXPIRE")); + final IBundleProvider bundleProvider = myPatientDao.search(params); + assertThat(toUnqualifiedVersionlessIds(bundleProvider), containsInAnyOrder(pid1, pid2)); + searchUuid1 = bundleProvider.getUuid(); + Validate.notBlank(searchUuid1); + } + + sleepAtLeast(250); + String searchUuid2; { SearchParameterMap params = new SearchParameterMap(); @@ -118,7 +232,7 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test { } }); - StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 1400); + StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 800); myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); newTxTemplate().execute(new TransactionCallbackWithoutResult() { @@ -134,7 +248,7 @@ public class FhirResourceDaoDstu3SearchPageExpiryTest extends BaseJpaDstu3Test { } }); - StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 2200); + StaleSearchDeletingSvcImpl.setNowForUnitTests(search3timestamp.get() + 1100); myStaleSearchDeletingSvc.pollForStaleSearchesAndDeleteThem(); newTxTemplate().execute(new TransactionCallbackWithoutResult() { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java index 215265ae47c..94a4f343d45 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/SystemProviderTransactionSearchDstu2Test.java @@ -125,7 +125,7 @@ public class SystemProviderTransactionSearchDstu2Test extends BaseJpaDstu2Test { .addEntry() .getRequest() .setMethod(HTTPVerbEnum.GET) - .setUrl("Patient?_count=5"); + .setUrl("Patient?_count=5&_sort=name"); myDaoConfig.setMaximumSearchResultCountInTransaction(100);