diff options
| -rwxr-xr-x | tests/cli.sh | 59 | ||||
| -rwxr-xr-x | tests/helpers.sh | 61 |
2 files changed, 120 insertions, 0 deletions
diff --git a/tests/cli.sh b/tests/cli.sh index 5b95573..77d7cce 100755 --- a/tests/cli.sh +++ b/tests/cli.sh @@ -1016,6 +1016,62 @@ test_generate_image_jobs_waits_for_any_finished_imagemagick() { test::teardown } +test_generate_image_jobs_limits_parallel_identify() { + local active_file + local config_file + local fake_bin + local identify_count + local lock_file + local log_file + local max_file + local max_seen + + test::setup + fake_bin="$TEST_TMPDIR/bin" + config_file="$TEST_TMPDIR/photoalbum.conf" + lock_file="$TEST_TMPDIR/identify.lock" + active_file="$TEST_TMPDIR/identify.active" + max_file="$TEST_TMPDIR/identify.max" + log_file="$TEST_TMPDIR/identify.log" + + test::install_parallel_identify_spy "$fake_bin" + mkdir -p "$TEST_TMPDIR/incoming" + printf 'fake image\n' > "$TEST_TMPDIR/incoming/01.jpg" + printf 'fake image\n' > "$TEST_TMPDIR/incoming/02.jpg" + printf 'fake image\n' > "$TEST_TMPDIR/incoming/03.jpg" + printf 'fake image\n' > "$TEST_TMPDIR/incoming/04.jpg" + test::write_album_config \ + "$config_file" "$TEST_TMPDIR/incoming" "$TEST_TMPDIR/dist" \ + 'Parallel identify album' 40 + + ( + cd "$TEST_TMPDIR" + PATH="$fake_bin:$PATH" \ + TEST_PARALLEL_IDENTIFY_LOCK="$lock_file" \ + TEST_PARALLEL_IDENTIFY_ACTIVE="$active_file" \ + TEST_PARALLEL_IDENTIFY_MAX="$max_file" \ + TEST_PARALLEL_IDENTIFY_LOG="$log_file" \ + "$TEST_PHOTOALBUM" --image-jobs 2 --generate + ) + + max_seen=$(<"$max_file") + if (( max_seen > 2 )); then + echo 'FAIL: expected at most 2 parallel ImageMagick identify jobs' >&2 + echo "max_seen=$max_seen" >&2 + cat "$log_file" >&2 + exit 1 + fi + + identify_count=$(grep -c '^start ' "$log_file") + if (( identify_count != 4 )); then + echo 'FAIL: expected one identify call per source image' >&2 + echo "identify_count=$identify_count" >&2 + cat "$log_file" >&2 + exit 1 + fi + test::teardown +} + test_repeated_output_flags_use_last_value() { local config_file local fake_bin @@ -3502,6 +3558,9 @@ main() { '--generate --image-jobs waits for any finished ImageMagick job' \ test_generate_image_jobs_waits_for_any_finished_imagemagick test::run_case \ + '--generate --image-jobs limits ImageMagick identify parallelism' \ + test_generate_image_jobs_limits_parallel_identify + test::run_case \ 'repeated output flags use last value' \ test_repeated_output_flags_use_last_value test::run_case \ diff --git a/tests/helpers.sh b/tests/helpers.sh index 71f96d9..bb52b67 100755 --- a/tests/helpers.sh +++ b/tests/helpers.sh @@ -324,6 +324,67 @@ MAGICK cp "$bin_dir/magick" "$bin_dir/convert" } +test::install_parallel_identify_spy() { + local -r bin_dir="$1"; shift + + mkdir -p "$bin_dir" + + cat > "$bin_dir/magick" <<'MAGICK' +#!/usr/bin/env bash +set -euo pipefail + +if [ "${1:-}" = identify ]; then + lock_file="${TEST_PARALLEL_IDENTIFY_LOCK:?}" + active_file="${TEST_PARALLEL_IDENTIFY_ACTIVE:?}" + max_file="${TEST_PARALLEL_IDENTIFY_MAX:?}" + log_file="${TEST_PARALLEL_IDENTIFY_LOG:?}" + + ( + flock 9 + active=0 + if [ -f "$active_file" ]; then + active=$(<"$active_file") + fi + active=$(( active + 1 )) + printf '%s\n' "$active" > "$active_file" + + max=0 + if [ -f "$max_file" ]; then + max=$(<"$max_file") + fi + if (( active > max )); then + printf '%s\n' "$active" > "$max_file" + fi + + printf 'start %s %s\n' "$active" "$*" >> "$log_file" + ) 9>"$lock_file" + + sleep 0.1 + printf ' exif:Make: ParallelCam\n' + + ( + flock 9 + active=$(<"$active_file") + active=$(( active - 1 )) + printf '%s\n' "$active" > "$active_file" + printf 'finish %s %s\n' "$active" "$*" >> "$log_file" + ) 9>"$lock_file" + exit 0 +fi + +dest="${@: -1}" +mkdir -p "$(dirname "$dest")" +{ + printf 'fake image\n' + printf 'args:' + printf ' %q' "$@" + printf '\n' +} > "$dest" +MAGICK + chmod 0755 "$bin_dir/magick" + cp "$bin_dir/magick" "$bin_dir/convert" +} + test::install_failing_imagemagick() { local -r bin_dir="$1"; shift |
