Archived
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>
This commit is contained in:
1 parent
5a213788c7
commit
f233547ce8
52 files changed
+2444
-193
No files matched your search
@@ -112,11 +112,12 @@ class ConfigCommandTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void show_withNonExistentConfig_shouldShowDefaultsMessage() {
|
||||
int exitCode = cmd.execute("config", "show");
|
||||
void show_withDefaults_shouldShowDefaultsIndicator() {
|
||||
// Using --defaults explicitly shows the default values indicator
|
||||
int exitCode = cmd.execute("config", "show", "--defaults");
|
||||
|
||||
assertThat(exitCode).isZero();
|
||||
assertThat(outWriter.toString()).contains("(none found, using defaults)");
|
||||
assertThat(outWriter.toString()).contains("Showing default values");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
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;
|
||||
|
||||
import picocli.CommandLine;
|
||||
|
||||
/**
|
||||
* Tests for ConfigInstallCommand.
|
||||
*/
|
||||
class ConfigInstallCommandTest {
|
||||
|
||||
@TempDir
|
||||
private Path tempDir;
|
||||
|
||||
private StringWriter outWriter;
|
||||
private StringWriter errWriter;
|
||||
private CommandLine cmd;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
outWriter = new StringWriter();
|
||||
errWriter = new StringWriter();
|
||||
cmd = new CommandLine(new BarnCommand());
|
||||
cmd.setOut(new PrintWriter(outWriter));
|
||||
cmd.setErr(new PrintWriter(errWriter));
|
||||
}
|
||||
|
||||
@Nested
|
||||
class InstallWithForce {
|
||||
|
||||
@Test
|
||||
void install_withForce_shouldCreateAllFiles() {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
int exitCode = cmd.execute("config", "install",
|
||||
"--directory", targetDir.toString(),
|
||||
"--force");
|
||||
|
||||
assertThat(exitCode).isZero();
|
||||
assertThat(Files.exists(targetDir.resolve("barn.conf"))).isTrue();
|
||||
assertThat(Files.exists(targetDir.resolve("high.load"))).isTrue();
|
||||
assertThat(Files.exists(targetDir.resolve("medium.load"))).isTrue();
|
||||
assertThat(Files.exists(targetDir.resolve("low.load"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void install_withForce_shouldCreateValidBarnConf() throws Exception {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
cmd.execute("config", "install", "--directory", targetDir.toString(), "--force");
|
||||
|
||||
String content = Files.readString(targetDir.resolve("barn.conf"));
|
||||
assertThat(content).contains("[service]");
|
||||
assertThat(content).contains("[load_levels]");
|
||||
assertThat(content).contains("[jobs]");
|
||||
assertThat(content).contains("[cleanup]");
|
||||
assertThat(content).contains("[storage]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void install_withForce_shouldCreateValidHighLoad() throws Exception {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
cmd.execute("config", "install", "--directory", targetDir.toString(), "--force");
|
||||
|
||||
String content = Files.readString(targetDir.resolve("high.load"));
|
||||
assertThat(content).contains("ffmpeg");
|
||||
assertThat(content).contains("x264");
|
||||
}
|
||||
|
||||
@Test
|
||||
void install_withForce_shouldCreateValidLowLoad() throws Exception {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
cmd.execute("config", "install", "--directory", targetDir.toString(), "--force");
|
||||
|
||||
String content = Files.readString(targetDir.resolve("low.load"));
|
||||
assertThat(content).contains("curl");
|
||||
assertThat(content).contains("wget");
|
||||
}
|
||||
|
||||
@Test
|
||||
void install_withForce_shouldOverwriteExistingFiles() throws Exception {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
Files.createDirectories(targetDir);
|
||||
Files.writeString(targetDir.resolve("barn.conf"), "old content");
|
||||
|
||||
int exitCode = cmd.execute("config", "install",
|
||||
"--directory", targetDir.toString(),
|
||||
"--force");
|
||||
|
||||
assertThat(exitCode).isZero();
|
||||
String content = Files.readString(targetDir.resolve("barn.conf"));
|
||||
assertThat(content).contains("[service]");
|
||||
assertThat(content).doesNotContain("old content");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ShowOption {
|
||||
|
||||
@Test
|
||||
void install_withShow_shouldNotWriteFiles() {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
int exitCode = cmd.execute("config", "install",
|
||||
"--directory", targetDir.toString(),
|
||||
"--show");
|
||||
|
||||
assertThat(exitCode).isZero();
|
||||
assertThat(Files.exists(targetDir)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void install_withShow_shouldDisplayFileList() {
|
||||
Path targetDir = tempDir.resolve("barn-config");
|
||||
|
||||
cmd.execute("config", "install",
|
||||
"--directory", targetDir.toString(),
|
||||
"--show");
|
||||
|
||||
String output = outWriter.toString();
|
||||
assertThat(output).contains("barn.conf");
|
||||
assertThat(output).contains("high.load");
|
||||
assertThat(output).contains("medium.load");
|
||||
assertThat(output).contains("low.load");
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class HelpOption {
|
||||
|
||||
@Test
|
||||
void install_withHelp_shouldShowUsage() {
|
||||
int exitCode = cmd.execute("config", "install", "--help");
|
||||
|
||||
assertThat(exitCode).isZero();
|
||||
assertThat(outWriter.toString()).contains("Install default configuration files");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -50,6 +51,8 @@ class HumanFormatterTest {
|
||||
assertThat(result).contains("Job: job-12345678");
|
||||
assertThat(result).contains("State:");
|
||||
assertThat(result).contains("running");
|
||||
assertThat(result).contains("Load:");
|
||||
assertThat(result).contains("medium");
|
||||
assertThat(result).contains("Tag:");
|
||||
assertThat(result).contains("test-tag");
|
||||
}
|
||||
@@ -115,11 +118,13 @@ class HumanFormatterTest {
|
||||
|
||||
assertThat(result).contains("ID");
|
||||
assertThat(result).contains("STATE");
|
||||
assertThat(result).contains("LOAD");
|
||||
assertThat(result).contains("TAG");
|
||||
assertThat(result).contains("job-11111111");
|
||||
assertThat(result).contains("job-22222222");
|
||||
assertThat(result).contains("running");
|
||||
assertThat(result).contains("queued");
|
||||
assertThat(result).contains("medium");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -195,7 +200,8 @@ class HumanFormatterTest {
|
||||
state == JobState.RUNNING ? 12345L : null,
|
||||
null,
|
||||
0,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
|
||||
@@ -213,7 +219,8 @@ class HumanFormatterTest {
|
||||
null,
|
||||
null,
|
||||
0,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
|
||||
@@ -231,7 +238,8 @@ class HumanFormatterTest {
|
||||
null,
|
||||
null,
|
||||
0,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -195,7 +196,8 @@ class JsonFormatterTest {
|
||||
state == JobState.RUNNING ? 12345L : null,
|
||||
null,
|
||||
0,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,8 @@ class ServiceInstallCommandTest {
|
||||
int exitCode = cmd.execute("service", "install", "--help");
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(stdout.toString()).contains("Install Barn as a system service");
|
||||
assertThat(stdout.toString()).contains("--user");
|
||||
assertThat(stdout.toString()).contains("--binary");
|
||||
assertThat(stdout.toString()).contains("--barn-dir");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,7 +82,6 @@ class ServiceInstallCommandTest {
|
||||
int exitCode = cmd.execute("service", "uninstall", "--help");
|
||||
assertThat(exitCode).isEqualTo(0);
|
||||
assertThat(stdout.toString()).contains("Uninstall Barn system service");
|
||||
assertThat(stdout.toString()).contains("--user");
|
||||
assertThat(stdout.toString()).contains("--force");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.cli;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.Duration;
|
||||
@@ -207,7 +208,8 @@ class XmlFormatterTest {
|
||||
state == JobState.RUNNING ? 12345L : null,
|
||||
null,
|
||||
0,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.samsonmedia.barn.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests for CommandMatcher sealed interface and implementations.
|
||||
*/
|
||||
class CommandMatcherTest {
|
||||
|
||||
@Nested
|
||||
class ExecutableNameMatcherTests {
|
||||
|
||||
@Test
|
||||
void matches_withMatchingExecutableName_shouldReturnTrue() {
|
||||
var matcher = new CommandMatcher.ExecutableName("ffmpeg");
|
||||
|
||||
assertThat(matcher.matches(List.of("ffmpeg", "-i", "input.mp4"))).isTrue();
|
||||
assertThat(matcher.matches(List.of("/usr/bin/ffmpeg", "-i", "input.mp4"))).isTrue();
|
||||
assertThat(matcher.matches(List.of("/opt/local/bin/ffmpeg", "-version"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withNonMatchingExecutableName_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.ExecutableName("ffmpeg");
|
||||
|
||||
assertThat(matcher.matches(List.of("curl", "http://example.com"))).isFalse();
|
||||
assertThat(matcher.matches(List.of("/usr/bin/curl", "-O", "file"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withPartialMatch_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.ExecutableName("ffmpeg");
|
||||
|
||||
// ffmpegXXX should not match
|
||||
assertThat(matcher.matches(List.of("ffmpegXXX", "-i", "input.mp4"))).isFalse();
|
||||
// prefix should not match
|
||||
assertThat(matcher.matches(List.of("/usr/bin/ffmpegXXX", "-version"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withEmptyCommand_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.ExecutableName("ffmpeg");
|
||||
|
||||
assertThat(matcher.matches(List.of())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CommandMatcher.ExecutableName(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withBlank_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CommandMatcher.ExecutableName(" "))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class FullPathMatcherTests {
|
||||
|
||||
@Test
|
||||
void matches_withExactPath_shouldReturnTrue() {
|
||||
var matcher = new CommandMatcher.FullPath(Path.of("/usr/local/bin/ffmpeg"));
|
||||
|
||||
assertThat(matcher.matches(List.of("/usr/local/bin/ffmpeg", "-i", "input.mp4"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withDifferentPath_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.FullPath(Path.of("/usr/local/bin/ffmpeg"));
|
||||
|
||||
assertThat(matcher.matches(List.of("/usr/bin/ffmpeg", "-i", "input.mp4"))).isFalse();
|
||||
assertThat(matcher.matches(List.of("ffmpeg", "-i", "input.mp4"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withEmptyCommand_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.FullPath(Path.of("/usr/local/bin/ffmpeg"));
|
||||
|
||||
assertThat(matcher.matches(List.of())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CommandMatcher.FullPath(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class DirectoryMatcherTests {
|
||||
|
||||
@Test
|
||||
void matches_withCommandInDirectory_shouldReturnTrue() {
|
||||
var matcher = new CommandMatcher.Directory(Path.of("/opt/encoders/"));
|
||||
|
||||
assertThat(matcher.matches(List.of("/opt/encoders/x264", "-i", "input.mp4"))).isTrue();
|
||||
assertThat(matcher.matches(List.of("/opt/encoders/x265", "-o", "output.mp4"))).isTrue();
|
||||
assertThat(matcher.matches(List.of("/opt/encoders/av1an", "-i", "input.mp4"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withCommandNotInDirectory_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.Directory(Path.of("/opt/encoders/"));
|
||||
|
||||
assertThat(matcher.matches(List.of("/usr/bin/ffmpeg", "-i", "input.mp4"))).isFalse();
|
||||
assertThat(matcher.matches(List.of("ffmpeg", "-i", "input.mp4"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withSubdirectory_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.Directory(Path.of("/opt/encoders/"));
|
||||
|
||||
// Subdirectories should not match
|
||||
assertThat(matcher.matches(List.of("/opt/encoders/subdir/ffmpeg", "-version"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matches_withEmptyCommand_shouldReturnFalse() {
|
||||
var matcher = new CommandMatcher.Directory(Path.of("/opt/encoders/"));
|
||||
|
||||
assertThat(matcher.matches(List.of())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CommandMatcher.Directory(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -287,6 +287,51 @@ class ConfigLoaderTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class LoadLevelsConfig {
|
||||
|
||||
@Test
|
||||
void load_withLoadLevels_shouldParseValues() throws IOException {
|
||||
Path configFile = tempDir.resolve("barn.conf");
|
||||
Files.writeString(configFile, """
|
||||
[load_levels]
|
||||
max_high_jobs = 4
|
||||
max_medium_jobs = 16
|
||||
max_low_jobs = 64
|
||||
""");
|
||||
|
||||
ConfigLoader loader = new ConfigLoader(Map.of());
|
||||
Config config = loader.loadFromFile(configFile);
|
||||
|
||||
assertThat(config.loadLevels().maxHighJobs()).isEqualTo(4);
|
||||
assertThat(config.loadLevels().maxMediumJobs()).isEqualTo(16);
|
||||
assertThat(config.loadLevels().maxLowJobs()).isEqualTo(64);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_withNoLoadLevels_shouldUseDefaults() {
|
||||
ConfigLoader loader = new ConfigLoader(Map.of());
|
||||
Config config = loader.load();
|
||||
|
||||
assertThat(config.loadLevels().maxHighJobs()).isEqualTo(2);
|
||||
assertThat(config.loadLevels().maxMediumJobs()).isEqualTo(8);
|
||||
assertThat(config.loadLevels().maxLowJobs()).isEqualTo(32);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_withEnvOverride_shouldOverrideLoadLevelValues() {
|
||||
// Environment variables use BARN_LOADLEVELS_ (no underscore) because
|
||||
// the parser splits on first underscore for section name
|
||||
Map<String, String> env = new HashMap<>();
|
||||
env.put("BARN_LOADLEVELS_MAX_HIGH_JOBS", "10");
|
||||
|
||||
ConfigLoader loader = new ConfigLoader(env);
|
||||
Config config = loader.load();
|
||||
|
||||
assertThat(config.loadLevels().maxHighJobs()).isEqualTo(10);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class RetryOnExitCodes {
|
||||
|
||||
|
||||
@@ -222,13 +222,15 @@ class ConfigTest {
|
||||
ServiceConfig.withDefaults(),
|
||||
JobsConfig.withDefaults(),
|
||||
CleanupConfig.withDefaults(),
|
||||
StorageConfig.withDefaults()
|
||||
StorageConfig.withDefaults(),
|
||||
LoadLevelConfig.withDefaults()
|
||||
);
|
||||
|
||||
assertThat(config.service()).isNotNull();
|
||||
assertThat(config.jobs()).isNotNull();
|
||||
assertThat(config.cleanup()).isNotNull();
|
||||
assertThat(config.storage()).isNotNull();
|
||||
assertThat(config.loadLevels()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -237,7 +239,8 @@ class ConfigTest {
|
||||
null,
|
||||
JobsConfig.withDefaults(),
|
||||
CleanupConfig.withDefaults(),
|
||||
StorageConfig.withDefaults()))
|
||||
StorageConfig.withDefaults(),
|
||||
LoadLevelConfig.withDefaults()))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("service");
|
||||
}
|
||||
@@ -248,11 +251,24 @@ class ConfigTest {
|
||||
ServiceConfig.withDefaults(),
|
||||
null,
|
||||
CleanupConfig.withDefaults(),
|
||||
StorageConfig.withDefaults()))
|
||||
StorageConfig.withDefaults(),
|
||||
LoadLevelConfig.withDefaults()))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("jobs");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNullLoadLevels_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Config(
|
||||
ServiceConfig.withDefaults(),
|
||||
JobsConfig.withDefaults(),
|
||||
CleanupConfig.withDefaults(),
|
||||
StorageConfig.withDefaults(),
|
||||
null))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("loadLevels");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withDefaults_shouldReturnFullyPopulatedConfig() {
|
||||
Config config = Config.withDefaults();
|
||||
@@ -261,6 +277,7 @@ class ConfigTest {
|
||||
assertThat(config.jobs()).isNotNull();
|
||||
assertThat(config.cleanup()).isNotNull();
|
||||
assertThat(config.storage()).isNotNull();
|
||||
assertThat(config.loadLevels()).isNotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.samsonmedia.barn.config;
|
||||
|
||||
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.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
/**
|
||||
* Tests for LoadFileParser.
|
||||
*/
|
||||
class LoadFileParserTest {
|
||||
|
||||
@TempDir
|
||||
private Path tempDir;
|
||||
|
||||
@Test
|
||||
void parse_withEmptyFile_shouldReturnEmptyList() throws IOException {
|
||||
Path loadFile = tempDir.resolve("empty.load");
|
||||
Files.writeString(loadFile, "");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withOnlyComments_shouldReturnEmptyList() throws IOException {
|
||||
Path loadFile = tempDir.resolve("comments.load");
|
||||
Files.writeString(loadFile, """
|
||||
# This is a comment
|
||||
# Another comment
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withOnlyBlankLines_shouldReturnEmptyList() throws IOException {
|
||||
Path loadFile = tempDir.resolve("blank.load");
|
||||
Files.writeString(loadFile, """
|
||||
|
||||
\s\s
|
||||
\t
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withExecutableNames_shouldReturnExecutableNameMatchers() throws IOException {
|
||||
Path loadFile = tempDir.resolve("executables.load");
|
||||
Files.writeString(loadFile, """
|
||||
ffmpeg
|
||||
ffprobe
|
||||
handbrake
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(3);
|
||||
assertThat(matchers.get(0)).isInstanceOf(CommandMatcher.ExecutableName.class);
|
||||
assertThat(matchers.get(1)).isInstanceOf(CommandMatcher.ExecutableName.class);
|
||||
assertThat(matchers.get(2)).isInstanceOf(CommandMatcher.ExecutableName.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withFullPaths_shouldReturnFullPathMatchers() throws IOException {
|
||||
Path loadFile = tempDir.resolve("paths.load");
|
||||
Files.writeString(loadFile, """
|
||||
/usr/local/bin/ffmpeg
|
||||
/opt/media/encoders/x264
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(2);
|
||||
assertThat(matchers.get(0)).isInstanceOf(CommandMatcher.FullPath.class);
|
||||
assertThat(matchers.get(1)).isInstanceOf(CommandMatcher.FullPath.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withDirectoryPaths_shouldReturnDirectoryMatchers() throws IOException {
|
||||
Path loadFile = tempDir.resolve("dirs.load");
|
||||
Files.writeString(loadFile, """
|
||||
/opt/encoders/
|
||||
/usr/local/media/
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(2);
|
||||
assertThat(matchers.get(0)).isInstanceOf(CommandMatcher.Directory.class);
|
||||
assertThat(matchers.get(1)).isInstanceOf(CommandMatcher.Directory.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withMixedEntries_shouldReturnCorrectMatchers() throws IOException {
|
||||
Path loadFile = tempDir.resolve("mixed.load");
|
||||
Files.writeString(loadFile, """
|
||||
# High load commands
|
||||
ffmpeg
|
||||
/usr/local/bin/custom-encoder
|
||||
/opt/encoders/
|
||||
|
||||
# More commands
|
||||
handbrake
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(4);
|
||||
assertThat(matchers.get(0)).isInstanceOf(CommandMatcher.ExecutableName.class);
|
||||
assertThat(matchers.get(1)).isInstanceOf(CommandMatcher.FullPath.class);
|
||||
assertThat(matchers.get(2)).isInstanceOf(CommandMatcher.Directory.class);
|
||||
assertThat(matchers.get(3)).isInstanceOf(CommandMatcher.ExecutableName.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withNonExistentFile_shouldThrowIOException() {
|
||||
Path nonExistent = tempDir.resolve("nonexistent.load");
|
||||
|
||||
assertThatThrownBy(() -> LoadFileParser.parse(nonExistent))
|
||||
.isInstanceOf(IOException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withNullPath_shouldThrowException() {
|
||||
assertThatThrownBy(() -> LoadFileParser.parse(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parse_withWhitespaceAroundEntries_shouldTrimEntries() throws IOException {
|
||||
Path loadFile = tempDir.resolve("whitespace.load");
|
||||
Files.writeString(loadFile, """
|
||||
ffmpeg \s
|
||||
\t curl\t
|
||||
""");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parse(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(2);
|
||||
assertThat(matchers.get(0).matches(List.of("ffmpeg", "-version"))).isTrue();
|
||||
assertThat(matchers.get(1).matches(List.of("curl", "--help"))).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseIfExists_withExistingFile_shouldReturnMatchers() throws IOException {
|
||||
Path loadFile = tempDir.resolve("existing.load");
|
||||
Files.writeString(loadFile, "ffmpeg\n");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parseIfExists(loadFile);
|
||||
|
||||
assertThat(matchers).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void parseIfExists_withNonExistentFile_shouldReturnEmptyList() {
|
||||
Path nonExistent = tempDir.resolve("nonexistent.load");
|
||||
|
||||
List<CommandMatcher> matchers = LoadFileParser.parseIfExists(nonExistent);
|
||||
|
||||
assertThat(matchers).isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.samsonmedia.barn.config;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.samsonmedia.barn.jobs.LoadLevel;
|
||||
|
||||
/**
|
||||
* Tests for LoadLevelConfig record.
|
||||
*/
|
||||
class LoadLevelConfigTest {
|
||||
|
||||
@Test
|
||||
void constructor_withValidValues_shouldCreateConfig() {
|
||||
var config = new LoadLevelConfig(4, 16, 64);
|
||||
|
||||
assertThat(config.maxHighJobs()).isEqualTo(4);
|
||||
assertThat(config.maxMediumJobs()).isEqualTo(16);
|
||||
assertThat(config.maxLowJobs()).isEqualTo(64);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withZeroHighJobs_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new LoadLevelConfig(0, 8, 32))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("maxHighJobs must be at least 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNegativeHighJobs_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new LoadLevelConfig(-1, 8, 32))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("maxHighJobs must be at least 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withZeroMediumJobs_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new LoadLevelConfig(2, 0, 32))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("maxMediumJobs must be at least 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withZeroLowJobs_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new LoadLevelConfig(2, 8, 0))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("maxLowJobs must be at least 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void withDefaults_shouldReturnDefaultValues() {
|
||||
var config = LoadLevelConfig.withDefaults();
|
||||
|
||||
assertThat(config.maxHighJobs()).isEqualTo(LoadLevelConfig.DEFAULT_MAX_HIGH_JOBS);
|
||||
assertThat(config.maxMediumJobs()).isEqualTo(LoadLevelConfig.DEFAULT_MAX_MEDIUM_JOBS);
|
||||
assertThat(config.maxLowJobs()).isEqualTo(LoadLevelConfig.DEFAULT_MAX_LOW_JOBS);
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultConstants_shouldMatchLoadLevelDefaults() {
|
||||
assertThat(LoadLevelConfig.DEFAULT_MAX_HIGH_JOBS).isEqualTo(LoadLevel.HIGH.getDefaultMaxJobs());
|
||||
assertThat(LoadLevelConfig.DEFAULT_MAX_MEDIUM_JOBS).isEqualTo(LoadLevel.MEDIUM.getDefaultMaxJobs());
|
||||
assertThat(LoadLevelConfig.DEFAULT_MAX_LOW_JOBS).isEqualTo(LoadLevel.LOW.getDefaultMaxJobs());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMaxJobsFor_shouldReturnCorrectLimit() {
|
||||
var config = new LoadLevelConfig(4, 16, 64);
|
||||
|
||||
assertThat(config.getMaxJobsFor(LoadLevel.HIGH)).isEqualTo(4);
|
||||
assertThat(config.getMaxJobsFor(LoadLevel.MEDIUM)).isEqualTo(16);
|
||||
assertThat(config.getMaxJobsFor(LoadLevel.LOW)).isEqualTo(64);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMaxJobsFor_withNull_shouldThrowException() {
|
||||
var config = LoadLevelConfig.withDefaults();
|
||||
|
||||
assertThatThrownBy(() -> config.getMaxJobsFor(null))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("level must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTotalMaxJobs_shouldReturnSum() {
|
||||
var config = new LoadLevelConfig(4, 16, 64);
|
||||
|
||||
assertThat(config.getTotalMaxJobs()).isEqualTo(84);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -212,7 +213,8 @@ class CrashRecoveryTest {
|
||||
config.service(),
|
||||
new JobsConfig(config.jobs().defaultTimeoutSeconds(), 0, 1, 1.0, List.of()),
|
||||
config.cleanup(),
|
||||
config.storage()
|
||||
config.storage(),
|
||||
config.loadLevels()
|
||||
);
|
||||
CrashRecovery noRetryCrashRecovery = new CrashRecovery(
|
||||
repository, heartbeatChecker, dirs, noRetryConfig);
|
||||
@@ -296,7 +298,8 @@ class CrashRecoveryTest {
|
||||
pid,
|
||||
heartbeat,
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
|
||||
@@ -161,7 +162,8 @@ class HeartbeatCheckerTest {
|
||||
state == JobState.RUNNING ? 12345L : null, // pid
|
||||
heartbeat,
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import com.samsonmedia.barn.config.JobsConfig;
|
||||
import com.samsonmedia.barn.config.LoadLevelConfig;
|
||||
import com.samsonmedia.barn.jobs.Job;
|
||||
import com.samsonmedia.barn.jobs.JobRepository;
|
||||
import com.samsonmedia.barn.state.BarnDirectories;
|
||||
@@ -189,13 +190,15 @@ class JobSchedulerTest {
|
||||
|
||||
@Test
|
||||
void getStatus_whenNotRunning_shouldShowNotRunning() {
|
||||
// Legacy constructor with maxConcurrentJobs=2 creates config with 1 HIGH + 2 MEDIUM + 1 LOW = 4 total
|
||||
scheduler = new JobScheduler(repository, runner, dirs, 2,
|
||||
Duration.ofMillis(100), Duration.ofSeconds(5));
|
||||
|
||||
JobScheduler.SchedulerStatus status = scheduler.getStatus();
|
||||
|
||||
assertThat(status.isRunning()).isFalse();
|
||||
assertThat(status.maxConcurrentJobs()).isEqualTo(2);
|
||||
assertThat(status.maxConcurrentJobs()).isEqualTo(4); // 1 + 2 + 1
|
||||
assertThat(status.maxMediumJobs()).isEqualTo(2); // The configured value
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -279,6 +282,56 @@ class JobSchedulerTest {
|
||||
assertThat(status.maxConcurrentJobs()).isEqualTo(8);
|
||||
assertThat(status.isRunning()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void schedulerStatus_withPerLevelFields_shouldContainAllFields() {
|
||||
JobScheduler.SchedulerStatus status = new JobScheduler.SchedulerStatus(
|
||||
10, 5, 42, true, 2, 5, 3, 4, 8, 30);
|
||||
|
||||
assertThat(status.runningJobs()).isEqualTo(10);
|
||||
assertThat(status.queuedJobs()).isEqualTo(5);
|
||||
assertThat(status.maxConcurrentJobs()).isEqualTo(42);
|
||||
assertThat(status.isRunning()).isTrue();
|
||||
assertThat(status.runningHighJobs()).isEqualTo(2);
|
||||
assertThat(status.runningMediumJobs()).isEqualTo(5);
|
||||
assertThat(status.runningLowJobs()).isEqualTo(3);
|
||||
assertThat(status.maxHighJobs()).isEqualTo(4);
|
||||
assertThat(status.maxMediumJobs()).isEqualTo(8);
|
||||
assertThat(status.maxLowJobs()).isEqualTo(30);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class PerLevelScheduling {
|
||||
|
||||
@Test
|
||||
void constructor_withLoadLevelConfig_shouldUsePerLevelLimits() throws IOException {
|
||||
LoadLevelConfig levelConfig = new LoadLevelConfig(2, 4, 8);
|
||||
scheduler = new JobScheduler(repository, runner, dirs, levelConfig,
|
||||
Duration.ofMillis(100), Duration.ofSeconds(5));
|
||||
|
||||
JobScheduler.SchedulerStatus status = scheduler.getStatus();
|
||||
|
||||
assertThat(status.maxHighJobs()).isEqualTo(2);
|
||||
assertThat(status.maxMediumJobs()).isEqualTo(4);
|
||||
assertThat(status.maxLowJobs()).isEqualTo(8);
|
||||
assertThat(status.maxConcurrentJobs()).isEqualTo(14); // 2 + 4 + 8
|
||||
}
|
||||
|
||||
@Test
|
||||
void getStatus_shouldReportPerLevelRunningCounts() throws IOException {
|
||||
LoadLevelConfig levelConfig = new LoadLevelConfig(2, 4, 8);
|
||||
scheduler = new JobScheduler(repository, runner, dirs, levelConfig,
|
||||
Duration.ofMillis(100), Duration.ofSeconds(5));
|
||||
|
||||
JobScheduler.SchedulerStatus status = scheduler.getStatus();
|
||||
|
||||
// Initially no jobs are running
|
||||
assertThat(status.runningHighJobs()).isZero();
|
||||
assertThat(status.runningMediumJobs()).isZero();
|
||||
assertThat(status.runningLowJobs()).isZero();
|
||||
assertThat(status.runningJobs()).isZero();
|
||||
}
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.jobs;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -23,7 +24,7 @@ class JobManifestTest {
|
||||
List.of("echo", "hello"),
|
||||
"test-tag",
|
||||
now,
|
||||
3, 30, 2.0
|
||||
3, 30, 2.0, MEDIUM
|
||||
);
|
||||
|
||||
assertThat(manifest.id()).isEqualTo("job-12345");
|
||||
@@ -38,7 +39,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withNullId_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
null, List.of("echo"), null, Instant.now(), 3, 30, 2.0))
|
||||
null, List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("id");
|
||||
}
|
||||
@@ -46,7 +47,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withBlankId_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
" ", List.of("echo"), null, Instant.now(), 3, 30, 2.0))
|
||||
" ", List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("blank");
|
||||
}
|
||||
@@ -54,7 +55,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withEmptyCommand_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
"job-12345", List.of(), null, Instant.now(), 3, 30, 2.0))
|
||||
"job-12345", List.of(), null, Instant.now(), 3, 30, 2.0, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("empty");
|
||||
}
|
||||
@@ -62,7 +63,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withNegativeMaxRetries_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), -1, 30, 2.0))
|
||||
"job-12345", List.of("echo"), null, Instant.now(), -1, 30, 2.0, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("maxRetries");
|
||||
}
|
||||
@@ -70,7 +71,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withNegativeRetryDelay_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, -1, 2.0))
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, -1, 2.0, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("retryDelaySeconds");
|
||||
}
|
||||
@@ -78,7 +79,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void constructor_withBackoffMultiplierLessThanOne_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 0.5))
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 0.5, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("retryBackoffMultiplier");
|
||||
}
|
||||
@@ -107,7 +108,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void calculateRetryDelay_shouldUseExponentialBackoff() {
|
||||
JobManifest manifest = new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0);
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM);
|
||||
|
||||
assertThat(manifest.calculateRetryDelay(0)).isEqualTo(30); // 30 * 2^0
|
||||
assertThat(manifest.calculateRetryDelay(1)).isEqualTo(60); // 30 * 2^1
|
||||
@@ -117,7 +118,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void calculateRetryDelay_withNegativeCount_shouldThrowException() {
|
||||
JobManifest manifest = new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0);
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM);
|
||||
|
||||
assertThatThrownBy(() -> manifest.calculateRetryDelay(-1))
|
||||
.isInstanceOf(IllegalArgumentException.class);
|
||||
@@ -126,7 +127,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void canRetry_withRetriesAvailable_shouldReturnTrue() {
|
||||
JobManifest manifest = new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0);
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM);
|
||||
|
||||
assertThat(manifest.canRetry(0)).isTrue();
|
||||
assertThat(manifest.canRetry(1)).isTrue();
|
||||
@@ -136,7 +137,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void canRetry_withNoRetriesLeft_shouldReturnFalse() {
|
||||
JobManifest manifest = new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0);
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 3, 30, 2.0, MEDIUM);
|
||||
|
||||
assertThat(manifest.canRetry(3)).isFalse();
|
||||
assertThat(manifest.canRetry(4)).isFalse();
|
||||
@@ -145,7 +146,7 @@ class JobManifestTest {
|
||||
@Test
|
||||
void canRetry_withZeroMaxRetries_shouldAlwaysReturnFalse() {
|
||||
JobManifest manifest = new JobManifest(
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 0, 30, 2.0);
|
||||
"job-12345", List.of("echo"), null, Instant.now(), 0, 30, 2.0, MEDIUM);
|
||||
|
||||
assertThat(manifest.canRetry(0)).isFalse();
|
||||
}
|
||||
|
||||
@@ -83,6 +83,39 @@ class JobRepositoryTest {
|
||||
|
||||
assertThat(job.tag()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_shouldDefaultToMediumLoadLevel() throws IOException {
|
||||
Job job = repository.create(List.of("echo"), null, config);
|
||||
|
||||
assertThat(job.loadLevel()).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_withLoadLevel_shouldUseSpecifiedLevel() throws IOException {
|
||||
Job job = repository.create(List.of("ffmpeg", "-version"), null, config, LoadLevel.HIGH);
|
||||
|
||||
assertThat(job.loadLevel()).isEqualTo(LoadLevel.HIGH);
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_withLoadLevel_shouldPersistInManifest() throws IOException {
|
||||
Job job = repository.create(List.of("curl", "-h"), null, config, LoadLevel.LOW);
|
||||
|
||||
Path manifestPath = dirs.getJobDir(job.id()).resolve("manifest.json");
|
||||
String content = Files.readString(manifestPath);
|
||||
assertThat(content).contains("\"loadLevel\" : \"LOW\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void create_withLoadLevel_shouldBeReadableByFindById() throws IOException {
|
||||
Job created = repository.create(List.of("wget"), null, config, LoadLevel.LOW);
|
||||
|
||||
Optional<Job> found = repository.findById(created.id());
|
||||
|
||||
assertThat(found).isPresent();
|
||||
assertThat(found.get().loadLevel()).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.jobs;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -23,7 +24,7 @@ class JobTest {
|
||||
List.of("echo", "hello"),
|
||||
"test-tag",
|
||||
Instant.now(),
|
||||
null, null, null, null, null, null, 0, null
|
||||
null, null, null, null, null, null, 0, null, MEDIUM
|
||||
);
|
||||
|
||||
assertThat(job.id()).isEqualTo("job-12345");
|
||||
@@ -36,7 +37,7 @@ class JobTest {
|
||||
void constructor_withNullId_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Job(
|
||||
null, JobState.QUEUED, List.of("echo"), null,
|
||||
Instant.now(), null, null, null, null, null, null, 0, null))
|
||||
Instant.now(), null, null, null, null, null, null, 0, null, MEDIUM))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("id");
|
||||
}
|
||||
@@ -45,7 +46,7 @@ class JobTest {
|
||||
void constructor_withBlankId_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Job(
|
||||
" ", JobState.QUEUED, List.of("echo"), null,
|
||||
Instant.now(), null, null, null, null, null, null, 0, null))
|
||||
Instant.now(), null, null, null, null, null, null, 0, null, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("blank");
|
||||
}
|
||||
@@ -54,7 +55,7 @@ class JobTest {
|
||||
void constructor_withNullState_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Job(
|
||||
"job-12345", null, List.of("echo"), null,
|
||||
Instant.now(), null, null, null, null, null, null, 0, null))
|
||||
Instant.now(), null, null, null, null, null, null, 0, null, MEDIUM))
|
||||
.isInstanceOf(NullPointerException.class)
|
||||
.hasMessageContaining("state");
|
||||
}
|
||||
@@ -63,7 +64,7 @@ class JobTest {
|
||||
void constructor_withEmptyCommand_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Job(
|
||||
"job-12345", JobState.QUEUED, List.of(), null,
|
||||
Instant.now(), null, null, null, null, null, null, 0, null))
|
||||
Instant.now(), null, null, null, null, null, null, 0, null, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("empty");
|
||||
}
|
||||
@@ -72,7 +73,7 @@ class JobTest {
|
||||
void constructor_withNegativeRetryCount_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new Job(
|
||||
"job-12345", JobState.QUEUED, List.of("echo"), null,
|
||||
Instant.now(), null, null, null, null, null, null, -1, null))
|
||||
Instant.now(), null, null, null, null, null, null, -1, null, MEDIUM))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("retryCount");
|
||||
}
|
||||
@@ -231,4 +232,29 @@ class JobTest {
|
||||
assertThat(queued.isQueued()).isTrue();
|
||||
assertThat(running.isQueued()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void createQueued_shouldDefaultToMediumLoadLevel() {
|
||||
Job job = Job.createQueued("job-12345", List.of("echo", "test"), "my-tag");
|
||||
|
||||
assertThat(job.loadLevel()).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createQueued_withLoadLevel_shouldUseSpecifiedLevel() {
|
||||
Job job = Job.createQueued("job-12345", List.of("ffmpeg", "-i", "input.mp4"),
|
||||
"transcode", LoadLevel.HIGH);
|
||||
|
||||
assertThat(job.loadLevel()).isEqualTo(LoadLevel.HIGH);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withLoadLevel_shouldCreateCopyWithNewLoadLevel() {
|
||||
Job original = Job.createQueued("job-12345", List.of("echo"), null);
|
||||
|
||||
Job updated = original.withLoadLevel(LoadLevel.LOW);
|
||||
|
||||
assertThat(updated.loadLevel()).isEqualTo(LoadLevel.LOW);
|
||||
assertThat(original.loadLevel()).isEqualTo(LoadLevel.MEDIUM); // Original unchanged
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.samsonmedia.barn.jobs;
|
||||
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* Tests for LoadLevelClassifier.
|
||||
*/
|
||||
class LoadLevelClassifierTest {
|
||||
|
||||
@TempDir
|
||||
private Path tempDir;
|
||||
|
||||
@Nested
|
||||
class ClassificationTests {
|
||||
|
||||
private LoadLevelClassifier classifier;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
// Create high.load with ffmpeg, x264
|
||||
Files.writeString(tempDir.resolve("high.load"), """
|
||||
# High load commands
|
||||
ffmpeg
|
||||
x264
|
||||
/usr/local/bin/handbrake
|
||||
""");
|
||||
|
||||
// Create empty medium.load
|
||||
Files.writeString(tempDir.resolve("medium.load"), """
|
||||
# Medium load - empty by default
|
||||
""");
|
||||
|
||||
// Create low.load with curl, wget
|
||||
Files.writeString(tempDir.resolve("low.load"), """
|
||||
# Low load commands
|
||||
curl
|
||||
wget
|
||||
rclone
|
||||
""");
|
||||
|
||||
classifier = LoadLevelClassifier.load(tempDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_withHighLoadCommand_shouldReturnHigh() {
|
||||
assertThat(classifier.classify(List.of("ffmpeg", "-i", "input.mp4"))).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(classifier.classify(List.of("/usr/bin/ffmpeg", "-version"))).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(classifier.classify(List.of("x264", "--preset", "slow"))).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(classifier.classify(List.of("/usr/local/bin/handbrake", "-i", "input"))).isEqualTo(LoadLevel.HIGH);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_withLowLoadCommand_shouldReturnLow() {
|
||||
assertThat(classifier.classify(List.of("curl", "http://example.com"))).isEqualTo(LoadLevel.LOW);
|
||||
assertThat(classifier.classify(List.of("/usr/bin/wget", "-O", "file"))).isEqualTo(LoadLevel.LOW);
|
||||
assertThat(classifier.classify(List.of("rclone", "sync", "source:", "dest:"))).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_withUnknownCommand_shouldReturnMedium() {
|
||||
assertThat(classifier.classify(List.of("echo", "hello"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(classifier.classify(List.of("/bin/bash", "-c", "ls"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(classifier.classify(List.of("python3", "script.py"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_withEmptyCommand_shouldReturnMedium() {
|
||||
assertThat(classifier.classify(List.of())).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> classifier.classify(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class LoadingTests {
|
||||
|
||||
@Test
|
||||
void load_withAllFilesPresent_shouldLoadAllMatchers() throws IOException {
|
||||
Files.writeString(tempDir.resolve("high.load"), "ffmpeg\n");
|
||||
Files.writeString(tempDir.resolve("medium.load"), "custom-tool\n");
|
||||
Files.writeString(tempDir.resolve("low.load"), "curl\n");
|
||||
|
||||
LoadLevelClassifier classifier = LoadLevelClassifier.load(tempDir);
|
||||
|
||||
assertThat(classifier.classify(List.of("ffmpeg"))).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(classifier.classify(List.of("custom-tool"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(classifier.classify(List.of("curl"))).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_withMissingFiles_shouldTreatAsEmpty() throws IOException {
|
||||
// No load files exist
|
||||
|
||||
LoadLevelClassifier classifier = LoadLevelClassifier.load(tempDir);
|
||||
|
||||
// Everything should default to MEDIUM
|
||||
assertThat(classifier.classify(List.of("ffmpeg"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(classifier.classify(List.of("curl"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void load_withNullPath_shouldThrowException() {
|
||||
assertThatThrownBy(() -> LoadLevelClassifier.load(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class PriorityTests {
|
||||
|
||||
@Test
|
||||
void classify_whenInMultipleLists_shouldPrioritizeHighOverLow() throws IOException {
|
||||
// Put ffmpeg in both high and low
|
||||
Files.writeString(tempDir.resolve("high.load"), "ffmpeg\n");
|
||||
Files.writeString(tempDir.resolve("low.load"), "ffmpeg\n");
|
||||
|
||||
LoadLevelClassifier classifier = LoadLevelClassifier.load(tempDir);
|
||||
|
||||
// HIGH should win
|
||||
assertThat(classifier.classify(List.of("ffmpeg"))).isEqualTo(LoadLevel.HIGH);
|
||||
}
|
||||
|
||||
@Test
|
||||
void classify_whenInMediumAndLow_shouldPrioritizeMediumOverLow() throws IOException {
|
||||
Files.writeString(tempDir.resolve("medium.load"), "curl\n");
|
||||
Files.writeString(tempDir.resolve("low.load"), "curl\n");
|
||||
|
||||
LoadLevelClassifier classifier = LoadLevelClassifier.load(tempDir);
|
||||
|
||||
// MEDIUM should win over LOW
|
||||
assertThat(classifier.classify(List.of("curl"))).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.samsonmedia.barn.jobs;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests for LoadLevel enum.
|
||||
*/
|
||||
class LoadLevelTest {
|
||||
|
||||
@Test
|
||||
void values_shouldContainAllLoadLevels() {
|
||||
assertThat(LoadLevel.values()).containsExactly(
|
||||
LoadLevel.HIGH,
|
||||
LoadLevel.MEDIUM,
|
||||
LoadLevel.LOW
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withValidLowercase_shouldReturnLoadLevel() {
|
||||
assertThat(LoadLevel.fromString("high")).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(LoadLevel.fromString("medium")).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(LoadLevel.fromString("low")).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withValidUppercase_shouldReturnLoadLevel() {
|
||||
assertThat(LoadLevel.fromString("HIGH")).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(LoadLevel.fromString("MEDIUM")).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(LoadLevel.fromString("LOW")).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withMixedCase_shouldReturnLoadLevel() {
|
||||
assertThat(LoadLevel.fromString("High")).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(LoadLevel.fromString("Medium")).isEqualTo(LoadLevel.MEDIUM);
|
||||
assertThat(LoadLevel.fromString("Low")).isEqualTo(LoadLevel.LOW);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withWhitespace_shouldTrimAndParse() {
|
||||
assertThat(LoadLevel.fromString(" high ")).isEqualTo(LoadLevel.HIGH);
|
||||
assertThat(LoadLevel.fromString(" medium ")).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> LoadLevel.fromString(null))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("cannot be null or blank");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withBlank_shouldThrowException() {
|
||||
assertThatThrownBy(() -> LoadLevel.fromString(" "))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("cannot be null or blank");
|
||||
}
|
||||
|
||||
@Test
|
||||
void fromString_withInvalidValue_shouldThrowException() {
|
||||
assertThatThrownBy(() -> LoadLevel.fromString("invalid"))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("Invalid load level")
|
||||
.hasMessageContaining("invalid")
|
||||
.hasMessageContaining("high, medium, low");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toLowercase_shouldReturnLowercaseString() {
|
||||
assertThat(LoadLevel.HIGH.toLowercase()).isEqualTo("high");
|
||||
assertThat(LoadLevel.MEDIUM.toLowercase()).isEqualTo("medium");
|
||||
assertThat(LoadLevel.LOW.toLowercase()).isEqualTo("low");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDefaultMaxJobs_shouldReturnCorrectDefaults() {
|
||||
assertThat(LoadLevel.HIGH.getDefaultMaxJobs()).isEqualTo(2);
|
||||
assertThat(LoadLevel.MEDIUM.getDefaultMaxJobs()).isEqualTo(8);
|
||||
assertThat(LoadLevel.LOW.getDefaultMaxJobs()).isEqualTo(32);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getDefault_shouldReturnMedium() {
|
||||
assertThat(LoadLevel.getDefault()).isEqualTo(LoadLevel.MEDIUM);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.jobs;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -260,7 +261,8 @@ class RetryCalculatorTest {
|
||||
12345L,
|
||||
Instant.now(),
|
||||
retryCount,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
|
||||
@@ -278,7 +280,8 @@ class RetryCalculatorTest {
|
||||
12345L,
|
||||
Instant.now(),
|
||||
retryCount,
|
||||
null
|
||||
null,
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.logging;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -183,7 +184,8 @@ class JobContextTest {
|
||||
null, // pid
|
||||
null, // heartbeat
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.samsonmedia.barn.logging;
|
||||
|
||||
import static com.samsonmedia.barn.jobs.LoadLevel.MEDIUM;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@@ -200,7 +201,8 @@ class JobLogEventsTest {
|
||||
null, // pid
|
||||
null, // heartbeat
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
|
||||
@@ -218,7 +220,8 @@ class JobLogEventsTest {
|
||||
pid,
|
||||
Instant.now(), // heartbeat
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
|
||||
@@ -236,7 +239,8 @@ class JobLogEventsTest {
|
||||
null, // pid
|
||||
null, // heartbeat
|
||||
0, // retryCount
|
||||
null // retryAt
|
||||
null, // retryAt
|
||||
MEDIUM
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import com.samsonmedia.barn.config.CleanupConfig;
|
||||
import com.samsonmedia.barn.config.Config;
|
||||
import com.samsonmedia.barn.config.JobsConfig;
|
||||
import com.samsonmedia.barn.config.LoadLevelConfig;
|
||||
import com.samsonmedia.barn.config.LogLevel;
|
||||
import com.samsonmedia.barn.config.ServiceConfig;
|
||||
import com.samsonmedia.barn.config.StorageConfig;
|
||||
@@ -45,7 +46,8 @@ class BarnServiceTest {
|
||||
CleanupConfig cleanupConfig = new CleanupConfig(false, 60, 24, false, 168);
|
||||
StorageConfig storageConfig = new StorageConfig(tempDir, 50, false);
|
||||
|
||||
config = new Config(serviceConfig, jobsConfig, cleanupConfig, storageConfig);
|
||||
config = new Config(serviceConfig, jobsConfig, cleanupConfig, storageConfig,
|
||||
LoadLevelConfig.withDefaults());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user