|
| 1 | +package com.baeldung.java9.process; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by sanaulla on 2/23/2017. |
| 17 | + */ |
| 18 | + |
| 19 | +public class ProcessAPIEnhancementsTest { |
| 20 | + |
| 21 | + Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsTest.class); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException { |
| 25 | + ProcessHandle processHandle = ProcessHandle.current(); |
| 26 | + ProcessHandle.Info processInfo = processHandle.info(); |
| 27 | + assertNotNull(processHandle.getPid()); |
| 28 | + assertEquals(false, processInfo.arguments() |
| 29 | + .isPresent()); |
| 30 | + assertEquals(true, processInfo.command() |
| 31 | + .isPresent()); |
| 32 | + assertTrue(processInfo.command() |
| 33 | + .get() |
| 34 | + .contains("java")); |
| 35 | + |
| 36 | + assertEquals(true, processInfo.startInstant() |
| 37 | + .isPresent()); |
| 38 | + assertEquals(true, processInfo.totalCpuDuration() |
| 39 | + .isPresent()); |
| 40 | + assertEquals(true, processInfo.user() |
| 41 | + .isPresent()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException { |
| 46 | + |
| 47 | + String javaCmd = ProcessUtils.getJavaCmd() |
| 48 | + .getAbsolutePath(); |
| 49 | + ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version"); |
| 50 | + Process process = processBuilder.inheritIO() |
| 51 | + .start(); |
| 52 | + ProcessHandle processHandle = process.toHandle(); |
| 53 | + ProcessHandle.Info processInfo = processHandle.info(); |
| 54 | + assertNotNull(processHandle.getPid()); |
| 55 | + assertEquals(false, processInfo.arguments() |
| 56 | + .isPresent()); |
| 57 | + assertEquals(true, processInfo.command() |
| 58 | + .isPresent()); |
| 59 | + assertTrue(processInfo.command() |
| 60 | + .get() |
| 61 | + .contains("java")); |
| 62 | + assertEquals(true, processInfo.startInstant() |
| 63 | + .isPresent()); |
| 64 | + assertEquals(true, processInfo.totalCpuDuration() |
| 65 | + .isPresent()); |
| 66 | + assertEquals(true, processInfo.user() |
| 67 | + .isPresent()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() { |
| 72 | + Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses(); |
| 73 | + liveProcesses.filter(ProcessHandle::isAlive) |
| 74 | + .forEach(ph -> { |
| 75 | + assertNotNull(ph.getPid()); |
| 76 | + assertEquals(true, ph.info() |
| 77 | + .command() |
| 78 | + .isPresent()); |
| 79 | + assertEquals(true, ph.info() |
| 80 | + .startInstant() |
| 81 | + .isPresent()); |
| 82 | + assertEquals(true, ph.info() |
| 83 | + .totalCpuDuration() |
| 84 | + .isPresent()); |
| 85 | + assertEquals(true, ph.info() |
| 86 | + .user() |
| 87 | + .isPresent()); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException { |
| 93 | + int childProcessCount = 5; |
| 94 | + for (int i = 0; i < childProcessCount; i++) { |
| 95 | + String javaCmd = ProcessUtils.getJavaCmd() |
| 96 | + .getAbsolutePath(); |
| 97 | + ProcessBuilder processBuilder |
| 98 | + = new ProcessBuilder(javaCmd, "-version"); |
| 99 | + processBuilder.inheritIO().start(); |
| 100 | + } |
| 101 | + |
| 102 | + Stream<ProcessHandle> children = ProcessHandle.current() |
| 103 | + .children(); |
| 104 | + children.filter(ProcessHandle::isAlive) |
| 105 | + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() |
| 106 | + .command())); |
| 107 | + Stream<ProcessHandle> descendants = ProcessHandle.current() |
| 108 | + .descendants(); |
| 109 | + descendants.filter(ProcessHandle::isAlive) |
| 110 | + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() |
| 111 | + .command())); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception { |
| 116 | + String javaCmd = ProcessUtils.getJavaCmd() |
| 117 | + .getAbsolutePath(); |
| 118 | + ProcessBuilder processBuilder |
| 119 | + = new ProcessBuilder(javaCmd, "-version"); |
| 120 | + Process process = processBuilder.inheritIO() |
| 121 | + .start(); |
| 122 | + ProcessHandle processHandle = process.toHandle(); |
| 123 | + |
| 124 | + log.info("PID: {} has started", processHandle.getPid()); |
| 125 | + CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit(); |
| 126 | + onProcessExit.get(); |
| 127 | + assertEquals(false, processHandle.isAlive()); |
| 128 | + onProcessExit.thenAccept(ph -> { |
| 129 | + log.info("PID: {} has stopped", ph.getPid()); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments