feat(state): implement filesystem state management system (#3)

- Add AtomicFiles utility for atomic file operations (write-to-tmp-then-rename)
- Add JobState enum (queued, running, succeeded, failed, canceled)
- Create BarnDirectories for managing directory structure:
  - jobs/, locks/, logs/ top-level directories
  - Per-job: work/input/, work/output/, logs/ subdirectories
  - Lock files for scheduler and individual jobs
- Implement StateFiles for reading/writing job metadata:
  - State, tag, timestamps (created_at, started_at, finished_at, heartbeat)
  - Exit codes (integer and symbolic), error messages
  - PID, retry count, retry history, stage
  - All timestamps in ISO-8601 format
- Add FileLock for cross-process file locking using FileChannel.tryLock()
- Handle OverlappingFileLockException for same-JVM lock attempts
- Add comprehensive tests (92 new tests, 211 total)

Closes #3

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:04:01 +00:00
1 parent 79f89be47c
commit 647e602fe9
10 files changed
+1917

No files matched your search

@@ -0,0 +1,144 @@
package com.samsonmedia.barn.state;
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.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Tests for AtomicFiles utility class.
*/
class AtomicFilesTest {
@TempDir
private Path tempDir;
@Test
void writeAtomically_withValidContent_shouldWriteFile() throws IOException {
Path target = tempDir.resolve("test.txt");
AtomicFiles.writeAtomically(target, "hello world");
assertThat(Files.readString(target)).isEqualTo("hello world");
}
@Test
void writeAtomically_withExistingFile_shouldOverwrite() throws IOException {
Path target = tempDir.resolve("test.txt");
Files.writeString(target, "old content");
AtomicFiles.writeAtomically(target, "new content");
assertThat(Files.readString(target)).isEqualTo("new content");
}
@Test
void writeAtomically_shouldNotLeaveTempFile() throws IOException {
Path target = tempDir.resolve("test.txt");
AtomicFiles.writeAtomically(target, "content");
assertThat(Files.exists(tempDir.resolve("test.txt.tmp"))).isFalse();
}
@Test
void writeAtomically_withNullTarget_shouldThrowException() {
assertThatThrownBy(() -> AtomicFiles.writeAtomically(null, "content"))
.isInstanceOf(NullPointerException.class);
}
@Test
void writeAtomically_withNullContent_shouldThrowException() {
Path target = tempDir.resolve("test.txt");
assertThatThrownBy(() -> AtomicFiles.writeAtomically(target, null))
.isInstanceOf(NullPointerException.class);
}
@Test
void readSafely_withExistingFile_shouldReturnContent() throws IOException {
Path file = tempDir.resolve("test.txt");
Files.writeString(file, " hello ");
Optional<String> result = AtomicFiles.readSafely(file);
assertThat(result).hasValue("hello");
}
@Test
void readSafely_withNonexistentFile_shouldReturnEmpty() throws IOException {
Path file = tempDir.resolve("nonexistent.txt");
Optional<String> result = AtomicFiles.readSafely(file);
assertThat(result).isEmpty();
}
@Test
void readSafely_withNullPath_shouldThrowException() {
assertThatThrownBy(() -> AtomicFiles.readSafely(null))
.isInstanceOf(NullPointerException.class);
}
@Test
void readOrEmpty_withExistingFile_shouldReturnContent() throws IOException {
Path file = tempDir.resolve("test.txt");
Files.writeString(file, "content");
String result = AtomicFiles.readOrEmpty(file);
assertThat(result).isEqualTo("content");
}
@Test
void readOrEmpty_withNonexistentFile_shouldReturnEmptyString() throws IOException {
Path file = tempDir.resolve("nonexistent.txt");
String result = AtomicFiles.readOrEmpty(file);
assertThat(result).isEmpty();
}
@Test
void deleteIfExists_withExistingFile_shouldDeleteAndReturnTrue() throws IOException {
Path file = tempDir.resolve("test.txt");
Files.writeString(file, "content");
boolean result = AtomicFiles.deleteIfExists(file);
assertThat(result).isTrue();
assertThat(Files.exists(file)).isFalse();
}
@Test
void deleteIfExists_withNonexistentFile_shouldReturnFalse() throws IOException {
Path file = tempDir.resolve("nonexistent.txt");
boolean result = AtomicFiles.deleteIfExists(file);
assertThat(result).isFalse();
}
@Test
void ensureParentDirectories_withNestedPath_shouldCreateDirectories() throws IOException {
Path nested = tempDir.resolve("a/b/c/file.txt");
AtomicFiles.ensureParentDirectories(nested);
assertThat(Files.exists(tempDir.resolve("a/b/c"))).isTrue();
}
@Test
void ensureParentDirectories_withExistingParent_shouldNotFail() throws IOException {
Path file = tempDir.resolve("file.txt");
AtomicFiles.ensureParentDirectories(file);
// No exception means success
}
}
@@ -0,0 +1,191 @@
package com.samsonmedia.barn.state;
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 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 BarnDirectories.
*/
class BarnDirectoriesTest {
@TempDir
private Path tempDir;
private BarnDirectories directories;
@BeforeEach
void setUp() {
directories = new BarnDirectories(tempDir);
}
@Nested
class PathResolution {
@Test
void getBaseDir_shouldReturnBaseDir() {
assertThat(directories.getBaseDir()).isEqualTo(tempDir);
}
@Test
void getJobsDir_shouldReturnJobsSubdir() {
assertThat(directories.getJobsDir()).isEqualTo(tempDir.resolve("jobs"));
}
@Test
void getLocksDir_shouldReturnLocksSubdir() {
assertThat(directories.getLocksDir()).isEqualTo(tempDir.resolve("locks"));
}
@Test
void getLogsDir_shouldReturnLogsSubdir() {
assertThat(directories.getLogsDir()).isEqualTo(tempDir.resolve("logs"));
}
@Test
void getJobDir_shouldReturnJobSpecificDir() {
assertThat(directories.getJobDir("job-123"))
.isEqualTo(tempDir.resolve("jobs/job-123"));
}
@Test
void getJobWorkDir_shouldReturnWorkSubdir() {
assertThat(directories.getJobWorkDir("job-123"))
.isEqualTo(tempDir.resolve("jobs/job-123/work"));
}
@Test
void getJobInputDir_shouldReturnInputSubdir() {
assertThat(directories.getJobInputDir("job-123"))
.isEqualTo(tempDir.resolve("jobs/job-123/work/input"));
}
@Test
void getJobOutputDir_shouldReturnOutputSubdir() {
assertThat(directories.getJobOutputDir("job-123"))
.isEqualTo(tempDir.resolve("jobs/job-123/work/output"));
}
@Test
void getJobLogsDir_shouldReturnLogsSubdir() {
assertThat(directories.getJobLogsDir("job-123"))
.isEqualTo(tempDir.resolve("jobs/job-123/logs"));
}
@Test
void getJobLockFile_shouldReturnLockFile() {
assertThat(directories.getJobLockFile("job-123"))
.isEqualTo(tempDir.resolve("locks/job-job-123.lock"));
}
@Test
void getSchedulerLockFile_shouldReturnSchedulerLock() {
assertThat(directories.getSchedulerLockFile())
.isEqualTo(tempDir.resolve("locks/scheduler.lock"));
}
@Test
void getDaemonLogFile_shouldReturnBarnLog() {
assertThat(directories.getDaemonLogFile())
.isEqualTo(tempDir.resolve("logs/barn.log"));
}
}
@Nested
class DirectoryCreation {
@Test
void initialize_shouldCreateTopLevelDirectories() throws IOException {
directories.initialize();
assertThat(Files.isDirectory(directories.getJobsDir())).isTrue();
assertThat(Files.isDirectory(directories.getLocksDir())).isTrue();
assertThat(Files.isDirectory(directories.getLogsDir())).isTrue();
}
@Test
void initialize_withExistingDirectories_shouldNotFail() throws IOException {
directories.initialize();
directories.initialize(); // Second call should not fail
}
@Test
void createJobDirs_shouldCreateFullJobStructure() throws IOException {
directories.initialize();
directories.createJobDirs("job-123");
assertThat(Files.isDirectory(directories.getJobDir("job-123"))).isTrue();
assertThat(Files.isDirectory(directories.getJobInputDir("job-123"))).isTrue();
assertThat(Files.isDirectory(directories.getJobOutputDir("job-123"))).isTrue();
assertThat(Files.isDirectory(directories.getJobLogsDir("job-123"))).isTrue();
}
@Test
void createJobDirs_withNullJobId_shouldThrowException() {
assertThatThrownBy(() -> directories.createJobDirs(null))
.isInstanceOf(NullPointerException.class);
}
}
@Nested
class JobExistence {
@Test
void jobExists_withExistingJob_shouldReturnTrue() throws IOException {
directories.initialize();
directories.createJobDirs("job-123");
assertThat(directories.jobExists("job-123")).isTrue();
}
@Test
void jobExists_withNonexistentJob_shouldReturnFalse() throws IOException {
directories.initialize();
assertThat(directories.jobExists("nonexistent")).isFalse();
}
}
@Nested
class JobDeletion {
@Test
void deleteJobDir_shouldRemoveJobDirectory() throws IOException {
directories.initialize();
directories.createJobDirs("job-123");
Files.writeString(directories.getJobDir("job-123").resolve("state"), "running");
directories.deleteJobDir("job-123");
assertThat(directories.jobExists("job-123")).isFalse();
}
@Test
void deleteJobDir_withNonexistentJob_shouldNotFail() throws IOException {
directories.initialize();
directories.deleteJobDir("nonexistent"); // Should not throw
}
@Test
void deleteJobDir_withNullJobId_shouldThrowException() {
assertThatThrownBy(() -> directories.deleteJobDir(null))
.isInstanceOf(NullPointerException.class);
}
}
@Test
void constructor_withNullBaseDir_shouldThrowException() {
assertThatThrownBy(() -> new BarnDirectories(null))
.isInstanceOf(NullPointerException.class);
}
}
@@ -0,0 +1,135 @@
package com.samsonmedia.barn.state;
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.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
/**
* Tests for FileLock.
*/
class FileLockTest {
@TempDir
private Path tempDir;
@Test
void tryAcquire_shouldAcquireLock() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
Optional<FileLock> lock = FileLock.tryAcquire(lockFile);
assertThat(lock).isPresent();
lock.get().close();
}
@Test
void tryAcquire_shouldCreateLockFile() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
try (FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow()) {
assertThat(lock).isNotNull();
assertThat(Files.exists(lockFile)).isTrue();
}
}
@Test
void tryAcquire_withNestedPath_shouldCreateParentDirs() throws IOException {
Path lockFile = tempDir.resolve("a/b/c/test.lock");
try (FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow()) {
assertThat(lock).isNotNull();
assertThat(Files.exists(lockFile)).isTrue();
}
}
@Test
void tryAcquire_whenAlreadyLocked_shouldReturnEmpty() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
try (FileLock lock1 = FileLock.tryAcquire(lockFile).orElseThrow()) {
assertThat(lock1).isNotNull();
Optional<FileLock> lock2 = FileLock.tryAcquire(lockFile);
assertThat(lock2).isEmpty();
}
}
@Test
void close_shouldReleaseLock() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
FileLock lock1 = FileLock.tryAcquire(lockFile).orElseThrow();
lock1.close();
// Should be able to acquire lock again
Optional<FileLock> lock2 = FileLock.tryAcquire(lockFile);
assertThat(lock2).isPresent();
lock2.get().close();
}
@Test
void close_shouldDeleteLockFile() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow();
lock.close();
assertThat(Files.exists(lockFile)).isFalse();
}
@Test
void getLockFile_shouldReturnLockFilePath() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
try (FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow()) {
assertThat(lock.getLockFile()).isEqualTo(lockFile);
}
}
@Test
void isLocked_withLockedFile_shouldReturnTrue() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
try (FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow()) {
assertThat(lock).isNotNull();
assertThat(FileLock.isLocked(lockFile)).isTrue();
}
}
@Test
void isLocked_withUnlockedFile_shouldReturnFalse() throws IOException {
Path lockFile = tempDir.resolve("test.lock");
// Acquire and release
FileLock lock = FileLock.tryAcquire(lockFile).orElseThrow();
lock.close();
assertThat(FileLock.isLocked(lockFile)).isFalse();
}
@Test
void isLocked_withNonexistentFile_shouldReturnFalse() {
Path lockFile = tempDir.resolve("nonexistent.lock");
assertThat(FileLock.isLocked(lockFile)).isFalse();
}
@Test
void tryAcquire_withNullPath_shouldThrowException() {
assertThatThrownBy(() -> FileLock.tryAcquire(null))
.isInstanceOf(NullPointerException.class);
}
@Test
void isLocked_withNullPath_shouldThrowException() {
assertThatThrownBy(() -> FileLock.isLocked(null))
.isInstanceOf(NullPointerException.class);
}
}
@@ -0,0 +1,94 @@
package com.samsonmedia.barn.state;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
/**
* Tests for JobState enum.
*/
class JobStateTest {
@Test
void fromString_withValidLowercase_shouldReturnState() {
assertThat(JobState.fromString("queued")).isEqualTo(JobState.QUEUED);
assertThat(JobState.fromString("running")).isEqualTo(JobState.RUNNING);
assertThat(JobState.fromString("succeeded")).isEqualTo(JobState.SUCCEEDED);
assertThat(JobState.fromString("failed")).isEqualTo(JobState.FAILED);
assertThat(JobState.fromString("canceled")).isEqualTo(JobState.CANCELED);
}
@Test
void fromString_withValidUppercase_shouldReturnState() {
assertThat(JobState.fromString("QUEUED")).isEqualTo(JobState.QUEUED);
assertThat(JobState.fromString("RUNNING")).isEqualTo(JobState.RUNNING);
}
@Test
void fromString_withMixedCase_shouldReturnState() {
assertThat(JobState.fromString("Running")).isEqualTo(JobState.RUNNING);
assertThat(JobState.fromString("Failed")).isEqualTo(JobState.FAILED);
}
@Test
void fromString_withWhitespace_shouldTrimAndParse() {
assertThat(JobState.fromString(" queued ")).isEqualTo(JobState.QUEUED);
}
@Test
void fromString_withNull_shouldThrowException() {
assertThatThrownBy(() -> JobState.fromString(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("cannot be null or blank");
}
@Test
void fromString_withBlank_shouldThrowException() {
assertThatThrownBy(() -> JobState.fromString(" "))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void fromString_withInvalidValue_shouldThrowException() {
assertThatThrownBy(() -> JobState.fromString("invalid"))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Invalid job state")
.hasMessageContaining("queued, running, succeeded, failed, canceled");
}
@Test
void toLowercase_shouldReturnLowercaseName() {
assertThat(JobState.QUEUED.toLowercase()).isEqualTo("queued");
assertThat(JobState.RUNNING.toLowercase()).isEqualTo("running");
assertThat(JobState.SUCCEEDED.toLowercase()).isEqualTo("succeeded");
assertThat(JobState.FAILED.toLowercase()).isEqualTo("failed");
assertThat(JobState.CANCELED.toLowercase()).isEqualTo("canceled");
}
@Test
void isTerminal_forTerminalStates_shouldReturnTrue() {
assertThat(JobState.SUCCEEDED.isTerminal()).isTrue();
assertThat(JobState.FAILED.isTerminal()).isTrue();
assertThat(JobState.CANCELED.isTerminal()).isTrue();
}
@Test
void isTerminal_forActiveStates_shouldReturnFalse() {
assertThat(JobState.QUEUED.isTerminal()).isFalse();
assertThat(JobState.RUNNING.isTerminal()).isFalse();
}
@Test
void isActive_forActiveStates_shouldReturnTrue() {
assertThat(JobState.QUEUED.isActive()).isTrue();
assertThat(JobState.RUNNING.isActive()).isTrue();
}
@Test
void isActive_forTerminalStates_shouldReturnFalse() {
assertThat(JobState.SUCCEEDED.isActive()).isFalse();
assertThat(JobState.FAILED.isActive()).isFalse();
assertThat(JobState.CANCELED.isActive()).isFalse();
}
}
@@ -0,0 +1,338 @@
package com.samsonmedia.barn.state;
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.Instant;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.OptionalLong;
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 StateFiles.
*/
class StateFilesTest {
@TempDir
private Path tempDir;
private Path jobDir;
private StateFiles stateFiles;
@BeforeEach
void setUp() throws IOException {
jobDir = tempDir.resolve("job-123");
Files.createDirectories(jobDir);
stateFiles = new StateFiles(jobDir);
}
@Nested
class StateTests {
@Test
void writeState_shouldWritePlainText() throws IOException {
stateFiles.writeState(JobState.RUNNING);
String content = Files.readString(jobDir.resolve("state"));
assertThat(content).isEqualTo("running");
}
@Test
void readState_withExistingFile_shouldReturnState() throws IOException {
Files.writeString(jobDir.resolve("state"), "queued");
Optional<JobState> result = stateFiles.readState();
assertThat(result).hasValue(JobState.QUEUED);
}
@Test
void readState_withNonexistentFile_shouldReturnEmpty() throws IOException {
Optional<JobState> result = stateFiles.readState();
assertThat(result).isEmpty();
}
@Test
void writeAndReadState_shouldRoundTrip() throws IOException {
stateFiles.writeState(JobState.FAILED);
Optional<JobState> result = stateFiles.readState();
assertThat(result).hasValue(JobState.FAILED);
}
}
@Nested
class TagTests {
@Test
void writeAndReadTag_shouldRoundTrip() throws IOException {
stateFiles.writeTag("my-tag");
Optional<String> result = stateFiles.readTag();
assertThat(result).hasValue("my-tag");
}
@Test
void readTag_withNonexistentFile_shouldReturnEmpty() throws IOException {
Optional<String> result = stateFiles.readTag();
assertThat(result).isEmpty();
}
}
@Nested
class TimestampTests {
private final Instant testTimestamp = Instant.parse("2026-01-21T18:42:10Z");
@Test
void writeCreatedAt_shouldWriteIso8601() throws IOException {
stateFiles.writeCreatedAt(testTimestamp);
String content = Files.readString(jobDir.resolve("created_at"));
assertThat(content).isEqualTo("2026-01-21T18:42:10Z");
}
@Test
void readCreatedAt_shouldParseIso8601() throws IOException {
Files.writeString(jobDir.resolve("created_at"), "2026-01-21T18:42:10Z");
Optional<Instant> result = stateFiles.readCreatedAt();
assertThat(result).hasValue(testTimestamp);
}
@Test
void writeAndReadStartedAt_shouldRoundTrip() throws IOException {
stateFiles.writeStartedAt(testTimestamp);
Optional<Instant> result = stateFiles.readStartedAt();
assertThat(result).hasValue(testTimestamp);
}
@Test
void writeAndReadFinishedAt_shouldRoundTrip() throws IOException {
stateFiles.writeFinishedAt(testTimestamp);
Optional<Instant> result = stateFiles.readFinishedAt();
assertThat(result).hasValue(testTimestamp);
}
@Test
void writeAndReadHeartbeat_shouldRoundTrip() throws IOException {
stateFiles.writeHeartbeat(testTimestamp);
Optional<Instant> result = stateFiles.readHeartbeat();
assertThat(result).hasValue(testTimestamp);
}
@Test
void writeAndReadRetryAt_shouldRoundTrip() throws IOException {
stateFiles.writeRetryAt(testTimestamp);
Optional<Instant> result = stateFiles.readRetryAt();
assertThat(result).hasValue(testTimestamp);
}
@Test
void readTimestamp_withInvalidFormat_shouldReturnEmpty() throws IOException {
Files.writeString(jobDir.resolve("created_at"), "not a timestamp");
Optional<Instant> result = stateFiles.readCreatedAt();
assertThat(result).isEmpty();
}
@Test
void readTimestamp_withNonexistentFile_shouldReturnEmpty() throws IOException {
Optional<Instant> result = stateFiles.readCreatedAt();
assertThat(result).isEmpty();
}
}
@Nested
class ExitCodeTests {
@Test
void writeExitCode_withInteger_shouldWrite() throws IOException {
stateFiles.writeExitCode(0);
String content = Files.readString(jobDir.resolve("exit_code"));
assertThat(content).isEqualTo("0");
}
@Test
void writeExitCode_withSymbolic_shouldWrite() throws IOException {
stateFiles.writeExitCode("orphaned_process");
String content = Files.readString(jobDir.resolve("exit_code"));
assertThat(content).isEqualTo("orphaned_process");
}
@Test
void readExitCode_withInteger_shouldReturnInt() throws IOException {
Files.writeString(jobDir.resolve("exit_code"), "42");
OptionalInt result = stateFiles.readExitCode();
assertThat(result).hasValue(42);
}
@Test
void readExitCode_withSymbolic_shouldReturnEmpty() throws IOException {
Files.writeString(jobDir.resolve("exit_code"), "orphaned_process");
OptionalInt result = stateFiles.readExitCode();
assertThat(result).isEmpty();
}
@Test
void readExitCodeString_withSymbolic_shouldReturnString() throws IOException {
Files.writeString(jobDir.resolve("exit_code"), "orphaned_process");
Optional<String> result = stateFiles.readExitCodeString();
assertThat(result).hasValue("orphaned_process");
}
}
@Nested
class ErrorTests {
@Test
void writeAndReadError_shouldRoundTrip() throws IOException {
stateFiles.writeError("Something went wrong");
Optional<String> result = stateFiles.readError();
assertThat(result).hasValue("Something went wrong");
}
@Test
void readError_withNonexistentFile_shouldReturnEmpty() throws IOException {
Optional<String> result = stateFiles.readError();
assertThat(result).isEmpty();
}
}
@Nested
class PidTests {
@Test
void writeAndReadPid_shouldRoundTrip() throws IOException {
stateFiles.writePid(12345L);
OptionalLong result = stateFiles.readPid();
assertThat(result).hasValue(12345L);
}
@Test
void readPid_withNonNumeric_shouldReturnEmpty() throws IOException {
Files.writeString(jobDir.resolve("pid"), "not a number");
OptionalLong result = stateFiles.readPid();
assertThat(result).isEmpty();
}
@Test
void readPid_withNonexistentFile_shouldReturnEmpty() throws IOException {
OptionalLong result = stateFiles.readPid();
assertThat(result).isEmpty();
}
}
@Nested
class RetryCountTests {
@Test
void writeAndReadRetryCount_shouldRoundTrip() throws IOException {
stateFiles.writeRetryCount(2);
OptionalInt result = stateFiles.readRetryCount();
assertThat(result).hasValue(2);
}
@Test
void readRetryCount_withNonexistentFile_shouldReturnEmpty() throws IOException {
OptionalInt result = stateFiles.readRetryCount();
assertThat(result).isEmpty();
}
}
@Nested
class RetryHistoryTests {
@Test
void appendRetryHistory_shouldAddEntry() throws IOException {
stateFiles.appendRetryHistory("Attempt 1: exit_code=1");
Optional<String> result = stateFiles.readRetryHistory();
assertThat(result).hasValue("Attempt 1: exit_code=1");
}
@Test
void appendRetryHistory_shouldAppendToExisting() throws IOException {
stateFiles.appendRetryHistory("Attempt 1: exit_code=1");
stateFiles.appendRetryHistory("Attempt 2: exit_code=2");
Optional<String> result = stateFiles.readRetryHistory();
assertThat(result).hasValue("Attempt 1: exit_code=1\nAttempt 2: exit_code=2");
}
}
@Nested
class StageTests {
@Test
void writeAndReadStage_shouldRoundTrip() throws IOException {
stateFiles.writeStage("transcode");
Optional<String> result = stateFiles.readStage();
assertThat(result).hasValue("transcode");
}
@Test
void readStage_withNonexistentFile_shouldReturnEmpty() throws IOException {
Optional<String> result = stateFiles.readStage();
assertThat(result).isEmpty();
}
}
@Test
void getJobDir_shouldReturnJobDir() {
assertThat(stateFiles.getJobDir()).isEqualTo(jobDir);
}
@Test
void constructor_withNullJobDir_shouldThrowException() {
assertThatThrownBy(() -> new StateFiles(null))
.isInstanceOf(NullPointerException.class);
}
}