From b2f9dd457876cf5408b68ed15c3e6707bcea9d5b Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Fri, 28 Feb 2020 16:38:13 -0500 Subject: [PATCH] extend pointcutlatch and simplify FhirClientResourceRetriever (#1737) * extend pointcutlatch and simplify FhirClientResourceRetriever * extend pointcutlatch and simplify FhirClientResourceRetriever --- .../FhirClientResourceRetriever.java | 2 +- .../uhn/test/concurrency/PointcutLatch.java | 38 ++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/standalone/FhirClientResourceRetriever.java b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/standalone/FhirClientResourceRetriever.java index 2a4f5ba24ab..d112209ae2d 100644 --- a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/standalone/FhirClientResourceRetriever.java +++ b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/standalone/FhirClientResourceRetriever.java @@ -45,6 +45,6 @@ public class FhirClientResourceRetriever implements IResourceRetriever { public IBaseResource getResource(IIdType payloadId) throws ResourceGoneException { RuntimeResourceDefinition resourceDef = myFhirContext.getResourceDefinition(payloadId.getResourceType()); - return myClient.search().forResource(resourceDef.getName()).withIdAndCompartment(payloadId.getIdPart(), payloadId.getResourceType()).execute(); + return myClient.read().resource(resourceDef.getName()).withId(payloadId).execute(); } } diff --git a/hapi-fhir-test-utilities/src/main/java/ca/uhn/test/concurrency/PointcutLatch.java b/hapi-fhir-test-utilities/src/main/java/ca/uhn/test/concurrency/PointcutLatch.java index 0c95bca015b..7d9b94b7ed8 100644 --- a/hapi-fhir-test-utilities/src/main/java/ca/uhn/test/concurrency/PointcutLatch.java +++ b/hapi-fhir-test-utilities/src/main/java/ca/uhn/test/concurrency/PointcutLatch.java @@ -51,6 +51,7 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch { private int myDefaultTimeoutSeconds = DEFAULT_TIMEOUT_SECONDS; private final Pointcut myPointcut; private int myInitialCount; + private boolean myExactMatch; public PointcutLatch(Pointcut thePointcut) { @@ -69,19 +70,32 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch { } @Override - public void setExpectedCount(int count) { + public void setExpectedCount(int theCount) { + this.setExpectedCount(theCount, true); + } + + public void setExpectedCount(int theCount, boolean theExactMatch) { if (myCountdownLatch.get() != null) { throw new PointcutLatchException("setExpectedCount() called before previous awaitExpected() completed."); } - createLatch(count); - ourLog.info("Expecting {} calls to {} latch", count, name); + myExactMatch = theExactMatch; + createLatch(theCount); + if (theExactMatch) { + ourLog.info("Expecting exactly {} calls to {} latch", theCount, name); + } else { + ourLog.info("Expecting at least {} calls to {} latch", theCount, name); + } } - private void createLatch(int count) { + public void setExpectAtLeast(int theCount) { + setExpectedCount(theCount, false); + } + + private void createLatch(int theCount) { myFailures.set(Collections.synchronizedList(new ArrayList<>())); myCalledWith.set(Collections.synchronizedList(new ArrayList<>())); - myCountdownLatch.set(new CountDownLatch(count)); - myInitialCount = count; + myCountdownLatch.set(new CountDownLatch(theCount)); + myInitialCount = theCount; } private void addFailure(String failure) { @@ -153,10 +167,14 @@ public class PointcutLatch implements IAnonymousInterceptor, IPointcutLatch { @Override public void invoke(Pointcut thePointcut, HookParams theArgs) { CountDownLatch latch = myCountdownLatch.get(); - if (latch == null) { - throw new PointcutLatchException("invoke() called outside of setExpectedCount() .. awaitExpected(). Probably got more invocations than expected or clear() was called before invoke() arrived.", theArgs); - } else if (latch.getCount() <= 0) { - addFailure("invoke() called when countdown was zero."); + if (myExactMatch) { + if (latch == null) { + throw new PointcutLatchException("invoke() called outside of setExpectedCount() .. awaitExpected(). Probably got more invocations than expected or clear() was called before invoke() arrived.", theArgs); + } else if (latch.getCount() <= 0) { + addFailure("invoke() called when countdown was zero."); + } + } else if (latch == null || latch.getCount() <= 0) { + return; } if (myCalledWith.get() != null) {