From b5f0bf1e09ab77b80ae865a36878104806e8f9dc Mon Sep 17 00:00:00 2001 From: John Blum Date: Tue, 3 Sep 2019 18:35:46 -0700 Subject: [PATCH] Add Test Framework support for Mock Pool registration with the Apache Geode o.a.g.cache.client.PoolManager. --- .../tests/mock/GemFireMockObjectsSupport.java | 30 ++++++++++-- ...oolRegisteredWithPoolManagerUnitTests.java | 47 +++++++++++++++---- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java index 50014fd..459e0d8 100644 --- a/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java +++ b/spring-data-geode-test/src/main/java/org/springframework/data/gemfire/tests/mock/GemFireMockObjectsSupport.java @@ -57,6 +57,7 @@ import java.util.Properties; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.TimeUnit; @@ -150,6 +151,7 @@ import org.springframework.data.gemfire.tests.mock.support.MockObjectInvocationE import org.springframework.data.gemfire.tests.util.FileSystemUtils; import org.springframework.data.gemfire.tests.util.ObjectUtils; import org.springframework.data.gemfire.util.ArrayUtils; +import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.data.util.ReflectionUtils; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -157,7 +159,7 @@ import org.springframework.util.StringUtils; /** * The {@link GemFireMockObjectsSupport} class is an abstract base class encapsulating factory methods for creating - * Mock GemFire Objects (e.g. {@link Cache}, {@link ClientCache}, {@link Region}, etc). + * Apache Geode or Pivotal GemFire Mock Objects (e.g. {@link Cache}, {@link ClientCache}, {@link Region}, etc). * * @author John Blum * @see java.io.File @@ -249,6 +251,8 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { private static final Map> regionAttributes = new ConcurrentHashMap<>(); + private static final Set registeredPoolNames = new ConcurrentSkipListSet<>(); + private static final String CACHE_FACTORY_DS_PROPS_FIELD_NAME = "dsProps"; private static final String CACHE_FACTORY_INTERNAL_CACHE_BUILDER_FIELD_NAME = "internalCacheBuilder"; private static final String CLIENT_CACHE_FACTORY_DS_PROPS_FIELD_NAME = "dsProps"; @@ -276,6 +280,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { regions.clear(); regionAttributes.clear(); + unregisterManagedPools(); closePools(); destroyGemFireObjects(); } @@ -313,6 +318,16 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { cachedGemFireObjects.clear(); } + static synchronized void unregisterManagedPools() { + + CollectionUtils.nullSafeMap(PoolManager.getAll()).values().stream() + .filter(Objects::nonNull) + .filter(pool -> registeredPoolNames.contains(pool.getName())) + .forEach(GemFireMockObjectsSupport::unregister); + + registeredPoolNames.clear(); + } + /** * Caches the given {@link Object GemFire object} in order to release resources on shutdown. * @@ -1707,7 +1722,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { when(mockPool.getSubscriptionRedundancy()).thenReturn(subscriptionRedundancy.get()); when(mockPool.getThreadLocalConnections()).thenReturn(threadLocalConnections.get()); - //register(mockPool); + register(mockPool); return mockPool; }); @@ -1717,7 +1732,16 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { private static Pool register(Pool pool) { - PoolManagerImpl.getPMI().register(pool); + if (registeredPoolNames.add(pool.getName())) { + PoolManagerImpl.getPMI().register(pool); + } + + return pool; + } + + private static Pool unregister(Pool pool) { + + PoolManagerImpl.getPMI().unregister(pool); return pool; } diff --git a/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java index ecffe66..59c884e 100644 --- a/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java +++ b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java @@ -17,19 +17,21 @@ package org.springframework.data.gemfire; import static org.assertj.core.api.Assertions.assertThat; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; - import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; import org.apache.geode.cache.client.PoolManager; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; import org.springframework.data.gemfire.config.annotation.ClientCacheApplication; import org.springframework.data.gemfire.config.annotation.EnablePool; +import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport; import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; @@ -56,11 +58,20 @@ import org.springframework.test.context.junit4.SpringRunner; public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests { @Autowired + @Qualifier("DEFAULT") private Pool defaultPool; + @Autowired + @Qualifier("MOCK") + private Pool mockPool; + @AfterClass public static void tearDown() { - //assertThat(PoolManager.find("DEFAULT")).isNull(); + + GemFireMockObjectsSupport.destroy(); + + assertThat(PoolManager.find("DEFAULT")).isNull(); + assertThat(PoolManager.find("MOCK")).isNull(); } @Before @@ -68,11 +79,14 @@ public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests { assertThat(this.defaultPool).isNotNull(); assertThat(this.defaultPool.getName()).isEqualTo("DEFAULT"); + + assertThat(this.mockPool).isNotNull(); + assertThat(this.mockPool.getName()).isEqualTo("MOCK"); } @Test @DirtiesContext - @Ignore("Apache Geode/Pivotal GemFire does not support Mock Pools") + //@Ignore("Apache Geode/Pivotal GemFire does not support Mock Pools") public void defaultPoolRegisteredWithPoolManager() { Pool geodeDefaultPool = PoolManager.find("DEFAULT"); @@ -82,9 +96,24 @@ public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests { assertThat(geodeDefaultPool).isSameAs(this.defaultPool); } + @Test + public void mockPoolRegisteredWithPoolManager() { + + Pool mockGeodePool = PoolManager.find("MOCK"); + + assertThat(mockGeodePool).isNotNull(); + assertThat(mockGeodePool.getName()).isEqualTo("MOCK"); + assertThat(mockGeodePool).isSameAs(this.mockPool); + } + @ClientCacheApplication @EnableGemFireMockObjects @EnablePool(name = "DEFAULT") - static class TestConfiguration { } + static class TestConfiguration { + @Bean("MOCK") + Pool mockPool() { + return GemFireMockObjectsSupport.mockPoolFactory().create("MOCK"); + } + } }