1818
1919import static org .assertj .core .api .Assertions .assertThat ;
2020
21- import java .io .IOException ;
2221import java .util .ArrayList ;
2322import java .util .List ;
2423
2524import org .junit .AfterClass ;
2625import org .junit .BeforeClass ;
27- import org .junit .Ignore ;
2826import org .junit .Test ;
2927import 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-
4329import org .springframework .beans .factory .annotation .Autowired ;
30+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
31+ import org .springframework .data .gemfire .config .annotation .CacheServerApplication ;
4432import org .springframework .data .gemfire .config .annotation .ClientCacheApplication ;
33+ import org .springframework .data .gemfire .function .annotation .GemfireFunction ;
4534import 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 ;
4639import org .springframework .data .gemfire .process .ProcessWrapper ;
4740import org .springframework .data .gemfire .test .support .ClientServerIntegrationTestsSupport ;
4841import org .springframework .test .context .ContextConfiguration ;
4942import 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 )
5851public 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}
0 commit comments