SGF-434 - Add a durable GemFire client cache test to assert proper behavior by SDG.
Removed the ClientCacheFactoryBean.readyForEvents() method and moved the logic to onApplicationEvent(..). Updated the spring-gemfire 1.6 and 1.7 XSD with the new keep-alive attribue on the client-cache element. Changed the generic signature of the CacheFactoryBean class back to com.gemstone.gemfire.cache.Cache due to the Spring container bean resolution Exception when using JavaConfig in SDG 1.7 with GemFire 8.1 and core Spring Framework 4.1.7. (cherry picked from commit a458115c7f145bce9f02af71572920894ba66465) Signed-off-by: John Blum <jblum@pivotal.io>
This commit is contained in:
@@ -27,13 +27,13 @@ import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -42,8 +42,8 @@ import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.data.gemfire.TestUtils;
|
||||
import org.springframework.data.gemfire.config.GemfireConstants;
|
||||
import org.springframework.data.util.ReflectionUtils;
|
||||
|
||||
import com.gemstone.gemfire.cache.CacheClosedException;
|
||||
import com.gemstone.gemfire.cache.GemFireCache;
|
||||
import com.gemstone.gemfire.cache.client.ClientCache;
|
||||
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
|
||||
@@ -60,7 +60,7 @@ import com.gemstone.gemfire.pdx.PdxSerializer;
|
||||
* @see org.mockito.Mockito
|
||||
* @see org.junit.Test
|
||||
* @see org.springframework.data.gemfire.client.ClientCacheFactoryBean
|
||||
* @see org.springframework.data.gemfire.TestUtils
|
||||
* @see com.gemstone.gemfire.cache.client.ClientCache
|
||||
* @since 1.7.0
|
||||
*/
|
||||
public class ClientCacheFactoryBeanTest {
|
||||
@@ -415,78 +415,83 @@ public class ClientCacheFactoryBeanTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventSignalsReadyForEvents() throws Exception {
|
||||
ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onApplicationEventCallsClientCacheReadyForEvents() {
|
||||
final ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
|
||||
when(mockClientCache.isClosed()).thenReturn(false);
|
||||
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
|
||||
@Override protected <T extends GemFireCache> T fetchCache() {
|
||||
return (T) mockClientCache;
|
||||
}
|
||||
};
|
||||
|
||||
clientCacheFactoryBean.setReadyForEvents(true);
|
||||
|
||||
ReflectionUtils.setField(ReflectionUtils.findField(ClientCacheFactoryBean.class,
|
||||
new org.springframework.util.ReflectionUtils.FieldFilter() {
|
||||
@Override public boolean matches(final Field field) {
|
||||
return field.getName().equals("cache");
|
||||
}
|
||||
}), clientCacheFactoryBean, mockClientCache);
|
||||
|
||||
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(true));
|
||||
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
|
||||
|
||||
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
|
||||
|
||||
verify(mockClientCache, times(1)).isClosed();
|
||||
verify(mockClientCache, times(1)).readyForEvents();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventDoesNotSignalReadyForEventsWhenClientCacheIsClosed() {
|
||||
ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onApplicationEventDoesNotCallClientCacheReadyForEventsWhenClientCacheFactoryBeanReadyForEventsIsFalse() {
|
||||
final ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
|
||||
when(mockClientCache.isClosed()).thenReturn(true);
|
||||
doThrow(new RuntimeException("test")).when(mockClientCache).readyForEvents();
|
||||
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
|
||||
@Override protected <T extends GemFireCache> T fetchCache() {
|
||||
return (T) mockClientCache;
|
||||
}
|
||||
};
|
||||
|
||||
clientCacheFactoryBean.setReadyForEvents(true);
|
||||
clientCacheFactoryBean.setReadyForEvents(false);
|
||||
|
||||
ReflectionUtils.setField(ReflectionUtils.findField(ClientCacheFactoryBean.class,
|
||||
new org.springframework.util.ReflectionUtils.FieldFilter() {
|
||||
@Override public boolean matches(final Field field) {
|
||||
return field.getName().equals("cache");
|
||||
}
|
||||
}), clientCacheFactoryBean, mockClientCache);
|
||||
|
||||
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(true));
|
||||
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(false));
|
||||
|
||||
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
|
||||
|
||||
verify(mockClientCache, times(1)).isClosed();
|
||||
verify(mockClientCache, never()).readyForEvents();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onApplicationEventDoesNotSignalReadyForEventsWhenClientCacheFactoryBeanReadyForEventsIsFalse() {
|
||||
ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onApplicationEventHandlesIllegalStateException() {
|
||||
final ClientCache mockClientCache = mock(ClientCache.class, "MockClientCache");
|
||||
|
||||
when(mockClientCache.isClosed()).thenReturn(false);
|
||||
doThrow(new IllegalStateException("non-durable client")).when(mockClientCache).readyForEvents();
|
||||
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean();
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
|
||||
@Override protected <T extends GemFireCache> T fetchCache() {
|
||||
return (T) mockClientCache;
|
||||
}
|
||||
};
|
||||
|
||||
clientCacheFactoryBean.setReadyForEvents(false);
|
||||
clientCacheFactoryBean.setReadyForEvents(true);
|
||||
|
||||
ReflectionUtils.setField(ReflectionUtils.findField(ClientCacheFactoryBean.class,
|
||||
new org.springframework.util.ReflectionUtils.FieldFilter() {
|
||||
@Override public boolean matches(final Field field) {
|
||||
return field.getName().equals("cache");
|
||||
}
|
||||
}), clientCacheFactoryBean, mockClientCache);
|
||||
|
||||
assertThat(clientCacheFactoryBean.getReadyForEvents(), is(false));
|
||||
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
|
||||
|
||||
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
|
||||
|
||||
verify(mockClientCache, never()).isClosed();
|
||||
verify(mockClientCache, never()).readyForEvents();
|
||||
verify(mockClientCache, times(1)).readyForEvents();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void onApplicationEventHandlesCacheClosedException() {
|
||||
ClientCacheFactoryBean clientCacheFactoryBean = new ClientCacheFactoryBean() {
|
||||
@Override protected <T extends GemFireCache> T fetchCache() {
|
||||
throw new CacheClosedException("test");
|
||||
}
|
||||
};
|
||||
|
||||
clientCacheFactoryBean.setReadyForEvents(true);
|
||||
|
||||
assertThat(clientCacheFactoryBean.isReadyForEvents(), is(true));
|
||||
|
||||
clientCacheFactoryBean.onApplicationEvent(mock(ContextRefreshedEvent.class, "MockContextRefreshedEvent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -541,7 +546,7 @@ public class ClientCacheFactoryBeanTest {
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void setPoolNameToInvalidValue() {
|
||||
public void setPoolNameWithAnIllegalArgument() {
|
||||
try {
|
||||
new ClientCacheFactoryBean().setPoolName(" ");
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.data.gemfire.client;
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assume.assumeThat;
|
||||
|
||||
@@ -221,12 +222,16 @@ public class DurableClientCacheIntegrationTest extends AbstractGemFireClientServ
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (RUN_COUNT.get() == 2 && bean instanceof ClientCache) {
|
||||
// NOTE pending event count is possibly 3 because it includes the 2 puts from the client cache producer
|
||||
// as well as the "marker"
|
||||
assertThat(((ClientCache) bean).getDefaultPool().getPendingEventCount(), is(equalTo(
|
||||
RUN_COUNT.get() == 1 ? -2 : 3)));
|
||||
pause(TimeUnit.SECONDS.toMillis(3));
|
||||
if (bean instanceof ClientCache) {
|
||||
if (RUN_COUNT.get() == 1) {
|
||||
assertThat(((ClientCache) bean).getDefaultPool().getPendingEventCount(), is(equalTo(-2)));
|
||||
}
|
||||
else {
|
||||
// NOTE pending event count is possibly 3 because it includes the 2 puts from the client cache producer
|
||||
// as well as the "marker"
|
||||
assertThat(((ClientCache) bean).getDefaultPool().getPendingEventCount(), is(greaterThanOrEqualTo(2)));
|
||||
pause(TimeUnit.SECONDS.toMillis(3));
|
||||
}
|
||||
}
|
||||
|
||||
return bean;
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
* 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.test;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
@@ -19,30 +21,34 @@ import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author David Turanski
|
||||
*
|
||||
* @author John Blum
|
||||
*/
|
||||
public class GemfireTestApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
private static Log logger = LogFactory.getLog(GemfireTestApplicationContextInitializer.class);
|
||||
private static final Log LOG = LogFactory.getLog(GemfireTestApplicationContextInitializer.class);
|
||||
|
||||
public static final String GEMFIRE_TEST_RUNNER_DISABLED = "org.springframework.data.gemfire.test.GemfireTestRunner.nomock";
|
||||
|
||||
/* (non-Javadoc)
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.context.ApplicationContextInitializer#initialize(org.springframework.context.ConfigurableApplicationContext)
|
||||
*/
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
if (StringUtils.hasText(System.getProperty(GEMFIRE_TEST_RUNNER_DISABLED))) {
|
||||
String value = System.getProperty(GEMFIRE_TEST_RUNNER_DISABLED);
|
||||
String gemfireTestRunnerDisabled = System.getProperty(GEMFIRE_TEST_RUNNER_DISABLED, Boolean.FALSE.toString());
|
||||
|
||||
if (!("NO".equalsIgnoreCase(value) || "FALSE".equalsIgnoreCase(value))) {
|
||||
logger.warn(String.format("Mocks disabled. Using real GemFire components: %1$s = %2$s",
|
||||
GEMFIRE_TEST_RUNNER_DISABLED, value));
|
||||
return;
|
||||
}
|
||||
if (isGemFireTestRunnerDisable(gemfireTestRunnerDisabled)) {
|
||||
LOG.warn(String.format("WARNING - Mocks disabled; Using real GemFire components (%1$s = %2$s)",
|
||||
GEMFIRE_TEST_RUNNER_DISABLED, gemfireTestRunnerDisabled));
|
||||
}
|
||||
else {
|
||||
applicationContext.getBeanFactory().addBeanPostProcessor(new GemfireTestBeanPostProcessor());
|
||||
}
|
||||
}
|
||||
|
||||
applicationContext.getBeanFactory().addBeanPostProcessor(new GemfireTestBeanPostProcessor());
|
||||
private boolean isGemFireTestRunnerDisable(final String systemPropertyValue) {
|
||||
return (Boolean.valueOf(StringUtils.trimAllWhitespace(systemPropertyValue))
|
||||
|| "yes".equalsIgnoreCase(systemPropertyValue));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user