From 13f6ab3e195419ff8a3987f9b98035f1581292cd Mon Sep 17 00:00:00 2001
From: Amos Shi <ashi@openjdk.org>
Date: Thu, 8 Aug 2024 08:14:34 +0000
Subject: [PATCH] 8314614: jdk/jshell/ImportTest.java failed with
 "InternalError: Failed remote listen" 8312140: jdk/jshell tests failed with
 JDI socket timeouts

Backport-of: 14193a049ebcbd3639962729c631293b8d3a9114
---
 .../jdk/jshell/AnalyzeSnippetTest.java        |  1 +
 .../jdk/jshell/CustomInputToolBuilder.java    |  3 +-
 .../jdk/jshell/ExecutionControlTestBase.java  | 20 +------
 .../FailOverDirectExecutionControlTest.java   | 10 +---
 ...ilOverExecutionControlDyingLaunchTest.java |  4 +-
 ...OverExecutionControlHangingLaunchTest.java |  4 +-
 ...OverExecutionControlHangingListenTest.java |  4 +-
 .../jshell/FailOverExecutionControlTest.java  |  4 +-
 .../langtools/jdk/jshell/IdGeneratorTest.java |  3 +-
 test/langtools/jdk/jshell/KullaTesting.java   |  7 ++-
 test/langtools/jdk/jshell/Presets.java        | 59 +++++++++++++++++++
 .../langtools/jdk/jshell/ReplToolTesting.java |  2 +-
 .../langtools/jdk/jshell/StartOptionTest.java |  2 +-
 test/langtools/jdk/jshell/ToolReloadTest.java |  2 +-
 test/langtools/jdk/jshell/UITesting.java      |  3 +-
 15 files changed, 88 insertions(+), 40 deletions(-)
 create mode 100644 test/langtools/jdk/jshell/Presets.java

diff --git a/test/langtools/jdk/jshell/AnalyzeSnippetTest.java b/test/langtools/jdk/jshell/AnalyzeSnippetTest.java
index b566a023caf..3e2e1a839e2 100644
--- a/test/langtools/jdk/jshell/AnalyzeSnippetTest.java
+++ b/test/langtools/jdk/jshell/AnalyzeSnippetTest.java
@@ -64,6 +64,7 @@ public class AnalyzeSnippetTest {
         state = JShell.builder()
                 .out(new PrintStream(new ByteArrayOutputStream()))
                 .err(new PrintStream(new ByteArrayOutputStream()))
+                .executionEngine(Presets.TEST_DEFAULT_EXECUTION)
                 .build();
         sca = state.sourceCodeAnalysis();
     }
