Eagerly create, initialize and register the 'DEFAULT' Pool with the o.a.g.cache.client.PoolManager when spying on the ClientCacheFactory and create() is called.

This commit is contained in:
John Blum
2019-09-09 18:35:16 -07:00
parent 9d2484855d
commit 56cf069384
3 changed files with 153 additions and 17 deletions

View File

@@ -1604,6 +1604,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
AtomicInteger readTimeout = new AtomicInteger(PoolFactory.DEFAULT_READ_TIMEOUT);
AtomicInteger retryAttempts = new AtomicInteger(PoolFactory.DEFAULT_RETRY_ATTEMPTS);
AtomicInteger socketBufferSize = new AtomicInteger(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE);
AtomicInteger socketConnectTimeout = new AtomicInteger(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT);
AtomicInteger statisticInterval = new AtomicInteger(PoolFactory.DEFAULT_STATISTIC_INTERVAL);
AtomicInteger subscriptionAckInterval = new AtomicInteger(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL);
AtomicInteger subscriptionMessageTrackingTimeout = new AtomicInteger(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT);
@@ -1663,6 +1664,9 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockPoolFactory.setSocketBufferSize(anyInt()))
.thenAnswer(newSetter(socketBufferSize, mockPoolFactory));
when(mockPoolFactory.setSocketConnectTimeout(anyInt()))
.thenAnswer(newSetter(socketConnectTimeout, mockPoolFactory));
when(mockPoolFactory.setStatisticInterval(anyInt()))
.thenAnswer(newSetter(statisticInterval, mockPoolFactory));
@@ -1715,6 +1719,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockPool.getServerGroup()).thenReturn(serverGroup.get());
when(mockPool.getServers()).thenReturn(servers);
when(mockPool.getSocketBufferSize()).thenReturn(socketBufferSize.get());
when(mockPool.getSocketConnectTimeout()).thenReturn(socketConnectTimeout.get());
when(mockPool.getStatisticInterval()).thenReturn(statisticInterval.get());
when(mockPool.getSubscriptionAckInterval()).thenReturn(subscriptionAckInterval.get());
when(mockPool.getSubscriptionEnabled()).thenReturn(subscriptionEnabled.get());
@@ -2997,7 +3002,6 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
AtomicReference<String> pdxDiskStoreName = new AtomicReference<>(null);
AtomicReference<PdxSerializer> pdxSerializer = new AtomicReference<>(null);
AtomicReference<Pool> defaultPool = new AtomicReference<>(null);
ClientCacheFactory clientCacheFactorySpy = spy(clientCacheFactory);
@@ -3088,6 +3092,11 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolSocketBufferSize(anyInt());
doAnswer(invocation -> {
mockPoolFactory.setSocketConnectTimeout(invocation.getArgument(0));
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolSocketConnectTimeout(anyInt());
doAnswer(invocation -> {
mockPoolFactory.setStatisticInterval(invocation.getArgument(0));
return clientCacheFactorySpy;
@@ -3123,17 +3132,12 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
ClientCache mockClientCache = mockClientCache();
Pool mockDefaultPool = mockPoolFactory.create("DEFAULT");
when(mockClientCache.getCurrentServers()).thenAnswer(invocation ->
Collections.unmodifiableSet(new HashSet<>(mockClientCache.getDefaultPool().getServers())));
when(mockClientCache.getDefaultPool()).thenAnswer(invocation -> {
if (defaultPool.get() == null) {
defaultPool.set(mockPoolFactory.create("DEFAULT"));
}
return defaultPool.get();
});
when(mockClientCache.getDefaultPool()).thenReturn(mockDefaultPool);
when(mockClientCache.getPdxDiskStore()).thenAnswer(newGetter(pdxDiskStoreName));
when(mockClientCache.getPdxIgnoreUnreadFields()).thenAnswer(newGetter(pdxIgnoreUnreadFields));

View File

@@ -17,6 +17,9 @@ package org.springframework.data.gemfire;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.InetSocketAddress;
import java.util.Collections;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolManager;
@@ -29,8 +32,10 @@ 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.client.PoolFactoryBean;
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
import org.springframework.data.gemfire.config.annotation.EnablePool;
import org.springframework.data.gemfire.support.ConnectionEndpoint;
import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport;
import org.springframework.data.gemfire.tests.mock.annotation.EnableGemFireMockObjects;
import org.springframework.test.annotation.DirtiesContext;
@@ -65,6 +70,10 @@ public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests {
@Qualifier("MOCK")
private Pool mockPool;
@Autowired
@Qualifier("TEST")
private Pool testPool;
@AfterClass
public static void tearDown() {
@@ -89,21 +98,32 @@ public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests {
//@Ignore("Apache Geode/Pivotal GemFire does not support Mock Pools")
public void defaultPoolRegisteredWithPoolManager() {
Pool geodeDefaultPool = PoolManager.find("DEFAULT");
Pool defaultPool = PoolManager.find("DEFAULT");
assertThat(geodeDefaultPool).isNotNull();
assertThat(geodeDefaultPool.getName()).isEqualTo("DEFAULT");
assertThat(geodeDefaultPool).isSameAs(this.defaultPool);
assertThat(defaultPool).isNotNull();
assertThat(defaultPool.getName()).isEqualTo("DEFAULT");
assertThat(defaultPool).isSameAs(this.defaultPool);
}
@Test
public void mockPoolRegisteredWithPoolManager() {
Pool mockGeodePool = PoolManager.find("MOCK");
Pool mockPool = PoolManager.find("MOCK");
assertThat(mockGeodePool).isNotNull();
assertThat(mockGeodePool.getName()).isEqualTo("MOCK");
assertThat(mockGeodePool).isSameAs(this.mockPool);
assertThat(mockPool).isNotNull();
assertThat(mockPool.getName()).isEqualTo("MOCK");
assertThat(mockPool).isSameAs(this.mockPool);
}
@Test
public void testPoolRegisteredWithPoolManager() {
Pool testPool = PoolManager.find("TEST");
assertThat(testPool).isNotNull();
assertThat(testPool.getName()).isEqualTo("TEST");
assertThat(testPool.getLocators()).containsExactly(new InetSocketAddress("skullbox", 12345));
assertThat(testPool).isSameAs(this.testPool);
}
@ClientCacheApplication
@@ -115,5 +135,16 @@ public class MockClientCacheDefaultPoolRegisteredWithPoolManagerUnitTests {
Pool mockPool() {
return GemFireMockObjectsSupport.mockPoolFactory().create("MOCK");
}
@Bean("TEST")
PoolFactoryBean testPool() {
PoolFactoryBean testPool = new PoolFactoryBean();
testPool.setName("TEST");
testPool.setLocators(Collections.singleton(new ConnectionEndpoint("skullbox", 12345)));
return testPool;
}
}
}

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2019 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
*
* https://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.tests;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientCacheFactory;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolManager;
import org.junit.Test;
import org.springframework.data.gemfire.tests.mock.GemFireMockObjectsSupport;
/**
* Unit Tests asserting that when the {@link ClientCacheFactory} is spied on, the {@literal DEFAULT} {@link Pool}
* is eagerly created, configured and initialized.
*
* @author John Blum
* @see org.junit.Test
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.ClientCacheFactory
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolManager
* @since 0.0.8
*/
public class ClientCacheFactorySpyEagerlyInitializesDefaultPoolUnitTests {
@Test
public void clientCacheFactorySpyEagerlyInitializesDefaultPool() {
ClientCacheFactory clientCacheFactory = GemFireMockObjectsSupport.spyOn(new ClientCacheFactory())
.set("name", "TestClientCache")
.setPoolFreeConnectionTimeout(30000)
.setPoolIdleTimeout(120000)
.setPoolLoadConditioningInterval(60000)
.setPoolMaxConnections(250)
.setPoolMinConnections(75)
.setPoolMultiuserAuthentication(true)
.setPoolPingInterval(5000)
.setPoolPRSingleHopEnabled(true)
.setPoolReadTimeout(10000)
.setPoolRetryAttempts(2)
.setPoolServerGroup("TestServerGroup")
.setPoolSocketBufferSize(16384)
.setPoolSocketConnectTimeout(20000)
.setPoolStatisticInterval(2000)
.setPoolSubscriptionAckInterval(15000)
.setPoolSubscriptionEnabled(true)
.setPoolSubscriptionMessageTrackingTimeout(300000)
.setPoolSubscriptionRedundancy(2)
.setPoolThreadLocalConnections(false);
assertThat(PoolManager.find("DEFAULT")).isNull();
ClientCache testClientCache = clientCacheFactory.create();
assertThat(testClientCache).isNotNull();
assertThat(testClientCache.getName()).isEqualTo("TestClientCache");
Pool defaultPool = PoolManager.find("DEFAULT");
assertThat(defaultPool).isNotNull();
assertThat(defaultPool).isSameAs(testClientCache.getDefaultPool());
assertThat(defaultPool.getName()).isEqualTo("DEFAULT");
assertThat(defaultPool.getFreeConnectionTimeout()).isEqualTo(30000);
assertThat(defaultPool.getIdleTimeout()).isEqualTo(120000);
assertThat(defaultPool.getLoadConditioningInterval()).isEqualTo(60000);
assertThat(defaultPool.getMaxConnections()).isEqualTo(250);
assertThat(defaultPool.getMinConnections()).isEqualTo(75);
assertThat(defaultPool.getMultiuserAuthentication()).isEqualTo(true);
assertThat(defaultPool.getPingInterval()).isEqualTo(5000);
assertThat(defaultPool.getPRSingleHopEnabled()).isEqualTo(true);
assertThat(defaultPool.getReadTimeout()).isEqualTo(10000);
assertThat(defaultPool.getRetryAttempts()).isEqualTo(2);
assertThat(defaultPool.getServerGroup()).isEqualTo("TestServerGroup");
assertThat(defaultPool.getSocketBufferSize()).isEqualTo(16384);
assertThat(defaultPool.getSocketBufferSize()).isEqualTo(16384);
assertThat(defaultPool.getSocketConnectTimeout()).isEqualTo(20000);
assertThat(defaultPool.getStatisticInterval()).isEqualTo(2000);
assertThat(defaultPool.getSubscriptionAckInterval()).isEqualTo(15000);
assertThat(defaultPool.getSubscriptionEnabled()).isEqualTo(true);
assertThat(defaultPool.getSubscriptionMessageTrackingTimeout()).isEqualTo(300000);
assertThat(defaultPool.getSubscriptionRedundancy()).isEqualTo(2);
assertThat(defaultPool.getThreadLocalConnections()).isEqualTo(false);
}
}