You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by dh...@apache.org on 2019/07/22 22:09:18 UTC

[geode] 05/18: Rename RegionEntriesGaugeTest and use rules

This is an automated email from the ASF dual-hosted git repository.

dhemery pushed a commit to branch GEODE-7001-region-entry-count
in repository https://gitbox.apache.org/repos/asf/geode.git

commit 1c1356e470e8b425c65c8c4be664b23a8f388467
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Tue Jul 16 13:13:40 2019 -0700

    Rename RegionEntriesGaugeTest and use rules
---
 ...etricsTest.java => RegionEntriesGaugeTest.java} | 172 ++++++++-------------
 1 file changed, 65 insertions(+), 107 deletions(-)

diff --git a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionEntriesGaugeTest.java
similarity index 57%
rename from geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java
rename to geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionEntriesGaugeTest.java
index 6857ee9..4326d87 100644
--- a/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionMetricsTest.java
+++ b/geode-assembly/src/acceptanceTest/java/org/apache/geode/metrics/RegionEntriesGaugeTest.java
@@ -16,20 +16,12 @@
 package org.apache.geode.metrics;
 
 import static java.util.Arrays.asList;
-import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.List;
-import java.util.jar.JarEntry;
-import java.util.jar.JarOutputStream;
 
 import io.micrometer.core.instrument.Gauge;
-import org.apache.commons.io.IOUtils;
-import org.apache.logging.log4j.Logger;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -45,23 +37,31 @@ import org.apache.geode.cache.client.ClientRegionShortcut;
 import org.apache.geode.cache.execute.Function;
 import org.apache.geode.cache.execute.FunctionContext;
 import org.apache.geode.internal.AvailablePortHelper;
-import org.apache.geode.internal.logging.LogService;
-import org.apache.geode.test.compiler.ClassBuilder;
+import org.apache.geode.metrics.rules.MetricsPublishingServiceJarRule;
+import org.apache.geode.metrics.rules.SingleFunctionJarRule;
 import org.apache.geode.test.junit.categories.MetricsTest;
 import org.apache.geode.test.junit.rules.gfsh.GfshRule;
 
 @Category(MetricsTest.class)
-public class RegionMetricsTest {
-  static final Logger logger = LogService.getLogger();
+public class RegionEntriesGaugeTest {
 
   @Rule
   public GfshRule gfshRule = new GfshRule();
 
-
   @Rule
   public TemporaryFolder temporaryFolder = new TemporaryFolder();
 
+  @Rule
+  public MetricsPublishingServiceJarRule metricsPublishingServiceJarRule =
+      new MetricsPublishingServiceJarRule("metrics-publishing-service.jar",
+          SimpleMetricsPublishingService.class);
+
+  @Rule
+  public SingleFunctionJarRule functionJarRule =
+      new SingleFunctionJarRule("function.jar", GetMemberRegionEntriesGaugeFunction.class);
+
   private static final String SPACE = " ";
+
   private File locatorFolder;
   private File server1Folder;
   private File server2Folder;
@@ -85,17 +85,13 @@ public class RegionMetricsTest {
         "--dir=" + locatorFolder.getAbsolutePath(),
         "--port=" + locatorPort);
 
-    String metricsPublishingServiceJarPath =
-        newJarForMetricsPublishingServiceClass(SimpleMetricsPublishingService.class,
-            "metrics-publishing-service.jar");
-
     String startServer1Command = String.join(SPACE,
         "start server",
         "--name=" + "server1",
         "--dir=" + server1Folder.getAbsolutePath(),
         "--server-port=" + availablePorts[1],
         "--locators=localhost[" + locatorPort + "]",
-        "--classpath=" + metricsPublishingServiceJarPath);
+        "--classpath=" + metricsPublishingServiceJarRule.absolutePath());
 
     String startServer2Command = String.join(SPACE,
         "start server",
@@ -103,36 +99,20 @@ public class RegionMetricsTest {
         "--dir=" + server2Folder.getAbsolutePath(),
         "--server-port=" + availablePorts[2],
         "--locators=localhost[" + locatorPort + "]",
-        "--classpath=" + metricsPublishingServiceJarPath);
+        "--classpath=" + metricsPublishingServiceJarRule.absolutePath());
 
     gfshRule.execute(startLocatorCommand, startServer1Command, startServer2Command);
 
-//    String functionJarPath =
-//        newJarForFunctionClass(GetMemberRegionEntriesGaugeFunction.class, "function.jar");
-//    String deployCommand = "deploy --jar=" + functionJarPath;
-//    String listFunctionsCommand = "list functions";
-//
-//    gfshRule.execute(connectToLocatorCommand, deployCommand, listFunctionsCommand);
-
-    clientCache = new ClientCacheFactory().addPoolLocator("localhost", locatorPort).create();
-
-
-  }
-
-  public void createRegion(String regionName, String regionType)
-  {
     String connectToLocatorCommand =
         "connect --locator=localhost[" + locatorPort + "]";
 
-    String createRegionCommand = String.join(SPACE,
-        "create region",
-        "--name=" + regionName,
-        "--type=" + regionType);
+    // Deploy function to members
+    String deployCommand = functionJarRule.deployCommand();
+    String listFunctionsCommand = "list functions";
 
-    gfshRule.execute(connectToLocatorCommand, createRegionCommand);
+    gfshRule.execute(connectToLocatorCommand, deployCommand, listFunctionsCommand);
 
-    region = clientCache.<String, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
-        .create(regionName);
+    clientCache = new ClientCacheFactory().addPoolLocator("localhost", locatorPort).create();
   }
 
   @After
