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 2018/04/08 10:24:58 UTC

[maven-surefire] branch 1512 created (now 3c472ff)

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

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


      at 3c472ff  [SUREFIRE-1512] ProcessInfo for Windows is prone to timezone offset changes

This branch includes the following new commits:

     new 3c472ff  [SUREFIRE-1512] ProcessInfo for Windows is prone to timezone offset changes

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


-- 
To stop receiving notification emails like this one, please contact
tibordigana@apache.org.

[maven-surefire] 01/01: [SUREFIRE-1512] ProcessInfo for Windows is prone to timezone offset changes

Posted by ti...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3c472ff6e1ca4fef9e3cf2beda35734d00af510b
Author: Tibor17 <ti...@apache.org>
AuthorDate: Sun Apr 8 12:24:39 2018 +0200

    [SUREFIRE-1512] ProcessInfo for Windows is prone to timezone offset changes
---
 .../apache/maven/surefire/booter/PpidChecker.java  | 47 +++++++++++++++-------
 .../apache/maven/surefire/booter/ProcessInfo.java  |  2 +-
 2 files changed, 33 insertions(+), 16 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 32e3ad7..d56a82a 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
@@ -22,6 +22,8 @@ package org.apache.maven.surefire.booter;
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.Charset;
+import java.text.SimpleDateFormat;
+import java.util.Locale;
 import java.util.Queue;
 import java.util.Scanner;
 import java.util.StringTokenizer;
@@ -29,6 +31,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static java.lang.Integer.parseInt;
 import static java.lang.Long.parseLong;
 import static java.util.concurrent.TimeUnit.DAYS;
 import static java.util.concurrent.TimeUnit.HOURS;
@@ -39,6 +42,8 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank;
 import static org.apache.commons.lang3.SystemUtils.IS_OS_HP_UX;
 import static org.apache.commons.lang3.SystemUtils.IS_OS_UNIX;
 import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
+import static org.apache.maven.surefire.booter.ProcessInfo.unixProcessInfo;
+import static org.apache.maven.surefire.booter.ProcessInfo.windowsProcessInfo;
 import static org.apache.maven.surefire.booter.ProcessInfo.ERR_PROCESS_INFO;
 import static org.apache.maven.surefire.booter.ProcessInfo.INVALID_PROCESS_INFO;
 
@@ -50,6 +55,12 @@ import static org.apache.maven.surefire.booter.ProcessInfo.INVALID_PROCESS_INFO;
  */
 final class PpidChecker
 {
+    private static final int MINUTES_TO_MILLIS = 60 * 1000;
+    // 25 chars https://superuser.com/questions/937380/get-creation-time-of-file-in-milliseconds/937401#937401
+    private static final int WMIC_CREATION_DATE_FORMAT_LENGTH = 25;
+    private static final int WMIC_CREATION_DATE_TIMESTAMP_LENGTH = 18;
+    private static final SimpleDateFormat WMIC_CREATION_DATE_FORMAT
+            = new SimpleDateFormat( "yyyyMMddHHmmss'.'SSS", Locale.ROOT );
     private static final String WMIC_CREATION_DATE = "CreationDate";
     private static final String WINDOWS_SYSTEM_ROOT_ENV = "SystemRoot";
     private static final String RELATIVE_PATH_TO_WMIC = "System32\\Wbem";
@@ -151,7 +162,7 @@ final class PpidChecker
                                                  + fromHours( matcher )
                                                  + fromMinutes( matcher )
                                                  + fromSeconds( matcher );
-                        return ProcessInfo.unixProcessInfo( pluginPid, pidUptime );
+                        return unixProcessInfo( pluginPid, pidUptime );
                     }
                 }
                 return previousProcessInfo;
@@ -168,7 +179,7 @@ final class PpidChecker
             private boolean hasHeader;
 
             @Override
-            ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo )
+            ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo ) throws Exception
             {
                 if ( !previousProcessInfo.isValid() )
                 {
@@ -177,8 +188,18 @@ final class PpidChecker
                     {
                         if ( hasHeader )
                         {
-                            String startTimestamp = args.nextToken();
-                            return ProcessInfo.windowsProcessInfo( pluginPid, startTimestamp );
+                            String creationDate = args.nextToken();
+                            if ( creationDate.length() != WMIC_CREATION_DATE_FORMAT_LENGTH )
+                            {
+                                throw new IllegalStateException( "WMIC CreationDate should have 25 characters "
+                                        + creationDate );
+                            }
+                            String startTimestamp = creationDate.substring( 0, WMIC_CREATION_DATE_TIMESTAMP_LENGTH );
+                            int indexOfTimeZone = WMIC_CREATION_DATE_FORMAT_LENGTH - 4;
+                            long startTimestampMillisUTC =
+                                    WMIC_CREATION_DATE_FORMAT.parse( startTimestamp ).getTime()
+                                            - parseInt( creationDate.substring( indexOfTimeZone ) ) * MINUTES_TO_MILLIS;
+                            return windowsProcessInfo( pluginPid, startTimestampMillisUTC );
                         }
                         else
                         {
@@ -189,10 +210,9 @@ final class PpidChecker
                 return previousProcessInfo;
             }
         };
-        String pid = String.valueOf( pluginPid );
         String wmicPath = hasWmicStandardSystemPath() ? SYSTEM_PATH_TO_WMIC : "";
         return reader.execute( "CMD", "/A", "/X", "/C",
-                wmicPath + "wmic process where (ProcessId=" + pid + ") get " + WMIC_CREATION_DATE
+                wmicPath + "wmic process where (ProcessId=" + pluginPid + ") get " + WMIC_CREATION_DATE
         );
     }
 
@@ -210,7 +230,7 @@ final class PpidChecker
         return stopped;
     }
 
-    static String unixPathToPS()
+    private static String unixPathToPS()
     {
         return canExecuteLocalUnixPs() ? "/usr/bin/ps" : "/bin/ps";
     }
@@ -286,7 +306,7 @@ final class PpidChecker
             this.charset = charset;
         }
 
-        abstract ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo );
+        abstract ProcessInfo consumeLine( String line, ProcessInfo previousProcessInfo ) throws Exception;
 
         ProcessInfo execute( String... command )
         {
@@ -312,14 +332,11 @@ final class PpidChecker
                 int exitCode = process.waitFor();
                 return exitCode == 0 ? processInfo : INVALID_PROCESS_INFO;
             }
-            catch ( IOException e )
+            catch ( Exception e )
             {
-                DumpErrorSingleton.getSingleton().dumpException( e );
-                return ERR_PROCESS_INFO;
-            }
-            catch ( InterruptedException e )
-            {
-                DumpErrorSingleton.getSingleton().dumpException( e );
+                DumpErrorSingleton.getSingleton()
+                        .dumpException( e );
+
                 return ERR_PROCESS_INFO;
             }
             finally
diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProcessInfo.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProcessInfo.java
index 212e221..d106dd9 100644
--- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProcessInfo.java
+++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/ProcessInfo.java
@@ -47,7 +47,7 @@ final class ProcessInfo
         return new ProcessInfo( pid, etime );
     }
 
-    static ProcessInfo windowsProcessInfo( long pid, String startTimestamp )
+    static ProcessInfo windowsProcessInfo( long pid, long startTimestamp )
     {
         return new ProcessInfo( pid, requireNonNull( startTimestamp, "startTimestamp is NULL" ) );
     }

-- 
To stop receiving notification emails like this one, please contact
tibordigana@apache.org.