diff --git a/test/langtools/jdk/jshell/CustomInputToolBuilder.java b/test/langtools/jdk/jshell/CustomInputToolBuilder.java
index 3b3d5616a94..523981b3d91 100644
--- a/test/langtools/jdk/jshell/CustomInputToolBuilder.java
+++ b/test/langtools/jdk/jshell/CustomInputToolBuilder.java
@@ -90,7 +90,8 @@ public class CustomInputToolBuilder extends KullaTesting {
                     .interactiveTerminal(interactiveTerminal)
                     .promptCapture(true)
                     .persistence(new HashMap<>())
-                    .start("--no-startup");
+                    .start("--no-startup",
+                           "--execution", Presets.TEST_DEFAULT_EXECUTION);
 
             String actual = new String(out.toByteArray());
             List<String> actualLines = Arrays.asList(actual.split("\\R"));
diff --git a/test/langtools/jdk/jshell/ExecutionControlTestBase.java b/test/langtools/jdk/jshell/ExecutionControlTestBase.java
index 9035d84d4a5..20336c902cf 100644
--- a/test/langtools/jdk/jshell/ExecutionControlTestBase.java
+++ b/test/langtools/jdk/jshell/ExecutionControlTestBase.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,28 +25,14 @@ import javax.tools.Diagnostic;
 
 import org.testng.annotations.Test;
 import jdk.jshell.VarSnippet;
-import java.net.InetAddress;
 
 import static jdk.jshell.Snippet.Status.VALID;
 import static jdk.jshell.Snippet.SubKind.*;
 
 public class ExecutionControlTestBase extends KullaTesting {
 
-    String standardListenSpec() {
-        String loopback = InetAddress.getLoopbackAddress().getHostAddress();
-        return "jdi:hostname(" + loopback + ")";
-    }
-
-    String standardLaunchSpec() {
-        return "jdi:launch(true)";
-    }
-
-    String standardJdiSpec() {
-        return "jdi";
-    }
-
-    String standardSpecs() {
-        return "5(" + standardListenSpec() + "), 6(" + standardLaunchSpec() + "), 7(" + standardJdiSpec() + ")";
+    String alwaysPassingSpec() {
+        return "5(local)";
     }
 
     @Test
diff --git a/test/langtools/jdk/jshell/FailOverDirectExecutionControlTest.java b/test/langtools/jdk/jshell/FailOverDirectExecutionControlTest.java
index da838798f8e..a094ed4a86f 100644
--- a/test/langtools/jdk/jshell/FailOverDirectExecutionControlTest.java
+++ b/test/langtools/jdk/jshell/FailOverDirectExecutionControlTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -129,9 +129,7 @@ public class FailOverDirectExecutionControlTest extends ExecutionControlTestBase
         Map<String, String> pm = provider.defaultParameters();
         pm.put("0", "alwaysFailing");
         pm.put("1", "alwaysFailing");
-        pm.put("2", standardListenSpec());
-        pm.put("3", standardLaunchSpec());
-        pm.put("4", standardJdiSpec());
+        pm.put("2", "local");
         setUp(builder -> builder.executionEngine(provider, pm));
     }
 
@@ -159,9 +157,7 @@ public class FailOverDirectExecutionControlTest extends ExecutionControlTestBase
         assertTrue(log.contains("This operation intentionally broken"), log);
         log = logged.get(Level.FINEST).get(0);
         assertTrue(
-                log.contains("Success failover -- 2 = " + standardListenSpec())
-                || log.contains("Success failover -- 3 = " + standardLaunchSpec())
-                || log.contains("Success failover -- 4 = " + standardJdiSpec()),
+                log.contains("Success failover -- 2 = local"),
                 log);
     }
 }
diff --git a/test/langtools/jdk/jshell/FailOverExecutionControlDyingLaunchTest.java b/test/langtools/jdk/jshell/FailOverExecutionControlDyingLaunchTest.java
index f3218fab7c7..31011960880 100644
--- a/test/langtools/jdk/jshell/FailOverExecutionControlDyingLaunchTest.java
+++ b/test/langtools/jdk/jshell/FailOverExecutionControlDyingLaunchTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,6 @@ public class FailOverExecutionControlDyingLaunchTest extends ExecutionControlTes
     public void setUp() {
         setUp(builder -> builder.executionEngine(
                 "failover:0(jdi:remoteAgent(DyingRemoteAgent),launch(true)), "
-                    + standardSpecs()));
+                    + alwaysPassingSpec()));
     }
 }
diff --git a/test/langtools/jdk/jshell/FailOverExecutionControlHangingLaunchTest.java b/test/langtools/jdk/jshell/FailOverExecutionControlHangingLaunchTest.java
index 778d004915c..9958b7a3284 100644
--- a/test/langtools/jdk/jshell/FailOverExecutionControlHangingLaunchTest.java
+++ b/test/langtools/jdk/jshell/FailOverExecutionControlHangingLaunchTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,6 @@ public class FailOverExecutionControlHangingLaunchTest extends ExecutionControlT
     public void setUp() {
         setUp(builder -> builder.executionEngine(
                 "failover:0(jdi:remoteAgent(HangingRemoteAgent),launch(true)), "
-                        + standardSpecs()));
+                        + alwaysPassingSpec()));
     }
 }
