DATAGEODE-327 - Support configuring (client) SocketFactory, primarily for SNI support.

This commit is contained in:
John Blum
2020-07-15 00:24:58 -07:00
parent 51c9a232cc
commit 38c74f5300
23 changed files with 1712 additions and 895 deletions

View File

@@ -33,6 +33,7 @@ import org.apache.geode.cache.GemFireCache;
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.SocketFactory;
import org.apache.geode.distributed.DistributedSystem;
import org.springframework.beans.factory.FactoryBean;
@@ -99,6 +100,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
private Integer minConnections;
private Integer readTimeout;
private Integer retryAttempts;
private Integer serverConnectionTimeout;
private Integer socketBufferSize;
private Integer socketConnectTimeout;
private Integer statisticsInterval;
@@ -115,6 +117,8 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
private PoolResolver poolResolver = DEFAULT_POOL_RESOLVER;
private SocketFactory socketFactory;
private String durableClientId;
private String poolName;
private String serverGroup;
@@ -292,9 +296,11 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
clientCacheFactory.setPoolPRSingleHopEnabled(pool.getPRSingleHopEnabled(getPrSingleHopEnabled()));
clientCacheFactory.setPoolReadTimeout(pool.getReadTimeout(getReadTimeout()));
clientCacheFactory.setPoolRetryAttempts(pool.getRetryAttempts(getRetryAttempts()));
clientCacheFactory.setPoolServerConnectionTimeout(pool.getServerConnectionTimeout(getServerConnectionTimeout()));
clientCacheFactory.setPoolServerGroup(pool.getServerGroup(getServerGroup()));
clientCacheFactory.setPoolSocketBufferSize(pool.getSocketBufferSize(getSocketBufferSize()));
clientCacheFactory.setPoolSocketConnectTimeout(pool.getSocketConnectTimeout(getSocketConnectTimeout()));
clientCacheFactory.setPoolSocketFactory(pool.getSocketFactory(getSocketFactory()));
clientCacheFactory.setPoolStatisticInterval(pool.getStatisticInterval(getStatisticsInterval()));
clientCacheFactory.setPoolSubscriptionAckInterval(pool.getSubscriptionAckInterval(getSubscriptionAckInterval()));
clientCacheFactory.setPoolSubscriptionEnabled(pool.getSubscriptionEnabled(getSubscriptionEnabled()));
@@ -432,6 +438,22 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
((ClientCache) cache).close(isKeepAlive());
}
/**
* @inheritDoc
*/
@Override
protected void setCache(GemFireCache cache) {
super.setCache(cache);
}
/**
* @inheritDoc
*/
@Override
protected <T extends GemFireCache> T getCache() {
return super.getCache();
}
/**
* Returns the {@link Class} type of the {@link GemFireCache} produced by this {@link ClientCacheFactoryBean}.
*
@@ -785,6 +807,14 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
return this.retryAttempts;
}
public void setServerConnectionTimeout(Integer serverConnectionTimeout) {
this.serverConnectionTimeout = serverConnectionTimeout;
}
public Integer getServerConnectionTimeout() {
return this.serverConnectionTimeout;
}
public void setServerGroup(String serverGroup) {
this.serverGroup = serverGroup;
}
@@ -822,6 +852,14 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat
return this.socketConnectTimeout;
}
public void setSocketFactory(@Nullable SocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
public @NonNull SocketFactory getSocketFactory() {
return this.socketFactory;
}
public void setStatisticsInterval(Integer statisticsInterval) {
this.statisticsInterval = statisticsInterval;
}

View File

@@ -32,6 +32,7 @@ import org.apache.geode.cache.client.ClientCache;
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.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.apache.geode.distributed.DistributedSystem;
@@ -45,6 +46,7 @@ import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport;
import org.springframework.data.gemfire.support.ConnectionEndpoint;
import org.springframework.data.gemfire.support.ConnectionEndpointList;
import org.springframework.data.gemfire.util.DistributedSystemUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/**
@@ -96,6 +98,7 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
private int minConnections = PoolFactory.DEFAULT_MIN_CONNECTIONS;
private int readTimeout = PoolFactory.DEFAULT_READ_TIMEOUT;
private int retryAttempts = PoolFactory.DEFAULT_RETRY_ATTEMPTS;
private int serverConnectionTimeout = PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT;
private int socketBufferSize = PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE;
private int socketConnectTimeout = PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT;
private int statisticInterval = PoolFactory.DEFAULT_STATISTIC_INTERVAL;
@@ -115,12 +118,14 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
private volatile Pool pool;
private PoolConfigurer compositePoolConfigurer = (beanName, bean) ->
nullSafeCollection(poolConfigurers).forEach(poolConfigurer -> poolConfigurer.configure(beanName, bean));
nullSafeCollection(this.poolConfigurers).forEach(poolConfigurer -> poolConfigurer.configure(beanName, bean));
private PoolFactoryInitializer poolFactoryInitializer;
private PoolResolver poolResolver = DEFAULT_POOL_RESOLVER;
private SocketFactory socketFactory;
private String name;
private String serverGroup = PoolFactory.DEFAULT_SERVER_GROUP;
@@ -134,21 +139,24 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
*/
@Override
public void afterPropertiesSet() throws Exception {
init(Optional.ofNullable(resolvePool(resolvePoolName())));
init(resolvePool(resolvePoolName()));
}
@SuppressWarnings("all")
private void init(Optional<Pool> existingPool) {
/**
* Initializes the given {@link Pool}
*
* @param existingPool {@link Pool} to initialize.
* @see org.apache.geode.cache.client.Pool
*/
private void init(@Nullable Pool existingPool) {
if (existingPool.isPresent()) {
if (existingPool != null) {
this.pool = existingPool.get();
this.pool = existingPool;
this.springManagedPool = false;
logDebug(() -> String.format("A Pool with name [%s] already exists; Using existing Pool",
logDebug(() -> String.format("Pool [%s] already exists; Using existing Pool; PoolConfigurers will not be applied",
this.pool.getName()));
logDebug("PoolConfigurers will not be applied");
}
else {
logDebug("Pool [%s] not found; Lazily creating new Pool...", getName());
@@ -210,7 +218,6 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
/**
* Releases all system resources and destroys the {@link Pool} when created by this {@link PoolFactoryBean}.
*
* @throws Exception if the {@link Pool} destruction caused an error.
* @see org.springframework.beans.factory.DisposableBean#destroy()
*/
@Override
@@ -320,9 +327,11 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
it.setPRSingleHopEnabled(this.prSingleHopEnabled);
it.setReadTimeout(this.readTimeout);
it.setRetryAttempts(this.retryAttempts);
it.setServerConnectionTimeout(this.serverConnectionTimeout);
it.setServerGroup(this.serverGroup);
it.setSocketBufferSize(this.socketBufferSize);
it.setSocketConnectTimeout(this.socketConnectTimeout);
it.setSocketFactory(getSocketFactory());
it.setStatisticInterval(this.statisticInterval);
it.setSubscriptionAckInterval(this.subscriptionAckInterval);
it.setSubscriptionEnabled(this.subscriptionEnabled);
@@ -366,6 +375,14 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
return poolFactory;
}
/**
* @deprecated Use {@link #createPool(PoolFactory, String)} instead.
*/
@Deprecated
protected Pool create(PoolFactory poolFactory, String poolName) {
return createPool(poolFactory, poolName);
}
/**
* Creates a {@link Pool} with the given {@link String name} using the provided {@link PoolFactory}.
*
@@ -375,7 +392,7 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
* @see org.apache.geode.cache.client.PoolFactory#create(String)
* @see org.apache.geode.cache.client.Pool
*/
protected Pool create(PoolFactory poolFactory, String poolName) {
protected Pool createPool(PoolFactory poolFactory, String poolName) {
return poolFactory.create(poolName);
}
@@ -418,10 +435,10 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
}
/**
* Returns a reference to the Composite {@link PoolConfigurer} used to apply additional configuration
* Returns a reference to the {@literal Composite} {@link PoolConfigurer} used to apply additional configuration
* to this {@link PoolFactoryBean} on Spring container initialization.
*
* @return the Composite {@link PoolConfigurer}.
* @return the {@literal Composite} {@link PoolConfigurer}.
* @see org.springframework.data.gemfire.config.annotation.PoolConfigurer
*/
protected PoolConfigurer getCompositePoolConfigurer() {
@@ -539,6 +556,11 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
return PoolFactoryBean.this.retryAttempts;
}
@Override
public int getServerConnectionTimeout() {
return PoolFactoryBean.this.serverConnectionTimeout;
}
@Override
public String getServerGroup() {
return PoolFactoryBean.this.serverGroup;
@@ -559,6 +581,11 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
return PoolFactoryBean.this.socketConnectTimeout;
}
@Override
public SocketFactory getSocketFactory() {
return PoolFactoryBean.this.getSocketFactory();
}
@Override
public int getStatisticInterval() {
return PoolFactoryBean.this.statisticInterval;
@@ -742,6 +769,10 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
this.retryAttempts = retryAttempts;
}
public void setServerConnectionTimeout(int serverConnectionTimeout) {
this.serverConnectionTimeout = serverConnectionTimeout;
}
public void setServerGroup(String serverGroup) {
this.serverGroup = serverGroup;
}
@@ -767,6 +798,14 @@ public class PoolFactoryBean extends AbstractFactoryBeanSupport<Pool> implements
this.socketConnectTimeout = socketConnectTimeout;
}
public void setSocketFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
}
protected SocketFactory getSocketFactory() {
return this.socketFactory != null ? this.socketFactory : PoolFactory.DEFAULT_SOCKET_FACTORY;
}
public void setStatisticInterval(int statisticInterval) {
this.statisticInterval = statisticInterval;
}

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.function.Supplier;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.springframework.data.gemfire.util.CollectionUtils;
@@ -68,8 +69,9 @@ public abstract class DefaultableDelegatingPoolAdapter {
protected <T> T defaultIfNull(T defaultValue, Supplier<T> valueProvider) {
return prefersPool() ? SpringUtils.defaultIfNull(valueProvider.get(), defaultValue) :
(defaultValue != null ? defaultValue : valueProvider.get());
return prefersPool() ? SpringUtils.defaultIfNull(valueProvider.get(), defaultValue)
: defaultValue != null ? defaultValue
: valueProvider.get();
}
protected <E, T extends Collection<E>> T defaultIfEmpty(T defaultValue, Supplier<T> valueProvider) {
@@ -159,6 +161,10 @@ public abstract class DefaultableDelegatingPoolAdapter {
return defaultIfNull(defaultRetryAttempts, () -> getDelegate().getRetryAttempts());
}
public int getServerConnectionTimeout(Integer defaultServerConnectionTimeout) {
return defaultIfNull(defaultServerConnectionTimeout, () -> getDelegate().getServerConnectionTimeout());
}
public String getServerGroup(String defaultServerGroup) {
return defaultIfNull(defaultServerGroup, () -> getDelegate().getServerGroup());
}
@@ -175,6 +181,10 @@ public abstract class DefaultableDelegatingPoolAdapter {
return defaultIfNull(defaultSocketConnectTimeout, () -> getDelegate().getSocketConnectTimeout());
}
public SocketFactory getSocketFactory(SocketFactory defaultSocketFactory) {
return defaultIfNull(defaultSocketFactory, () -> getDelegate().getSocketFactory());
}
public int getStatisticInterval(Integer defaultStatisticInterval) {
return defaultIfNull(defaultStatisticInterval, () -> getDelegate().getStatisticInterval());
}

View File

@@ -14,7 +14,6 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.client.support;
import java.net.InetSocketAddress;
@@ -22,6 +21,7 @@ import java.util.List;
import java.util.Optional;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
/**
@@ -33,7 +33,9 @@ import org.apache.geode.cache.query.QueryService;
* when the {@link Pool} reference is <code>null</code>.
*
* @author John Blum
* @see java.net.InetSocketAddress
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.query.QueryService
* @see org.springframework.data.gemfire.client.support.FactoryDefaultsPoolAdapter
* @since 1.8.0
@@ -63,7 +65,10 @@ public abstract class DelegatingPoolAdapter extends FactoryDefaultsPoolAdapter {
@Override
public boolean isDestroyed() {
return Optional.ofNullable(getDelegate()).map(Pool::isDestroyed).orElseGet(super::isDestroyed);
return Optional.ofNullable(getDelegate())
.map(Pool::isDestroyed)
.orElseGet(super::isDestroyed);
}
@Override
@@ -186,6 +191,14 @@ public abstract class DelegatingPoolAdapter extends FactoryDefaultsPoolAdapter {
.orElseGet(super::getRetryAttempts);
}
@Override
public int getServerConnectionTimeout() {
return Optional.ofNullable(getDelegate())
.map(Pool::getServerConnectionTimeout)
.orElseGet(super::getServerConnectionTimeout);
}
@Override
public String getServerGroup() {
@@ -218,6 +231,14 @@ public abstract class DelegatingPoolAdapter extends FactoryDefaultsPoolAdapter {
.orElseGet(super::getSocketConnectTimeout);
}
@Override
public SocketFactory getSocketFactory() {
return Optional.ofNullable(getDelegate())
.map(Pool::getSocketFactory)
.orElseGet(super::getSocketFactory);
}
@Override
public int getStatisticInterval() {

View File

@@ -14,7 +14,6 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.client.support;
import java.net.InetSocketAddress;
@@ -23,6 +22,7 @@ import java.util.List;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.springframework.data.gemfire.GemfireUtils;
@@ -34,8 +34,11 @@ import org.springframework.data.gemfire.client.PoolAdapter;
* (e.g. freeConnectionTimeout, idleTimeout, etc).
*
* @author John Blum
* @see java.net.InetSocketAddress
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.query.Query
* @see org.springframework.data.gemfire.client.PoolAdapter
* @since 1.8.0
*/
@@ -117,6 +120,11 @@ public abstract class FactoryDefaultsPoolAdapter extends PoolAdapter {
return PoolFactory.DEFAULT_RETRY_ATTEMPTS;
}
@Override
public int getServerConnectionTimeout() {
return PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT;
}
@Override
public String getServerGroup() {
return PoolFactory.DEFAULT_SERVER_GROUP;
@@ -137,6 +145,11 @@ public abstract class FactoryDefaultsPoolAdapter extends PoolAdapter {
return PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT;
}
@Override
public SocketFactory getSocketFactory() {
return PoolFactory.DEFAULT_SOCKET_FACTORY;
}
@Override
public int getStatisticInterval() {
return PoolFactory.DEFAULT_STATISTIC_INTERVAL;

View File

@@ -57,7 +57,6 @@ import org.springframework.util.StringUtils;
* and {@link org.apache.geode.cache.client.ClientCache client caches}.
*
* @author John Blum
* @author Patrick Johnson
* @see java.lang.annotation.Annotation
* @see java.util.Properties
* @see org.apache.geode.cache.Cache
@@ -306,7 +305,7 @@ public abstract class AbstractCacheConfiguration extends AbstractAnnotationConfi
* @see #newCacheFactoryBean()
*/
protected <T extends CacheFactoryBean> T constructCacheFactoryBean() {
return configureCacheFactoryBean(this.newCacheFactoryBean());
return configureCacheFactoryBean(newCacheFactoryBean());
}
/**
@@ -356,7 +355,7 @@ public abstract class AbstractCacheConfiguration extends AbstractAnnotationConfi
return gemfireCache;
}
// TODO: REVIEW JAVADOC FROM HERE
// TODO: Review Javadoc from here
/**
* Determines whether this is a GemFire {@link org.apache.geode.cache.server.CacheServer} application,
@@ -599,7 +598,7 @@ public abstract class AbstractCacheConfiguration extends AbstractAnnotationConfi
}
public void add(Properties gemfireProperties) {
customGemFireProperties.add(gemfireProperties);
this.customGemFireProperties.add(gemfireProperties);
}
/**

View File

@@ -151,6 +151,11 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport
resolveProperty(poolProperty("retry-attempts"),
enablePoolAttributes.<Integer>getNumber("retryAttempts"))));
poolFactoryBean.addPropertyValue("serverConnectionTimeout",
resolveProperty(namedPoolProperty(poolName, "server-connection-timeout"),
resolveProperty(poolProperty("server-connection-timeout"),
enablePoolAttributes.<Integer>getNumber("serverConnectionTimeout"))));
poolFactoryBean.addPropertyValue("serverGroup",
resolveProperty(namedPoolProperty(poolName, "server-group"),
resolveProperty(poolProperty("server-group"),
@@ -166,6 +171,15 @@ public class AddPoolConfiguration extends AbstractAnnotationConfigSupport
resolveProperty(poolProperty("socket-connect-timeout"),
enablePoolAttributes.<Integer>getNumber("socketConnectTimeout"))));
String resolvedSocketFactoryBeanName =
resolveProperty(namedPoolProperty(poolName, "socket-factory-bean-name"),
resolveProperty(poolProperty("socket-factory-bean-name"),
enablePoolAttributes.getString("socketFactoryBeanName")));
Optional.ofNullable(resolvedSocketFactoryBeanName)
.filter(StringUtils::hasText)
.ifPresent(beanName -> poolFactoryBean.addPropertyReference("socketFactory", beanName));
poolFactoryBean.addPropertyValue("statisticInterval",
resolveProperty(namedPoolProperty(poolName, "statistic-interval"),
resolveProperty(poolProperty("statistic-interval"),

View File

@@ -23,9 +23,11 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.geode.cache.client.AllConnectionsInUseException;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.control.ResourceManager;
import org.springframework.beans.factory.BeanFactory;
@@ -40,11 +42,15 @@ import org.springframework.data.gemfire.support.GemfireBeanFactoryLocator;
*
* @author John Blum
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.control.ResourceManager
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.context.annotation.Import
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfiguration
* @see org.springframework.data.gemfire.support.GemfireBeanFactoryLocator
* @since 1.9.0
*/
@Target(ElementType.TYPE)
@@ -217,7 +223,7 @@ public @interface ClientCacheApplication {
/**
* Configures the name of this GemFire member in the cluster (distributed system).
*
* Defaults to {@literal SpringBasedCacheClientApplication}.
* Defaults to {@literal SpringBasedClientCacheApplication}.
*
* Use either the {@literal spring.data.gemfire.name} or the {@literal spring.data.gemfire.cache.name} property
* in {@literal application.properties}.
@@ -276,6 +282,24 @@ public @interface ClientCacheApplication {
*/
int retryAttempts() default PoolFactory.DEFAULT_RETRY_ATTEMPTS;
/**
* Configures the server connection timeout for the {@literal DEFAULT} {@literal Pool}.
*
* If the pool has a max connections setting, operations will block if there is no free connection for a specific
* server. The server connection timeout specifies how long those operations will block waiting for a free
* connection for a specific server before receiving an {@link AllConnectionsInUseException}. If max connections
* is not set this setting has no effect. This setting differs from {@link #freeConnectionTimeout()}, which sets
* the wait time for any server connection in the pool, whereas this setting sets the wait time for a free
* connection to a specific server.
*
* Defaults to {@link PoolFactory#DEFAULT_SERVER_CONNECTION_TIMEOUT}.
*
* Use either the {@literal spring.data.gemfire.pool.default.server-connection-timeout} property
* or the {@literal spring.data.gemfire.pool.server-connection-timeout} property
* in {@literal application.properties}.
*/
int serverConnectionTimeout() default PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT;
/**
* Configures the group that all servers in which this pool connects to must belong to.
*
@@ -319,6 +343,18 @@ public @interface ClientCacheApplication {
*/
int socketConnectTimeout() default PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT;
/**
* Configures the {@link SocketFactory} {@link String bean name} used by the {@literal DEFAULT} {@link Pool}
* to create connections to both Locators (if configured using {@link #locators()}) and Servers.
*
* Defaults to unset.
*
* Use either the {@literal spring.data.gemfire.pool.default.socket-factory-bean-name} property
* or the {@literal spring.data.gemfire.pool.socket-factory-bean-name} property
* in {@literal application.properties}.
*/
String socketFactoryBeanName() default "";
/**
* Configures how often to send client statistics to the server.
*

View File

@@ -25,8 +25,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.server.CacheServer;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -40,6 +43,7 @@ import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.config.support.ClientRegionPoolBeanFactoryPostProcessor;
import org.springframework.data.gemfire.support.ConnectionEndpoint;
import org.springframework.data.gemfire.support.ConnectionEndpointList;
import org.springframework.lang.NonNull;
import org.springframework.util.StringUtils;
/**
@@ -47,21 +51,24 @@ import org.springframework.util.StringUtils;
* a {@link org.apache.geode.cache.client.ClientCache} instance in a Spring application context.
*
* @author John Blum
* @see java.lang.annotation.Annotation
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.server.CacheServer
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.beans.factory.config.BeanDefinition
* @see org.springframework.beans.factory.support.BeanDefinitionBuilder
* @see org.springframework.context.annotation.Bean
* @see org.springframework.context.annotation.Configuration
* @see org.springframework.core.annotation.AnnotationAttributes
* @see org.springframework.core.type.AnnotationMetadata
* @see org.springframework.data.gemfire.CacheFactoryBean
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
* @see org.springframework.data.gemfire.config.annotation.AbstractCacheConfiguration
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer
* @see org.springframework.data.gemfire.config.support.ClientRegionPoolBeanFactoryPostProcessor
* @see org.springframework.data.gemfire.support.ConnectionEndpoint
* @see org.springframework.data.gemfire.support.ConnectionEndpointList
* @since 1.0.0
* @since 1.9.0
*/
@Configuration
@SuppressWarnings("unused")
@@ -72,7 +79,7 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
protected static final boolean DEFAULT_READY_FOR_EVENTS = false;
protected static final String DEFAULT_NAME = "SpringBasedCacheClientApplication";
protected static final String DEFAULT_NAME = "SpringBasedClientCacheApplication";
private boolean readyForEvents = DEFAULT_READY_FOR_EVENTS;
@@ -89,6 +96,7 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
private Integer minConnections;
private Integer readTimeout;
private Integer retryAttempts;
private Integer serverConnectionTimeout;
private Integer socketBufferSize;
private Integer socketConnectTimeout;
private Integer statisticsInterval;
@@ -107,6 +115,7 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
private String durableClientId;
private String serverGroup;
private String socketFactoryBeanName;
/**
* Bean declaration for a single, peer {@link ClientCache} instance.
@@ -138,10 +147,12 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
gemfireCache.setReadTimeout(getReadTimeout());
gemfireCache.setReadyForEvents(getReadyForEvents());
gemfireCache.setRetryAttempts(getRetryAttempts());
gemfireCache.setServerConnectionTimeout(getServerConnectionTimeout());
gemfireCache.setServerGroup(getServerGroup());
gemfireCache.setServers(getPoolServers());
gemfireCache.setSocketBufferSize(getSocketBufferSize());
gemfireCache.setSocketConnectTimeout(getSocketConnectTimeout());
gemfireCache.setSocketFactory(resolveSocketFactory());
gemfireCache.setStatisticsInterval(getStatisticsInterval());
gemfireCache.setSubscriptionAckInterval(getSubscriptionAckInterval());
gemfireCache.setSubscriptionEnabled(getSubscriptionEnabled());
@@ -152,7 +163,27 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
return gemfireCache;
}
@SuppressWarnings("all")
@NonNull SocketFactory resolveSocketFactory() {
BeanFactory beanFactory = getBeanFactory();
return Optional.ofNullable(getSocketFactoryBeanName())
.filter(StringUtils::hasText)
.filter(socketFactoryBeanName -> beanFactory.isTypeMatch(socketFactoryBeanName, SocketFactory.class))
.map(socketFactoryBeanName -> beanFactory.getBean(socketFactoryBeanName, SocketFactory.class))
.orElseGet(() -> {
String socketFactoryBeanName = getSocketFactoryBeanName();
if (StringUtils.hasText(socketFactoryBeanName) && beanFactory.containsBean(socketFactoryBeanName)) {
throw new BeanNotOfRequiredTypeException(socketFactoryBeanName, SocketFactory.class,
beanFactory.getType(socketFactoryBeanName));
}
return null;
});
}
private List<ClientCacheConfigurer> resolveClientCacheConfigurers() {
return Optional.ofNullable(this.clientCacheConfigurers)
@@ -178,7 +209,7 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
* Configures Spring container infrastructure components and beans used by Spring Data GemFire
* to enable Pivotal GemFire or Apache Geode to function properly inside a Spring context.
*
* This overridden method configures and registers additional Spring components and bean applicable to
* This overridden method configures and registers additional Spring components and beans applicable to
* {@link ClientCache ClientCaches}.
*
* @param importMetadata {@link AnnotationMetadata} containing annotation meta-data
@@ -289,6 +320,11 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
resolveProperty(poolProperty("retry-attempts"),
(Integer) clientCacheApplicationAttributes.get("retryAttempts"))));
setServerConnectionTimeout(
resolveProperty(namedPoolProperty("default", "server-connection-timeout"),
resolveProperty(poolProperty("server-connection-timeout"),
(Integer) clientCacheApplicationAttributes.get("serverConnectionTimeout"))));
setServerGroup(
resolveProperty(namedPoolProperty("default", "server-group"),
resolveProperty(poolProperty("server-group"),
@@ -304,6 +340,11 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
resolveProperty(poolProperty("socket-connect-timeout"),
(Integer) clientCacheApplicationAttributes.get("socketConnectTimeout"))));
setSocketFactoryBeanName(
resolveProperty(namedPoolProperty("default", "socket-factory-bean-name"),
resolveProperty(poolProperty("socket-factory-bean-name"),
(String) clientCacheApplicationAttributes.get("socketFactoryBeanName"))));
setStatisticsInterval(
resolveProperty(namedPoolProperty("default", "statistic-interval"),
resolveProperty(poolProperty("statistic-interval"),
@@ -410,6 +451,14 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
return ClientCacheApplication.class;
}
/**
* {@inheritDoc}
*/
@Override
protected BeanFactory getBeanFactory() {
return super.getBeanFactory();
}
void setDurableClientId(String durableClientId) {
this.durableClientId = durableClientId;
}
@@ -538,6 +587,14 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
return this.retryAttempts;
}
void setServerConnectionTimeout(Integer serverConnectionTimeout) {
this.serverConnectionTimeout = serverConnectionTimeout;
}
protected Integer getServerConnectionTimeout() {
return this.serverConnectionTimeout;
}
void setServerGroup(String serverGroup) {
this.serverGroup = serverGroup;
}
@@ -562,6 +619,14 @@ public class ClientCacheConfiguration extends AbstractCacheConfiguration {
return this.socketConnectTimeout;
}
void setSocketFactoryBeanName(String socketFactoryBeanName) {
this.socketFactoryBeanName = socketFactoryBeanName;
}
protected String getSocketFactoryBeanName() {
return this.socketFactoryBeanName;
}
void setStatisticsInterval(Integer statisticsInterval) {
this.statisticsInterval = statisticsInterval;
}

View File

@@ -23,8 +23,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.geode.cache.client.AllConnectionsInUseException;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.springframework.context.annotation.Import;
import org.springframework.data.gemfire.GemfireUtils;
@@ -198,6 +200,24 @@ public @interface EnablePool {
*/
int retryAttempts() default PoolFactory.DEFAULT_RETRY_ATTEMPTS;
/**
* Configures the server connection timeout for {@literal this} {@literal Pool}.
*
* If the pool has a max connections setting, operations will block if there is no free connection for a specific
* server. The server connection timeout specifies how long those operations will block waiting for a free
* connection for a specific server before receiving an {@link AllConnectionsInUseException}. If max connections
* is not set this setting has no effect. This setting differs from {@link #freeConnectionTimeout()}, which sets
* the wait time for any server connection in the pool, whereas this setting sets the wait time for a free
* connection to a specific server.
*
* Defaults to {@link PoolFactory#DEFAULT_SERVER_CONNECTION_TIMEOUT}.
*
* Use either the {@literal spring.data.gemfire.pool.<poolName>.server-connection-timeout} property
* or the {@literal spring.data.gemfire.pool.server-connection-timeout} property
* in {@literal application.properties}.
*/
int serverConnectionTimeout() default PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT;
/**
* Configures the group that all servers in which this pool connects to must belong to.
*
@@ -256,6 +276,18 @@ public @interface EnablePool {
*/
int socketConnectTimeout() default PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT;
/**
* Configures the {@link SocketFactory} {@link String bean name} used by {@literal this} {@link Pool}
* to create connections to both Locators (if configured using {@link #locators()}) and Servers.
*
* Defaults to unset.
*
* Use either the {@literal spring.data.gemfire.pool.<poolName>.socket-factory-bean-name} property
* or the {@literal spring.data.gemfire.pool.socket-factory-bean-name} property
* in {@literal application.properties}.
*/
String socketFactoryBeanName() default "";
/**
* Configures how often to send client statistics to the server.
*

View File

@@ -14,7 +14,6 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.config.annotation;
import java.lang.annotation.Documented;
@@ -24,6 +23,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.geode.cache.client.Pool;
import org.springframework.context.annotation.Import;
/**
@@ -31,6 +32,7 @@ import org.springframework.context.annotation.Import;
* to be defined and used in a GemFire client cache application configured with Spring (Data GemFire).
*
* @author John Blum
* @see org.apache.geode.cache.client.Pool
* @see org.springframework.data.gemfire.config.annotation.AddPoolsConfiguration
* @see org.springframework.data.gemfire.config.annotation.EnablePool
* @see org.springframework.data.gemfire.config.annotation.PoolConfigurer
@@ -45,7 +47,7 @@ import org.springframework.context.annotation.Import;
public @interface EnablePools {
/**
* Enables the definition of multiple GemFire {@link org.apache.geode.cache.client.Pool Pools}.
* Enables the definition of multiple GemFire {@link Pool Pools}.
*/
EnablePool[] pools() default {};

View File

@@ -16,16 +16,17 @@
package org.springframework.data.gemfire.client;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException;
@@ -33,10 +34,12 @@ import java.net.InetSocketAddress;
import java.util.Collections;
import org.junit.Test;
import org.mockito.InOrder;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.springframework.beans.factory.BeanFactory;
@@ -56,6 +59,7 @@ import org.springframework.data.util.ReflectionUtils;
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.apache.geode.cache.client.SocketFactory
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.data.gemfire.client.PoolFactoryBean
* @see org.springframework.data.gemfire.client.PoolResolver
@@ -73,7 +77,6 @@ public class PoolFactoryBeanUnitTests {
}
@Test
@SuppressWarnings("deprecation")
public void afterPropertiesSetCreatesPool() throws Exception {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
@@ -84,8 +87,10 @@ public class PoolFactoryBeanUnitTests {
PoolResolver mockPoolResolver = mock(PoolResolver.class);
when(mockPoolFactory.create(eq("GemFirePool"))).thenReturn(mockPool);
when(mockPoolResolver.resolve(anyString())).thenReturn(null);
SocketFactory mockSocketFactory = mock(SocketFactory.class);
doReturn(mockPool).when(mockPoolFactory).create(eq("GemFirePool"));
doReturn(null).when(mockPoolResolver).resolve(anyString());
PoolFactoryBean poolFactoryBean = spy(new PoolFactoryBean());
@@ -108,10 +113,12 @@ public class PoolFactoryBeanUnitTests {
poolFactoryBean.setPrSingleHopEnabled(true);
poolFactoryBean.setReadTimeout(30000);
poolFactoryBean.setRetryAttempts(10);
poolFactoryBean.setServerConnectionTimeout(10000);
poolFactoryBean.setServerGroup("TestServerGroup");
poolFactoryBean.setServers(Collections.singletonList(newConnectionEndpoint("localhost", 12345)));
poolFactoryBean.setSocketBufferSize(32768);
poolFactoryBean.setSocketConnectTimeout(5000);
poolFactoryBean.setSocketFactory(mockSocketFactory);
poolFactoryBean.setStatisticInterval(1000);
poolFactoryBean.setSubscriptionAckInterval(500);
poolFactoryBean.setSubscriptionEnabled(true);
@@ -135,9 +142,11 @@ public class PoolFactoryBeanUnitTests {
verify(mockPoolFactory, times(1)).setPRSingleHopEnabled(eq(true));
verify(mockPoolFactory, times(1)).setReadTimeout(eq(30000));
verify(mockPoolFactory, times(1)).setRetryAttempts(eq(10));
verify(mockPoolFactory, times(1)).setServerConnectionTimeout(eq(10000));
verify(mockPoolFactory, times(1)).setServerGroup(eq("TestServerGroup"));
verify(mockPoolFactory, times(1)).setSocketBufferSize(eq(32768));
verify(mockPoolFactory, times(1)).setSocketConnectTimeout(eq(5000));
verify(mockPoolFactory, times(1)).setSocketFactory(eq(mockSocketFactory));
verify(mockPoolFactory, times(1)).setStatisticInterval(eq(1000));
verify(mockPoolFactory, times(1)).setSubscriptionAckInterval(eq(500));
verify(mockPoolFactory, times(1)).setSubscriptionEnabled(eq(true));
@@ -150,7 +159,6 @@ public class PoolFactoryBeanUnitTests {
verify(mockPoolResolver, times(2)).resolve(eq("GemFirePool"));
}
@SuppressWarnings("all")
@Test(expected = IllegalArgumentException.class)
public void afterPropertiesSetWithUnspecifiedName() throws Exception {
@@ -184,8 +192,6 @@ public class PoolFactoryBeanUnitTests {
assertThat(poolFactoryBean.getBeanName()).isEqualTo("gemfirePool");
assertThat(poolFactoryBean.getName()).isEqualTo("TestPool");
assertThat(poolFactoryBean.getLocators()).isEmpty();
assertThat(poolFactoryBean.getServers()).isEmpty();
poolFactoryBean.afterPropertiesSet();
@@ -201,8 +207,6 @@ public class PoolFactoryBeanUnitTests {
assertThat(poolFactoryBean.getBeanName()).isEqualTo("swimPool");
assertThat(poolFactoryBean.getName()).isNull();
assertThat(poolFactoryBean.getLocators()).isEmpty();
assertThat(poolFactoryBean.getServers()).isEmpty();
poolFactoryBean.afterPropertiesSet();
@@ -214,7 +218,7 @@ public class PoolFactoryBeanUnitTests {
Pool mockPool = mock(Pool.class);
when(mockPool.isDestroyed()).thenReturn(false);
doReturn(false).when(mockPool).isDestroyed();
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
@@ -223,9 +227,14 @@ public class PoolFactoryBeanUnitTests {
assertThat(TestUtils.<Pool>readField("pool", poolFactoryBean)).isNull();
verify(mockPool, times(1)).isDestroyed();
verify(mockPool, times(1)).releaseThreadLocalConnection();
verify(mockPool, times(1)).destroy(eq(false));
InOrder order = inOrder(mockPool);
order.verify(mockPool, times(1)).isDestroyed();
order.verify(mockPool, times(1)).releaseThreadLocalConnection();
order.verify(mockPool, times(1)).destroy(eq(false));
order.verify(mockPool, times(1)).getName();
verifyNoMoreInteractions(mockPool);
}
@Test
@@ -239,13 +248,11 @@ public class PoolFactoryBeanUnitTests {
poolFactoryBean.setPool(mockPool);
poolFactoryBean.destroy();
verify(mockPool, never()).isDestroyed();
verify(mockPool, never()).releaseThreadLocalConnection();
verify(mockPool, never()).destroy(anyBoolean());
verifyNoInteractions(mockPool);
}
@Test
public void destroyUninitializedPool() throws Exception {
public void destroyUninitializedPool() {
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
@@ -254,10 +261,22 @@ public class PoolFactoryBeanUnitTests {
}
@Test
public void getObjectType() {
public void getObjectTypeEqualsPoolClass() {
assertThat(new PoolFactoryBean().getObjectType()).isEqualTo(Pool.class);
}
@Test
public void getObjectTypeEqualsPoolInstanceType() {
Pool mockPool = mock(Pool.class);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setPool(mockPool);
assertThat(poolFactoryBean.getObjectType()).isEqualTo(mockPool.getClass());
}
@Test
public void isSingleton() {
assertThat(new PoolFactoryBean().isSingleton()).isTrue();
@@ -350,6 +369,8 @@ public class PoolFactoryBeanUnitTests {
@Test
public void getPoolWhenPoolIsUnset() {
SocketFactory mockSocketFactory = mock(SocketFactory.class);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
poolFactoryBean.setFreeConnectionTimeout(5000);
@@ -363,10 +384,12 @@ public class PoolFactoryBeanUnitTests {
poolFactoryBean.setPrSingleHopEnabled(true);
poolFactoryBean.setReadTimeout(30000);
poolFactoryBean.setRetryAttempts(1);
poolFactoryBean.setServerConnectionTimeout(10000);
poolFactoryBean.setServerGroup("TestGroup");
poolFactoryBean.setServers(ArrayUtils.asArray(newConnectionEndpoint("boombox", 12480)));
poolFactoryBean.setSocketBufferSize(16384);
poolFactoryBean.setSocketConnectTimeout(5000);
poolFactoryBean.setSocketFactory(mockSocketFactory);
poolFactoryBean.setStatisticInterval(500);
poolFactoryBean.setSubscriptionAckInterval(200);
poolFactoryBean.setSubscriptionEnabled(true);
@@ -391,10 +414,12 @@ public class PoolFactoryBeanUnitTests {
assertThat(pool.getPRSingleHopEnabled()).isTrue();
assertThat(pool.getReadTimeout()).isEqualTo(30000);
assertThat(pool.getRetryAttempts()).isEqualTo(1);
assertThat(pool.getServerConnectionTimeout()).isEqualTo(10000);
assertThat(pool.getServerGroup()).isEqualTo("TestGroup");
assertThat(pool.getServers()).isEqualTo(Collections.singletonList(newSocketAddress("boombox", 12480)));
assertThat(pool.getSocketBufferSize()).isEqualTo(16384);
assertThat(pool.getSocketConnectTimeout()).isEqualTo(5000);
assertThat(pool.getSocketFactory()).isEqualTo(mockSocketFactory);
assertThat(pool.getStatisticInterval()).isEqualTo(500);
assertThat(pool.getSubscriptionAckInterval()).isEqualTo(200);
assertThat(pool.getSubscriptionEnabled()).isTrue();
@@ -500,7 +525,7 @@ public class PoolFactoryBeanUnitTests {
}
@Test
public void getPoolAndDestroyWithPool() throws Exception {
public void getPoolAndDestroyWithPool() {
Pool mockPool = mock(Pool.class);
@@ -522,9 +547,9 @@ public class PoolFactoryBeanUnitTests {
}
@Test
public void getPoolAndDestroyWithoutPool() throws Exception {
public void getPoolAndDestroyWithoutPool() {
PoolFactoryBean poolFactoryBean = spy(new PoolFactoryBean());;
PoolFactoryBean poolFactoryBean = spy(new PoolFactoryBean());
doThrow(newIllegalStateException("test")).when(poolFactoryBean).destroy();
@@ -593,4 +618,24 @@ public class PoolFactoryBeanUnitTests {
assertThat(poolFactoryBean.getPoolResolver()).isEqualTo(PoolFactoryBean.DEFAULT_POOL_RESOLVER);
}
@Test
public void setAndGetSocketFactory() {
SocketFactory mockSocketFactory = mock(SocketFactory.class);
PoolFactoryBean poolFactoryBean = new PoolFactoryBean();
assertThat(poolFactoryBean.getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
poolFactoryBean.setSocketFactory(mockSocketFactory);
assertThat(poolFactoryBean.getSocketFactory()).isEqualTo(mockSocketFactory);
poolFactoryBean.setSocketFactory(null);
assertThat(poolFactoryBean.getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
verifyNoInteractions(mockSocketFactory);
}
}

View File

@@ -14,19 +14,14 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.client.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import java.net.InetSocketAddress;
@@ -34,38 +29,38 @@ import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.query.QueryService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.springframework.data.gemfire.GemfireUtils;
/**
* Additional unit tests for {@link DefaultableDelegatingPoolAdapter} testing defaults.
* Unit Tests for {@link DefaultableDelegatingPoolAdapter}.
*
* @author John Blum
* @see org.junit.Rule
* @see java.net.InetSocketAddress
* @see org.junit.Test
* @see org.junit.rules.ExpectedException
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.junit.MockitoJUnitRunner
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.query.QueryService
* @see org.springframework.data.gemfire.client.support.DefaultableDelegatingPoolAdapter
* @since 1.8.0
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultableDelegatingPoolAdapterTest {
@Rule
public ExpectedException exception = ExpectedException.none();
private DefaultableDelegatingPoolAdapter poolAdapter;
@Mock
@@ -75,6 +70,10 @@ public class DefaultableDelegatingPoolAdapterTest {
private QueryService mockQueryService;
@Mock
private SocketFactory mockSocketFactory;
@Mock
@SuppressWarnings("rawtypes")
private Supplier mockSupplier;
private static InetSocketAddress newSocketAddress(String host, int port) {
@@ -98,11 +97,13 @@ public class DefaultableDelegatingPoolAdapterTest {
when(this.mockPool.getQueryService()).thenReturn(null);
when(this.mockPool.getReadTimeout()).thenReturn(30000);
when(this.mockPool.getRetryAttempts()).thenReturn(1);
when(this.mockPool.getServerConnectionTimeout()).thenReturn(10000);
when(this.mockPool.getServerGroup()).thenReturn("TestGroup");
when(this.mockPool.getServers()).thenReturn(Collections.singletonList(
newSocketAddress("localhost", GemfireUtils.DEFAULT_CACHE_SERVER_PORT)));
when(this.mockPool.getSocketBufferSize()).thenReturn(16384);
when(this.mockPool.getSocketConnectTimeout()).thenReturn(5000);
when(this.mockPool.getSocketFactory()).thenReturn(PoolFactory.DEFAULT_SOCKET_FACTORY);
when(this.mockPool.getStatisticInterval()).thenReturn(1000);
when(this.mockPool.getSubscriptionAckInterval()).thenReturn(200);
when(this.mockPool.getSubscriptionEnabled()).thenReturn(true);
@@ -110,45 +111,51 @@ public class DefaultableDelegatingPoolAdapterTest {
when(this.mockPool.getSubscriptionRedundancy()).thenReturn(2);
when(this.mockPool.getSubscriptionTimeoutMultiplier()).thenReturn(3);
when(this.mockPool.getThreadLocalConnections()).thenReturn(false);
setupPoolAdapter();
}
//@Before
@Before
public void setupPoolAdapter() {
this.poolAdapter = DefaultableDelegatingPoolAdapter.from(this.mockPool);
}
@Test
public void fromMockPoolAsDelegate() {
assertThat(this.poolAdapter.getDelegate(), is(sameInstance(this.mockPool)));
public void fromMockPool() {
assertThat(this.poolAdapter.getDelegate()).isSameAs(this.mockPool);
}
@Test
public void fromNullAsDelegate() {
@Test(expected = IllegalArgumentException.class)
public void fromNull() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Pool delegate must not be null");
try {
DefaultableDelegatingPoolAdapter.from(null);
}
catch (IllegalArgumentException expected) {
DefaultableDelegatingPoolAdapter.from(null);
assertThat(expected).hasMessage("Pool delegate must not be null");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void prefersDefaultsToPoolDelegateAndIsMutable() {
assertThat(this.poolAdapter.getPreference(), is(equalTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_POOL)));
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.getPreference()).isEqualTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_POOL);
assertThat(this.poolAdapter.prefersDefault()).isFalse();
assertThat(this.poolAdapter.prefersPool()).isTrue();
this.poolAdapter.preferDefault();
assertThat(this.poolAdapter.getPreference(), is(equalTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_DEFAULT)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.getPreference()).isEqualTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_DEFAULT);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.prefersPool()).isFalse();
this.poolAdapter.preferPool();
assertThat(this.poolAdapter.getPreference(), is(equalTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_POOL)));
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.getPreference()).isEqualTo(DefaultableDelegatingPoolAdapter.Preference.PREFER_POOL);
assertThat(this.poolAdapter.prefersDefault()).isFalse();
assertThat(this.poolAdapter.prefersPool()).isTrue();
}
@Test
@@ -157,24 +164,24 @@ public class DefaultableDelegatingPoolAdapterTest {
this.poolAdapter = this.poolAdapter.preferDefault();
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier),
is(equalTo("default")));
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier)).isEqualTo("default");
verifyZeroInteractions(this.mockSupplier);
verifyNoInteractions(this.mockSupplier);
}
@Test
@SuppressWarnings("unchecked")
public void defaultIfNullWhenPrefersDefaultUsesPoolValueWhenDefaultIsNull() {
this.poolAdapter = this.poolAdapter.preferDefault();
when(this.mockSupplier.get()).thenReturn("pool");
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.defaultIfNull(null, this.mockSupplier),
is(equalTo("pool")));
this.poolAdapter = this.poolAdapter.preferDefault();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.defaultIfNull(null, this.mockSupplier)).isEqualTo("pool");
verify(this.mockSupplier, times(1)).get();
}
@@ -183,13 +190,13 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfNullWhenPrefersPoolUsesPoolValue() {
this.poolAdapter = this.poolAdapter.preferPool();
when(mockSupplier.get()).thenReturn("pool");
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier),
is(equalTo("pool")));
this.poolAdapter = this.poolAdapter.preferPool();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersPool()).isTrue();
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier)).isEqualTo("pool");
verify(this.mockSupplier, times(1)).get();
}
@@ -198,13 +205,13 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfNullWhenPrefersPoolUsesDefaultWhenPoolValueIsNull() {
this.poolAdapter = this.poolAdapter.preferPool();
when(this.mockSupplier.get()).thenReturn(null);
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier),
is(equalTo("default")));
this.poolAdapter = this.poolAdapter.preferPool();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersPool()).isTrue();
assertThat(this.poolAdapter.defaultIfNull("default", this.mockSupplier)).isEqualTo("default");
verify(this.mockSupplier, times(1)).get();
}
@@ -213,28 +220,30 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersDefaultUsesDefault() {
this.poolAdapter = this.poolAdapter.preferDefault();
List<Object> defaultList = Collections.singletonList("default");
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier), is(equalTo(defaultList)));
this.poolAdapter = this.poolAdapter.preferDefault();
verifyZeroInteractions(this.mockSupplier);
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier)).isEqualTo(defaultList);
verifyNoInteractions(this.mockSupplier);
}
@Test
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersDefaultUsesPoolValueWhenDefaultIsNull() {
this.poolAdapter = this.poolAdapter.preferDefault();
List<Object> poolList = Collections.singletonList("pool");
when(this.mockSupplier.get()).thenReturn(poolList);
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(null, this.mockSupplier), is(equalTo(poolList)));
this.poolAdapter = this.poolAdapter.preferDefault();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(null, this.mockSupplier)).isEqualTo(poolList);
verify(this.mockSupplier, times(1)).get();
}
@@ -243,15 +252,15 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersDefaultUsesPoolValueWhenDefaultIsEmpty() {
this.poolAdapter = this.poolAdapter.preferDefault();
List<Object> poolList = Collections.singletonList("pool");
when(this.mockSupplier.get()).thenReturn(poolList);
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(Collections.emptyList(), this.mockSupplier),
is(equalTo(poolList)));
this.poolAdapter = this.poolAdapter.preferDefault();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(Collections.emptyList(), this.mockSupplier)).isEqualTo(poolList);
verify(this.mockSupplier, times(1)).get();
}
@@ -260,15 +269,16 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersPoolUsesPoolValue() {
this.poolAdapter = this.poolAdapter.preferPool();
List<Object> poolList = Collections.singletonList("pool");
when(this.mockSupplier.get()).thenReturn(poolList);
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(Collections.<Object>singletonList("default"), this.mockSupplier),
is(equalTo(poolList)));
this.poolAdapter = this.poolAdapter.preferPool();
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersPool()).isTrue();
assertThat((List<Object>) this.poolAdapter
.defaultIfEmpty(Collections.<Object>singletonList("default"), this.mockSupplier)).isEqualTo(poolList);
verify(this.mockSupplier, times(1)).get();
}
@@ -277,15 +287,16 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersPoolUsesDefaultWhenPoolValueIsNull() {
this.poolAdapter = this.poolAdapter.preferPool();
List<Object> defaultList = Collections.singletonList("default");
when(this.mockSupplier.get()).thenReturn(null);
List<Object> defaultList = Collections.singletonList("default");
this.poolAdapter = this.poolAdapter.preferPool();
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier),
is(equalTo(defaultList)));
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersPool()).isTrue();
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier))
.isEqualTo(defaultList);
verify(this.mockSupplier, times(1)).get();
}
@@ -294,15 +305,16 @@ public class DefaultableDelegatingPoolAdapterTest {
@SuppressWarnings("unchecked")
public void defaultIfEmptyWhenPrefersPoolUsesDefaultWhenPoolValueIsEmpty() {
assertThat(this.poolAdapter.preferPool(), is(sameInstance(this.poolAdapter)));
List<Object> defaultList = Collections.singletonList("default");
when(this.mockSupplier.get()).thenReturn(Collections.emptyList());
List<Object> defaultList = Collections.singletonList("default");
this.poolAdapter = this.poolAdapter.preferPool();
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier),
is(equalTo(defaultList)));
assertThat(this.poolAdapter).isNotNull();
assertThat(this.poolAdapter.prefersPool()).isTrue();
assertThat((List<Object>) this.poolAdapter.defaultIfEmpty(defaultList, this.mockSupplier))
.isEqualTo(defaultList);
verify(this.mockSupplier, times(1)).get();
}
@@ -310,38 +322,40 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterPreferringDefaultsUsesNonNullDefaults() {
assertThat(this.poolAdapter.preferDefault(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.preferDefault()).isSameAs(this.poolAdapter);
List<InetSocketAddress> defaultLocator = Collections.singletonList(newSocketAddress("boombox", 21668));
List<InetSocketAddress> defaultServer = Collections.singletonList(newSocketAddress("skullbox", 42424));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.getFreeConnectionTimeout(10000), is(equalTo(10000)));
assertThat(this.poolAdapter.getIdleTimeout(300000L), is(equalTo(300000L)));
assertThat(this.poolAdapter.getLoadConditioningInterval(60000), is(equalTo(60000)));
assertThat(this.poolAdapter.getLocators(defaultLocator), is(equalTo(defaultLocator)));
assertThat(this.poolAdapter.getMaxConnections(100), is(equalTo(100)));
assertThat(this.poolAdapter.getMinConnections(10), is(equalTo(10)));
assertThat(this.poolAdapter.getMultiuserAuthentication(false), is(equalTo(false)));
assertThat(this.poolAdapter.getName(), is(equalTo("TestPool")));
assertThat(this.poolAdapter.getPendingEventCount(), is(equalTo(2)));
assertThat(this.poolAdapter.getPingInterval(20000L), is(equalTo(20000L)));
assertThat(this.poolAdapter.getPRSingleHopEnabled(false), is(equalTo(false)));
assertThat(this.poolAdapter.getQueryService(this.mockQueryService), is(equalTo(this.mockQueryService)));
assertThat(this.poolAdapter.getReadTimeout(20000), is(equalTo(20000)));
assertThat(this.poolAdapter.getRetryAttempts(2), is(equalTo(2)));
assertThat(this.poolAdapter.getServerGroup("MockGroup"), is(equalTo("MockGroup")));
assertThat(this.poolAdapter.getServers(defaultServer), is(equalTo(defaultServer)));
assertThat(this.poolAdapter.getSocketBufferSize(8192), is(equalTo(8192)));
assertThat(this.poolAdapter.getSocketConnectTimeout(10000), is(equalTo(10000)));
assertThat(this.poolAdapter.getStatisticInterval(2000), is(equalTo(2000)));
assertThat(this.poolAdapter.getSubscriptionAckInterval(50), is(equalTo(50)));
assertThat(this.poolAdapter.getSubscriptionEnabled(false), is(equalTo(false)));
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(15000), is(equalTo(15000)));
assertThat(this.poolAdapter.getSubscriptionRedundancy(1), is(equalTo(1)));
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(1), is(equalTo(1)));
assertThat(this.poolAdapter.getThreadLocalConnections(true), is(equalTo(true)));
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.getFreeConnectionTimeout(10000)).isEqualTo(10000);
assertThat(this.poolAdapter.getIdleTimeout(300000L)).isEqualTo(300000L);
assertThat(this.poolAdapter.getLoadConditioningInterval(60000)).isEqualTo(60000);
assertThat(this.poolAdapter.getLocators(defaultLocator)).isEqualTo(defaultLocator);
assertThat(this.poolAdapter.getMaxConnections(100)).isEqualTo(100);
assertThat(this.poolAdapter.getMinConnections(10)).isEqualTo(10);
assertThat(this.poolAdapter.getMultiuserAuthentication(false)).isFalse();
assertThat(this.poolAdapter.getName()).isEqualTo("TestPool");
assertThat(this.poolAdapter.getPendingEventCount()).isEqualTo(2);
assertThat(this.poolAdapter.getPingInterval(20000L)).isEqualTo(20000L);
assertThat(this.poolAdapter.getPRSingleHopEnabled(false)).isFalse();
assertThat(this.poolAdapter.getQueryService(this.mockQueryService)).isEqualTo(this.mockQueryService);
assertThat(this.poolAdapter.getReadTimeout(20000)).isEqualTo(20000);
assertThat(this.poolAdapter.getRetryAttempts(2)).isEqualTo(2);
assertThat(this.poolAdapter.getServerConnectionTimeout(123)).isEqualTo(123);
assertThat(this.poolAdapter.getServerGroup("MockGroup")).isEqualTo("MockGroup");
assertThat(this.poolAdapter.getServers(defaultServer)).isEqualTo(defaultServer);
assertThat(this.poolAdapter.getSocketBufferSize(8192)).isEqualTo(8192);
assertThat(this.poolAdapter.getSocketConnectTimeout(10000)).isEqualTo(10000);
assertThat(this.poolAdapter.getSocketFactory(this.mockSocketFactory)).isEqualTo(this.mockSocketFactory);
assertThat(this.poolAdapter.getStatisticInterval(2000)).isEqualTo(2000);
assertThat(this.poolAdapter.getSubscriptionAckInterval(50)).isEqualTo(50);
assertThat(this.poolAdapter.getSubscriptionEnabled(false)).isFalse();
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(15000)).isEqualTo(15000);
assertThat(this.poolAdapter.getSubscriptionRedundancy(1)).isEqualTo(1);
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(1)).isEqualTo(1);
assertThat(this.poolAdapter.getThreadLocalConnections(true)).isTrue();
verify(this.mockPool, times(1)).getName();
verify(this.mockPool, times(1)).getPendingEventCount();
@@ -351,38 +365,40 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterPreferringDefaultsUsesPoolValuesWhenSomeDefaultValuesAreNull() {
assertThat(this.poolAdapter.preferDefault(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.preferDefault()).isSameAs(this.poolAdapter);
List<InetSocketAddress> defaultLocator = Collections.singletonList(newSocketAddress("boombox", 21668));
List<InetSocketAddress> poolServer = Collections.singletonList(newSocketAddress("localhost", 40404));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.getFreeConnectionTimeout(null), is(equalTo(5000)));
assertThat(this.poolAdapter.getIdleTimeout(null), is(equalTo(120000L)));
assertThat(this.poolAdapter.getLoadConditioningInterval(60000), is(equalTo(60000)));
assertThat(this.poolAdapter.getLocators(defaultLocator), is(equalTo(defaultLocator)));
assertThat(this.poolAdapter.getMaxConnections(null), is(equalTo(500)));
assertThat(this.poolAdapter.getMinConnections(50), is(equalTo(50)));
assertThat(this.poolAdapter.getMultiuserAuthentication(null), is(equalTo(true)));
assertThat(this.poolAdapter.getName(), is(equalTo("TestPool")));
assertThat(this.poolAdapter.getPendingEventCount(), is(equalTo(2)));
assertThat(this.poolAdapter.getPingInterval(null), is(equalTo(15000L)));
assertThat(this.poolAdapter.getPRSingleHopEnabled(true), is(equalTo(true)));
assertThat(this.poolAdapter.getQueryService(null), is(nullValue()));
assertThat(this.poolAdapter.getReadTimeout(20000), is(equalTo(20000)));
assertThat(this.poolAdapter.getRetryAttempts(null), is(equalTo(1)));
assertThat(this.poolAdapter.getServerGroup("MockGroup"), is(equalTo("MockGroup")));
assertThat(this.poolAdapter.getServers(null), is(equalTo(poolServer)));
assertThat(this.poolAdapter.getSocketBufferSize(32768), is(equalTo(32768)));
assertThat(this.poolAdapter.getSocketConnectTimeout(null), is(equalTo(5000)));
assertThat(this.poolAdapter.getStatisticInterval(null), is(equalTo(1000)));
assertThat(this.poolAdapter.getSubscriptionAckInterval(50), is(equalTo(50)));
assertThat(this.poolAdapter.getSubscriptionEnabled(true), is(equalTo(true)));
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(null), is(equalTo(20000)));
assertThat(this.poolAdapter.getSubscriptionRedundancy(1), is(equalTo(1)));
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(null), is(equalTo(3)));
assertThat(this.poolAdapter.getThreadLocalConnections(null), is(equalTo(false)));
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.getFreeConnectionTimeout(null)).isEqualTo(5000);
assertThat(this.poolAdapter.getIdleTimeout(null)).isEqualTo(120000L);
assertThat(this.poolAdapter.getLoadConditioningInterval(60000)).isEqualTo(60000);
assertThat(this.poolAdapter.getLocators(defaultLocator)).isEqualTo(defaultLocator);
assertThat(this.poolAdapter.getMaxConnections(null)).isEqualTo(500);
assertThat(this.poolAdapter.getMinConnections(50)).isEqualTo(50);
assertThat(this.poolAdapter.getMultiuserAuthentication(null)).isTrue();
assertThat(this.poolAdapter.getName()).isEqualTo("TestPool");
assertThat(this.poolAdapter.getPendingEventCount()).isEqualTo(2);
assertThat(this.poolAdapter.getPingInterval(null)).isEqualTo(15000L);
assertThat(this.poolAdapter.getPRSingleHopEnabled(true)).isTrue();
assertThat(this.poolAdapter.getQueryService(null)).isNull();
assertThat(this.poolAdapter.getReadTimeout(20000)).isEqualTo(20000);
assertThat(this.poolAdapter.getRetryAttempts(null)).isEqualTo(1);
assertThat(this.poolAdapter.getServerConnectionTimeout(null)).isEqualTo(10000);
assertThat(this.poolAdapter.getServerGroup("MockGroup")).isEqualTo("MockGroup");
assertThat(this.poolAdapter.getServers(null)).isEqualTo(poolServer);
assertThat(this.poolAdapter.getSocketBufferSize(32768)).isEqualTo(32768);
assertThat(this.poolAdapter.getSocketConnectTimeout(null)).isEqualTo(5000);
assertThat(this.poolAdapter.getSocketFactory(null)).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(this.poolAdapter.getStatisticInterval(null)).isEqualTo(1000);
assertThat(this.poolAdapter.getSubscriptionAckInterval(50)).isEqualTo(50);
assertThat(this.poolAdapter.getSubscriptionEnabled(true)).isTrue();
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(null)).isEqualTo(20000);
assertThat(this.poolAdapter.getSubscriptionRedundancy(1)).isEqualTo(1);
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(null)).isEqualTo(3);
assertThat(this.poolAdapter.getThreadLocalConnections(null)).isFalse();
verify(this.mockPool, times(1)).getFreeConnectionTimeout();
verify(this.mockPool, times(1)).getIdleTimeout();
@@ -393,8 +409,10 @@ public class DefaultableDelegatingPoolAdapterTest {
verify(this.mockPool, times(1)).getPingInterval();
verify(this.mockPool, times(1)).getQueryService();
verify(this.mockPool, times(1)).getRetryAttempts();
verify(this.mockPool, times(1)).getServerConnectionTimeout();
verify(this.mockPool, times(1)).getServers();
verify(this.mockPool, times(1)).getSocketConnectTimeout();
verify(this.mockPool, times(1)).getSocketFactory();
verify(this.mockPool, times(1)).getStatisticInterval();
verify(this.mockPool, times(1)).getSubscriptionMessageTrackingTimeout();
verify(this.mockPool, times(1)).getSubscriptionTimeoutMultiplier();
@@ -405,37 +423,39 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterPreferringDefaultsUsesPoolValuesExclusivelyWhenAllDefaultValuesAreNull() {
assertThat(this.poolAdapter.preferDefault(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.preferDefault()).isSameAs(this.poolAdapter);
List<InetSocketAddress> poolServer = Collections.singletonList(newSocketAddress("localhost", 40404));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.getFreeConnectionTimeout(null), is(equalTo(5000)));
assertThat(this.poolAdapter.getIdleTimeout(null), is(equalTo(120000L)));
assertThat(this.poolAdapter.getLoadConditioningInterval(null), is(equalTo(300000)));
assertThat(this.poolAdapter.getLocators(null), is(equalTo(Collections.<InetSocketAddress>emptyList())));
assertThat(this.poolAdapter.getMaxConnections(null), is(equalTo(500)));
assertThat(this.poolAdapter.getMinConnections(null), is(equalTo(50)));
assertThat(this.poolAdapter.getMultiuserAuthentication(null), is(equalTo(true)));
assertThat(this.poolAdapter.getName(), is(equalTo("TestPool")));
assertThat(this.poolAdapter.getPendingEventCount(), is(equalTo(2)));
assertThat(this.poolAdapter.getPingInterval(null), is(equalTo(15000L)));
assertThat(this.poolAdapter.getPRSingleHopEnabled(null), is(equalTo(true)));
assertThat(this.poolAdapter.getQueryService(null), is(nullValue()));
assertThat(this.poolAdapter.getReadTimeout(null), is(equalTo(30000)));
assertThat(this.poolAdapter.getRetryAttempts(null), is(equalTo(1)));
assertThat(this.poolAdapter.getServerGroup(null), is(equalTo("TestGroup")));
assertThat(this.poolAdapter.getServers(null), is(equalTo(poolServer)));
assertThat(this.poolAdapter.getSocketBufferSize(null), is(equalTo(16384)));
assertThat(this.poolAdapter.getSocketConnectTimeout(null), is(equalTo(5000)));
assertThat(this.poolAdapter.getStatisticInterval(null), is(equalTo(1000)));
assertThat(this.poolAdapter.getSubscriptionAckInterval(null), is(equalTo(200)));
assertThat(this.poolAdapter.getSubscriptionEnabled(null), is(equalTo(true)));
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(null), is(equalTo(20000)));
assertThat(this.poolAdapter.getSubscriptionRedundancy(null), is(equalTo(2)));
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(null), is(equalTo(3)));
assertThat(this.poolAdapter.getThreadLocalConnections(null), is(equalTo(false)));
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
assertThat(this.poolAdapter.getFreeConnectionTimeout(null)).isEqualTo(5000);
assertThat(this.poolAdapter.getIdleTimeout(null)).isEqualTo(120000L);
assertThat(this.poolAdapter.getLoadConditioningInterval(null)).isEqualTo(300000);
assertThat(this.poolAdapter.getLocators(null)).isEqualTo(Collections.<InetSocketAddress>emptyList());
assertThat(this.poolAdapter.getMaxConnections(null)).isEqualTo(500);
assertThat(this.poolAdapter.getMinConnections(null)).isEqualTo(50);
assertThat(this.poolAdapter.getMultiuserAuthentication(null)).isTrue();
assertThat(this.poolAdapter.getName()).isEqualTo("TestPool");
assertThat(this.poolAdapter.getPendingEventCount()).isEqualTo(2);
assertThat(this.poolAdapter.getPingInterval(null)).isEqualTo(15000L);
assertThat(this.poolAdapter.getPRSingleHopEnabled(null)).isTrue();
assertThat(this.poolAdapter.getQueryService(null)).isNull();
assertThat(this.poolAdapter.getReadTimeout(null)).isEqualTo(30000);
assertThat(this.poolAdapter.getRetryAttempts(null)).isEqualTo(1);
assertThat(this.poolAdapter.getServerConnectionTimeout(null)).isEqualTo(10000);
assertThat(this.poolAdapter.getServerGroup(null)).isEqualTo("TestGroup");
assertThat(this.poolAdapter.getServers(null)).isEqualTo(poolServer);
assertThat(this.poolAdapter.getSocketBufferSize(null)).isEqualTo(16384);
assertThat(this.poolAdapter.getSocketConnectTimeout(null)).isEqualTo(5000);
assertThat(this.poolAdapter.getSocketFactory(null)).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(this.poolAdapter.getStatisticInterval(null)).isEqualTo(1000);
assertThat(this.poolAdapter.getSubscriptionAckInterval(null)).isEqualTo(200);
assertThat(this.poolAdapter.getSubscriptionEnabled(null)).isTrue();
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(null)).isEqualTo(20000);
assertThat(this.poolAdapter.getSubscriptionRedundancy(null)).isEqualTo(2);
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(null)).isEqualTo(3);
assertThat(this.poolAdapter.getThreadLocalConnections(null)).isFalse();
verify(this.mockPool, times(1)).getFreeConnectionTimeout();
verify(this.mockPool, times(1)).getIdleTimeout();
@@ -451,10 +471,12 @@ public class DefaultableDelegatingPoolAdapterTest {
verify(this.mockPool, times(1)).getQueryService();
verify(this.mockPool, times(1)).getReadTimeout();
verify(this.mockPool, times(1)).getRetryAttempts();
verify(this.mockPool, times(1)).getServerConnectionTimeout();
verify(this.mockPool, times(1)).getServerGroup();
verify(this.mockPool, times(1)).getServers();
verify(this.mockPool, times(1)).getSocketBufferSize();
verify(this.mockPool, times(1)).getSocketConnectTimeout();
verify(this.mockPool, times(1)).getSocketFactory();
verify(this.mockPool, times(1)).getStatisticInterval();
verify(this.mockPool, times(1)).getSubscriptionAckInterval();
verify(this.mockPool, times(1)).getSubscriptionEnabled();
@@ -468,36 +490,39 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterPreferringPoolUsesUseNonNullPoolValues() {
assertThat(this.poolAdapter.preferPool(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.preferPool()).isSameAs(this.poolAdapter);
List<InetSocketAddress> defaultServer = Collections.singletonList(newSocketAddress("jambox", 12480));
List<InetSocketAddress> poolServer = Collections.singletonList(newSocketAddress("localhost", 40404));
assertThat(this.poolAdapter.getFreeConnectionTimeout(15000), is(equalTo(5000)));
assertThat(this.poolAdapter.getIdleTimeout(60000L), is(equalTo(120000L)));
assertThat(this.poolAdapter.getLoadConditioningInterval(180000), is(equalTo(300000)));
assertThat(this.poolAdapter.getLocators(Collections.emptyList()), is(equalTo(Collections.<InetSocketAddress>emptyList())));
assertThat(this.poolAdapter.getMaxConnections(999), is(equalTo(500)));
assertThat(this.poolAdapter.getMinConnections(99), is(equalTo(50)));
assertThat(this.poolAdapter.getMultiuserAuthentication(false), is(equalTo(true)));
assertThat(this.poolAdapter.getName(), is(equalTo("TestPool")));
assertThat(this.poolAdapter.getPendingEventCount(), is(equalTo(2)));
assertThat(this.poolAdapter.getPingInterval(20000L), is(equalTo(15000L)));
assertThat(this.poolAdapter.getPRSingleHopEnabled(false), is(equalTo(true)));
assertThat(this.poolAdapter.getQueryService(null), is(nullValue()));
assertThat(this.poolAdapter.getReadTimeout(20000), is(equalTo(30000)));
assertThat(this.poolAdapter.getRetryAttempts(4), is(equalTo(1)));
assertThat(this.poolAdapter.getServerGroup("MockGroup"), is(equalTo("TestGroup")));
assertThat(this.poolAdapter.getServers(defaultServer), is(equalTo(poolServer)));
assertThat(this.poolAdapter.getSocketBufferSize(8192), is(equalTo(16384)));
assertThat(this.poolAdapter.getSocketConnectTimeout(8192), is(equalTo(5000)));
assertThat(this.poolAdapter.getStatisticInterval(2000), is(equalTo(1000)));
assertThat(this.poolAdapter.getSubscriptionAckInterval(50), is(equalTo(200)));
assertThat(this.poolAdapter.getSubscriptionEnabled(false), is(equalTo(true)));
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(30000), is(equalTo(20000)));
assertThat(this.poolAdapter.getSubscriptionRedundancy(1), is(equalTo(2)));
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(2), is(equalTo(3)));
assertThat(this.poolAdapter.getThreadLocalConnections(true), is(equalTo(false)));
assertThat(this.poolAdapter.getFreeConnectionTimeout(15000)).isEqualTo(5000);
assertThat(this.poolAdapter.getIdleTimeout(60000L)).isEqualTo(120000L);
assertThat(this.poolAdapter.getLoadConditioningInterval(180000)).isEqualTo(300000);
assertThat(this.poolAdapter.getLocators(Collections.emptyList()))
.isEqualTo(Collections.<InetSocketAddress>emptyList());
assertThat(this.poolAdapter.getMaxConnections(999)).isEqualTo(500);
assertThat(this.poolAdapter.getMinConnections(99)).isEqualTo(50);
assertThat(this.poolAdapter.getMultiuserAuthentication(false)).isTrue();
assertThat(this.poolAdapter.getName()).isEqualTo("TestPool");
assertThat(this.poolAdapter.getPendingEventCount()).isEqualTo(2);
assertThat(this.poolAdapter.getPingInterval(20000L)).isEqualTo(15000L);
assertThat(this.poolAdapter.getPRSingleHopEnabled(false)).isTrue();
assertThat(this.poolAdapter.getQueryService(null)).isNull();
assertThat(this.poolAdapter.getReadTimeout(20000)).isEqualTo(30000);
assertThat(this.poolAdapter.getRetryAttempts(4)).isEqualTo(1);
assertThat(this.poolAdapter.getServerConnectionTimeout(12345)).isEqualTo(10000);
assertThat(this.poolAdapter.getServerGroup("MockGroup")).isEqualTo("TestGroup");
assertThat(this.poolAdapter.getServers(defaultServer)).isEqualTo(poolServer);
assertThat(this.poolAdapter.getSocketBufferSize(8192)).isEqualTo(16384);
assertThat(this.poolAdapter.getSocketConnectTimeout(8192)).isEqualTo(5000);
assertThat(this.poolAdapter.getSocketFactory(this.mockSocketFactory)).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(this.poolAdapter.getStatisticInterval(2000)).isEqualTo(1000);
assertThat(this.poolAdapter.getSubscriptionAckInterval(50)).isEqualTo(200);
assertThat(this.poolAdapter.getSubscriptionEnabled(false)).isTrue();
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(30000)).isEqualTo(20000);
assertThat(this.poolAdapter.getSubscriptionRedundancy(1)).isEqualTo(2);
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(2)).isEqualTo(3);
assertThat(this.poolAdapter.getThreadLocalConnections(true)).isFalse();
verify(this.mockPool, times(1)).getFreeConnectionTimeout();
verify(this.mockPool, times(1)).getIdleTimeout();
@@ -513,10 +538,12 @@ public class DefaultableDelegatingPoolAdapterTest {
verify(this.mockPool, times(1)).getQueryService();
verify(this.mockPool, times(1)).getReadTimeout();
verify(this.mockPool, times(1)).getRetryAttempts();
verify(this.mockPool, times(1)).getServerConnectionTimeout();
verify(this.mockPool, times(1)).getServerGroup();
verify(this.mockPool, times(1)).getServers();
verify(this.mockPool, times(1)).getSocketBufferSize();
verify(this.mockPool, times(1)).getSocketConnectTimeout();
verify(this.mockPool, times(1)).getSocketFactory();
verify(this.mockPool, times(1)).getStatisticInterval();
verify(this.mockPool, times(1)).getSubscriptionAckInterval();
verify(this.mockPool, times(1)).getSubscriptionEnabled();
@@ -530,15 +557,15 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterDestroyUsesPoolRegardlessOfPreference() {
assertThat(this.poolAdapter.preferDefault(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.preferDefault()).isSameAs(this.poolAdapter);
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
this.poolAdapter.destroy();
assertThat(this.poolAdapter.preferPool(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.preferPool()).isSameAs(this.poolAdapter);
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersPool()).isTrue();
this.poolAdapter.destroy(true);
@@ -549,15 +576,15 @@ public class DefaultableDelegatingPoolAdapterTest {
@Test
public void poolAdapterReleaseThreadLocalConnections() {
assertThat(this.poolAdapter.preferDefault(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersDefault(), is(true));
assertThat(this.poolAdapter.preferDefault()).isSameAs(this.poolAdapter);
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersDefault()).isTrue();
this.poolAdapter.releaseThreadLocalConnection();
assertThat(this.poolAdapter.preferPool(), is(sameInstance(this.poolAdapter)));
assertThat(this.poolAdapter.getDelegate(), is(equalTo(this.mockPool)));
assertThat(this.poolAdapter.prefersPool(), is(true));
assertThat(this.poolAdapter.preferPool()).isSameAs(this.poolAdapter);
assertThat(this.poolAdapter.getDelegate()).isEqualTo(this.mockPool);
assertThat(this.poolAdapter.prefersPool()).isTrue();
this.poolAdapter.releaseThreadLocalConnection();

View File

@@ -14,67 +14,61 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.client.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import java.net.InetSocketAddress;
import java.util.Collections;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.query.QueryService;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.query.QueryService;
import org.springframework.data.gemfire.GemfireUtils;
/**
* The DelegatingPoolAdapterTest class is a test suite of test cases testing the contract and functionality
* of the {@link DelegatingPoolAdapter} class.
* Unit Tests for {@link DelegatingPoolAdapter}.
*
* @author John Blum
* @see org.junit.Rule
* @see java.net.InetSocketAddress
* @see org.junit.Test
* @see org.junit.rules.ExpectedException
* @see org.junit.runner.RunWith
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.junit.MockitoJUnitRunner
* @see DelegatingPoolAdapter
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.apache.geode.cache.client.SocketFactory
* @see org.apache.geode.cache.query.QueryService
* @see org.springframework.data.gemfire.client.support.DelegatingPoolAdapter
* @since 1.8.0
*/
@RunWith(MockitoJUnitRunner.class)
public class DelegatingPoolAdapterTest {
@Rule
public ExpectedException exception = ExpectedException.none();
@Mock
private Pool mockPool;
@Mock
private QueryService mockQueryService;
@Mock
private SocketFactory mockSocketFactory;
private InetSocketAddress newSocketAddress(String host, int port) {
return new InetSocketAddress(host, port);
}
@@ -98,10 +92,12 @@ public class DelegatingPoolAdapterTest {
when(this.mockPool.getQueryService()).thenReturn(this.mockQueryService);
when(this.mockPool.getReadTimeout()).thenReturn(30000);
when(this.mockPool.getRetryAttempts()).thenReturn(1);
when(this.mockPool.getServerConnectionTimeout()).thenReturn(10000);
when(this.mockPool.getServerGroup()).thenReturn("TestGroup");
when(this.mockPool.getServers()).thenReturn(Collections.singletonList(newSocketAddress("xghost", 12480)));
when(this.mockPool.getSocketBufferSize()).thenReturn(16384);
when(this.mockPool.getSocketConnectTimeout()).thenReturn(5000);
when(this.mockPool.getSocketFactory()).thenReturn(this.mockSocketFactory);
when(this.mockPool.getStatisticInterval()).thenReturn(1000);
when(this.mockPool.getSubscriptionAckInterval()).thenReturn(200);
when(this.mockPool.getSubscriptionEnabled()).thenReturn(true);
@@ -113,7 +109,7 @@ public class DelegatingPoolAdapterTest {
@Test
public void delegateEqualsMockPool() {
assertThat(DelegatingPoolAdapter.from(this.mockPool).getDelegate(), is(equalTo(this.mockPool)));
assertThat(DelegatingPoolAdapter.from(this.mockPool).getDelegate()).isEqualTo(this.mockPool);
}
@Test
@@ -121,33 +117,35 @@ public class DelegatingPoolAdapterTest {
Pool pool = DelegatingPoolAdapter.from(this.mockPool);
assertThat(pool.isDestroyed(), is(equalTo(false)));
assertThat(pool.getFreeConnectionTimeout(), is(equalTo(10000)));
assertThat(pool.getIdleTimeout(), is(equalTo(120000L)));
assertThat(pool.getLoadConditioningInterval(), is(equalTo(300000)));
assertThat(pool.getMaxConnections(), is(equalTo(500)));
assertThat(pool.getMinConnections(), is(equalTo(50)));
assertThat(pool.getMultiuserAuthentication(), is(equalTo(true)));
assertThat(pool.getLocators(), is(equalTo(Collections.singletonList(newSocketAddress("skullbox", 11235)))));
assertThat(pool.getName(), is(equalTo("MockPool")));
assertThat(pool.getOnlineLocators(), is(equalTo(Collections.singletonList(newSocketAddress("trinity", 10101)))));
assertThat(pool.getPendingEventCount(), is(equalTo(2)));
assertThat(pool.getPingInterval(), is(equalTo(15000L)));
assertThat(pool.getPRSingleHopEnabled(), is(equalTo(true)));
assertThat(pool.getQueryService(), is(equalTo(this.mockQueryService)));
assertThat(pool.getReadTimeout(), is(equalTo(30000)));
assertThat(pool.getRetryAttempts(), is(equalTo(1)));
assertThat(pool.getServerGroup(), is(equalTo("TestGroup")));
assertThat(pool.getServers(), is(equalTo(Collections.singletonList(newSocketAddress("xghost", 12480)))));
assertThat(pool.getSocketBufferSize(), is(equalTo(16384)));
assertThat(pool.getSocketConnectTimeout(), is(equalTo(5000)));
assertThat(pool.getStatisticInterval(), is(equalTo(1000)));
assertThat(pool.getSubscriptionAckInterval(), is(equalTo(200)));
assertThat(pool.getSubscriptionEnabled(), is(equalTo(true)));
assertThat(pool.getSubscriptionMessageTrackingTimeout(), is(equalTo(60000)));
assertThat(pool.getSubscriptionRedundancy(), is(equalTo(2)));
assertThat(pool.getSubscriptionTimeoutMultiplier(), is(equalTo(3)));
assertThat(pool.getThreadLocalConnections(), is(equalTo(false)));
assertThat(pool.isDestroyed()).isFalse();
assertThat(pool.getFreeConnectionTimeout()).isEqualTo(10000);
assertThat(pool.getIdleTimeout()).isEqualTo(120000L);
assertThat(pool.getLoadConditioningInterval()).isEqualTo(300000);
assertThat(pool.getMaxConnections()).isEqualTo(500);
assertThat(pool.getMinConnections()).isEqualTo(50);
assertThat(pool.getMultiuserAuthentication()).isTrue();
assertThat(pool.getLocators()).isEqualTo(Collections.singletonList(newSocketAddress("skullbox", 11235)));
assertThat(pool.getName()).isEqualTo("MockPool");
assertThat(pool.getOnlineLocators()).isEqualTo(Collections.singletonList(newSocketAddress("trinity", 10101)));
assertThat(pool.getPendingEventCount()).isEqualTo(2);
assertThat(pool.getPingInterval()).isEqualTo(15000L);
assertThat(pool.getPRSingleHopEnabled()).isTrue();
assertThat(pool.getQueryService()).isEqualTo(this.mockQueryService);
assertThat(pool.getReadTimeout()).isEqualTo(30000);
assertThat(pool.getRetryAttempts()).isEqualTo(1);
assertThat(pool.getServerConnectionTimeout()).isEqualTo(10000);
assertThat(pool.getServerGroup()).isEqualTo("TestGroup");
assertThat(pool.getServers()).isEqualTo(Collections.singletonList(newSocketAddress("xghost", 12480)));
assertThat(pool.getSocketBufferSize()).isEqualTo(16384);
assertThat(pool.getSocketConnectTimeout()).isEqualTo(5000);
assertThat(pool.getSocketFactory()).isEqualTo(this.mockSocketFactory);
assertThat(pool.getStatisticInterval()).isEqualTo(1000);
assertThat(pool.getSubscriptionAckInterval()).isEqualTo(200);
assertThat(pool.getSubscriptionEnabled()).isTrue();
assertThat(pool.getSubscriptionMessageTrackingTimeout()).isEqualTo(60000);
assertThat(pool.getSubscriptionRedundancy()).isEqualTo(2);
assertThat(pool.getSubscriptionTimeoutMultiplier()).isEqualTo(3);
assertThat(pool.getThreadLocalConnections()).isFalse();
verify(this.mockPool, times(1)).isDestroyed();
verify(this.mockPool, times(1)).getFreeConnectionTimeout();
@@ -164,10 +162,12 @@ public class DelegatingPoolAdapterTest {
verify(this.mockPool, times(1)).getQueryService();
verify(this.mockPool, times(1)).getReadTimeout();
verify(this.mockPool, times(1)).getRetryAttempts();
verify(this.mockPool, times(1)).getServerConnectionTimeout();
verify(this.mockPool, times(1)).getServerGroup();
verify(this.mockPool, times(1)).getServers();
verify(this.mockPool, times(1)).getSocketBufferSize();
verify(this.mockPool, times(1)).getSocketConnectTimeout();
verify(this.mockPool, times(1)).getSocketFactory();
verify(this.mockPool, times(1)).getStatisticInterval();
verify(this.mockPool, times(1)).getSubscriptionAckInterval();
verify(this.mockPool, times(1)).getSubscriptionEnabled();
@@ -178,7 +178,7 @@ public class DelegatingPoolAdapterTest {
}
@Test
public void destroyWithDelegateCallsDestroy() {
public void destroyUsingDelegateCallsDestroy() {
DelegatingPoolAdapter.from(this.mockPool).destroy();
verify(this.mockPool, times(1)).destroy();
}
@@ -200,65 +200,78 @@ public class DelegatingPoolAdapterTest {
Pool pool = DelegatingPoolAdapter.from(null);
assertThat(pool.getFreeConnectionTimeout(), is(equalTo(PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT)));
assertThat(pool.getIdleTimeout(), is(equalTo(PoolFactory.DEFAULT_IDLE_TIMEOUT)));
assertThat(pool.getLoadConditioningInterval(), is(equalTo(PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL)));
assertThat(pool.getMaxConnections(), is(equalTo(PoolFactory.DEFAULT_MAX_CONNECTIONS)));
assertThat(pool.getMinConnections(), is(equalTo(PoolFactory.DEFAULT_MIN_CONNECTIONS)));
assertThat(pool.getMultiuserAuthentication(), is(equalTo(PoolFactory.DEFAULT_MULTIUSER_AUTHENTICATION)));
assertThat(pool.getOnlineLocators(), is(equalTo(Collections.EMPTY_LIST)));
assertThat(pool.getPingInterval(), is(equalTo(PoolFactory.DEFAULT_PING_INTERVAL)));
assertThat(pool.getPRSingleHopEnabled(), is(equalTo(PoolFactory.DEFAULT_PR_SINGLE_HOP_ENABLED)));
assertThat(pool.getReadTimeout(), is(equalTo(PoolFactory.DEFAULT_READ_TIMEOUT)));
assertThat(pool.getRetryAttempts(), is(equalTo(PoolFactory.DEFAULT_RETRY_ATTEMPTS)));
assertThat(pool.getServerGroup(), is(equalTo(PoolFactory.DEFAULT_SERVER_GROUP)));
assertThat(pool.getSocketBufferSize(), is(equalTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE)));
assertThat(pool.getSocketConnectTimeout(), is(equalTo(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT)));
assertThat(pool.getStatisticInterval(), is(equalTo(PoolFactory.DEFAULT_STATISTIC_INTERVAL)));
assertThat(pool.getSubscriptionAckInterval(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL)));
assertThat(pool.getSubscriptionEnabled(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED)));
assertThat(pool.getSubscriptionMessageTrackingTimeout(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT)));
assertThat(pool.getSubscriptionRedundancy(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY)));
assertThat(pool.getSubscriptionTimeoutMultiplier(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_TIMEOUT_MULTIPLIER)));
assertThat(pool.getThreadLocalConnections(), is(equalTo(PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS)));
assertThat(pool.getFreeConnectionTimeout()).isEqualTo(PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT);
assertThat(pool.getIdleTimeout()).isEqualTo(PoolFactory.DEFAULT_IDLE_TIMEOUT);
assertThat(pool.getLoadConditioningInterval()).isEqualTo(PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL);
assertThat(pool.getMaxConnections()).isEqualTo(PoolFactory.DEFAULT_MAX_CONNECTIONS);
assertThat(pool.getMinConnections()).isEqualTo(PoolFactory.DEFAULT_MIN_CONNECTIONS);
assertThat(pool.getMultiuserAuthentication()).isEqualTo(PoolFactory.DEFAULT_MULTIUSER_AUTHENTICATION);
assertThat(pool.getOnlineLocators()).isEqualTo(Collections.EMPTY_LIST);
assertThat(pool.getPingInterval()).isEqualTo(PoolFactory.DEFAULT_PING_INTERVAL);
assertThat(pool.getPRSingleHopEnabled()).isEqualTo(PoolFactory.DEFAULT_PR_SINGLE_HOP_ENABLED);
assertThat(pool.getReadTimeout()).isEqualTo(PoolFactory.DEFAULT_READ_TIMEOUT);
assertThat(pool.getRetryAttempts()).isEqualTo(PoolFactory.DEFAULT_RETRY_ATTEMPTS);
assertThat(pool.getServerConnectionTimeout()).isEqualTo(PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT);
assertThat(pool.getServerGroup()).isEqualTo(PoolFactory.DEFAULT_SERVER_GROUP);
assertThat(pool.getSocketBufferSize()).isEqualTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE);
assertThat(pool.getSocketConnectTimeout()).isEqualTo(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT);
assertThat(pool.getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(pool.getStatisticInterval()).isEqualTo(PoolFactory.DEFAULT_STATISTIC_INTERVAL);
assertThat(pool.getSubscriptionAckInterval()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL);
assertThat(pool.getSubscriptionEnabled()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED);
assertThat(pool.getSubscriptionMessageTrackingTimeout()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT);
assertThat(pool.getSubscriptionRedundancy()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY);
assertThat(pool.getSubscriptionTimeoutMultiplier()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_TIMEOUT_MULTIPLIER);
assertThat(pool.getThreadLocalConnections()).isEqualTo(PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS);
verifyZeroInteractions(this.mockPool);
verifyNoInteractions(this.mockPool);
}
@Test
@Test(expected = UnsupportedOperationException.class)
public void destroyedWithNullIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(is(equalTo(DelegatingPoolAdapter.NOT_IMPLEMENTED)));
try {
DelegatingPoolAdapter.from(null).isDestroyed();
}
catch (UnsupportedOperationException expected) {
DelegatingPoolAdapter.from(null).isDestroyed();
assertThat(expected).hasMessage(DelegatingPoolAdapter.NOT_IMPLEMENTED);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void locatorsWithNullIsEqualToEmptyList() {
assertThat(DelegatingPoolAdapter.from(null).getLocators(), is(equalTo(Collections.<InetSocketAddress>emptyList())));
assertThat(DelegatingPoolAdapter.from(null).getLocators())
.isEqualTo(Collections.<InetSocketAddress>emptyList());
}
@Test
public void nameWithNullIsEqualToDefault() {
assertThat(DelegatingPoolAdapter.from(null).getName(), is(equalTo(DelegatingPoolAdapter.DEFAULT_POOL_NAME)));
assertThat(DelegatingPoolAdapter.from(null).getName()).isEqualTo(DelegatingPoolAdapter.DEFAULT_POOL_NAME);
}
@Test
public void pendingEventCountWithNullIsEqualToZero() {
assertThat(DelegatingPoolAdapter.from(null).getPendingEventCount(), is(equalTo(0)));
assertThat(DelegatingPoolAdapter.from(null).getPendingEventCount()).isEqualTo(0);
}
@Test
public void queryServiceWithNullIsNull() {
assertThat(DelegatingPoolAdapter.from(null).getQueryService(), is(nullValue()));
assertThat(DelegatingPoolAdapter.from(null).getQueryService()).isNull();
}
@Test
public void socketFactoryWithNullIsEqualToDefaultSocketFactory() {
assertThat(DelegatingPoolAdapter.from(null).getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
}
@Test
public void serversWithNullIsEqualToLocalhostListeningOnDefaultCacheServerPort() {
assertThat(DelegatingPoolAdapter.from(null).getServers(), is(equalTo(Collections.singletonList(
newSocketAddress("localhost", GemfireUtils.DEFAULT_CACHE_SERVER_PORT)))));
assertThat(DelegatingPoolAdapter.from(null).getServers()).isEqualTo(Collections.singletonList(
newSocketAddress("localhost", GemfireUtils.DEFAULT_CACHE_SERVER_PORT)));
}
@Test

View File

@@ -14,35 +14,29 @@
* limitations under the License.
*
*/
package org.springframework.data.gemfire.client.support;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.function.Supplier;
import org.junit.Test;
import org.apache.geode.cache.client.PoolFactory;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.data.gemfire.GemfireUtils;
/**
* Unit tests for {@link FactoryDefaultsPoolAdapter}.
* Unit Tests for {@link FactoryDefaultsPoolAdapter}.
*
* @author John Blum
* @see org.junit.Rule
* @see java.net.InetSocketAddress
* @see org.junit.Test
* @see org.junit.rules.ExpectedException
* @see org.springframework.data.gemfire.client.support.FactoryDefaultsPoolAdapter
* @see org.apache.geode.cache.client.Pool
* @see org.apache.geode.cache.client.PoolFactory
* @see org.springframework.data.gemfire.client.support.FactoryDefaultsPoolAdapter
* @since 1.8.0
*/
public class FactoryDefaultsPoolAdapterTest {
@@ -51,112 +45,99 @@ public class FactoryDefaultsPoolAdapterTest {
private FactoryDefaultsPoolAdapter poolAdapter = new FactoryDefaultsPoolAdapter() { };
@Rule
public ExpectedException exception = ExpectedException.none();
protected InetSocketAddress newSocketAddress(String host, int port) {
private InetSocketAddress newSocketAddress(String host, int port) {
return new InetSocketAddress(host, port);
}
@Test
public void defaultPoolAdapterConfigurationPropertiesReturnDefaultFactorySettings() {
assertThat(this.poolAdapter.getFreeConnectionTimeout(), is(equalTo(PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT)));
assertThat(this.poolAdapter.getIdleTimeout(), is(equalTo(PoolFactory.DEFAULT_IDLE_TIMEOUT)));
assertThat(this.poolAdapter.getLoadConditioningInterval(), is(equalTo(PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL)));
assertThat(this.poolAdapter.getMaxConnections(), is(equalTo(PoolFactory.DEFAULT_MAX_CONNECTIONS)));
assertThat(this.poolAdapter.getMinConnections(), is(equalTo(PoolFactory.DEFAULT_MIN_CONNECTIONS)));
assertThat(this.poolAdapter.getMultiuserAuthentication(), is(equalTo(PoolFactory.DEFAULT_MULTIUSER_AUTHENTICATION)));
assertThat(this.poolAdapter.getPRSingleHopEnabled(), is(equalTo(PoolFactory.DEFAULT_PR_SINGLE_HOP_ENABLED)));
assertThat(this.poolAdapter.getPingInterval(), is(equalTo(PoolFactory.DEFAULT_PING_INTERVAL)));
assertThat(this.poolAdapter.getReadTimeout(), is(equalTo(PoolFactory.DEFAULT_READ_TIMEOUT)));
assertThat(this.poolAdapter.getRetryAttempts(), is(equalTo(PoolFactory.DEFAULT_RETRY_ATTEMPTS)));
assertThat(this.poolAdapter.getServerGroup(), is(equalTo(PoolFactory.DEFAULT_SERVER_GROUP)));
assertThat(this.poolAdapter.getSocketBufferSize(), is(equalTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE)));
assertThat(this.poolAdapter.getSocketConnectTimeout(), is(equalTo(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT)));
assertThat(this.poolAdapter.getStatisticInterval(), is(equalTo(PoolFactory.DEFAULT_STATISTIC_INTERVAL)));
assertThat(this.poolAdapter.getSubscriptionAckInterval(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL)));
assertThat(this.poolAdapter.getSubscriptionEnabled(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED)));
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout(),
is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT)));
assertThat(this.poolAdapter.getSubscriptionRedundancy(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY)));
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier(), is(equalTo(PoolFactory.DEFAULT_SUBSCRIPTION_TIMEOUT_MULTIPLIER)));
assertThat(this.poolAdapter.getThreadLocalConnections(), is(equalTo(PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS)));
assertThat(this.poolAdapter.getFreeConnectionTimeout()).isEqualTo(PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT);
assertThat(this.poolAdapter.getIdleTimeout()).isEqualTo(PoolFactory.DEFAULT_IDLE_TIMEOUT);
assertThat(this.poolAdapter.getLoadConditioningInterval()).isEqualTo(PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL);
assertThat(this.poolAdapter.getMaxConnections()).isEqualTo(PoolFactory.DEFAULT_MAX_CONNECTIONS);
assertThat(this.poolAdapter.getMinConnections()).isEqualTo(PoolFactory.DEFAULT_MIN_CONNECTIONS);
assertThat(this.poolAdapter.getMultiuserAuthentication()).isEqualTo(PoolFactory.DEFAULT_MULTIUSER_AUTHENTICATION);
assertThat(this.poolAdapter.getPRSingleHopEnabled()).isEqualTo(PoolFactory.DEFAULT_PR_SINGLE_HOP_ENABLED);
assertThat(this.poolAdapter.getPingInterval()).isEqualTo(PoolFactory.DEFAULT_PING_INTERVAL);
assertThat(this.poolAdapter.getReadTimeout()).isEqualTo(PoolFactory.DEFAULT_READ_TIMEOUT);
assertThat(this.poolAdapter.getRetryAttempts()).isEqualTo(PoolFactory.DEFAULT_RETRY_ATTEMPTS);
assertThat(this.poolAdapter.getServerConnectionTimeout()).isEqualTo(PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT);
assertThat(this.poolAdapter.getServerGroup()).isEqualTo(PoolFactory.DEFAULT_SERVER_GROUP);
assertThat(this.poolAdapter.getSocketBufferSize()).isEqualTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE);
assertThat(this.poolAdapter.getSocketConnectTimeout()).isEqualTo(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT);
assertThat(this.poolAdapter.getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(this.poolAdapter.getStatisticInterval()).isEqualTo(PoolFactory.DEFAULT_STATISTIC_INTERVAL);
assertThat(this.poolAdapter.getSubscriptionAckInterval()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL);
assertThat(this.poolAdapter.getSubscriptionEnabled()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED);
assertThat(this.poolAdapter.getSubscriptionMessageTrackingTimeout()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT);
assertThat(this.poolAdapter.getSubscriptionRedundancy()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY);
assertThat(this.poolAdapter.getSubscriptionTimeoutMultiplier()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_TIMEOUT_MULTIPLIER);
assertThat(this.poolAdapter.getThreadLocalConnections()).isEqualTo(PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS);
}
@Test
public void locatorsReturnsEmptyList() {
assertThat(this.poolAdapter.getLocators(), is(equalTo(Collections.<InetSocketAddress>emptyList())));
assertThat(this.poolAdapter.getLocators()).isEqualTo(Collections.<InetSocketAddress>emptyList());
}
@Test
public void nameReturnsDefault() {
assertThat(this.poolAdapter.getName(), is(equalTo(FactoryDefaultsPoolAdapter.DEFAULT_POOL_NAME)));
assertThat(this.poolAdapter.getName()).isEqualTo(FactoryDefaultsPoolAdapter.DEFAULT_POOL_NAME);
}
@Test
public void onlineLocatorsIsEmptyList() {
assertThat(this.poolAdapter.getOnlineLocators(), is(equalTo(Collections.EMPTY_LIST)));
assertThat(this.poolAdapter.getOnlineLocators()).isEqualTo(Collections.EMPTY_LIST);
}
@Test
public void queryServiceIsNull() {
assertThat(this.poolAdapter.getQueryService(), is(nullValue()));
assertThat(this.poolAdapter.getQueryService()).isNull();
}
@Test
public void serversReturnsLocalhostListeningOnDefaultCacheServerPort() {
assertThat(this.poolAdapter.getServers(), is(equalTo(Collections.singletonList(
newSocketAddress("localhost", DEFAULT_CACHE_SERVER_PORT)))));
assertThat(this.poolAdapter.getServers()).isEqualTo(Collections.singletonList(
newSocketAddress("localhost", DEFAULT_CACHE_SERVER_PORT)));
}
@Test
private <T> T testPoolOperationIsUnsupported(Supplier<T> poolOperation) {
try {
return poolOperation.get();
}
catch (UnsupportedOperationException expected) {
assertThat(expected).hasMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = UnsupportedOperationException.class)
public void isDestroyedIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
this.poolAdapter.isDestroyed();
testPoolOperationIsUnsupported(() -> this.poolAdapter.isDestroyed());
}
@Test
@Test(expected = UnsupportedOperationException.class)
public void getPendingEventCountIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
this.poolAdapter.getPendingEventCount();
testPoolOperationIsUnsupported(() -> this.poolAdapter.getPendingEventCount());
}
@Test
@Test(expected = UnsupportedOperationException.class)
public void destroyedIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
this.poolAdapter.destroy();
testPoolOperationIsUnsupported(() -> { this.poolAdapter.destroy(); return null; });
}
@Test
@Test(expected = UnsupportedOperationException.class)
public void destroyedWithKeepAliveIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
this.poolAdapter.destroy(false);
testPoolOperationIsUnsupported(() -> { this.poolAdapter.destroy(false); return null; });
}
@Test
@Test(expected = UnsupportedOperationException.class)
public void releaseThreadLocalConnectionsIsUnsupported() {
exception.expect(UnsupportedOperationException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(FactoryDefaultsPoolAdapter.NOT_IMPLEMENTED);
this.poolAdapter.releaseThreadLocalConnection();
testPoolOperationIsUnsupported(() -> { this.poolAdapter.releaseThreadLocalConnection(); return null; });
}
}

View File

@@ -89,8 +89,8 @@ public class ClientCacheApplicationIntegrationTests {
}
@Override
public void close() {
}
public void close() { }
};
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2020 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import java.util.Optional;
import org.junit.After;
import org.junit.Test;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.SocketFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects;
/**
* Integration Tests for {@link ClientCacheConfiguration}.
*
* @author John Blum
* @see org.apache.geode.cache.client.ClientCache
* @see org.apache.geode.cache.client.Pool
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.context.ConfigurableApplicationContext
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfiguration
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @since 2.4.0
*/
@SuppressWarnings("unused")
public class ClientCacheConfigurationIntegrationTests {
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
Optional.ofNullable(this.applicationContext).ifPresent(ConfigurableApplicationContext::close);
}
private ConfigurableApplicationContext newApplicationContext(Class<?>... annotatedClasses) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(annotatedClasses);
applicationContext.registerShutdownHook();
applicationContext.refresh();
return applicationContext;
}
@Test
public void clientCacheDefaultPoolWithCustomSocketFactory() {
this.applicationContext =
newApplicationContext(ClientCacheDefaultPoolWithCustomSocketFactoryConfiguration.class);
assertThat(this.applicationContext).isNotNull();
ClientCache clientCache = this.applicationContext.getBean(ClientCache.class);
assertThat(clientCache).isNotNull();
Pool defaultPool = clientCache.getDefaultPool();
SocketFactory mockSocketFactory = this.applicationContext.getBean("mockSocketFactory", SocketFactory.class);
assertThat(defaultPool).isNotNull();
assertThat(defaultPool.getName()).isEqualTo("DEFAULT");
assertThat(defaultPool.getServerConnectionTimeout()).isEqualTo(60000);
assertThat(mockSocketFactory).isNotNull();
assertThat(defaultPool.getSocketFactory()).isEqualTo(mockSocketFactory);
}
@Test
public void clientCacheDefaultPoolWithDefaultSocketFactory() {
this.applicationContext =
newApplicationContext(ClientCacheDefaultPoolWithDefaultSocketFactoryConfiguration.class);
assertThat(this.applicationContext).isNotNull();
ClientCache clientCache = this.applicationContext.getBean(ClientCache.class);
assertThat(clientCache).isNotNull();
Pool defaultPool = clientCache.getDefaultPool();
assertThat(defaultPool).isNotNull();
assertThat(defaultPool.getName()).isEqualTo("DEFAULT");
assertThat(defaultPool.getSocketFactory()).isEqualTo(SocketFactory.DEFAULT);
}
@EnableGemFireMockObjects
@ClientCacheApplication(
name = "ClientCacheDefaultPoolWithCustomSocketFactoryConfiguration",
logLevel = "error",
serverConnectionTimeout = 60000,
socketFactoryBeanName = "mockSocketFactory"
)
static class ClientCacheDefaultPoolWithCustomSocketFactoryConfiguration {
@Bean
SocketFactory mockSocketFactory() {
return mock(SocketFactory.class);
}
}
@EnableGemFireMockObjects
@ClientCacheApplication(name = "ClientCacheDefaultPoolWithCustomSocketFactoryConfiguration", logLevel = "error")
static class ClientCacheDefaultPoolWithDefaultSocketFactoryConfiguration {
@Bean
javax.net.SocketFactory mockSocketFactory() {
return mock(javax.net.SocketFactory.class);
}
}
}

View File

@@ -0,0 +1,298 @@
/*
* Copyright 2020 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.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import org.junit.Test;
import org.apache.geode.cache.TransactionListener;
import org.apache.geode.cache.TransactionWriter;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.util.GatewayConflictResolver;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.core.io.Resource;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.support.ConnectionEndpoint;
/**
* Unit Tests for {@link ClientCacheConfiguration}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.core.io.Resource
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
* @see org.springframework.data.gemfire.config.annotation.ClientCacheConfiguration
* @since 2.4.0
*/
public class ClientCacheConfigurationUnitTests {
@Test
public void configuresClientCacheFactoryBean() {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
ClassLoader mockBeanClassLoader = mock(ClassLoader.class);
GatewayConflictResolver mockGatewayConflictResolver = mock(GatewayConflictResolver.class);
List<ConnectionEndpoint> poolLocators =
Collections.singletonList(new ConnectionEndpoint("localhost", 12345));
Properties gemfireProperties = new Properties();
Resource mockResource = mock(Resource.class, "cache.xml");
SocketFactory mockSocketFactory = mock(SocketFactory.class);
TransactionListener mockTransactionListener = mock(TransactionListener.class);
TransactionWriter mockTransactionWriter = mock(TransactionWriter.class);
ClientCacheFactoryBean clientCacheFactoryBean = spy(new ClientCacheFactoryBean());
ClientCacheConfiguration configuration = spy(new ClientCacheConfiguration());
doReturn(gemfireProperties).when(configuration).gemfireProperties();
doReturn(clientCacheFactoryBean).when(configuration).newCacheFactoryBean();
doReturn(mockSocketFactory).when(configuration).resolveSocketFactory();
configuration.setBeanClassLoader(mockBeanClassLoader);
configuration.setBeanFactory(mockBeanFactory);
configuration.setCacheXml(mockResource);
configuration.setClose(true);
configuration.setCopyOnRead(true);
configuration.setCriticalHeapPercentage(90.0f);
configuration.setCriticalOffHeapPercentage(95.0f);
configuration.setEvictionHeapPercentage(75.0f);
configuration.setEvictionOffHeapPercentage(90.0f);
configuration.setGatewayConflictResolver(mockGatewayConflictResolver);
configuration.setTransactionListeners(Collections.singletonList(mockTransactionListener));
configuration.setTransactionWriter(mockTransactionWriter);
configuration.setUseBeanFactoryLocator(true);
configuration.setDurableClientId("abc123");
configuration.setDurableClientTimeout(300000);
configuration.setFreeConnectionTimeout(30000);
configuration.setIdleTimeout(300000L);
configuration.setKeepAlive(true);
configuration.setPoolLocators(poolLocators);
configuration.setLoadConditioningInterval(120000);
configuration.setMaxConnections(500);
configuration.setMinConnections(51);
configuration.setMultiUserAuthentication(false);
configuration.setPingInterval(15000L);
configuration.setPrSingleHopEnabled(true);
configuration.setReadTimeout(60000);
configuration.setReadyForEvents(true);
configuration.setRetryAttempts(2);
configuration.setServerConnectionTimeout(60000);
configuration.setServerGroup("TestGroup");
configuration.setSocketBufferSize(8192);
configuration.setSocketConnectTimeout(30000);
configuration.setSocketFactoryBeanName("testSocketFactory");
configuration.setStatisticsInterval(5000);
configuration.setSubscriptionAckInterval(15000);
configuration.setSubscriptionEnabled(true);
configuration.setSubscriptionMessageTrackingTimeout(60000);
configuration.setSubscriptionRedundancy(1);
configuration.setThreadLocalConnections(false);
assertThat(configuration.gemfireCache()).isEqualTo(clientCacheFactoryBean);
verify(clientCacheFactoryBean, times(1)).setBeanClassLoader(eq(mockBeanClassLoader));
verify(clientCacheFactoryBean, times(1)).setBeanFactory(eq(mockBeanFactory));
verify(clientCacheFactoryBean, times(1)).setCacheXml(eq(mockResource));
verify(clientCacheFactoryBean, times(1)).setClose(eq(true));
verify(clientCacheFactoryBean, times(1)).setCopyOnRead(eq(true));
verify(clientCacheFactoryBean, times(1)).setCriticalHeapPercentage(eq(90.0f));
verify(clientCacheFactoryBean, times(1)).setCriticalOffHeapPercentage(eq(95.0f));
verify(clientCacheFactoryBean, times(1)).setEvictionHeapPercentage(eq(75.0f));
verify(clientCacheFactoryBean, times(1)).setEvictionOffHeapPercentage(eq(90.0f));
verify(clientCacheFactoryBean, times(1)).setGatewayConflictResolver(eq(mockGatewayConflictResolver));
verify(clientCacheFactoryBean, times(1)).setJndiDataSources(eq(Collections.emptyList()));
verify(clientCacheFactoryBean, times(1)).setTransactionListeners(eq(Collections.singletonList(mockTransactionListener)));
verify(clientCacheFactoryBean, times(1)).setTransactionWriter(eq(mockTransactionWriter));
verify(clientCacheFactoryBean, times(1)).setUseBeanFactoryLocator(eq(true));
verify(clientCacheFactoryBean, times(1)).setDurableClientId(eq("abc123"));
verify(clientCacheFactoryBean, times(1)).setDurableClientTimeout(eq(300000));
verify(clientCacheFactoryBean, times(1)).setFreeConnectionTimeout(eq(30000));
verify(clientCacheFactoryBean, times(1)).setIdleTimeout(eq(300000L));
verify(clientCacheFactoryBean, times(1)).setKeepAlive(eq(true));
verify(clientCacheFactoryBean, times(1)).setLocators(eq(poolLocators));
verify(clientCacheFactoryBean, times(1)).setLoadConditioningInterval(eq(120000));
verify(clientCacheFactoryBean, times(1)).setMaxConnections(eq(500));
verify(clientCacheFactoryBean, times(1)).setMinConnections(eq(51));
verify(clientCacheFactoryBean, times(1)).setMultiUserAuthentication(eq(false));
verify(clientCacheFactoryBean, times(1)).setPingInterval(eq(15000L));
verify(clientCacheFactoryBean, times(1)).setPrSingleHopEnabled(eq(true));
verify(clientCacheFactoryBean, times(1)).setReadTimeout(eq(60000));
verify(clientCacheFactoryBean, times(1)).setReadyForEvents(eq(true));
verify(clientCacheFactoryBean, times(1)).setRetryAttempts(eq(2));
verify(clientCacheFactoryBean, times(1)).setServerConnectionTimeout(eq(60000));
verify(clientCacheFactoryBean, times(1)).setServerGroup(eq("TestGroup"));
verify(clientCacheFactoryBean, times(1)).setSocketBufferSize(eq(8192));
verify(clientCacheFactoryBean, times(1)).setSocketConnectTimeout(eq(30000));
verify(clientCacheFactoryBean, times(1)).setSocketFactory(eq(mockSocketFactory));
verify(clientCacheFactoryBean, times(1)).setStatisticsInterval(eq(5000));
verify(clientCacheFactoryBean, times(1)).setSubscriptionAckInterval(eq(15000));
verify(clientCacheFactoryBean, times(1)).setSubscriptionEnabled(eq(true));
verify(clientCacheFactoryBean, times(1)).setSubscriptionMessageTrackingTimeout(eq(60000));
verify(clientCacheFactoryBean, times(1)).setSubscriptionRedundancy(eq(1));
verify(clientCacheFactoryBean, times(1)).setThreadLocalConnections(eq(false));
}
@Test
public void resolveSocketFactoryFromBeanFactory() {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
ClientCacheConfiguration configuration = new ClientCacheConfiguration();
SocketFactory mockSocketFactory = mock(SocketFactory.class);
doReturn(true).when(mockBeanFactory)
.isTypeMatch(eq("testSocketFactoryBean"), eq(SocketFactory.class));
doReturn(mockSocketFactory).when(mockBeanFactory)
.getBean(eq("testSocketFactoryBean"), eq(SocketFactory.class));
configuration.setBeanFactory(mockBeanFactory);
configuration.setSocketFactoryBeanName("testSocketFactoryBean");
assertThat(configuration.getBeanFactory()).isEqualTo(mockBeanFactory);
assertThat(configuration.getSocketFactoryBeanName()).isEqualTo("testSocketFactoryBean");
assertThat(configuration.resolveSocketFactory()).isEqualTo(mockSocketFactory);
verify(mockBeanFactory, times(1))
.isTypeMatch(eq("testSocketFactoryBean"), eq(SocketFactory.class));
verify(mockBeanFactory, times(1))
.getBean(eq("testSocketFactoryBean"), eq(SocketFactory.class));
verifyNoMoreInteractions(mockBeanFactory);
verifyNoInteractions(mockSocketFactory);
}
public void testResolveSocketFactoryWithInvalidSocketFactoryBeanNameConfiguration(String socketFactoryBeanName) {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
ClientCacheConfiguration configuration = new ClientCacheConfiguration();
configuration.setBeanFactory(mockBeanFactory);
configuration.setSocketFactoryBeanName(socketFactoryBeanName);
assertThat(configuration.getBeanFactory()).isEqualTo(mockBeanFactory);
assertThat(configuration.getSocketFactoryBeanName()).isEqualTo(socketFactoryBeanName);
assertThat(configuration.resolveSocketFactory()).isNull();
verifyNoInteractions(mockBeanFactory);
}
@Test
public void resolveSocketFactoryWhenSocketFactoryBeanNameIsNull() {
testResolveSocketFactoryWithInvalidSocketFactoryBeanNameConfiguration(null);
}
@Test
public void resolveSocketFactoryWhenSocketFactoryBeanNameIsEmpty() {
testResolveSocketFactoryWithInvalidSocketFactoryBeanNameConfiguration("");
}
@Test
public void resolveSocketFactoryWhenSocketFactoryBeanNameIsBlank() {
testResolveSocketFactoryWithInvalidSocketFactoryBeanNameConfiguration(" ");
}
@Test(expected = BeanNotOfRequiredTypeException.class)
public void resolveSocketFactoryWhenSocketFactoryBeanIsNotTypeMatch() {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
doReturn(false).when(mockBeanFactory).isTypeMatch(anyString(), eq(SocketFactory.class));
doReturn(true).when(mockBeanFactory).containsBean(eq("testSocketFactory"));
doReturn(javax.net.SocketFactory.class).when(mockBeanFactory).getType(eq("testSocketFactory"));
ClientCacheConfiguration configuration = new ClientCacheConfiguration();
configuration.setBeanFactory(mockBeanFactory);
configuration.setSocketFactoryBeanName("testSocketFactory");
assertThat(configuration.getBeanFactory()).isEqualTo(mockBeanFactory);
assertThat(configuration.getSocketFactoryBeanName()).isEqualTo("testSocketFactory");
try {
configuration.resolveSocketFactory();
}
catch (BeanNotOfRequiredTypeException expected) {
assertThat(expected)
.hasMessageContaining("Bean named 'testSocketFactory' is expected to be of type '%s' but was actually of type '%s'",
SocketFactory.class.getName(), javax.net.SocketFactory.class.getName());
assertThat(expected).hasNoCause();
throw expected;
}
finally {
verify(mockBeanFactory, times(1))
.isTypeMatch(eq("testSocketFactory"), eq(SocketFactory.class));
verify(mockBeanFactory, times(1)).containsBean(eq("testSocketFactory"));
verify(mockBeanFactory, times(1)).getType(eq("testSocketFactory"));
verifyNoMoreInteractions(mockBeanFactory);
}
}
@Test
public void resolveSocketFactoryWhenBeanOfSocketFactoryTypeIsNotFound() {
BeanFactory mockBeanFactory = mock(BeanFactory.class);
doReturn(false).when(mockBeanFactory)
.isTypeMatch(eq("mockSocketFactory"), eq(SocketFactory.class));
doReturn(false).when(mockBeanFactory).containsBean(eq("mockSocketFactory"));
ClientCacheConfiguration configuration = new ClientCacheConfiguration();
configuration.setBeanFactory(mockBeanFactory);
configuration.setSocketFactoryBeanName("mockSocketFactory");
assertThat(configuration.getBeanFactory()).isEqualTo(mockBeanFactory);
assertThat(configuration.getSocketFactoryBeanName()).isEqualTo("mockSocketFactory");
assertThat(configuration.resolveSocketFactory()).isNull();
verify(mockBeanFactory, times(1))
.isTypeMatch(eq("mockSocketFactory"), eq(SocketFactory.class));
verify(mockBeanFactory, times(1)).containsBean(eq("mockSocketFactory"));
verifyNoMoreInteractions(mockBeanFactory);
}
}

View File

@@ -27,6 +27,7 @@ import org.junit.Test;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.apache.geode.cache.control.ResourceManager;
import org.apache.geode.pdx.PdxSerializer;
@@ -93,9 +94,9 @@ public class ClientCachePropertiesIntegrationTests {
.withProperty("spring.data.gemfire.pool.ping-interval", 5000L)
.withProperty("spring.data.gemfire.pool.pr-single-hop-enabled", false)
.withProperty("spring.data.gemfire.pool.read-timeout", 20000L)
.withProperty("spring.data.gemfire.pool.default.read-timeout", 5000L)
.withProperty("spring.data.gemfire.pool.default.read-timeout", 15000L)
.withProperty("spring.data.gemfire.pool.retry-attempts", 2)
.withProperty("spring.data.gemfire.pool.server-group", "testGroup")
.withProperty("spring.data.gemfire.pool.server-group", "TestGroup")
.withProperty("spring.data.gemfire.pool.default.subscription-redundancy", 2);
this.applicationContext = newApplicationContext(testPropertySource, TestClientCacheConfiguration.class);
@@ -137,11 +138,13 @@ public class ClientCachePropertiesIntegrationTests {
assertThat(defaultPool.getName()).isEqualTo("DEFAULT");
assertThat(defaultPool.getPingInterval()).isEqualTo(5000L);
assertThat(defaultPool.getPRSingleHopEnabled()).isFalse();
assertThat(defaultPool.getReadTimeout()).isEqualTo(5000);
assertThat(defaultPool.getReadTimeout()).isEqualTo(15000);
assertThat(defaultPool.getRetryAttempts()).isEqualTo(2);
assertThat(defaultPool.getServerGroup()).isEqualTo("testGroup");
assertThat(defaultPool.getServerConnectionTimeout()).isEqualTo(PoolFactory.DEFAULT_SERVER_CONNECTION_TIMEOUT);
assertThat(defaultPool.getServerGroup()).isEqualTo("TestGroup");
assertThat(defaultPool.getSocketBufferSize()).isEqualTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE);
assertThat(defaultPool.getSocketConnectTimeout()).isEqualTo(20001);
assertThat(defaultPool.getSocketFactory()).isEqualTo(PoolFactory.DEFAULT_SOCKET_FACTORY);
assertThat(defaultPool.getStatisticInterval()).isEqualTo(500);
assertThat(defaultPool.getSubscriptionAckInterval()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL);
assertThat(defaultPool.getSubscriptionEnabled()).isTrue();
@@ -181,9 +184,11 @@ public class ClientCachePropertiesIntegrationTests {
.withProperty("spring.data.gemfire.pool.default.read-timeout", 5000)
.withProperty("spring.data.gemfire.pool.default.ready-for-events", true)
.withProperty("spring.data.gemfire.pool.default.retry-attempts", 2)
.withProperty("spring.data.gemfire.pool.default.server-connection-timeout", 60000)
.withProperty("spring.data.gemfire.pool.default.server-group", "testGroup")
.withProperty("spring.data.gemfire.pool.default.socket-buffer-size", 65535)
.withProperty("spring.data.gemfire.pool.default.socket-connect-timeout", 30001)
.withProperty("spring.data.gemfire.pool.default.socket-factory-bean-name", "mockSocketFactory")
.withProperty("spring.data.gemfire.pool.default.statistic-interval", 100)
.withProperty("spring.data.gemfire.pool.default.subscription-ack-interval", 250)
.withProperty("spring.data.gemfire.pool.default.subscription-enabled", true)
@@ -212,7 +217,10 @@ public class ClientCachePropertiesIntegrationTests {
PdxSerializer mockPdxSerializer = this.applicationContext.getBean("mockPdxSerializer", PdxSerializer.class);
SocketFactory mockSocketFactory = this.applicationContext.getBean("mockSocketFactory", SocketFactory.class);
assertThat(mockPdxSerializer).isNotNull();
assertThat(mockSocketFactory).isNotNull();
assertThat(clientCacheFactoryBean.getDurableClientId()).isEqualTo("123");
assertThat(clientCacheFactoryBean.getDurableClientTimeout()).isEqualTo(600);
assertThat(clientCacheFactoryBean.getUseClusterConfiguration()).isFalse();
@@ -248,9 +256,11 @@ public class ClientCachePropertiesIntegrationTests {
assertThat(defaultPool.getPRSingleHopEnabled()).isFalse();
assertThat(defaultPool.getReadTimeout()).isEqualTo(5000);
assertThat(defaultPool.getRetryAttempts()).isEqualTo(2);
assertThat(defaultPool.getServerConnectionTimeout()).isEqualTo(60000);
assertThat(defaultPool.getServerGroup()).isEqualTo("testGroup");
assertThat(defaultPool.getSocketBufferSize()).isEqualTo(65535);
assertThat(defaultPool.getSocketConnectTimeout()).isEqualTo(30001);
assertThat(defaultPool.getSocketFactory()).isEqualTo(mockSocketFactory);
assertThat(defaultPool.getStatisticInterval()).isEqualTo(100);
assertThat(defaultPool.getSubscriptionAckInterval()).isEqualTo(250);
assertThat(defaultPool.getSubscriptionEnabled()).isTrue();
@@ -304,5 +314,10 @@ public class ClientCachePropertiesIntegrationTests {
PdxSerializer mockPdxSerializer() {
return mock(PdxSerializer.class);
}
@Bean
SocketFactory mockSocketFactory() {
return mock(SocketFactory.class);
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.data.gemfire.config.annotation;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import java.net.InetSocketAddress;
import java.util.Optional;
@@ -26,6 +27,7 @@ import org.junit.Test;
import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolFactory;
import org.apache.geode.cache.client.SocketFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -52,6 +54,7 @@ import org.springframework.mock.env.MockPropertySource;
* @see org.springframework.data.gemfire.test.mock.annotation.EnableGemFireMockObjects
* @since 2.0.0
*/
@SuppressWarnings("unused")
public class PoolPropertiesIntegrationTests {
private ConfigurableApplicationContext applicationContext;
@@ -79,8 +82,9 @@ public class PoolPropertiesIntegrationTests {
private void assertPool(Pool pool, int freeConnectionTimeout, long idleTimeout, int loadConditioningInterval,
int maxConnections, int minConnections, boolean multiUserAuthentication, String name, long pingInterval,
boolean prSinglehopEnabled, int readTimeout, int retryAttempts, String serverGroup, int socketBufferSize,
int socketConnectTimeout, int statisticInterval, int subscriptionAckInterval, boolean subscriptionEnabled,
boolean prSinglehopEnabled, int readTimeout, int retryAttempts, int serverConnectionTimeout,
String serverGroup, int socketBufferSize, int socketConnectTimeout, SocketFactory socketFactory,
int statisticInterval, int subscriptionAckInterval, boolean subscriptionEnabled,
int subscriptionMessageTrackingTimeout, int subscriptionRedundancy, boolean threadLocalConnections) {
assertThat(pool).isNotNull();
@@ -95,9 +99,11 @@ public class PoolPropertiesIntegrationTests {
assertThat(pool.getPRSingleHopEnabled()).isEqualTo(prSinglehopEnabled);
assertThat(pool.getReadTimeout()).isEqualTo(readTimeout);
assertThat(pool.getRetryAttempts()).isEqualTo(retryAttempts);
assertThat(pool.getServerConnectionTimeout()).isEqualTo(serverConnectionTimeout);
assertThat(pool.getServerGroup()).isEqualTo(serverGroup);
assertThat(pool.getSocketBufferSize()).isEqualTo(socketBufferSize);
assertThat(pool.getSocketConnectTimeout()).isEqualTo(socketConnectTimeout);
assertThat(pool.getSocketFactory()).isEqualTo(socketFactory);
assertThat(pool.getStatisticInterval()).isEqualTo(statisticInterval);
assertThat(pool.getSubscriptionAckInterval()).isEqualTo(subscriptionAckInterval);
assertThat(pool.getSubscriptionEnabled()).isEqualTo(subscriptionEnabled);
@@ -112,14 +118,14 @@ public class PoolPropertiesIntegrationTests {
MockPropertySource testPropertySource = new MockPropertySource()
.withProperty("spring.data.gemfire.pool.free-connection-timeout", 5000)
.withProperty("spring.data.gemfire.pool.locators", "skullbox[11235]")
.withProperty("spring.data.gemfire.pool.max-connections", 500)
.withProperty("spring.data.gemfire.pool.min-connections", 25)
.withProperty("spring.data.gemfire.pool.max-connections", 400)
.withProperty("spring.data.gemfire.pool.min-connections", 10)
.withProperty("spring.data.gemfire.pool.ping-interval", 5000L)
.withProperty("spring.data.gemfire.pool.pr-single-hop-enabled", false)
.withProperty("spring.data.gemfire.pool.read-timeout", 15000)
.withProperty("spring.data.gemfire.pool.default.read-timeout", 5000L)
.withProperty("spring.data.gemfire.pool.retry-attempts", 2)
.withProperty("spring.data.gemfire.pool.server-group", "testGroup")
.withProperty("spring.data.gemfire.pool.server-group", "TestGroup")
.withProperty("spring.data.gemfire.pool.default.socket-connect-timeout", 5000)
.withProperty("spring.data.gemfire.pool.subscription-enabled", true)
.withProperty("spring.data.gemfire.pool.TestPool.subscription-redundancy", 2);
@@ -132,21 +138,27 @@ public class PoolPropertiesIntegrationTests {
Pool testPool = this.applicationContext.getBean("TestPool", Pool.class);
SocketFactory mockSocketFactory = this.applicationContext.getBean("mockSocketFactory", SocketFactory.class);
assertThat(testPool).isNotNull();
assertThat(mockSocketFactory).isNotNull();
assertThat(testPool.getFreeConnectionTimeout()).isEqualTo(5000);
assertThat(testPool.getIdleTimeout()).isEqualTo(10000L);
assertThat(testPool.getLoadConditioningInterval()).isEqualTo(100000);
assertThat(testPool.getLocators()).contains(new InetSocketAddress("skullbox", 11235));
assertThat(testPool.getMaxConnections()).isEqualTo(500);
assertThat(testPool.getMinConnections()).isEqualTo(25);
assertThat(testPool.getMaxConnections()).isEqualTo(400);
assertThat(testPool.getMinConnections()).isEqualTo(10);
assertThat(testPool.getMultiuserAuthentication()).isFalse();
assertThat(testPool.getName()).isEqualTo("TestPool");
assertThat(testPool.getPingInterval()).isEqualTo(5000L);
assertThat(testPool.getPRSingleHopEnabled()).isFalse();
assertThat(testPool.getReadTimeout()).isEqualTo(15000);
assertThat(testPool.getRetryAttempts()).isEqualTo(1);
assertThat(testPool.getServerGroup()).isEqualTo("testGroup");
assertThat(testPool.getServerConnectionTimeout()).isEqualTo(60000);
assertThat(testPool.getServerGroup()).isEqualTo("TestGroup");
assertThat(testPool.getSocketBufferSize()).isEqualTo(PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE);
assertThat(testPool.getSocketConnectTimeout()).isEqualTo(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT);
assertThat(testPool.getSocketFactory()).isEqualTo(mockSocketFactory);
assertThat(testPool.getSubscriptionAckInterval()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL);
assertThat(testPool.getSubscriptionEnabled()).isEqualTo(true);
assertThat(testPool.getSubscriptionMessageTrackingTimeout()).isEqualTo(PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT);
@@ -158,8 +170,8 @@ public class PoolPropertiesIntegrationTests {
public void multiPoolConfiguration() {
MockPropertySource testPropertySource = new MockPropertySource()
.withProperty("spring.data.gemfire.pool.free-connection-timeout", 5000)
.withProperty("spring.data.gemfire.pool.idle-timeout", 10000L)
.withProperty("spring.data.gemfire.pool.free-connection-timeout", 30000)
.withProperty("spring.data.gemfire.pool.idle-timeout", 300000L)
.withProperty("spring.data.gemfire.pool.load-conditioning-interval", 120000)
.withProperty("spring.data.gemfire.pool.locators", "localhost[10334]")
.withProperty("spring.data.gemfire.pool.max-connections", 500)
@@ -169,9 +181,11 @@ public class PoolPropertiesIntegrationTests {
.withProperty("spring.data.gemfire.pool.pr-single-hop-enabled", false)
.withProperty("spring.data.gemfire.pool.read-timeout", 15000L)
.withProperty("spring.data.gemfire.pool.retry-attempts", 2)
.withProperty("spring.data.gemfire.pool.server-connection-timeout", 30000)
.withProperty("spring.data.gemfire.pool.server-group", "testGroup")
.withProperty("spring.data.gemfire.pool.socket-buffer-size", 8192)
.withProperty("spring.data.gemfire.pool.socket-connect-timeout", 5000)
.withProperty("spring.data.gemfire.pool.socket-factory-bean-name", "mockSocketFactoryOne")
.withProperty("spring.data.gemfire.pool.statistic-interval", 1000)
.withProperty("spring.data.gemfire.pool.subscription-ack-interval", 5000)
.withProperty("spring.data.gemfire.pool.subscription-enabled", true)
@@ -189,9 +203,11 @@ public class PoolPropertiesIntegrationTests {
.withProperty("spring.data.gemfire.pool.default.pr-single-hop-enabled", false)
.withProperty("spring.data.gemfire.pool.default.read-timeout", 2000L)
.withProperty("spring.data.gemfire.pool.default.retry-attempts", 1)
.withProperty("spring.data.gemfire.pool.default.server-connection-timeout", 60000)
.withProperty("spring.data.gemfire.pool.default.server-group", "testDefaultGroup")
.withProperty("spring.data.gemfire.pool.default.socket-buffer-size", 16384)
.withProperty("spring.data.gemfire.pool.default.socket-connect-timeout", 10000)
.withProperty("spring.data.gemfire.pool.default.socket-factory-bean-name", "")
.withProperty("spring.data.gemfire.pool.default.statistic-interval", 500)
.withProperty("spring.data.gemfire.pool.default.subscription-ack-interval", 250)
.withProperty("spring.data.gemfire.pool.default.subscription-enabled", true)
@@ -212,6 +228,7 @@ public class PoolPropertiesIntegrationTests {
.withProperty("spring.data.gemfire.pool.TestPoolTwo.server-group", "testTwoGroup")
.withProperty("spring.data.gemfire.pool.TestPoolTwo.socket-buffer-size", 65536)
.withProperty("spring.data.gemfire.pool.TestPoolTwo.socket-connect-timeout", 15000)
.withProperty("spring.data.gemfire.pool.TestPoolTwo.socket-factory-bean-name", "mockSocketFactoryTwo")
.withProperty("spring.data.gemfire.pool.TestPoolTwo.statistic-interval", 2000)
.withProperty("spring.data.gemfire.pool.TestPoolTwo.subscription-ack-interval", 500)
.withProperty("spring.data.gemfire.pool.TestPoolTwo.subscription-enabled", true)
@@ -232,19 +249,26 @@ public class PoolPropertiesIntegrationTests {
Pool defaultPool = gemfireCache.getDefaultPool();
SocketFactory mockSocketFactoryOne = this.applicationContext.getBean("mockSocketFactoryOne", SocketFactory.class);
SocketFactory mockSocketFactoryTwo = this.applicationContext.getBean("mockSocketFactoryTwo", SocketFactory.class);
assertThat(mockSocketFactoryOne).isNotNull();
assertThat(mockSocketFactoryTwo).isNotNull();
assertThat(mockSocketFactoryOne).isNotSameAs(mockSocketFactoryTwo);
assertPool(defaultPool, 15000, 20000L, 180000,
275, 27, true, "DEFAULT", 15000L,
false, 2000, 1, "testDefaultGroup",
16384, 10000, 500, 250,
false, 2000, 1, 60000, "testDefaultGroup",
16384, 10000, SocketFactory.DEFAULT, 500, 250,
true, 300000, 3,
true);
Pool testPoolOne = this.applicationContext.getBean("TestPoolOne", Pool.class);
assertPool(testPoolOne, 5000, 10000L, 120000,
assertPool(testPoolOne, 30000, 300000L, 120000,
500, 50, true, "TestPoolOne", 5000L,
false, 15000, 2, "testGroup",
8192, 5000,1000, 5000,
false, 15000, 2, 30000, "testGroup",
8192, 5000, mockSocketFactoryOne,1000, 5000,
true, 180000, 2,
true);
@@ -252,15 +276,16 @@ public class PoolPropertiesIntegrationTests {
assertPool(testPoolTwo, 20000, 15000L, 60000,
1000, 100, true, "TestPoolTwo", 20000L,
false, 5000, 4, "testTwoGroup",
65536, 15000,2000, 500,
false, 5000, 4, 30000, "testTwoGroup",
65536, 15000, mockSocketFactoryTwo, 2000, 500,
true, 300000, 4,
true);
}
@EnableGemFireMockObjects
@ClientCacheApplication
@EnablePool(name = "TestPool", idleTimeout = 10000L, maxConnections = 200, minConnections = 20)
@EnablePool(name = "TestPool", idleTimeout = 10000L, maxConnections = 200, minConnections = 20,
serverConnectionTimeout = 60000, socketFactoryBeanName = "mockSocketFactory")
static class TestPoolConfiguration {
@Bean
@@ -270,6 +295,11 @@ public class PoolPropertiesIntegrationTests {
beanFactory.setRetryAttempts(1);
};
}
@Bean
SocketFactory mockSocketFactory() {
return mock(SocketFactory.class);
}
}
@EnableGemFireMockObjects
@@ -278,6 +308,16 @@ public class PoolPropertiesIntegrationTests {
@EnablePool(name = "TestPoolOne"),
@EnablePool(name = "TestPoolTwo")
})
static class TestPoolsConfiguration { }
static class TestPoolsConfiguration {
@Bean
SocketFactory mockSocketFactoryOne() {
return mock(SocketFactory.class);
}
@Bean
SocketFactory mockSocketFactoryTwo() {
return mock(SocketFactory.class);
}
}
}

View File

@@ -99,6 +99,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.SocketFactory;
import org.apache.geode.cache.control.ResourceManager;
import org.apache.geode.cache.execute.RegionFunctionContext;
import org.apache.geode.cache.lucene.LuceneIndex;
@@ -1434,6 +1435,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
AtomicInteger minConnections = new AtomicInteger(PoolFactory.DEFAULT_MIN_CONNECTIONS);
AtomicInteger readTimeout = new AtomicInteger(PoolFactory.DEFAULT_READ_TIMEOUT);
AtomicInteger retryAttempts = new AtomicInteger(PoolFactory.DEFAULT_RETRY_ATTEMPTS);
AtomicInteger serverConnectionTimeout = new AtomicInteger(PoolFactory.DEFAULT_SOCKET_CONNECT_TIMEOUT);
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);
@@ -1445,6 +1447,7 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
AtomicLong pingInterval = new AtomicLong(PoolFactory.DEFAULT_PING_INTERVAL);
AtomicReference<String> serverGroup = new AtomicReference<>(PoolFactory.DEFAULT_SERVER_GROUP);
AtomicReference<SocketFactory> socketFactory = new AtomicReference<>(PoolFactory.DEFAULT_SOCKET_FACTORY);
List<InetSocketAddress> locators = new ArrayList<>();
List<InetSocketAddress> servers = new ArrayList<>();
@@ -1489,12 +1492,18 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockPoolFactory.setRetryAttempts(anyInt()))
.thenAnswer(newSetter(retryAttempts, mockPoolFactory));
when(mockPoolFactory.setServerConnectionTimeout(anyInt()))
.thenAnswer(newSetter(serverConnectionTimeout, mockPoolFactory));
when(mockPoolFactory.setServerGroup(anyString()))
.thenAnswer(newSetter(serverGroup, mockPoolFactory));
when(mockPoolFactory.setSocketBufferSize(anyInt()))
.thenAnswer(newSetter(socketBufferSize, mockPoolFactory));
when(mockPoolFactory.setSocketFactory(any()))
.thenAnswer(newSetter(socketFactory, mockPoolFactory));
when(mockPoolFactory.setSocketConnectTimeout(anyInt()))
.thenAnswer(newSetter(socketConnectTimeout, mockPoolFactory));
@@ -1547,10 +1556,12 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
when(mockPool.getPRSingleHopEnabled()).thenReturn(prSingleHopEnabled.get());
when(mockPool.getReadTimeout()).thenReturn(readTimeout.get());
when(mockPool.getRetryAttempts()).thenReturn(retryAttempts.get());
when(mockPool.getServerConnectionTimeout()).thenReturn(serverConnectionTimeout.get());
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.getSocketFactory()).thenReturn(socketFactory.get());
when(mockPool.getStatisticInterval()).thenReturn(statisticInterval.get());
when(mockPool.getSubscriptionAckInterval()).thenReturn(subscriptionAckInterval.get());
when(mockPool.getSubscriptionEnabled()).thenReturn(subscriptionEnabled.get());
@@ -2740,6 +2751,11 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolRetryAttempts(anyInt());
doAnswer(invocation -> {
mockPoolFactory.setServerConnectionTimeout(invocation.getArgument(0));
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolServerConnectionTimeout(anyInt());
doAnswer(invocation -> {
mockPoolFactory.setServerGroup(invocation.getArgument(0));
return clientCacheFactorySpy;
@@ -2755,6 +2771,11 @@ public abstract class GemFireMockObjectsSupport extends MockObjectsSupport {
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolSocketConnectTimeout(anyInt());
doAnswer(invocation -> {
mockPoolFactory.setSocketFactory(invocation.getArgument(0));
return clientCacheFactorySpy;
}).when(clientCacheFactorySpy).setPoolSocketFactory(any());
doAnswer(invocation -> {
mockPoolFactory.setStatisticInterval(invocation.getArgument(0));
return clientCacheFactorySpy;

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.gemfire.test.mock;
import java.util.Collection;