Skip to content

Commit

Permalink
Add processHandleOfPid to KiwiEnvironment and DefaultEnvironment (#951)
Browse files Browse the repository at this point in the history
Closes #943
  • Loading branch information
sleberknight authored Apr 24, 2023
1 parent 53cc694 commit 64c5bb8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/org/kiwiproject/base/DefaultEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -143,6 +144,11 @@ public ProcessHandle currentProcessHandle() {
return ProcessHandle.current();
}

@Override
public Optional<ProcessHandle> processHandleOfPid(long pid) {
return ProcessHandle.of(pid);
}

@Override
public void sleep(long milliseconds) throws InterruptedException {
Thread.sleep(milliseconds);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalLong;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -189,6 +190,19 @@ public interface KiwiEnvironment {
*/
ProcessHandle currentProcessHandle();

/**
* Tries to obtain a {@link ProcessHandle} for a process with the given ID. If the process does not exist, then
* an empty Optional is returned.
*
* @param pid the process ID
* @return an Optional containing a ProcessHandle for the given process ID, or an empty Optional if the process
* does not exist
* @see ProcessHandle#of(long)
* @implNote Implementations may throw the same exceptions as {@link ProcessHandle#of(long)}, but are not required
* to do so.
*/
Optional<ProcessHandle> processHandleOfPid(long pid);

/**
* Sleep for the given number of milliseconds.
*
Expand Down
27 changes: 26 additions & 1 deletion src/test/java/org/kiwiproject/base/DefaultEnvironmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.kiwiproject.base.KiwiStrings.format;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
Expand All @@ -13,6 +15,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.kiwiproject.collect.KiwiMaps;

Expand All @@ -30,6 +33,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@DisplayName("DefaultKiwiEnvironment")
Expand Down Expand Up @@ -263,6 +267,27 @@ void shouldAllowUnsupportedExceptionsToEscape() {
}
}

@Nested
class ProcessHandleOfPid {

@Test
void shouldReturnProcessHandle_ForAnExistingProcess() {
var pid = ProcessHandle.current().pid();
var processHandle = env.processHandleOfPid(pid).orElseThrow();
assertThat(processHandle).isEqualTo(ProcessHandle.current());
}

@RepeatedTest(5)
void shouldReturnEmptyOptional_WhenProcessDoesNotExist() {
// use a range for pids that should never exist
var randomPid = ThreadLocalRandom.current().nextLong(100_000, 200_001);
assumeTrue(ProcessHandle.of(randomPid).isEmpty(),
() -> format("Expected process having pid {} not to exist, but it does", randomPid));

assertThat(env.processHandleOfPid(randomPid)).isEmpty();
}
}

@Test
void testSleep() throws InterruptedException {
long sleepTime = 50;
Expand Down Expand Up @@ -429,4 +454,4 @@ void testGetProperties() {
assertThat(props).isEqualTo(System.getProperties());
}

}
}

0 comments on commit 64c5bb8

Please sign in to comment.