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

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new b9587c2  GEODE-5716: add debug capability to the processes started 
using GfshRule (#2446)
b9587c2 is described below

commit b9587c24f3a3097e8333311b0072d7586aaa8c39
Author: jinmeiliao <[email protected]>
AuthorDate: Tue Sep 11 09:23:56 2018 -0700

    GEODE-5716: add debug capability to the processes started using GfshRule 
(#2446)
---
 .../test/junit/rules/gfsh/DebuggableCommand.java   | 30 ++++++++++++++++++
 .../geode/test/junit/rules/gfsh/GfshRule.java      | 36 +++++++++++++++++-----
 .../geode/test/junit/rules/gfsh/GfshScript.java    | 21 ++++++++-----
 3 files changed, 73 insertions(+), 14 deletions(-)

diff --git 
a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/DebuggableCommand.java
 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/DebuggableCommand.java
new file mode 100644
index 0000000..2d2e8a3
--- /dev/null
+++ 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/DebuggableCommand.java
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more 
contributor license
+ * agreements. See the NOTICE file distributed with this work for additional 
information regarding
+ * copyright ownership. The ASF licenses this file to You 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.apache.geode.test.junit.rules.gfsh;
+
+public class DebuggableCommand {
+  final int debugPort;
+  final String command;
+
+  public DebuggableCommand(String command) {
+    this.command = command;
+    this.debugPort = -1;
+  }
+
+  public DebuggableCommand(String command, int debugPort) {
+    this.command = command;
+    this.debugPort = debugPort;
+  }
+}
diff --git 
a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
index 0163897..5e1e01a 100644
--- 
a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
+++ 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshRule.java
@@ -38,6 +38,13 @@ import org.apache.geode.test.junit.rules.RequiresGeodeHome;
  * The {@code GfshRule} allows a test to execute Gfsh commands via the actual 
(fully-assembled) gfsh
  * binaries. Each call to {@link GfshRule#execute(GfshScript)} will invoke the 
given gfsh script in
  * a forked JVM. The {@link GfshRule#after()} method will attempt to clean up 
all forked JVMs.
+ *
+ * if you want to debug into the gfsh or the locator/servers started using 
this rule, you can do:
+ *
+ * GfshScript.of(new DebuggableCommand("start locator", 
30001)).execute(gfshRule, 30000);
+ *
+ * this will set the gfsh to be debuggable at port 30000, and the locator 
started to be debuggable
+ * at port 30001
  */
 public class GfshRule extends ExternalResource {
 
@@ -125,11 +132,15 @@ public class GfshRule extends ExternalResource {
   }
 
   public GfshExecution execute(GfshScript gfshScript) {
+    return execute(gfshScript, -1);
+  }
+
+  public GfshExecution execute(GfshScript gfshScript, int gfshDebugPort) {
     GfshExecution gfshExecution;
     try {
       File workingDir = new File(temporaryFolder.getRoot(), 
gfshScript.getName());
       workingDir.mkdirs();
-      Process process = toProcessBuilder(gfshScript, gfsh, workingDir).start();
+      Process process = toProcessBuilder(gfshScript, gfsh, workingDir, 
gfshDebugPort).start();
       gfshExecution = new GfshExecution(process, workingDir);
       gfshExecutions.add(gfshExecution);
       gfshScript.awaitIfNecessary(gfshExecution);
@@ -140,7 +151,8 @@ public class GfshRule extends ExternalResource {
     return gfshExecution;
   }
 
-  protected ProcessBuilder toProcessBuilder(GfshScript gfshScript, Path 
gfshPath, File workingDir) {
+  protected ProcessBuilder toProcessBuilder(GfshScript gfshScript, Path 
gfshPath, File workingDir,
+      int gfshDebugPort) {
     List<String> commandsToExecute = new ArrayList<>();
 
     if (isWindows()) {
@@ -149,16 +161,22 @@ public class GfshRule extends ExternalResource {
     }
     commandsToExecute.add(gfshPath.toAbsolutePath().toString());
 
-    for (String command : gfshScript.getCommands()) {
-      commandsToExecute.add("-e " + command);
+    for (DebuggableCommand command : gfshScript.getCommands()) {
+      if (command.debugPort > 0) {
+        commandsToExecute.add("-e " + command.command
+            + " 
--J='-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
+            + command.debugPort + "'");
+      } else {
+        commandsToExecute.add("-e " + command.command);
+      }
     }
 
     ProcessBuilder processBuilder = new ProcessBuilder(commandsToExecute);
     processBuilder.directory(workingDir);
 
     List<String> extendedClasspath = gfshScript.getExtendedClasspath();
+    Map<String, String> environmentMap = processBuilder.environment();
     if (!extendedClasspath.isEmpty()) {
-      Map<String, String> environmentMap = processBuilder.environment();
       String classpathKey = "CLASSPATH";
       String existingJavaArgs = environmentMap.get(classpathKey);
       String specified = String.join(PATH_SEPARATOR, extendedClasspath);
@@ -167,6 +185,10 @@ public class GfshRule extends ExternalResource {
               specified);
       environmentMap.put(classpathKey, newValue);
     }
+    if (gfshDebugPort > 0) {
+      environmentMap.put("JAVA_ARGS",
+          "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=" + 
gfshDebugPort);
+    }
 
     return processBuilder;
   }
@@ -180,7 +202,7 @@ public class GfshRule extends ExternalResource {
     String stopServerCommand = "stop server --dir=" + 
quoteArgument(dir.toString());
 
     GfshScript stopServerScript =
-        new 
GfshScript(stopServerCommand).withName("teardown-stop-server").awaitQuietly();
+        
GfshScript.of(stopServerCommand).withName("teardown-stop-server").awaitQuietly();
     execute(stopServerScript);
   }
 
@@ -188,7 +210,7 @@ public class GfshRule extends ExternalResource {
     String stopLocatorCommand = "stop locator --dir=" + 
quoteArgument(dir.toString());
 
     GfshScript stopServerScript =
-        new 
GfshScript(stopLocatorCommand).withName("teardown-stop-locator").awaitQuietly();
+        
GfshScript.of(stopLocatorCommand).withName("teardown-stop-locator").awaitQuietly();
     execute(stopServerScript);
   }
 
diff --git 
a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
index c127d70..0eb8b32 100644
--- 
a/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
+++ 
b/geode-junit/src/main/java/org/apache/geode/test/junit/rules/gfsh/GfshScript.java
@@ -17,12 +17,13 @@ package org.apache.geode.test.junit.rules.gfsh;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Random;
 import java.util.concurrent.TimeUnit;
 
 public class GfshScript {
-  private final String[] commands;
+  private final DebuggableCommand[] commands;
   private String name;
   private TimeUnit timeoutTimeUnit = TimeUnit.MINUTES;
   private int timeout = 4;
@@ -31,7 +32,7 @@ public class GfshScript {
   private List<String> extendedClasspath = new ArrayList<>();
   private Random random = new Random();
 
-  public GfshScript(String... commands) {
+  public GfshScript(DebuggableCommand... commands) {
     this.commands = commands;
     this.name = defaultName();
   }
@@ -40,6 +41,11 @@ public class GfshScript {
    * By default, this GfshScript will await at most 2 minutes and will expect 
success.
    */
   public static GfshScript of(String... commands) {
+    return new GfshScript(
+        
Arrays.stream(commands).map(DebuggableCommand::new).toArray(DebuggableCommand[]::new));
+  }
+
+  public static GfshScript of(DebuggableCommand... commands) {
     return new GfshScript(commands);
   }
 
@@ -103,7 +109,11 @@ public class GfshScript {
   }
 
   public GfshExecution execute(GfshRule gfshRule) {
-    return gfshRule.execute(this);
+    return execute(gfshRule, -1);
+  }
+
+  public GfshExecution execute(GfshRule gfshRule, int gfshDebugPort) {
+    return gfshRule.execute(this, gfshDebugPort);
   }
 
   protected void awaitIfNecessary(GfshExecution gfshExecution) {
@@ -158,7 +168,7 @@ public class GfshScript {
     return shouldAwait() && !awaitQuietly;
   }
 
-  public String[] getCommands() {
+  public DebuggableCommand[] getCommands() {
     return commands;
   }
 
@@ -169,7 +179,4 @@ public class GfshScript {
   private String defaultName() {
     return Long.toHexString(random.nextLong());
   }
-
-
-
 }

Reply via email to