This repository has been archived on 2026-10-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
barn/src/test/java/com/samsonmedia/barn/execution/HeartbeatCheckerTest.java
T
Daniel SamsonandClaude Opus 4.5 f233547ce8 feat(scheduler): add per-level job limits based on load classification
Implements #34 - Job Limits Per Load Level

Jobs are now classified into HIGH, MEDIUM, or LOW load levels based on
gitignore-style whitelist files (.load files). This allows concurrent
execution of different types of jobs (e.g., downloading while transcoding).

Changes:
- Add LoadLevel enum with default limits (HIGH=2, MEDIUM=8, LOW=32)
- Add LoadLevelConfig record for per-level max jobs configuration
- Add CommandMatcher sealed interface for matching commands by name/path/dir
- Add LoadFileParser for parsing .load whitelist files
- Add LoadLevelClassifier for auto-classifying commands
- Add ConfigInstallCommand (barn config install) for default config files
- Update JobScheduler to use per-level limits with EnumMap
- Add --load-level/-l option to barn run for manual override
- Add load level display to barn status and barn describe output
- Remove --user flag from service install/uninstall (system-wide only)
- Update documentation for load levels and whitelist files

Default whitelists:
- high.load: ffmpeg, ffprobe, handbrake, x264, x265, etc.
- low.load: curl, wget, rclone, rsync, aria2c, etc.
- medium.load: empty (unmatched commands default to MEDIUM)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 14:33:06 +00:00

170 lines
5.2 KiB
Java

package com.samsonmedia.barn.execution;
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import com.samsonmedia.barn.jobs.Job;
import com.samsonmedia.barn.state.JobState;
/**
* Tests for HeartbeatChecker.
*/
class HeartbeatCheckerTest {
private static final Duration DEFAULT_THRESHOLD = Duration.ofSeconds(30);
private HeartbeatChecker checker;
@BeforeEach
void setUp() {
checker = new HeartbeatChecker(DEFAULT_THRESHOLD);
}
@Nested
class Constructor {
@Test
void constructor_withNullThreshold_shouldThrowException() {
assertThatThrownBy(() -> new HeartbeatChecker(null))
.isInstanceOf(NullPointerException.class);
}
@Test
void constructor_withZeroThreshold_shouldThrowException() {
assertThatThrownBy(() -> new HeartbeatChecker(Duration.ZERO))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void constructor_withNegativeThreshold_shouldThrowException() {
assertThatThrownBy(() -> new HeartbeatChecker(Duration.ofSeconds(-1)))
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void constructor_withValidThreshold_shouldSucceed() {
HeartbeatChecker hc = new HeartbeatChecker(Duration.ofSeconds(10));
assertThat(hc.getThreshold()).isEqualTo(Duration.ofSeconds(10));
}
}
@Nested
class IsStale {
@Test
void isStale_withQueuedJob_shouldReturnFalse() {
Job job = createJob(JobState.QUEUED, null);
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withSucceededJob_shouldReturnFalse() {
Job job = createJob(JobState.SUCCEEDED, null);
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withFailedJob_shouldReturnFalse() {
Job job = createJob(JobState.FAILED, null);
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withCanceledJob_shouldReturnFalse() {
Job job = createJob(JobState.CANCELED, null);
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withRunningJobNoHeartbeat_shouldReturnTrue() {
Job job = createJob(JobState.RUNNING, null);
assertThat(checker.isStale(job)).isTrue();
}
@Test
void isStale_withRunningJobFreshHeartbeat_shouldReturnFalse() {
Instant recent = Instant.now().minus(Duration.ofSeconds(5));
Job job = createJob(JobState.RUNNING, recent);
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withRunningJobStaleHeartbeat_shouldReturnTrue() {
Instant stale = Instant.now().minus(Duration.ofSeconds(60));
Job job = createJob(JobState.RUNNING, stale);
assertThat(checker.isStale(job)).isTrue();
}
@Test
void isStale_withRunningJobBoundaryHeartbeat_shouldReturnFalse() {
// Heartbeat just inside threshold boundary should not be stale
// Add 100ms buffer to account for test execution time
Instant boundary = Instant.now().minus(DEFAULT_THRESHOLD).plusMillis(100);
Job job = createJob(JobState.RUNNING, boundary);
// Just inside the boundary is acceptable
assertThat(checker.isStale(job)).isFalse();
}
@Test
void isStale_withRunningJobJustPastThreshold_shouldReturnTrue() {
Instant pastThreshold = Instant.now().minus(DEFAULT_THRESHOLD).minusMillis(1);
Job job = createJob(JobState.RUNNING, pastThreshold);
assertThat(checker.isStale(job)).isTrue();
}
@Test
void isStale_withNullJob_shouldThrowException() {
assertThatThrownBy(() -> checker.isStale(null))
.isInstanceOf(NullPointerException.class);
}
}
@Nested
class GetThreshold {
@Test
void getThreshold_shouldReturnConfiguredThreshold() {
HeartbeatChecker hc = new HeartbeatChecker(Duration.ofMinutes(5));
assertThat(hc.getThreshold()).isEqualTo(Duration.ofMinutes(5));
}
}
private Job createJob(JobState state, Instant heartbeat) {
return new Job(
"job-12345678",
state,
List.of("echo", "test"),
null, // tag
Instant.now(), // createdAt
state == JobState.RUNNING ? Instant.now().minus(Duration.ofMinutes(1)) : null, // startedAt
null, // finishedAt
null, // exitCode
null, // error
state == JobState.RUNNING ? 12345L : null, // pid
heartbeat,
0, // retryCount
null, // retryAt
MEDIUM
);
}
}