Skip to content

Commit

Permalink
Add an end-to-end test for disk cache garbage collection.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 679550494
Change-Id: Ie397d3e498423ce1dd121e786c8e8e129bcb87f2
  • Loading branch information
tjgq authored and copybara-github committed Sep 27, 2024
1 parent 56882d5 commit c43cf67
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
39 changes: 39 additions & 0 deletions src/test/shell/bazel/disk_cache_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,43 @@ EOF
expect_log "(cached) PASSED"
}

function test_garbage_collection() {
local -r CACHE_DIR="${TEST_TMPDIR}/cache"
rm -rf "$CACHE_DIR"

mkdir -p a
touch a/BUILD

# Populate the disk cache with some fake entries totalling 4 MB in size.
create_file_with_size_and_mtime "${CACHE_DIR}/cas/123" 1M "202401010100"
create_file_with_size_and_mtime "${CACHE_DIR}/ac/456" 1M "202401010200"
create_file_with_size_and_mtime "${CACHE_DIR}/cas/abc" 1M "202401010300"
create_file_with_size_and_mtime "${CACHE_DIR}/ac/def" 1M "202401010400"

# Run a build and request an immediate garbage collection.
# Note that this build doesn't write anything to the disk cache.
bazel build --disk_cache="$CACHE_DIR" \
--experimental_disk_cache_gc_max_size=2M \
--experimental_disk_cache_gc_idle_delay=0 \
//a:BUILD >& $TEST_log || fail "Expected build to succeed"

# Give the idle task a bit of time to run.
sleep 1

# Expect the two oldest entries to have been deleted to reduce size to 2 MB.
assert_not_exists "${CACHE_DIR}/cas/123"
assert_not_exists "${CACHE_DIR}/ac/456"
assert_exists "${CACHE_DIR}/cas/abc"
assert_exists "${CACHE_DIR}/ac/def"
}

function create_file_with_size_and_mtime() {
local -r path=$1
local -r size=$2
local -r mtime=$3
mkdir -p "$(dirname "$path")"
dd if=/dev/zero of="$path" bs="$size" count=1
touch -t "$mtime" "$path"
}

run_suite "disk cache test"
16 changes: 0 additions & 16 deletions src/test/shell/integration/validation_actions_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,6 @@ EOF
chmod +x validation_actions/validation_tool
}

function assert_exists() {
path="$1"
[ -f "$path" ] && return 0

fail "Expected file '$path' to exist, but it did not"
return 1
}

function assert_not_exists() {
path="$1"
[ ! -f "$path" ] && return 0

fail "Expected file '$path' to not exist, but it did"
return 1
}

#### Tests #####################################################################

function test_validation_actions() {
Expand Down
26 changes: 26 additions & 0 deletions src/test/shell/unittest.bash
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,32 @@ function assert_contains_n() {
return 1
}

# Usage: assert_exists <file> [error-message]
# Asserts that the file exists.
function assert_exists() {
local file=$1
local message=${2:-"Expected '$file' to exist"}
if [[ -f "$file" ]]; then
return 0
fi

fail "$message"
return 1
}

# Usage: assert_not_exists <file> [error-message]
# Asserts that the file does not exist.
function assert_not_exists() {
local file=$1
local message=${2:-"Expected '$file' to not exist"}
if ! [[ -f "$file" ]]; then
return 0
fi

fail "$message"
return 1
}

# Usage: assert_empty_file <file> [error-message]
# Asserts that the file exists and is empty. On failure copies the file to
# undeclared outputs, prints the specified (optional) error message, and returns
Expand Down

0 comments on commit c43cf67

Please sign in to comment.