diff --git a/test/langtools/jdk/jshell/FailOverExecutionControlHangingListenTest.java b/test/langtools/jdk/jshell/FailOverExecutionControlHangingListenTest.java
index f22dd821f40..4f29bfe9c7a 100644
--- a/test/langtools/jdk/jshell/FailOverExecutionControlHangingListenTest.java
+++ b/test/langtools/jdk/jshell/FailOverExecutionControlHangingListenTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -45,6 +45,6 @@ public class FailOverExecutionControlHangingListenTest extends ExecutionControlT
         String loopback = InetAddress.getLoopbackAddress().getHostAddress();
          setUp(builder -> builder.executionEngine(
                 "failover:0(jdi:remoteAgent(HangingRemoteAgent),hostname(" + loopback + ")),"
-                        + standardSpecs()));
+                        + alwaysPassingSpec()));
     }
 }
diff --git a/test/langtools/jdk/jshell/FailOverExecutionControlTest.java b/test/langtools/jdk/jshell/FailOverExecutionControlTest.java
index 0843351815f..80dc56d72c4 100644
--- a/test/langtools/jdk/jshell/FailOverExecutionControlTest.java
+++ b/test/langtools/jdk/jshell/FailOverExecutionControlTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -41,7 +41,7 @@ public class FailOverExecutionControlTest extends ExecutionControlTestBase {
     @Override
     public void setUp() {
         setUp(builder -> builder.executionEngine("failover:0(expectedFailureNonExistent1), 1(expectedFailureNonExistent2), "
-                + standardSpecs()));
+                + alwaysPassingSpec()));
     }
 
 }
