#!/usr/bin/env bash set -euo pipefail cd "$(dirname "$0")/.." test_name="dependency-cache-test-$$" site_directory="${UCE_TEST_SITE_DIRECTORY:-site}" source_dir="$site_directory/$test_name" bin_directory="${BIN_DIRECTORY:-}" if [[ -z "$bin_directory" && -r /etc/uce/settings.cfg ]]; then bin_directory=$(awk -F= '/^[[:space:]]*BIN_DIRECTORY[[:space:]]*=/ {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2; exit}' /etc/uce/settings.cfg) fi bin_directory="${bin_directory:-/tmp/uce/work}" cache_dir="" mutation_file="/tmp/uce-dependency-mutation-$$" post_body="/tmp/uce-dependency-post-body-$$" post_headers="/tmp/uce-dependency-post-headers-$$" lock_ready_file="/tmp/uce-dependency-lock-ready-$$" http_host="${UCE_TEST_HTTP_HOST:-uce.openfu.com}" cleanup() { rm -rf "$source_dir" if [[ -n "$cache_dir" ]]; then rm -rf "$cache_dir" fi rm -f "$mutation_file" "$post_body" "$post_headers" "$lock_ready_file" } trap cleanup EXIT mkdir -p "$source_dir" cache_dir="$bin_directory$(realpath "$source_dir")" printf '%s\n' \ '#ifndef UCE_DEPENDENCY_CACHE_CHILD' \ '#define UCE_DEPENDENCY_CACHE_CHILD' \ 'String dependency_cache_marker() { return("dependency-marker-a"); }' \ '#endif' >"$source_dir/child.uce" printf '%s\n' \ '#load "child.uce"' \ 'CLI(Request& context) { print(dependency_cache_marker(), ":", request_perf()["worker_pid"].to_string()); }' \ "RENDER(Request& context) { if(context.params[\"REQUEST_METHOD\"] == \"POST\") file_put_contents(\"$mutation_file\", dependency_cache_marker()); print(dependency_cache_marker(), \":\", request_perf()[\"worker_pid\"].to_string()); }" >"$source_dir/parent.uce" assert_marker() { local path="$1" local expected="$2" local output output=$(scripts/uce-cli "/$test_name/$path.uce") if [[ "$output" != *"$expected"* ]]; then echo "$path returned stale module; expected $expected: $output" >&2 exit 1 fi } http_marker() { curl -fsS -H "Host: $http_host" "http://127.0.0.1/$test_name/parent.uce" } assert_marker parent dependency-marker-a if [[ "$(http_marker)" != *"dependency-marker-a"* ]]; then echo "HTTP warm-up did not return dependency-marker-a" >&2 exit 1 fi printf '%s\n' 'CLI(Request& context) { print("readable-source-marker"); }' >"$source_dir/unreadable.uce" chmod 000 "$source_dir/unreadable.uce" if unreadable_output=$(scripts/uce-cli "/$test_name/unreadable.uce" 2>&1); then echo "unreadable source unexpectedly compiled: $unreadable_output" >&2 exit 1 fi if [[ "$unreadable_output" != *"source file is not readable"* ]]; then echo "unreadable source did not report its actual compile error: $unreadable_output" >&2 exit 1 fi if [[ -e "$cache_dir/unreadable.uce.wasm" ]]; then echo "unreadable source published a wasm artifact" >&2 exit 1 fi chmod 644 "$source_dir/unreadable.uce" assert_marker unreadable readable-source-marker sed -i 's/dependency-marker-a/dependency-marker-b/' "$source_dir/child.uce" started_at=$(date +%s%N) http_during_rebuild=$(http_marker) elapsed_ms=$(( ($(date +%s%N) - started_at) / 1000000 )) if [[ "$http_during_rebuild" != *"dependency-marker-a"* && "$http_during_rebuild" != *"dependency-marker-b"* ]]; then echo "HTTP rebuild request returned neither complete artifact: $http_during_rebuild" >&2 exit 1 fi if (( elapsed_ms >= 2000 )); then echo "HTTP request spent ${elapsed_ms}ms rebuilding a stale artifact" >&2 exit 1 fi deadline=$((SECONDS + 15)) while [[ "$http_during_rebuild" != *"dependency-marker-b"* && $SECONDS -lt $deadline ]]; do sleep 0.2 http_during_rebuild=$(http_marker) done if [[ "$http_during_rebuild" != *"dependency-marker-b"* ]]; then echo "requested stale HTTP unit did not receive a demand-priority rebuild" >&2 exit 1 fi assert_marker parent dependency-marker-b # A proactive rebuild owns these same per-unit locks. While it publishes fresh # artifacts, requests must use the last complete artifacts instead of waiting # across the transitive graph. Atomic publication keeps those artifacts safe. parent_wasm="$cache_dir/parent.uce.wasm" child_wasm="$cache_dir/child.uce.wasm" ( exec 8>"$parent_wasm.lock" exec 9>"$child_wasm.lock" flock 8 flock 9 : >"$lock_ready_file" sleep 3 ) & rebuild_lock_pid=$! deadline=$((SECONDS + 5)) while [[ ! -e "$lock_ready_file" && $SECONDS -lt $deadline ]]; do if ! kill -0 "$rebuild_lock_pid" 2>/dev/null; then echo "test rebuild lock process exited before acquiring locks" >&2 exit 1 fi sleep 0.05 done if [[ ! -e "$lock_ready_file" ]]; then echo "test rebuild lock process did not acquire locks before deadline" >&2 exit 1 fi sed -i 's/dependency-marker-b/dependency-marker-d/' "$source_dir/child.uce" started_at=$(date +%s%N) http_during_lock=$(http_marker) elapsed_ms=$(( ($(date +%s%N) - started_at) / 1000000 )) if [[ "$http_during_lock" != *"dependency-marker-b"* ]]; then echo "HTTP request did not serve the last complete artifact during rebuild: $http_during_lock" >&2 exit 1 fi if (( elapsed_ms >= 2000 )); then echo "HTTP request waited ${elapsed_ms}ms for an active transitive rebuild" >&2 exit 1 fi rm -f "$mutation_file" started_at=$(date +%s%N) post_status=$(curl -sS -o "$post_body" -D "$post_headers" -w '%{http_code}' -X POST -H "Host: $http_host" "http://127.0.0.1/$test_name/parent.uce") elapsed_ms=$(( ($(date +%s%N) - started_at) / 1000000 )) if [[ "$post_status" != "503" ]]; then echo "stale POST executed an application artifact instead of returning 503: status=$post_status body=$(cat "$post_body")" >&2 exit 1 fi if ! grep -qi '^Retry-After: 1' "$post_headers"; then echo "stale POST did not return a Retry-After header" >&2 exit 1 fi if [[ -e "$mutation_file" ]]; then echo "stale POST executed the old mutation handler: $(cat "$mutation_file")" >&2 exit 1 fi if (( elapsed_ms >= 2000 )); then echo "stale POST waited ${elapsed_ms}ms instead of failing closed promptly" >&2 exit 1 fi started_at=$(date +%s%N) assert_marker parent dependency-marker-d elapsed_ms=$(( ($(date +%s%N) - started_at) / 1000000 )) if (( elapsed_ms < 2000 )); then echo "CLI request returned before the locked rebuild published the current artifact (${elapsed_ms}ms)" >&2 exit 1 fi wait "$rebuild_lock_pid" # Warm every configured worker, then replace the artifact while retaining its # whole-second mtime. The worker cache must notice the nanosecond/ctime change. for _ in {1..16}; do assert_marker parent dependency-marker-d; done sed 's/dependency-marker-d/dependency-marker-c/' "$source_dir/child.uce" >"$source_dir/alternate-child.uce" sed 's/child.uce/alternate-child.uce/' "$source_dir/parent.uce" >"$source_dir/alternate.uce" assert_marker alternate dependency-marker-c alternate_wasm="$cache_dir/alternate.uce.wasm" parent_mtime=$(stat -c %Y "$parent_wasm") cp "$alternate_wasm" "$parent_wasm" touch -d "@$parent_mtime" "$parent_wasm" rm -f "$cache_dir/parent.uce.cwasm" for _ in {1..16}; do assert_marker parent dependency-marker-c; done worker_count=$(awk -F= '/^[[:space:]]*WORKER_COUNT[[:space:]]*=/ {gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2; exit}' /etc/uce/settings.cfg 2>/dev/null || true) worker_count="${worker_count:-4}" worker_pids="" for _ in {1..48}; do output=$(scripts/uce-cli "/$test_name/parent.uce") worker_pids+="${output##*:}"$'\n' done unique_workers=$(printf '%s' "$worker_pids" | sed '/^$/d' | sort -u | wc -l) if (( unique_workers > worker_count )); then echo "worker pool recycled during 48 requests: $unique_workers PIDs for $worker_count workers" >&2 exit 1 fi echo "dependency invalidation passed"