feat(execution): implement process execution engine (#5)

Add process execution infrastructure:

- ProcessUtils for cross-platform process management (isAlive, killTree)
- ProcessExecutor for command execution with output capture to log files
- ProcessMonitor for asynchronous process monitoring with events
- JobRunner for orchestrating job execution with state management

Features:
- Cross-platform support (Windows, macOS, Linux)
- Real-time stdout/stderr capture to log files
- Heartbeat monitoring during execution
- Graceful and forceful process termination

Includes 54 new tests for comprehensive coverage.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Daniel SamsonandClaude Opus 4.5 committed 2026-01-21 13:19:54 +00:00
1 parent c6e26bbedb
commit 818c45de0a
8 files changed
+1531

No files matched your search

@@ -0,0 +1,212 @@
package com.samsonmedia.barn.execution;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import com.samsonmedia.barn.config.JobsConfig;
import com.samsonmedia.barn.jobs.Job;
import com.samsonmedia.barn.jobs.JobRepository;
import com.samsonmedia.barn.state.BarnDirectories;
import com.samsonmedia.barn.state.JobState;
/**
* Tests for JobRunner.
*/
class JobRunnerTest {
@TempDir
private Path tempDir;
private BarnDirectories dirs;
private JobRepository repository;
private JobRunner runner;
private JobsConfig config;
@BeforeEach
void setUp() throws IOException {
dirs = new BarnDirectories(tempDir);
dirs.initialize();
repository = new JobRepository(dirs);
runner = new JobRunner(repository, dirs);
config = JobsConfig.withDefaults();
}
@Nested
class Run {
@Test
void run_withSuccessfulCommand_shouldReturnSuccessResult() throws IOException {
Job job = repository.create(getEchoCommand("hello"), "test", config);
JobRunner.JobResult result = runner.run(job);
assertThat(result.isSuccess()).isTrue();
assertThat(result.exitCode()).isZero();
assertThat(result.error()).isNull();
assertThat(result.duration()).isNotNull();
}
@Test
void run_shouldUpdateJobState() throws IOException {
Job job = repository.create(getEchoCommand("hello"), "test", config);
runner.run(job);
Job updated = repository.findById(job.id()).orElseThrow();
assertThat(updated.state()).isEqualTo(JobState.SUCCEEDED);
assertThat(updated.exitCode()).isZero();
assertThat(updated.finishedAt()).isNotNull();
}
@Test
void run_withFailingCommand_shouldReturnFailureResult() throws IOException {
Job job = repository.create(getFailingCommand(), "test", config);
JobRunner.JobResult result = runner.run(job);
assertThat(result.isSuccess()).isFalse();
assertThat(result.exitCode()).isNotEqualTo(0);
assertThat(result.error()).isNotNull();
}
@Test
void run_withFailingCommand_shouldMarkJobAsFailed() throws IOException {
Job job = repository.create(getFailingCommand(), "test", config);
runner.run(job);
Job updated = repository.findById(job.id()).orElseThrow();
assertThat(updated.state()).isEqualTo(JobState.FAILED);
}
@Test
void run_shouldCaptureStdout() throws IOException {
Job job = repository.create(getEchoCommand("test output"), "test", config);
runner.run(job);
Path stdoutFile = dirs.getJobLogsDir(job.id()).resolve("stdout.log");
assertThat(Files.exists(stdoutFile)).isTrue();
String content = Files.readString(stdoutFile);
assertThat(content).contains("test output");
}
@Test
void run_shouldCaptureStderr() throws IOException {
Job job = repository.create(getStderrCommand("error output"), "test", config);
runner.run(job);
Path stderrFile = dirs.getJobLogsDir(job.id()).resolve("stderr.log");
assertThat(Files.exists(stderrFile)).isTrue();
String content = Files.readString(stderrFile);
assertThat(content).contains("error output");
}
@Test
void run_shouldWritePid() throws IOException {
Job job = repository.create(getEchoCommand("test"), "test", config);
runner.run(job);
Job updated = repository.findById(job.id()).orElseThrow();
// PID was set during execution (may be null after completion on some platforms)
assertThat(updated.startedAt()).isNotNull();
}
@Test
void run_withNullJob_shouldThrowException() {
assertThatThrownBy(() -> runner.run(null))
.isInstanceOf(NullPointerException.class);
}
}
@Nested
class JobResultRecord {
@Test
void isSuccess_withZeroExitCode_shouldReturnTrue() {
JobRunner.JobResult result = new JobRunner.JobResult(0, Duration.ofSeconds(1), null);
assertThat(result.isSuccess()).isTrue();
}
@Test
void isSuccess_withNonZeroExitCode_shouldReturnFalse() {
JobRunner.JobResult result = new JobRunner.JobResult(1, Duration.ofSeconds(1), "error");
assertThat(result.isSuccess()).isFalse();
}
@Test
void isSuccess_withNegativeExitCode_shouldReturnFalse() {
JobRunner.JobResult result = new JobRunner.JobResult(-1, Duration.ofSeconds(1), "failed to start");
assertThat(result.isSuccess()).isFalse();
}
}
@Nested
class Constructor {
@Test
void constructor_withNullRepository_shouldThrowException() {
assertThatThrownBy(() -> new JobRunner(null, dirs))
.isInstanceOf(NullPointerException.class);
}
@Test
void constructor_withNullDirs_shouldThrowException() {
assertThatThrownBy(() -> new JobRunner(repository, null))
.isInstanceOf(NullPointerException.class);
}
@Test
void constructor_withAllDependencies_shouldSucceed() {
ProcessExecutor executor = new ProcessExecutor();
ProcessMonitor monitor = new ProcessMonitor();
JobRunner customRunner = new JobRunner(executor, monitor, repository, dirs);
assertThat(customRunner).isNotNull();
}
}
// Platform-agnostic command helpers
private List<String> getEchoCommand(String message) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", message);
}
return List.of("echo", message);
}
private List<String> getStderrCommand(String message) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", message, "1>&2");
}
return List.of("sh", "-c", "echo '" + message + "' >&2");
}
private List<String> getFailingCommand() {
if (isWindows()) {
return List.of("cmd", "/c", "exit", "1");
}
return List.of("sh", "-c", "exit 1");
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}
@@ -0,0 +1,240 @@
package com.samsonmedia.barn.execution;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Tests for ProcessExecutor.
*/
class ProcessExecutorTest {
@TempDir
private Path tempDir;
private ProcessExecutor executor;
private Path stdoutFile;
private Path stderrFile;
@BeforeEach
void setUp() {
executor = new ProcessExecutor();
stdoutFile = tempDir.resolve("logs/stdout.log");
stderrFile = tempDir.resolve("logs/stderr.log");
}
@Nested
class Execute {
@Test
void execute_withValidCommand_shouldStartProcess() throws IOException, InterruptedException {
List<String> command = getEchoCommand("hello");
Process process = executor.execute(command, tempDir, stdoutFile, stderrFile);
assertThat(process).isNotNull();
assertThat(process.pid()).isGreaterThan(0);
process.waitFor();
assertThat(process.exitValue()).isZero();
}
@Test
void execute_shouldCaptureStdout() throws IOException, InterruptedException {
List<String> command = getEchoCommand("hello world");
Process process = executor.execute(command, tempDir, stdoutFile, stderrFile);
process.waitFor();
assertThat(Files.exists(stdoutFile)).isTrue();
String content = Files.readString(stdoutFile);
assertThat(content).contains("hello world");
}
@Test
void execute_shouldCaptureStderr() throws IOException, InterruptedException {
// Use a command that writes to stderr
List<String> command = getStderrCommand("error message");
Process process = executor.execute(command, tempDir, stdoutFile, stderrFile);
process.waitFor();
assertThat(Files.exists(stderrFile)).isTrue();
String content = Files.readString(stderrFile);
assertThat(content).contains("error message");
}
@Test
void execute_shouldCreateParentDirectories() throws IOException, InterruptedException {
Path nestedStdout = tempDir.resolve("deep/nested/logs/stdout.log");
Path nestedStderr = tempDir.resolve("deep/nested/logs/stderr.log");
Process process = executor.execute(getEchoCommand("test"), tempDir, nestedStdout, nestedStderr);
process.waitFor();
assertThat(Files.exists(nestedStdout.getParent())).isTrue();
}
@Test
void execute_withEnvironment_shouldPassEnvironment() throws IOException, InterruptedException {
Map<String, String> env = Map.of("TEST_VAR", "test_value");
List<String> command = getEnvPrintCommand("TEST_VAR");
Process process = executor.execute(command, tempDir, stdoutFile, stderrFile, env);
process.waitFor();
String content = Files.readString(stdoutFile);
assertThat(content).contains("test_value");
}
@Test
void execute_withNullCommand_shouldThrowException() {
assertThatThrownBy(() ->
executor.execute(null, tempDir, stdoutFile, stderrFile))
.isInstanceOf(NullPointerException.class);
}
@Test
void execute_withEmptyCommand_shouldThrowException() {
assertThatThrownBy(() ->
executor.execute(List.of(), tempDir, stdoutFile, stderrFile))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void execute_withNullWorkingDir_shouldThrowException() {
assertThatThrownBy(() ->
executor.execute(getEchoCommand("test"), null, stdoutFile, stderrFile))
.isInstanceOf(NullPointerException.class);
}
}
@Nested
class IsRunning {
@Test
void isRunning_withRunningProcess_shouldReturnTrue() throws IOException, InterruptedException {
Process process = executor.execute(getSleepCommand(60), tempDir, stdoutFile, stderrFile);
try {
assertThat(executor.isRunning(process.pid())).isTrue();
} finally {
process.destroyForcibly();
process.waitFor();
}
}
@Test
void isRunning_withTerminatedProcess_shouldReturnFalse() throws IOException, InterruptedException {
Process process = executor.execute(getEchoCommand("test"), tempDir, stdoutFile, stderrFile);
process.waitFor();
// Process handle might still exist briefly after termination
Thread.sleep(100);
assertThat(executor.isRunning(process.pid())).isFalse();
}
}
@Nested
class Kill {
@Test
void kill_withRunningProcess_shouldKillProcess() throws IOException, InterruptedException {
Process process = executor.execute(getSleepCommand(60), tempDir, stdoutFile, stderrFile);
long pid = process.pid();
boolean killed = executor.kill(pid);
assertThat(killed).isTrue();
Thread.sleep(100);
assertThat(executor.isRunning(pid)).isFalse();
}
@Test
void kill_withNonexistentPid_shouldReturnFalse() {
assertThat(executor.kill(999999999L)).isFalse();
}
}
@Nested
class WaitFor {
@Test
void waitFor_withTimeout_shouldReturnExitCode() throws IOException, InterruptedException {
Process process = executor.execute(getEchoCommand("test"), tempDir, stdoutFile, stderrFile);
Optional<Integer> exitCode = executor.waitFor(process, Duration.ofSeconds(10));
assertThat(exitCode).isPresent().hasValue(0);
}
@Test
void waitFor_withExpiredTimeout_shouldReturnEmpty() throws IOException, InterruptedException {
Process process = executor.execute(getSleepCommand(60), tempDir, stdoutFile, stderrFile);
try {
Optional<Integer> exitCode = executor.waitFor(process, Duration.ofMillis(100));
assertThat(exitCode).isEmpty();
} finally {
process.destroyForcibly();
process.waitFor();
}
}
@Test
void waitForBlocking_shouldReturnExitCode() throws IOException, InterruptedException {
Process process = executor.execute(getEchoCommand("test"), tempDir, stdoutFile, stderrFile);
int exitCode = executor.waitFor(process);
assertThat(exitCode).isZero();
}
}
// Platform-agnostic command helpers
private List<String> getEchoCommand(String message) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", message);
}
return List.of("echo", message);
}
private List<String> getStderrCommand(String message) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", message, "1>&2");
}
return List.of("sh", "-c", "echo '" + message + "' >&2");
}
private List<String> getEnvPrintCommand(String varName) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", "%" + varName + "%");
}
return List.of("sh", "-c", "echo $" + varName);
}
private List<String> getSleepCommand(int seconds) {
if (isWindows()) {
return List.of("cmd", "/c", "timeout", "/t", String.valueOf(seconds), "/nobreak");
}
return List.of("sleep", String.valueOf(seconds));
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}
@@ -0,0 +1,224 @@
package com.samsonmedia.barn.execution;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* Tests for ProcessMonitor.
*/
class ProcessMonitorTest {
private ProcessMonitor monitor;
@BeforeEach
void setUp() {
monitor = new ProcessMonitor(Duration.ofMillis(100));
}
@Nested
class Monitor {
@Test
void monitor_shouldEmitStartedEvent() throws IOException, InterruptedException {
Process process = startShortProcess();
List<ProcessMonitor.ProcessEvent> events = new ArrayList<>();
CountDownLatch startedLatch = new CountDownLatch(1);
monitor.monitor(process, event -> {
events.add(event);
if (event instanceof ProcessMonitor.ProcessEvent.Started) {
startedLatch.countDown();
}
});
assertThat(startedLatch.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(events.get(0)).isInstanceOf(ProcessMonitor.ProcessEvent.Started.class);
process.waitFor();
}
@Test
void monitor_shouldEmitCompletedEvent() throws IOException, InterruptedException {
Process process = startShortProcess();
AtomicReference<ProcessMonitor.ProcessEvent.Completed> completedEvent = new AtomicReference<>();
CountDownLatch completedLatch = new CountDownLatch(1);
monitor.monitor(process, event -> {
if (event instanceof ProcessMonitor.ProcessEvent.Completed completed) {
completedEvent.set(completed);
completedLatch.countDown();
}
});
assertThat(completedLatch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(completedEvent.get()).isNotNull();
assertThat(completedEvent.get().exitCode()).isZero();
assertThat(completedEvent.get().isSuccess()).isTrue();
}
@Test
void monitor_withLongRunningProcess_shouldEmitHeartbeats() throws IOException, InterruptedException {
Process process = startSleepProcess(2);
List<ProcessMonitor.ProcessEvent.Heartbeat> heartbeats = new ArrayList<>();
CountDownLatch heartbeatLatch = new CountDownLatch(2);
monitor.monitor(process, event -> {
if (event instanceof ProcessMonitor.ProcessEvent.Heartbeat hb) {
heartbeats.add(hb);
heartbeatLatch.countDown();
}
});
try {
assertThat(heartbeatLatch.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(heartbeats.size()).isGreaterThanOrEqualTo(2);
} finally {
process.destroyForcibly();
process.waitFor();
}
}
@Test
void monitor_shouldReturnFuture() throws IOException, InterruptedException {
Process process = startShortProcess();
CompletableFuture<ProcessMonitor.ProcessEvent.Completed> future =
monitor.monitor(process, event -> { });
ProcessMonitor.ProcessEvent.Completed completed = future.join();
assertThat(completed.exitCode()).isZero();
}
@Test
void monitor_withNullProcess_shouldThrowException() {
assertThatThrownBy(() -> monitor.monitor(null, event -> { }))
.isInstanceOf(NullPointerException.class);
}
@Test
void monitor_withNullHandler_shouldThrowException() throws IOException {
Process process = startShortProcess();
try {
assertThatThrownBy(() -> monitor.monitor(process, null))
.isInstanceOf(NullPointerException.class);
} finally {
process.destroyForcibly();
}
}
}
@Nested
class MonitorBlocking {
@Test
void monitorBlocking_shouldReturnCompletedEvent() throws IOException, InterruptedException {
Process process = startShortProcess();
ProcessMonitor.ProcessEvent.Completed completed = monitor.monitorBlocking(process, event -> { });
assertThat(completed).isNotNull();
assertThat(completed.exitCode()).isZero();
}
@Test
void monitorBlocking_shouldReceiveAllEvents() throws IOException, InterruptedException {
Process process = startShortProcess();
List<ProcessMonitor.ProcessEvent> events = new ArrayList<>();
monitor.monitorBlocking(process, events::add);
assertThat(events).isNotEmpty();
assertThat(events.get(0)).isInstanceOf(ProcessMonitor.ProcessEvent.Started.class);
assertThat(events.get(events.size() - 1)).isInstanceOf(ProcessMonitor.ProcessEvent.Completed.class);
}
}
@Nested
class ProcessEventTypes {
@Test
void started_shouldContainPidAndTimestamp() throws IOException, InterruptedException {
Process process = startShortProcess();
AtomicReference<ProcessMonitor.ProcessEvent.Started> startedEvent = new AtomicReference<>();
monitor.monitorBlocking(process, event -> {
if (event instanceof ProcessMonitor.ProcessEvent.Started started) {
startedEvent.set(started);
}
});
assertThat(startedEvent.get()).isNotNull();
assertThat(startedEvent.get().pid()).isGreaterThan(0);
assertThat(startedEvent.get().timestamp()).isNotNull();
}
@Test
void completed_isSuccess_withZeroExitCode_shouldReturnTrue() {
var completed = new ProcessMonitor.ProcessEvent.Completed(1234, 0, java.time.Instant.now());
assertThat(completed.isSuccess()).isTrue();
}
@Test
void completed_isSuccess_withNonZeroExitCode_shouldReturnFalse() {
var completed = new ProcessMonitor.ProcessEvent.Completed(1234, 1, java.time.Instant.now());
assertThat(completed.isSuccess()).isFalse();
}
}
@Nested
class EventHandlerErrors {
@Test
void monitor_withThrowingHandler_shouldContinue() throws IOException, InterruptedException {
Process process = startShortProcess();
AtomicReference<ProcessMonitor.ProcessEvent.Completed> completedEvent = new AtomicReference<>();
monitor.monitorBlocking(process, event -> {
if (event instanceof ProcessMonitor.ProcessEvent.Started) {
throw new RuntimeException("Handler error");
}
if (event instanceof ProcessMonitor.ProcessEvent.Completed completed) {
completedEvent.set(completed);
}
});
// Should still receive completed event despite handler error
assertThat(completedEvent.get()).isNotNull();
}
}
// Helper methods
private Process startShortProcess() throws IOException {
if (isWindows()) {
return new ProcessBuilder("cmd", "/c", "echo", "test").start();
}
return new ProcessBuilder("echo", "test").start();
}
private Process startSleepProcess(int seconds) throws IOException {
if (isWindows()) {
return new ProcessBuilder("cmd", "/c", "timeout", "/t", String.valueOf(seconds), "/nobreak").start();
}
return new ProcessBuilder("sleep", String.valueOf(seconds)).start();
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}
@@ -0,0 +1,180 @@
package com.samsonmedia.barn.execution;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
/**
* Tests for ProcessUtils.
*/
class ProcessUtilsTest {
@Nested
class IsAlive {
@Test
void isAlive_withCurrentProcess_shouldReturnTrue() {
long currentPid = ProcessUtils.getCurrentPid();
assertThat(ProcessUtils.isAlive(currentPid)).isTrue();
}
@Test
void isAlive_withNonexistentPid_shouldReturnFalse() {
// Use a very high PID that's unlikely to exist
assertThat(ProcessUtils.isAlive(999999999L)).isFalse();
}
@Test
void isAlive_withRunningProcess_shouldReturnTrue() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(getSleepCommand(5));
Process process = builder.start();
try {
assertThat(ProcessUtils.isAlive(process.pid())).isTrue();
} finally {
process.destroyForcibly();
process.waitFor();
}
}
}
@Nested
class KillTree {
@Test
void killTree_withRunningProcess_shouldKillProcess() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(getSleepCommand(60));
Process process = builder.start();
long pid = process.pid();
assertThat(ProcessUtils.isAlive(pid)).isTrue();
boolean killed = ProcessUtils.killTree(pid);
assertThat(killed).isTrue();
// Wait a moment for the process to die
Thread.sleep(100);
assertThat(ProcessUtils.isAlive(pid)).isFalse();
}
@Test
void killTree_withNonexistentPid_shouldReturnFalse() {
assertThat(ProcessUtils.killTree(999999999L)).isFalse();
}
}
@Nested
class KillTreeForcibly {
@Test
void killTreeForcibly_withRunningProcess_shouldKillProcess() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(getSleepCommand(60));
Process process = builder.start();
long pid = process.pid();
assertThat(ProcessUtils.isAlive(pid)).isTrue();
boolean killed = ProcessUtils.killTreeForcibly(pid);
assertThat(killed).isTrue();
Thread.sleep(100);
assertThat(ProcessUtils.isAlive(pid)).isFalse();
}
@Test
void killTreeForcibly_withNonexistentPid_shouldReturnFalse() {
assertThat(ProcessUtils.killTreeForcibly(999999999L)).isFalse();
}
}
@Nested
class GetHandle {
@Test
void getHandle_withCurrentProcess_shouldReturnHandle() {
long currentPid = ProcessUtils.getCurrentPid();
Optional<ProcessHandle> handle = ProcessUtils.getHandle(currentPid);
assertThat(handle).isPresent();
assertThat(handle.get().pid()).isEqualTo(currentPid);
}
@Test
void getHandle_withNonexistentPid_shouldReturnEmpty() {
Optional<ProcessHandle> handle = ProcessUtils.getHandle(999999999L);
assertThat(handle).isEmpty();
}
}
@Nested
class GetExitCode {
@Test
void getExitCode_withRunningProcess_shouldReturnEmpty() throws IOException {
ProcessBuilder builder = new ProcessBuilder(getSleepCommand(60));
Process process = builder.start();
try {
assertThat(ProcessUtils.getExitCode(process)).isEmpty();
} finally {
process.destroyForcibly();
}
}
@Test
void getExitCode_withTerminatedProcess_shouldReturnCode() throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(getEchoCommand("test"));
Process process = builder.start();
process.waitFor();
assertThat(ProcessUtils.getExitCode(process)).isPresent().hasValue(0);
}
}
@Nested
class GetCurrentPid {
@Test
void getCurrentPid_shouldReturnPositiveValue() {
long pid = ProcessUtils.getCurrentPid();
assertThat(pid).isGreaterThan(0);
}
@Test
void getCurrentPid_shouldReturnConsistentValue() {
long pid1 = ProcessUtils.getCurrentPid();
long pid2 = ProcessUtils.getCurrentPid();
assertThat(pid1).isEqualTo(pid2);
}
}
// Platform-agnostic command helpers
private List<String> getSleepCommand(int seconds) {
if (isWindows()) {
return List.of("cmd", "/c", "timeout", "/t", String.valueOf(seconds), "/nobreak");
}
return List.of("sleep", String.valueOf(seconds));
}
private List<String> getEchoCommand(String message) {
if (isWindows()) {
return List.of("cmd", "/c", "echo", message);
}
return List.of("echo", message);
}
private boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}