Skip to content

Commit 00ab6dc

Browse files
Patrick Johnsonjxblum
authored andcommitted
Fix test issues and conditionally extract results when executing on single member.
Resolves spring-projectsgh-37.
1 parent 888e3f5 commit 00ab6dc

File tree

5 files changed

+53
-92
lines changed

5 files changed

+53
-92
lines changed

spring-data-geode/src/main/java/org/springframework/data/gemfire/function/execution/GemfireFunctionProxyFactoryBean.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import org.springframework.aop.framework.ProxyFactory;
2020
import org.springframework.aop.support.AopUtils;
2121
import org.springframework.beans.factory.FactoryBean;
22-
import org.springframework.data.gemfire.function.annotation.OnServers;
22+
import org.springframework.data.gemfire.function.annotation.OnMember;
23+
import org.springframework.data.gemfire.function.annotation.OnServer;
2324
import org.springframework.data.gemfire.support.AbstractFactoryBeanSupport;
2425
import org.springframework.lang.NonNull;
2526
import org.springframework.lang.Nullable;
@@ -126,13 +127,14 @@ protected Class<?> getFunctionExecutionInterface() {
126127
.getMethodMetadata(method)
127128
.getFunctionId();
128129

129-
return isFunctionExecutionForAllServers(method)
130-
? template.execute(functionId, args)
131-
: template.executeAndExtract(functionId, args);
130+
return isFunctionExecutedOnSingleServerOrSingleMember(method)
131+
? template.executeAndExtract(functionId, args)
132+
: template.execute(functionId, args);
132133
}
133134

134-
private boolean isFunctionExecutionForAllServers(Method method) {
135-
return method.getDeclaringClass().isAnnotationPresent(OnServers.class);
135+
protected boolean isFunctionExecutedOnSingleServerOrSingleMember(@NonNull Method method) {
136+
return method.getDeclaringClass().isAnnotationPresent(OnServer.class)
137+
|| method.getDeclaringClass().isAnnotationPresent(OnMember.class);
136138
}
137139

138140
protected Object resolveResult(MethodInvocation invocation, Object result) {

spring-data-geode/src/test/java/org/springframework/data/gemfire/function/execution/onservers/FunctionsReturnResultsFromAllServersIntegrationTests.java

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,39 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
import java.io.IOException;
2221
import java.util.ArrayList;
2322
import java.util.List;
2423

2524
import org.junit.AfterClass;
2625
import org.junit.BeforeClass;
27-
import org.junit.Ignore;
2826
import org.junit.Test;
2927
import org.junit.runner.RunWith;
3028

31-
import org.apache.geode.StatisticDescriptor;
32-
import org.apache.geode.Statistics;
33-
import org.apache.geode.StatisticsType;
34-
import org.apache.geode.cache.Cache;
35-
import org.apache.geode.cache.CacheFactory;
36-
import org.apache.geode.cache.execute.Function;
37-
import org.apache.geode.cache.execute.FunctionContext;
38-
import org.apache.geode.cache.execute.FunctionService;
39-
import org.apache.geode.cache.server.CacheServer;
40-
import org.apache.geode.distributed.internal.InternalDistributedSystem;
41-
import org.apache.geode.internal.statistics.StatisticsManager;
42-
4329
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31+
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
4432
import org.springframework.data.gemfire.config.annotation.ClientCacheApplication;
33+
import org.springframework.data.gemfire.function.annotation.GemfireFunction;
4534
import org.springframework.data.gemfire.function.config.EnableGemfireFunctionExecutions;
35+
import org.springframework.data.gemfire.function.config.EnableGemfireFunctions;
36+
import org.springframework.data.gemfire.function.sample.AllServersAdminFunctions;
37+
import org.springframework.data.gemfire.function.sample.Metric;
38+
import org.springframework.data.gemfire.function.sample.SingleServerAdminFunctions;
4639
import org.springframework.data.gemfire.process.ProcessWrapper;
4740
import org.springframework.data.gemfire.test.support.ClientServerIntegrationTestsSupport;
4841
import org.springframework.test.context.ContextConfiguration;
4942
import org.springframework.test.context.junit4.SpringRunner;
43+
import org.springframework.transaction.annotation.EnableTransactionManagement;
5044

5145
/**
5246
* @author Patrick Johnson
5347
*/
5448
@SuppressWarnings("unused")
5549
@RunWith(SpringRunner.class)
56-
@ContextConfiguration(classes = FunctionsReturnResultsFromAllServersIntegrationTests.GeodeClientConfiguration.class)
57-
@Ignore
50+
@ContextConfiguration(classes = FunctionsReturnResultsFromAllServersIntegrationTests.TestConfiguration.class)
5851
public class FunctionsReturnResultsFromAllServersIntegrationTests extends ClientServerIntegrationTestsSupport {
5952

60-
private static final int PORT_1 = 40407;
61-
private static final int PORT_2 = 40403;
53+
private static final int NUMBER_OF_METRICS = 10;
6254

6355
private static ProcessWrapper gemfireServer1;
6456
private static ProcessWrapper gemfireServer2;
@@ -72,15 +64,21 @@ public class FunctionsReturnResultsFromAllServersIntegrationTests extends Client
7264
@BeforeClass
7365
public static void startGemFireServer() throws Exception {
7466

75-
gemfireServer1 = run(MetricsFunctionServerProcess.class,
76-
String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, PORT_1));
67+
final int port1 = findAvailablePort();
68+
69+
gemfireServer1 = run(MetricsFunctionServerConfiguration.class,
70+
String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, port1));
71+
72+
waitForServerToStart(DEFAULT_HOSTNAME, port1);
7773

78-
waitForServerToStart(DEFAULT_HOSTNAME, PORT_1);
74+
final int port2 = findAvailablePort();
7975

80-
gemfireServer2 = run(MetricsFunctionServerProcess.class,
81-
String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, PORT_2));
76+
gemfireServer2 = run(MetricsFunctionServerConfiguration.class,
77+
String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, port2));
8278

