Repository: maven-surefire Updated Branches: refs/heads/master 30e7c4936 -> 4bf7c39b4
[SUREFIRE-1437] Improve unit tests. Call PID parser on all platforms in SystemUtilsTest. Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/4bf7c39b Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/4bf7c39b Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/4bf7c39b Branch: refs/heads/master Commit: 4bf7c39b40ad438fee7160472f282114d5ae57d8 Parents: 30e7c49 Author: Tibor17 <[email protected]> Authored: Wed Oct 11 22:36:02 2017 +0200 Committer: Tibor17 <[email protected]> Committed: Wed Oct 11 22:36:02 2017 +0200 ---------------------------------------------------------------------- .../maven/surefire/booter/SystemUtils.java | 54 +++++++++++++++++++- .../maven/surefire/booter/SystemUtilsTest.java | 19 ++++++- .../src/test/resources/proc/curproc/status | 1 + .../src/test/resources/proc/self/stat | 1 + 4 files changed, 72 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4bf7c39b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java ---------------------------------------------------------------------- diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java index 3a53ddf..38c9310 100644 --- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java +++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/SystemUtils.java @@ -257,9 +257,37 @@ public final class SystemUtils return null; } + /** + * $ cat /proc/self/stat + * <br> + * 48982 (cat) R 9744 48982 9744 34818 48982 8192 185 0 0 0 0 0 0 0 20 0 1 0 + * 137436614 103354368 134 18446744073709551615 4194304 4235780 140737488346592 + * 140737488343784 252896458544 0 0 0 0 0 0 0 17 2 0 0 0 0 0 + * <br> + * $ SELF_PID=$(cat /proc/self/stat) + * <br> + * $ echo $CPU_ID | gawk '{print $1}' + * <br> + * 48982 + * + * @return self PID + * @throws Exception i/o and number format exc + */ static Long pidStatusOnLinux() throws Exception { - FileReader input = new FileReader( "/proc/self/stat" ); + return pidStatusOnLinux( "" ); + } + + /** + * For testing purposes only. + * + * @param root shifted to test-classes + * @return same as in {@link #pidStatusOnLinux()} + * @throws Exception same as in {@link #pidStatusOnLinux()} + */ + static Long pidStatusOnLinux( String root ) throws Exception + { + FileReader input = new FileReader( root + "/proc/self/stat" ); try { // Reading and encoding 20 characters is bit faster than whole line. @@ -277,14 +305,36 @@ public final class SystemUtils /** * The process status. This file is read-only and returns a single * line containing multiple space-separated fields. + * <br> * See <a href="https://www.freebsd.org/cgi/man.cgi?query=procfs&sektion=5">procfs status</a> + * <br> + * # cat /proc/curproc/status + * <br> + * cat 60424 60386 60424 60386 5,0 ctty 972854153,236415 0,0 0,1043 nochan 0 0 0,0 prisoner + * <br> + * Fields are: + * <br> + * comm pid ppid pgid sid maj, min ctty, sldr start user/system time wmsg euid ruid rgid,egid, + * groups[1 .. NGROUPS] hostname * * @return current PID * @throws Exception if could not read /proc/curproc/status */ static Long pidStatusOnBSD() throws Exception { - BufferedReader input = new BufferedReader( new FileReader( "/proc/curproc/status" ) ); + return pidStatusOnBSD( "" ); + } + + /** + * For testing purposes only. + * + * @param root shifted to test-classes + * @return same as in {@link #pidStatusOnBSD()} + * @throws Exception same as in {@link #pidStatusOnBSD()} + */ + static Long pidStatusOnBSD( String root ) throws Exception + { + BufferedReader input = new BufferedReader( new FileReader( root + "/proc/curproc/status" ) ); try { String line = input.readLine(); http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4bf7c39b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java ---------------------------------------------------------------------- diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java index 6a8c095..6c1b808 100644 --- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java +++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/SystemUtilsTest.java @@ -38,7 +38,6 @@ import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX; import static org.apache.commons.lang3.SystemUtils.IS_OS_NET_BSD; import static org.apache.commons.lang3.SystemUtils.IS_OS_OPEN_BSD; import static org.fest.assertions.Assertions.assertThat; -import static org.junit.Assume.assumeFalse; import static org.junit.Assume.assumeTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyDouble; @@ -227,6 +226,15 @@ public class SystemUtilsTest } @Test + public void shouldBeMockPidStatusOnLinux() throws Exception + { + String root = new File( System.getProperty( "user.dir" ), "target/test-classes" ).getAbsolutePath(); + Long actualPid = SystemUtils.pidStatusOnLinux( root ); + assertThat( actualPid ) + .isEqualTo( 48982L ); + } + + @Test public void shouldBePidStatusOnBSD() throws Exception { assumeTrue( IS_OS_FREE_BSD || IS_OS_NET_BSD || IS_OS_OPEN_BSD ); @@ -239,6 +247,15 @@ public class SystemUtilsTest } @Test + public void shouldBeMockPidStatusOnBSD() throws Exception + { + String root = new File( System.getProperty( "user.dir" ), "target/test-classes" ).getAbsolutePath(); + Long actualPid = SystemUtils.pidStatusOnBSD( root ); + assertThat( actualPid ) + .isEqualTo( 60424L ); + } + + @Test public void shouldBePidOnJMX() { Long actualPid = SystemUtils.pidOnJMX(); http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4bf7c39b/surefire-booter/src/test/resources/proc/curproc/status ---------------------------------------------------------------------- diff --git a/surefire-booter/src/test/resources/proc/curproc/status b/surefire-booter/src/test/resources/proc/curproc/status new file mode 100644 index 0000000..9582e71 --- /dev/null +++ b/surefire-booter/src/test/resources/proc/curproc/status @@ -0,0 +1 @@ +cat 60424 60386 60424 60386 5,0 ctty 972854153,236415 0,0 0,1043 nochan 0 0 0,0 prisoner \ No newline at end of file http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/4bf7c39b/surefire-booter/src/test/resources/proc/self/stat ---------------------------------------------------------------------- diff --git a/surefire-booter/src/test/resources/proc/self/stat b/surefire-booter/src/test/resources/proc/self/stat new file mode 100644 index 0000000..a6d752b --- /dev/null +++ b/surefire-booter/src/test/resources/proc/self/stat @@ -0,0 +1 @@ +48982 (cat) R 9744 48982 9744 34818 48982 8192 185 0 0 0 0 0 0 0 20 0 1 0 137436614 103354368 134 18446744073709551615 4194304 4235780 140737488346592 140737488343784 252896458544 0 0 0 0 0 0 0 17 2 0 0 0 0 0 \ No newline at end of file
