Add a test

This commit is contained in:
James Agnew 2019-10-15 17:58:06 -04:00
parent bf476b9c32
commit fdcf5f5d73
5 changed files with 93 additions and 20 deletions

View File

@ -49,8 +49,9 @@ public class SearchParamExtractorR4Test {
}
@Override
public void refreshCacheIfNecessary() {
public boolean refreshCacheIfNecessary() {
// nothing
return false;
}
@Override

View File

@ -93,7 +93,7 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
@Override
public Map<String, RuntimeSearchParam> getActiveSearchParams(String theResourceName) {
requiresActiveSearchParams();
return myActiveSearchParams.get(theResourceName);
return getActiveSearchParams().get(theResourceName);
}
private void requiresActiveSearchParams() {
@ -207,12 +207,8 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
if (next.getCompositeOf() != null) {
next.getCompositeOf().sort((theO1, theO2) -> StringUtils.compare(theO1.getName(), theO2.getName()));
for (String nextBase : next.getBase()) {
if (!activeParamNamesToUniqueSearchParams.containsKey(nextBase)) {
activeParamNamesToUniqueSearchParams.put(nextBase, new HashMap<>());
}
if (!activeParamNamesToUniqueSearchParams.get(nextBase).containsKey(paramNames)) {
activeParamNamesToUniqueSearchParams.get(nextBase).put(paramNames, new ArrayList<>());
}
activeParamNamesToUniqueSearchParams.computeIfAbsent(nextBase, v -> new HashMap<>());
activeParamNamesToUniqueSearchParams.get(nextBase).computeIfAbsent(paramNames, t -> new ArrayList<>());
activeParamNamesToUniqueSearchParams.get(nextBase).get(paramNames).add(next);
}
}
@ -224,11 +220,21 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
myActiveParamNamesToUniqueSearchParams = activeParamNamesToUniqueSearchParams;
}
@VisibleForTesting
void setFhirContextForUnitTest(FhirContext theFhirContext) {
myFhirContext = theFhirContext;
}
@PostConstruct
public void postConstruct() {
myBuiltInSearchParams = createBuiltInSearchParamMap(myFhirContext);
}
@VisibleForTesting
void setSearchParamProviderForUnitTest(ISearchParamProvider theSearchParamProvider) {
mySearchParamProvider = theSearchParamProvider;
}
public int doRefresh(long theRefreshInterval) {
if (System.currentTimeMillis() - theRefreshInterval > myLastRefresh) {
StopWatch sw = new StopWatch();
@ -314,8 +320,10 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
myLastRefresh = System.currentTimeMillis();
ourLog.info("Refreshed search parameter cache in {}ms", sw.getMillis());
}
return myActiveSearchParams.size();
} else {
return 0;
}
}
protected abstract RuntimeSearchParam toRuntimeSp(SP theNextSp);
@ -344,11 +352,6 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
refreshCacheWithRetry();
}
@VisibleForTesting
public void setSearchParamProviderForUnitTest(ISearchParamProvider theSearchParamProvider) {
mySearchParamProvider = theSearchParamProvider;
}
int refreshCacheWithRetry() {
Retrier<Integer> refreshCacheRetrier = new Retrier<>(() -> {
synchronized (BaseSearchParamRegistry.this) {
@ -367,10 +370,12 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
}
@Override
public void refreshCacheIfNecessary() {
if (myActiveSearchParams == null ||
System.currentTimeMillis() - REFRESH_INTERVAL > myLastRefresh) {
public boolean refreshCacheIfNecessary() {
if (myActiveSearchParams == null || System.currentTimeMillis() - REFRESH_INTERVAL > myLastRefresh) {
refreshCacheWithRetry();
return true;
} else {
return false;
}
}
@ -380,6 +385,11 @@ public abstract class BaseSearchParamRegistry<SP extends IBaseResource> implemen
return Collections.unmodifiableMap(myActiveSearchParams);
}
@VisibleForTesting
void setSchedulerServiceForUnitTest(ISchedulerService theSchedulerService) {
mySchedulerService = theSchedulerService;
}
public static class SubmitJob implements Job {
@Autowired
private ISearchParamRegistry myTarget;

View File

@ -41,7 +41,7 @@ public interface ISearchParamRegistry {
*/
RuntimeSearchParam getActiveSearchParam(String theResourceName, String theParamName);
void refreshCacheIfNecessary();
boolean refreshCacheIfNecessary();
Map<String, Map<String, RuntimeSearchParam>> getActiveSearchParams();

View File

@ -8,7 +8,6 @@ import ca.uhn.fhir.jpa.model.entity.ModelConfig;
import ca.uhn.fhir.jpa.model.entity.ResourceIndexedSearchParamToken;
import ca.uhn.fhir.jpa.model.entity.ResourceTable;
import ca.uhn.fhir.jpa.searchparam.JpaRuntimeSearchParam;
import ca.uhn.fhir.jpa.searchparam.extractor.SearchParamExtractorDstu3;
import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry;
import ca.uhn.fhir.util.TestUtil;
import org.hl7.fhir.dstu3.hapi.ctx.DefaultProfileValidationSupport;
@ -44,8 +43,9 @@ public class SearchParamExtractorDstu3Test {
}
@Override
public void refreshCacheIfNecessary() {
public boolean refreshCacheIfNecessary() {
// nothing
return false;
}
@Override

View File

@ -0,0 +1,62 @@
package ca.uhn.fhir.jpa.searchparam.registry;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jpa.model.sched.ISchedulerService;
import ca.uhn.fhir.rest.server.SimpleBundleProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class BaseSearchParamRegistryTest {
@Mock
private ISchedulerService mySchedulerService;
@Mock
private ISearchParamProvider mySearchParamProvider;
@Test
public void testRefreshAfterExpiry() {
when(mySearchParamProvider.search(any())).thenReturn(new SimpleBundleProvider());
SearchParamRegistryR4 registry = new SearchParamRegistryR4();
registry.setSchedulerServiceForUnitTest(mySchedulerService);
registry.setFhirContextForUnitTest(FhirContext.forR4());
registry.setSearchParamProviderForUnitTest(mySearchParamProvider);
registry.postConstruct();
registry.requestRefresh();
assertEquals(146, registry.doRefresh(100000));
// Second time we don't need to run because we ran recently
assertEquals(0, registry.doRefresh(100000));
assertEquals(146, registry.getActiveSearchParams().size());
}
@Test
public void testRefreshCacheIfNeccessary() {
SearchParamRegistryR4 registry = new SearchParamRegistryR4();
when(mySearchParamProvider.search(any())).thenReturn(new SimpleBundleProvider());
when(mySearchParamProvider.refreshCache(any(), anyLong())).thenAnswer(t->{
registry.doRefresh(t.getArgument(1, Long.class));
return 0;
});
registry.setSchedulerServiceForUnitTest(mySchedulerService);
registry.setFhirContextForUnitTest(FhirContext.forR4());
registry.setSearchParamProviderForUnitTest(mySearchParamProvider);
registry.postConstruct();
registry.requestRefresh();
assertTrue(registry.refreshCacheIfNecessary());
assertFalse(registry.refreshCacheIfNecessary());
}
}