@@ -145,11 +125,12 @@ public class RegionMetricsTest {
     String stopLocatorCommand = "stop locator --dir=" + locatorFolder.getAbsolutePath();
 
     clientCache.close();
-    gfshRule.execute(connectToLocatorCommand, stopServer1Command, stopServer2Command, stopLocatorCommand);
+    gfshRule.execute(connectToLocatorCommand, stopServer1Command, stopServer2Command,
+        stopLocatorCommand);
   }
 
   @Test
-    public void whenPartitionRegionHasValues_regionEntriesGaugeShowsCountOfValues() {
+  public void whenPartitionRegionHasValues_regionEntriesGaugeShowsCountOfValues() {
     final List<String> keys = asList("a", "b", "c", "d", "e", "f", "g", "h");
     createRegion("partitionregion1", RegionShortcut.PARTITION.name());
 
@@ -159,21 +140,21 @@ public class RegionMetricsTest {
 
     region.destroy(keys.get(0));
 
-    assertThat(region.sizeOnServer()).isEqualTo(keys.size()-1);
-
-//    String connectToLocatorCommand =
-//        "connect --locator=localhost[" + locatorPort + "]";
-//    String executeFunctionCommand =
-//        "execute function --id=" + GetMemberRegionEntriesGaugeFunction.ID;
-//
-//    await().untilAsserted(() -> {
-//      String output =
-//          gfshRule.execute(connectToLocatorCommand, executeFunctionCommand).getOutputText();
-//
-//      assertThat(output.trim())
-//          .as("Returned gauge of region entries.")
-//          .endsWith("[" + keys.size() + ".0]");
-//    });
+    assertThat(region.sizeOnServer()).isEqualTo(keys.size() - 1);
+
+    // String connectToLocatorCommand =
+    // "connect --locator=localhost[" + locatorPort + "]";
+    // String executeFunctionCommand =
+    // "execute function --id=" + GetMemberRegionEntriesGaugeFunction.ID;
+    //
+    // await().untilAsserted(() -> {
+    // String output =
+    // gfshRule.execute(connectToLocatorCommand, executeFunctionCommand).getOutputText();
+    //
+    // assertThat(output.trim())
+    // .as("Returned gauge of region entries.")
+    // .endsWith("[" + keys.size() + ".0]");
+    // });
   }
 
   @Test
@@ -186,63 +167,40 @@ public class RegionMetricsTest {
     }
     region.destroy(keys.get(0));
 
-    assertThat(region.sizeOnServer()).isEqualTo(keys.size()-1);
-
-//    String connectToLocatorCommand =
-//        "connect --locator=localhost[" + locatorPort + "]";
-//    String executeFunctionCommand =
-//        "execute function --id=" + GetMemberRegionEntriesGaugeFunction.ID;
-//
-//    await().untilAsserted(() -> {
-//      String output =
-//          gfshRule.execute(connectToLocatorCommand, executeFunctionCommand).getOutputText();
-//
-//      assertThat(output.trim())
-//          .as("Returned gauge of region entries.")
-//          .endsWith("[" + keys.size() + ".0]");
-//    });
+    assertThat(region.sizeOnServer()).isEqualTo(keys.size() - 1);
+
+    // String connectToLocatorCommand =
+    // "connect --locator=localhost[" + locatorPort + "]";
+    // String executeFunctionCommand =
+    // "execute function --id=" + GetMemberRegionEntriesGaugeFunction.ID;
+    //
+    // await().untilAsserted(() -> {
+    // String output =
+    // gfshRule.execute(connectToLocatorCommand, executeFunctionCommand).getOutputText();
+    //
+    // assertThat(output.trim())
+    // .as("Returned gauge of region entries.")
+    // .endsWith("[" + keys.size() + ".0]");
+    // });
   }
 
-  private String newJarForMetricsPublishingServiceClass(Class clazz, String jarName)
-      throws IOException {
-    File jar = temporaryFolder.newFile(jarName);
-
-    String className = clazz.getName();
-    String classAsPath = className.replace('.', '/') + ".class";
-    InputStream stream = clazz.getClassLoader().getResourceAsStream(classAsPath);
-    byte[] bytes = IOUtils.toByteArray(stream);
-    try (FileOutputStream out = new FileOutputStream(jar)) {
-      JarOutputStream jarOutputStream = new JarOutputStream(out);
-
-      // Add the class file to the JAR file
-      JarEntry classEntry = new JarEntry(classAsPath);
-      classEntry.setTime(System.currentTimeMillis());
-      jarOutputStream.putNextEntry(classEntry);
-      jarOutputStream.write(bytes);
-      jarOutputStream.closeEntry();
-
-      String metaInfPath = "META-INF/services/org.apache.geode.metrics.MetricsPublishingService";
-
-      JarEntry metaInfEntry = new JarEntry(metaInfPath);
-      metaInfEntry.setTime(System.currentTimeMillis());
-      jarOutputStream.putNextEntry(metaInfEntry);
-      jarOutputStream.write(className.getBytes());
-      jarOutputStream.closeEntry();
-
-      jarOutputStream.close();
-    }
+  private void createRegion(String regionName, String regionType) {
+    String connectToLocatorCommand =
+        "connect --locator=localhost[" + locatorPort + "]";
 
-    return jar.getAbsolutePath();
-  }
+    String createRegionCommand = String.join(SPACE,
+        "create region",
+        "--name=" + regionName,
+        "--type=" + regionType);
 
-  private String newJarForFunctionClass(Class clazz, String jarName) throws IOException {
-    File jar = temporaryFolder.newFile(jarName);
-    new ClassBuilder().writeJarFromClass(clazz, jar);
-    return jar.getAbsolutePath();
+    gfshRule.execute(connectToLocatorCommand, createRegionCommand);
+
+    region = clientCache.<String, String>createClientRegionFactory(ClientRegionShortcut.PROXY)
+        .create(regionName);
   }
 
   public static class GetMemberRegionEntriesGaugeFunction implements Function<Void> {
-    static final String ID = "GetEventsReceivedCountFunction";
+    private static final String ID = "GetEventsReceivedCountFunction";
 
     @Override
     public void execute(FunctionContext<Void> context) {