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 2020/07/06 15:46:36 UTC

[maven-surefire] branch master updated: [SUREFIRE-1815] Thread interrupted state cleared on any console output

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e32f34e  [SUREFIRE-1815] Thread interrupted state cleared on any console output
e32f34e is described below

commit e32f34e5e2ef77adb1e5b0822bf3ff08c0eb3282
Author: Scott D <10...@users.noreply.github.com>
AuthorDate: Mon Jul 6 11:46:28 2020 -0400

    [SUREFIRE-1815] Thread interrupted state cleared on any console output
    
    Added a restoration of thread interrupt state to
    LegacyMasterProcessChannelEncoder. Added a unit test to ensure this is
    indeed fixed and not reintroduced.
---
 .../spi/LegacyMasterProcessChannelEncoder.java     | 11 +++++---
 .../spi/LegacyMasterProcessChannelEncoderTest.java | 29 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoder.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoder.java
index 443fdcd..3cbc8eb 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoder.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoder.java
@@ -298,11 +298,9 @@ public class LegacyMasterProcessChannelEncoder implements MasterProcessChannelEn
 
     private void encodeAndPrintEvent( StringBuilder event, boolean sendImmediately )
     {
+        final boolean wasInterrupted = Thread.interrupted();
         try
         {
-            //noinspection ResultOfMethodCallIgnored
-            Thread.interrupted();
-
             byte[] array = event.append( '\n' )
                 .toString()
                 .getBytes( STREAM_ENCODING );
@@ -334,6 +332,13 @@ public class LegacyMasterProcessChannelEncoder implements MasterProcessChannelEn
                     .dumpException( e );
             }
         }
+        finally
+        {
+            if ( wasInterrupted )
+            {
+                Thread.currentThread().interrupt();
+            }
+        }
     }
 
     static StringBuilder encode( ForkedProcessEventType operation, RunMode runMode, String... args )
diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoderTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoderTest.java
index d800e53..4e51318 100644
--- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoderTest.java
+++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/spi/LegacyMasterProcessChannelEncoderTest.java
@@ -1090,6 +1090,35 @@ public class LegacyMasterProcessChannelEncoderTest
                 .startsWith( ":maven-surefire-event:jvm-exit-error:UTF-8:MQ==:Mg==:NA==:" );
     }
 
+    @Test
+    public void testInterruptHandling() throws IOException
+    {
+        Stream out = Stream.newStream();
+        WritableBufferedByteChannel channel = newBufferedChannel( out );
+        LegacyMasterProcessChannelEncoder encoder = new LegacyMasterProcessChannelEncoder( channel );
+
+        Thread.currentThread().interrupt();
+        try
+        {
+            encoder.stdOut( "msg", false );
+            channel.close();
+        }
+        finally
+        {
+            // Clear the interrupt and make sure it survived the invocation
+            assertThat( Thread.interrupted() )
+                    .isTrue();
+        }
+
+        String expected = ":maven-surefire-event:std-out-stream:normal-run:UTF-8:bXNn:";
+
+        LineNumberReader printedLines = out.newReader( UTF_8 );
+        assertThat( printedLines.readLine() )
+                .isEqualTo( expected );
+        assertThat( printedLines.readLine() )
+                .isNull();
+    }
+
     private static class Stream extends PrintStream
     {
         private final ByteArrayOutputStream out;