83-
waitForServerToStart(DEFAULT_HOSTNAME, PORT_2);
79+
waitForServerToStart(DEFAULT_HOSTNAME, port2);
80+
81+
System.setProperty(GEMFIRE_POOL_SERVERS_PROPERTY, String.format("%s[%d],%s[%d]", DEFAULT_HOSTNAME, port1, DEFAULT_HOSTNAME, port2));
8482
}
8583

8684
@AfterClass
@@ -93,81 +91,42 @@ public static void stopGemFireServer() {
9391
public void executeFunctionOnAllServers() {
9492
List<List<Metric>> metrics = allServersAdminFunctions.getAllMetrics();
9593
assertThat(metrics.size()).isEqualTo(2);
94+
assertThat(metrics.get(0).size()).isEqualTo(NUMBER_OF_METRICS);
95+
assertThat(metrics.get(1).size()).isEqualTo(NUMBER_OF_METRICS);
9696
}
9797

9898
@Test
9999
public void executeFunctionOnSingleServer() {
100100
List<Metric> metrics = singleServerAdminFunctions.getAllMetrics();
101-
assertThat(metrics.size()).isEqualTo(672);
101+
assertThat(metrics.size()).isEqualTo(NUMBER_OF_METRICS);
102102
}
103103

104-
@ClientCacheApplication(servers = {
105-
@ClientCacheApplication.Server(port = PORT_1),
106-
@ClientCacheApplication.Server(port = PORT_2)}
107-
)
104+
@ClientCacheApplication
108105
@EnableGemfireFunctionExecutions(basePackageClasses = AllServersAdminFunctions.class)
109-
static class GeodeClientConfiguration { }
110-
111-
static class MetricsFunctionServerProcess {
112-
113-
private static final int DEFAULT_CACHE_SERVER_PORT = 40404;
114-
115-
private static final String CACHE_SERVER_PORT_PROPERTY = "spring.data.gemfire.cache.server.port";
116-
private static final String GEMFIRE_NAME = "MetricsServer" + getCacheServerPort();
117-
118-
public static void main(String[] args) throws Exception {
119-
registerFunctions(startCacheServer(newGemFireCache()));
120-
}
121-
122-
private static Cache newGemFireCache() {
123-
124-
return new CacheFactory()
125-
.set("name", GEMFIRE_NAME)
126-
.create();
127-
}
128-
129-
private static Cache startCacheServer(Cache gemfireCache) throws IOException {
130-
CacheServer cacheServer = gemfireCache.addCacheServer();
131-
cacheServer.setPort(getCacheServerPort());
132-
cacheServer.start();
133-
return gemfireCache;
134-
}
106+
@EnableTransactionManagement
107+
static class TestConfiguration {
108+
}
135109

136-
private static int getCacheServerPort() {
137-
return Integer.getInteger(CACHE_SERVER_PORT_PROPERTY, DEFAULT_CACHE_SERVER_PORT);
138-
}
110+
@CacheServerApplication
111+
@EnableGemfireFunctions
112+
public static class MetricsFunctionServerConfiguration {
139113

140-
private static Cache registerFunctions(Cache gemfireCache) {
114+
public static void main(String[] args) {
141115

142-
FunctionService.registerFunction(new GetAllMetricsFunction());
116+
AnnotationConfigApplicationContext applicationContext =
117+
new AnnotationConfigApplicationContext(MetricsFunctionServerConfiguration.class);
143118

144-
return gemfireCache;
119+
applicationContext.registerShutdownHook();
145120
}
146-
}
147-
148-
static class GetAllMetricsFunction implements Function<List<Metric>> {
149-
150-
private final InternalDistributedSystem system =
151-
(InternalDistributedSystem) CacheFactory.getAnyInstance().getDistributedSystem();
152121

153-
@Override
154-
public void execute(FunctionContext context) {
122+
@GemfireFunction(id = "GetAllMetricsFunction", hasResult = true)
123+
public List<Metric> getMetrics() {
155124
List<Metric> allMetrics = new ArrayList<>();
156-
StatisticsManager statisticsManager = system.getStatisticsManager();
157-
for (Statistics statistics : statisticsManager.getStatsList()) {
158-
StatisticsType statisticsType = statistics.getType();
159-
for (StatisticDescriptor descriptor : statisticsType.getStatistics()) {
160-
String statName = descriptor.getName();
161-
Metric metric = new Metric(statName, statistics.get(statName), statisticsType.getName(), statistics.getTextId());
162-
allMetrics.add(metric);
163-
}
125+
for (int i = 0; i < NUMBER_OF_METRICS; i++) {
126+
Metric metric = new Metric("statName" + i, i, "statCat" + i, "statType" + i);
127+
allMetrics.add(metric);
164128
}
165-
context.getResultSender().lastResult(allMetrics);
166-
}
167-
168-
@Override
169-
public String getId() {
170-
return getClass().getSimpleName();
129+
return allMetrics;
171130
}
172131
}
173132
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
package org.springframework.data.gemfire.function.execution.onservers;
17+
package org.springframework.data.gemfire.function.sample;
1818

1919
import java.util.List;
2020

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
package org.springframework.data.gemfire.function.execution.onservers;
17+
package org.springframework.data.gemfire.function.sample;
1818

1919
import java.io.Serializable;
2020

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*
1616
*/
17-
package org.springframework.data.gemfire.function.execution.onservers;
17+
package org.springframework.data.gemfire.function.sample;
1818

1919
import org.springframework.data.gemfire.function.annotation.FunctionId;
2020
import org.springframework.data.gemfire.function.annotation.OnServer;

0 commit comments

Comments
 (0)