diff --git a/test/langtools/jdk/jshell/IdGeneratorTest.java b/test/langtools/jdk/jshell/IdGeneratorTest.java
index 23727aef643..e8a38dfe7f0 100644
--- a/test/langtools/jdk/jshell/IdGeneratorTest.java
+++ b/test/langtools/jdk/jshell/IdGeneratorTest.java
@@ -53,7 +53,8 @@ public class IdGeneratorTest {
         return JShell.builder()
                 .in(inStream)
                 .out(new PrintStream(outStream))
-                .err(new PrintStream(errStream));
+                .err(new PrintStream(errStream))
+                .executionEngine(Presets.TEST_DEFAULT_EXECUTION);
     }
 
     public void testTempNameGenerator() {
diff --git a/test/langtools/jdk/jshell/KullaTesting.java b/test/langtools/jdk/jshell/KullaTesting.java
index 369b2ed1f44..d74f3484f4b 100644
--- a/test/langtools/jdk/jshell/KullaTesting.java
+++ b/test/langtools/jdk/jshell/KullaTesting.java
@@ -100,7 +100,9 @@ public class KullaTesting {
     private Set<Snippet> allSnippets = new LinkedHashSet<>();
 
     static {
-        JShell js = JShell.create();
+        JShell js = JShell.builder()
+                          .executionEngine(Presets.TEST_DEFAULT_EXECUTION)
+                          .build();
         MAIN_SNIPPET = js.eval("MAIN_SNIPPET").get(0).snippet();
         js.close();
         assertTrue(MAIN_SNIPPET != null, "Bad MAIN_SNIPPET set-up -- must not be null");
@@ -192,7 +194,8 @@ public class KullaTesting {
         JShell.Builder builder = JShell.builder()
                 .in(in)
                 .out(new PrintStream(outStream))
-                .err(new PrintStream(errStream));
+                .err(new PrintStream(errStream))
+                .executionEngine(Presets.TEST_DEFAULT_EXECUTION);
         bc.accept(builder);
         state = builder.build();
         allSnippets = new LinkedHashSet<>();
diff --git a/test/langtools/jdk/jshell/Presets.java b/test/langtools/jdk/jshell/Presets.java
new file mode 100644
index 00000000000..b9a93c967dc
--- /dev/null
+++ b/test/langtools/jdk/jshell/Presets.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.net.InetAddress;
+import java.util.*;
+
+public class Presets {
+    public static final String TEST_DEFAULT_EXECUTION;
+    public static final String TEST_STANDARD_EXECUTION;
+
+    static {
+        String loopback = InetAddress.getLoopbackAddress().getHostAddress();
+
+        TEST_DEFAULT_EXECUTION = "failover:0(jdi:hostname(" + loopback + "))," +
+                                 "1(jdi:launch(true)), 2(jdi), 3(local)";
+        TEST_STANDARD_EXECUTION = "failover:0(jdi:hostname(" + loopback + "))," +
+                                  "1(jdi:launch(true)), 2(jdi)";
+    }
+
+    public static String[] addExecutionIfMissing(String[] args) {
+        if (Arrays.stream(args).noneMatch(Presets::remoteRelatedOption)) {
+            List<String> augmentedArgs = new ArrayList<>();
+
+            augmentedArgs.add("--execution");
+            augmentedArgs.add(Presets.TEST_DEFAULT_EXECUTION);
+            augmentedArgs.addAll(List.of(args));
+
+            return augmentedArgs.toArray(s -> new String[s]);
+        }
+
+        return args;
+    }
+
+    private static boolean remoteRelatedOption(String option) {
+        return "--execution".equals(option) ||
+               "--add-modules".equals(option) ||
+               option.startsWith("-R");
+    }
+}
diff --git a/test/langtools/jdk/jshell/ReplToolTesting.java b/test/langtools/jdk/jshell/ReplToolTesting.java
index 6e0192e3aa7..1e9f9942f42 100644
--- a/test/langtools/jdk/jshell/ReplToolTesting.java
+++ b/test/langtools/jdk/jshell/ReplToolTesting.java
@@ -293,7 +293,7 @@ public class ReplToolTesting {
 
     private void testRaw(Locale locale, String[] args, ReplTest... tests) {
         testRawInit(tests);
-        testRawRun(locale, args);
+        testRawRun(locale, Presets.addExecutionIfMissing(args));
         testRawCheck(locale);
     }
 
diff --git a/test/langtools/jdk/jshell/StartOptionTest.java b/test/langtools/jdk/jshell/StartOptionTest.java
index df445d49750..aa8d9be03a9 100644
--- a/test/langtools/jdk/jshell/StartOptionTest.java
+++ b/test/langtools/jdk/jshell/StartOptionTest.java
@@ -81,7 +81,7 @@ public class StartOptionTest {
     protected int runShell(String... args) {
         try {
             return builder()
-                    .start(args);
+                    .start(Presets.addExecutionIfMissing(args));
         } catch (Exception ex) {
             fail("Repl tool died with exception", ex);
         }
diff --git a/test/langtools/jdk/jshell/ToolReloadTest.java b/test/langtools/jdk/jshell/ToolReloadTest.java
index 13d583e51f5..4709584cd12 100644
--- a/test/langtools/jdk/jshell/ToolReloadTest.java
+++ b/test/langtools/jdk/jshell/ToolReloadTest.java
@@ -201,7 +201,7 @@ public class ToolReloadTest extends ReplToolTesting {
     }
 
     public void testEnvBadModule() {
-        test(
+        test(new String[] {"--execution", Presets.TEST_STANDARD_EXECUTION},
                 (a) -> assertVariable(a, "int", "x", "5", "5"),
                 (a) -> assertMethod(a, "int m(int z) { return z * z; }",
                         "(int)int", "m"),
diff --git a/test/langtools/jdk/jshell/UITesting.java b/test/langtools/jdk/jshell/UITesting.java
index 848c0d52797..477e540e716 100644
--- a/test/langtools/jdk/jshell/UITesting.java
+++ b/test/langtools/jdk/jshell/UITesting.java
@@ -93,7 +93,8 @@ public class UITesting {
                         .promptCapture(true)
                         .persistence(new HashMap<>())
                         .locale(Locale.US)
-                        .run("--no-startup");
+                        .run("--no-startup",
+                             "--execution", Presets.TEST_DEFAULT_EXECUTION);
             } catch (Exception ex) {
                 throw new IllegalStateException(ex);
             }
-- 
GitLab