From c43cf67b40a2646ff7c5443ef7d4e3562e06ac08 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 27 Sep 2024 05:28:18 -0700 Subject: [PATCH] Add an end-to-end test for disk cache garbage collection. PiperOrigin-RevId: 679550494 Change-Id: Ie397d3e498423ce1dd121e786c8e8e129bcb87f2 --- src/test/shell/bazel/disk_cache_test.sh | 39 +++++++++++++++++++ .../integration/validation_actions_test.sh | 16 -------- src/test/shell/unittest.bash | 26 +++++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/test/shell/bazel/disk_cache_test.sh b/src/test/shell/bazel/disk_cache_test.sh index cb709e8db36509..5433f042ac943a 100755 --- a/src/test/shell/bazel/disk_cache_test.sh +++ b/src/test/shell/bazel/disk_cache_test.sh @@ -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" diff --git a/src/test/shell/integration/validation_actions_test.sh b/src/test/shell/integration/validation_actions_test.sh index 1f68bbabfaf246..77f788e23a0107 100755 --- a/src/test/shell/integration/validation_actions_test.sh +++ b/src/test/shell/integration/validation_actions_test.sh @@ -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() { diff --git a/src/test/shell/unittest.bash b/src/test/shell/unittest.bash index 46743cdfce70f9..41192a71d2b9d7 100644 --- a/src/test/shell/unittest.bash +++ b/src/test/shell/unittest.bash @@ -519,6 +519,32 @@ function assert_contains_n() { return 1 } +# Usage: assert_exists [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 [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 [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