SGF-149 implement chunking in PojoFunctionWrapper

This commit is contained in:
David Turanski
2013-01-22 20:11:45 -05:00
parent 7b52197f0f
commit 36a2a3ddbc
9 changed files with 390 additions and 50 deletions

View File

@@ -0,0 +1,139 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.springframework.data.gemfire.function;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import com.gemstone.gemfire.cache.execute.ResultSender;
/**
* @author David Turanski
*
*/
public class BatchingResultSenderTest {
@Test
public void testArrayChunking() {
testBatchingResultSender(new TestArrayResultSender(),1);
testBatchingResultSender(new TestArrayResultSender(),0);
testBatchingResultSender(new TestArrayResultSender(),9);
testBatchingResultSender(new TestArrayResultSender(),10);
testBatchingResultSender(new TestArrayResultSender(),1000);
}
@Test
public void testListChunking() {
testBatchingResultSender(new TestListResultSender(),1);
testBatchingResultSender(new TestListResultSender(),0);
testBatchingResultSender(new TestListResultSender(),9);
testBatchingResultSender(new TestListResultSender(),10);
testBatchingResultSender(new TestListResultSender(),1000);
}
private void testBatchingResultSender(AbstractTestResultSender resultSender, int batchSize){
BatchingResultSender brs = new BatchingResultSender(batchSize, resultSender);
List<Integer> result = new ArrayList<Integer>();
for (int i = 0; i< 100; i++) {
result.add(i);
}
//TODO: Clean this up. Ok for test code
if (resultSender instanceof TestArrayResultSender) {
brs.sendArrayResults(result.toArray(new Integer[100]));
} else {
brs.sendResults(result);
}
assertEquals(100,resultSender.getResults().size());
for(int i=0; i< 100; i++) {
assertEquals(i,resultSender.getResults().get(i));
}
}
public static abstract class AbstractTestResultSender implements ResultSender<Object> {
private List<Object> results = new ArrayList<Object>();
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.ResultSender#lastResult(java.lang.Object)
*/
@Override
public void lastResult(Object arg0) {
if (arg0 == null) {
return;
}
addResults(arg0, results);
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.ResultSender#sendException(java.lang.Throwable)
*/
@Override
public void sendException(Throwable arg0) {
fail();
}
/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.execute.ResultSender#sendResult(java.lang.Object)
*/
@Override
public void sendResult(Object arg0) {
if (arg0 == null) {
return;
}
addResults(arg0, results);
}
protected abstract void addResults(Object item, List<Object> results);
public List<Object> getResults() {
return this.results;
}
}
public static class TestArrayResultSender extends AbstractTestResultSender {
protected void addResults(Object arg0, List<Object> results) {
assertTrue(arg0.getClass().isArray());
Object[] array = (Object[]) arg0;
for (Object obj: array) {
results.add(obj);
}
}
}
public static class TestListResultSender extends AbstractTestResultSender {
protected void addResults(Object arg0, List<Object> results) {
if (arg0 == null) {
return;
}
assertTrue(arg0 instanceof Collection);
Collection<?> list = (Collection<?>) arg0;
results.addAll(list);
}
}
}

View File

@@ -102,7 +102,7 @@ public class FunctionIntegrationTests {
assertEquals(2,map.get("two").intValue());
assertEquals(3,map.get("three").intValue());
result = template.execute("collections",Arrays.asList(new Integer[]{1,2,3,4,5}));
result = template.executeAndExtract("collections",Arrays.asList(new Integer[]{1,2,3,4,5}));
assertTrue(result.getClass().getName(),result instanceof List);
List<?> list = (List<?>)result;
@@ -111,7 +111,13 @@ public class FunctionIntegrationTests {
assertEquals(i,list.get(i-1));
}
}
@Test
public void testArrayReturnTypes() {
GemfireOnRegionOperations template = new GemfireOnRegionFunctionTemplate(region);
Object result = template.executeAndExtract("arrays",new int[]{1,2,3,4,5});
assertTrue(result.getClass().getName(),result instanceof int[]);
}
/*
* This gets wrapped in a GemFire Function and registered on the forked server.
*/
@@ -143,5 +149,10 @@ public class FunctionIntegrationTests {
}
return new HashMap<String, Integer>(dataSet);
}
@GemfireFunction(id="arrays",batchSize=2)
public int[] collections(int[] args) {
return args;
}
}
}

View File

@@ -21,6 +21,7 @@ package org.springframework.data.gemfire.function.execution;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.AccessibleObject;
@@ -49,7 +50,6 @@ public class GemfireFunctionProxyFactoryBeanTests {
functionOperations = mock(GemfireFunctionOperations.class);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeAndExtractWithAnnotatedFunctionId() throws Throwable {
@@ -58,17 +58,17 @@ public class GemfireFunctionProxyFactoryBeanTests {
MethodInvocation invocation = new TestInvocation(IFoo.class).withMethodNameAndArgTypes("oneArg",String.class);
List results = Arrays.asList(new Integer[]{1});
int results = 1;
when(functionOperations.execute("oneArg",invocation.getArguments())).thenReturn(results);
when(functionOperations.executeAndExtract("oneArg",invocation.getArguments())).thenReturn(results);
Object result = proxy.invoke(invocation);
assertTrue(result instanceof Integer);
assertEquals(new Integer(1),result);
verify(functionOperations).executeAndExtract("oneArg",invocation.getArguments());
assertTrue(result.getClass().getName(), result instanceof Integer);
assertEquals(1,result);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "rawtypes" })
@Test
public void testInvoke() throws Throwable {
@@ -79,9 +79,9 @@ public class GemfireFunctionProxyFactoryBeanTests {
List results = Arrays.asList(new Integer[]{1,2,3});
when(functionOperations.execute("collections",invocation.getArguments())).thenReturn(results);
when(functionOperations.executeAndExtract("collections",invocation.getArguments())).thenReturn(results);
Object result = proxy.invoke(invocation);
verify(functionOperations).executeAndExtract("collections",invocation.getArguments()); ;
assertTrue(result instanceof List);
assertEquals(3,((List<?>)result).size());
}