You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2022/02/03 22:40:52 UTC

[maven-surefire] 01/02: more tests

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

tibordigana pushed a commit to branch InterruptedIOException
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 6d19a9ad12a8c4cfe601ab65f5ba4a9e188e7e1f
Author: Tibor Digaňa <ti...@apache.org>
AuthorDate: Thu Feb 3 23:39:37 2022 +0100

    more tests
---
 .../apache/maven/surefire/booter/PpidChecker.java  | 12 +--
 .../maven/surefire/booter/PpidCheckerTest.java     | 95 ++++++++++++++++++++++
 2 files changed, 102 insertions(+), 5 deletions(-)

diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/PpidChecker.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/PpidChecker.java
index 244331f..1fb8a30 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/PpidChecker.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/PpidChecker.java
@@ -24,6 +24,7 @@ import org.apache.maven.surefire.api.booter.DumpErrorSingleton;
 import javax.annotation.Nonnull;
 import java.io.File;
 import java.io.IOException;
+import java.io.InterruptedIOException;
 import java.nio.charset.Charset;
 import java.nio.file.Path;
 import java.text.SimpleDateFormat;
@@ -382,7 +383,7 @@ final class PpidChecker
      * This implementation is taylor made without using any Thread.
      * It's easy to destroy Process from other Thread.
      */
-    private abstract class ProcessInfoConsumer
+    abstract class ProcessInfoConsumer
     {
         private final String charset;
 
@@ -437,11 +438,9 @@ final class PpidChecker
             }
             catch ( Exception e )
             {
-                if ( !( e instanceof InterruptedException ) && !( e.getCause() instanceof InterruptedException ) )
+                if ( !( e instanceof InterruptedException || e instanceof InterruptedIOException
+                    || e.getCause() instanceof InterruptedException ) )
                 {
-                    //noinspection ResultOfMethodCallIgnored
-                    Thread.interrupted();
-
                     DumpErrorSingleton.getSingleton()
                             .dumpText( out.toString() );
 
@@ -449,6 +448,9 @@ final class PpidChecker
                             .dumpException( e );
                 }
 
+                //noinspection ResultOfMethodCallIgnored
+                Thread.interrupted();
+
                 return ERR_PROCESS_INFO;
             }
             finally
diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
index 396cb1a..c5d916e 100644
--- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
+++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/PpidCheckerTest.java
@@ -27,11 +27,15 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 
+import javax.annotation.Nonnull;
 import java.io.File;
+import java.io.IOException;
+import java.io.InterruptedIOException;
 import java.lang.management.ManagementFactory;
 import java.util.Random;
 import java.util.regex.Matcher;
 
+import static java.nio.charset.StandardCharsets.US_ASCII;
 import static java.nio.file.Files.readAllBytes;
 import static java.util.concurrent.TimeUnit.SECONDS;
 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_UNIX;
@@ -230,6 +234,97 @@ public class PpidCheckerTest
     }
 
     @Test
+    public void shouldStartedProcessThrowInterruptedException() throws Exception
+    {
+        String expectedPid = ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+        DumpErrorSingleton.getSingleton().init( reportsDir, dumpFileName );
+
+        PpidChecker checker = new PpidChecker( expectedPid );
+
+        PpidChecker.ProcessInfoConsumer consumer = checker.new ProcessInfoConsumer( US_ASCII.name() )
+        {
+            @Nonnull
+            @Override
+            ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo )
+                throws Exception
+            {
+                throw new InterruptedException();
+            }
+        };
+
+        String[] cmd =
+            IS_OS_WINDOWS
+                ? new String[]{"CMD", "/A", "/X", "/C", "dir"}
+                : new String[]{"/bin/sh", "-c", "ls"};
+
+        assertThat( consumer.execute( cmd ).isError() ).isTrue();
+        assertThat( new File( reportsDir, dumpFileName + ".dump" ) ).doesNotExist();
+    }
+
+    @Test
+    public void shouldStartedProcessThrowInterruptedIOException() throws Exception
+    {
+        String expectedPid = ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+        DumpErrorSingleton.getSingleton().init( reportsDir, dumpFileName );
+
+        PpidChecker checker = new PpidChecker( expectedPid );
+
+        PpidChecker.ProcessInfoConsumer consumer = checker.new ProcessInfoConsumer( US_ASCII.name() )
+        {
+            @Nonnull
+            @Override
+            ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo )
+                throws Exception
+            {
+                throw new InterruptedIOException();
+            }
+        };
+
+        String[] cmd =
+            IS_OS_WINDOWS
+                ? new String[]{"CMD", "/A", "/X", "/C", "dir"}
+                : new String[]{"/bin/sh", "-c", "ls"};
+
+        assertThat( consumer.execute( cmd ).isError() ).isTrue();
+        assertThat( new File( reportsDir, dumpFileName + ".dump" ) ).doesNotExist();
+    }
+
+    @Test
+    public void shouldStartedProcessThrowIOException() throws Exception
+    {
+        String expectedPid = ManagementFactory.getRuntimeMXBean().getName().split( "@" )[0].trim();
+        DumpErrorSingleton.getSingleton().init( reportsDir, dumpFileName );
+
+        PpidChecker checker = new PpidChecker( expectedPid );
+
+        PpidChecker.ProcessInfoConsumer consumer = checker.new ProcessInfoConsumer( US_ASCII.name() )
+        {
+            @Nonnull
+            @Override
+            ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo )
+                throws Exception
+            {
+                throw new IOException( "wrong command" );
+            }
+        };
+
+        String[] cmd =
+            IS_OS_WINDOWS
+                ? new String[]{"CMD", "/A", "/X", "/C", "dir"}
+                : new String[]{"/bin/sh", "-c", "ls"};
+
+        assertThat( consumer.execute( cmd ).isError() ).isTrue();
+
+        File dumpFile = new File( reportsDir, dumpFileName + ".dump" );
+
+        String error = new String( readAllBytes( dumpFile.toPath() ) );
+
+        assertThat( error )
+            .contains( IOException.class.getName() )
+            .contains( "wrong command" );
+    }
+
+    @Test
     public void shouldNotFindSuchPID()
     {
         PpidChecker checker = new PpidChecker( "1000000" );