From 692455d2df093d1c359e21ecd18dc3beddbeb3a1 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 23 May 2019 17:56:36 -0700 Subject: [PATCH] Add support to register the Mock Pool created by the Mock PoolFactory with the PoolManager. --- .../tests/mock/GemFireMockObjectsSupport.java | 36 +++++++- ...oolRegisteredWithPoolManagerUnitTests.java | 88 +++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java 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 6768302..d7a7499 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 @@ -65,6 +65,9 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; import java.util.stream.Collectors; +import org.mockito.ArgumentMatchers; +import org.mockito.stubbing.Answer; + import org.apache.geode.cache.AttributesMutator; import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheFactory; @@ -98,6 +101,7 @@ import org.apache.geode.cache.client.ClientRegionFactory; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.client.Pool; import org.apache.geode.cache.client.PoolFactory; +import org.apache.geode.cache.client.PoolManager; import org.apache.geode.cache.control.ResourceManager; import org.apache.geode.cache.execute.RegionFunctionContext; import org.apache.geode.cache.lucene.LuceneIndex; @@ -127,10 +131,10 @@ import org.apache.geode.cache.wan.GatewayTransportFilter; import org.apache.geode.compression.Compressor; import org.apache.geode.distributed.DistributedMember; import org.apache.geode.distributed.DistributedSystem; +import org.apache.geode.internal.cache.PoolManagerImpl; import org.apache.geode.pdx.PdxSerializer; import org.apache.lucene.analysis.Analyzer; -import org.mockito.ArgumentMatchers; -import org.mockito.stubbing.Answer; + import org.springframework.beans.factory.DisposableBean; import org.springframework.data.gemfire.IndexType; import org.springframework.data.gemfire.server.SubscriptionEvictionPolicy; @@ -180,6 +184,7 @@ import org.springframework.util.StringUtils; * @see org.apache.geode.cache.client.ClientRegionFactory * @see org.apache.geode.cache.client.Pool * @see org.apache.geode.cache.client.PoolFactory + * @see org.apache.geode.cache.client.PoolManager * @see org.apache.geode.cache.control.ResourceManager * @see org.apache.geode.cache.execute.RegionFunctionContext * @see org.apache.geode.cache.lucene.LuceneIndex @@ -257,9 +262,25 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { regions.clear(); regionAttributes.clear(); + closePools(); destroyGemFireObjects(); } + /** + * Closes all {@link Pool Pools}. + * + * @see org.apache.geode.cache.client.Pool + * @see org.apache.geode.cache.client.PoolManager + */ + static void closePools() { + + // TODO: add support for keepAlive (??) + ObjectUtils.doOperationSafely(() -> { + PoolManager.close(); + return null; + }, null); + } + /** * Destroys all {@link DisposableBean} based {@link Object GemFire objects}. */ @@ -581,7 +602,6 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { .orElseThrow(() -> newIllegalArgumentException("Region path [%s] is required", regionPath)); } - /* (non-Javadoc) */ @SuppressWarnings("unchecked") private static T mockCacheApi(T mockGemFireCache) { @@ -618,7 +638,6 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { return mockRegionServiceApi(mockGemFireCache); } - /* (non-Javadoc) */ private static T mockRegionServiceApi(T mockRegionService) { AtomicBoolean closed = new AtomicBoolean(false); @@ -1668,12 +1687,21 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport { when(mockPool.getSubscriptionRedundancy()).thenReturn(subscriptionRedundancy.get()); when(mockPool.getThreadLocalConnections()).thenReturn(threadLocalConnections.get()); + register(mockPool); + return mockPool; }); return mockPoolFactory; } + private static Pool register(Pool pool) { + + PoolManagerImpl.getPMI().register(pool); + + return pool; + } + public static Pool mockQueryService(Pool pool) { QueryService mockQueryService = mockQueryService(); 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 new file mode 100644 index 0000000..ebf9375 --- /dev/null +++ b/spring-data-geode-test/src/test/java/org/springframework/data/gemfire/MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests.java @@ -0,0 +1,88 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ +package org.springframework.data.gemfire; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.AfterClass; +import org.junit.Before; +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.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.gemfire.config.annotation.ClientCacheApplication; +import org.springframework.data.gemfire.config.annotation.EnablePool; +import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Unit Tests for registering the {@link ClientCache} {@literal DEFAULT} {@link Pool} with the {@link PoolManager}. + * + * @author John Blum + * @see org.apache.geode.cache.client.ClientCache + * @see org.apache.geode.cache.client.Pool + * @see org.apache.geode.cache.client.PoolManager + * @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication + * @see org.springframework.data.gemfire.config.annotation.EnablePool + * @see org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects + * @see org.springframework.test.annotation.DirtiesContext + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringRunner + * @since 1.0.0 + */ +@RunWith(SpringRunner.class) +@ContextConfiguration +@SuppressWarnings("unused") +public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests { + + @Autowired + private Pool defaultPool; + + @AfterClass + public static void tearDown() { + //assertThat(PoolManager.find("DEFAULT")).isNull(); + } + + @Before + public void setup() { + + assertThat(this.defaultPool).isNotNull(); + assertThat(this.defaultPool.getName()).isEqualTo("DEFAULT"); + } + + @Test + @DirtiesContext + public void defaultPoolRegisteredWithPoolManager() { + + Pool geodeDefaultPool = PoolManager.find("DEFAULT"); + + assertThat(geodeDefaultPool).isNotNull(); + assertThat(geodeDefaultPool.getName()).isEqualTo("DEFAULT"); + assertThat(geodeDefaultPool).isSameAs(this.defaultPool); + } + + @ClientCacheApplication + @EnableGemFireMockObjects + @EnablePool(name = "DEFAULT") + static class TestConfiguration { } + +}