Skip to content

Commit

Permalink
Add new methods to get current pid to KiwiEnvironment
Browse files Browse the repository at this point in the history
* Add currentPid() and tryGetCurrentPid() to KiwiEnvironment
* Add default implementations of these methods in DefaultEnvironment

Closes #640
  • Loading branch information
sleberknight committed Dec 7, 2021
1 parent 254d240 commit 26cbd32
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/kiwiproject/base/DefaultEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ public long nanoTime() {
return System.nanoTime();
}

@Override
public long currentPid() {
return ProcessHandle.current().pid();
}

@Override
public Optional<Long> tryGetCurrentPid() {
try {
return Optional.of(currentPid());
} catch (Exception e) {
LOG.trace("Unable to get current process ID", e);
return Optional.empty();
}
}

/**
* {@inheritDoc}
* <p>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/kiwiproject/base/KiwiEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ public interface KiwiEnvironment {
*/
long nanoTime();

/**
* Returns the process ID of the currently executing JVM. This method does not perform any error checking. Use
* {@link #tryGetCurrentPid()} for a version that returns an empty optional if it is unable to obtain the pid
* for any reason.
*
* @return the pid of the current process
* @see ProcessHandle#current()
* @see ProcessHandle#pid()
*/
long currentPid();

/**
* Tries to obtain the process ID of the currently executing JVM. If any problem occurs, it is caught and an
* empty optional is returned.
*
* @return an optional containing the pid of the current process, or empty if <em>any</em> problem occurred
* @see ProcessHandle#current()
* @see ProcessHandle#pid()
*/
Optional<Long> tryGetCurrentPid();

/**
* Returns the process ID of the currently executing JVM. This method does not perform any error checking. Use
* {@link #tryGetCurrentProcessId()} for a safer version, which returns the result as an integer (wrapped in an
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/org/kiwiproject/base/DefaultEnvironmentTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.kiwiproject.base;

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.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
Expand All @@ -10,6 +12,7 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.kiwiproject.collect.KiwiMaps;

Expand Down Expand Up @@ -195,6 +198,49 @@ private void assertNow(long envNow, long now, long delta) {
.isLessThan(delta);
}

@Nested
class CurrentPid {

@Test
void shouldGetCurrentPid() {
assertThatCode(() -> env.currentPid()).doesNotThrowAnyException();
}

@Test
void shouldAllowUnsupportedExceptionsToEscape() {
var envSpy = spy(env);
var errorMessage = "this fake JVM doesn't support getting the current pid for some reason";
doThrow(new UnsupportedOperationException(errorMessage))
.when(envSpy)
.currentPid();

assertThatThrownBy(envSpy::currentPid)
.isExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(errorMessage);
}
}

@Nested
class TryGetCurrentPid {

@Test
void shouldReturnOptionalContainingPid() {
var optionalPid = env.tryGetCurrentPid();
assertThat(optionalPid).isPresent();
}

@Test
void shouldReturnEmptyOptional_WhenCurrentPidCannotBeObtained() {
var envSpy = spy(env);
doThrow(new UnsupportedOperationException("this fake JVM doesn't support getting the current pid for some reason"))
.when(envSpy)
.currentPid();

var optionalPid = envSpy.tryGetCurrentPid();
assertThat(optionalPid).isEmpty();
}
}

@Test
void testCurrentProcessId() {
String pid = env.currentProcessId();
Expand Down

0 comments on commit 26cbd32

Please sign in to comment.