Add support to register the Mock Pool created by the Mock PoolFactory with the PoolManager.

This commit is contained in:
John Blum
2019-05-23 17:56:36 -07:00
parent 72ac07217e
commit 692455d2df
2 changed files with 120 additions and 4 deletions

View File

@@ -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 extends GemFireCache> T mockCacheApi(T mockGemFireCache) {
@@ -618,7 +638,6 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
return mockRegionServiceApi(mockGemFireCache);
}
/* (non-Javadoc) */
private static <T extends RegionService> 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();

View File

@@ -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 { }
}