diff --git a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java index 34d0258fd05..88788d77db5 100644 --- a/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java +++ b/hapi-fhir-jpaserver-subscription/src/main/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistry.java @@ -13,7 +13,7 @@ import java.util.Optional; @Component public class SubscriptionChannelRegistry { private final SubscriptionChannelCache mySubscriptionChannelCache = new SubscriptionChannelCache(); - // This map is a reference count so we know to destroy the channel if there are no more active subscriptions using it + // This map is a reference count so we know to destroy the channel when there are no more active subscriptions using it private final Multimap myActiveSubscriptionByChannelName = MultimapBuilder.hashKeys().arrayListValues().build(); @Autowired @@ -48,11 +48,12 @@ public class SubscriptionChannelRegistry { public void remove(ActiveSubscription theActiveSubscription) { String channelName = theActiveSubscription.getChannelName(); myActiveSubscriptionByChannelName.remove(channelName, theActiveSubscription); - // FIXME KHS test + // This was the last one. Shut down the channel if (!myActiveSubscriptionByChannelName.containsKey(channelName)) { SubscriptionChannelWithHandlers channel = mySubscriptionChannelCache.get(channelName); channel.close(); + mySubscriptionChannelCache.remove(channelName); } } diff --git a/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java new file mode 100644 index 00000000000..32b7802431d --- /dev/null +++ b/hapi-fhir-jpaserver-subscription/src/test/java/ca/uhn/fhir/jpa/subscription/module/channel/SubscriptionChannelRegistryTest.java @@ -0,0 +1,57 @@ +package ca.uhn.fhir.jpa.subscription.module.channel; + +import ca.uhn.fhir.jpa.model.entity.ModelConfig; +import ca.uhn.fhir.jpa.subscription.module.CanonicalSubscription; +import ca.uhn.fhir.jpa.subscription.module.cache.ActiveSubscription; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +public class SubscriptionChannelRegistryTest { + private static final String TEST_CHANNEL_NAME = "TEST_CHANNEL"; + @Autowired + SubscriptionChannelRegistry mySubscriptionChannelRegistry; + @MockBean + SubscriptionDeliveryHandlerFactory mySubscriptionDeliveryHandlerFactory; + @MockBean + SubscriptionChannelFactory mySubscriptionDeliveryChannelFactory; + @MockBean + ModelConfig myModelConfig; + + @Configuration + static class SpringConfig { + @Bean + SubscriptionChannelRegistry subscriptionChannelRegistry() { + return new SubscriptionChannelRegistry(); + } + } + + + @Test + public void testAddAddRemoveRemove() { + when(myModelConfig.isSubscriptionMatchingEnabled()).thenReturn(true); + + CanonicalSubscription cansubA = new CanonicalSubscription(); + ActiveSubscription activeSubscriptionA = new ActiveSubscription(cansubA, TEST_CHANNEL_NAME); + CanonicalSubscription cansubB = new CanonicalSubscription(); + ActiveSubscription activeSubscriptionB = new ActiveSubscription(cansubB, TEST_CHANNEL_NAME); + + assertNull(mySubscriptionChannelRegistry.get(TEST_CHANNEL_NAME)); + mySubscriptionChannelRegistry.add(activeSubscriptionA); + assertNotNull(mySubscriptionChannelRegistry.get(TEST_CHANNEL_NAME)); + mySubscriptionChannelRegistry.add(activeSubscriptionB); + mySubscriptionChannelRegistry.remove(activeSubscriptionB); + assertNotNull(mySubscriptionChannelRegistry.get(TEST_CHANNEL_NAME)); + mySubscriptionChannelRegistry.remove(activeSubscriptionA); + assertNull(mySubscriptionChannelRegistry.get(TEST_CHANNEL_NAME)); + } +}