util/cache: run basic cache tests on the single file cache
authorTimothy Arceri <tarceri@itsqueeze.com>
Tue, 7 Sep 2021 05:00:58 +0000 (15:00 +1000)
committerMarge Bot <eric+marge@anholt.net>
Fri, 17 Sep 2021 01:25:46 +0000 (01:25 +0000)
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12745>

src/util/tests/cache/cache_test.c

index ec1587d..58cafc2 100644 (file)
@@ -192,7 +192,7 @@ cache_exists(struct disk_cache *cache)
 #define CACHE_TEST_TMP "./cache-test-tmp"
 
 static void
-test_disk_cache_create(void)
+test_disk_cache_create(const char *cache_dir_name)
 {
    struct disk_cache *cache;
    int err;
@@ -255,8 +255,10 @@ test_disk_cache_create(void)
    expect_true(cache_exists(cache), "disk_cache_create with XDG_CACHE_HOME "
                "set");
 
-   check_directories_created(CACHE_TEST_TMP "/xdg-cache-home/"
-                             CACHE_DIR_NAME);
+   char *path;
+   asprintf(&path, "%s%s", CACHE_TEST_TMP "/xdg-cache-home/", cache_dir_name);
+   check_directories_created(path);
+   free(path);
 
    disk_cache_destroy(cache);
 
@@ -281,14 +283,16 @@ test_disk_cache_create(void)
    expect_true(cache_exists(cache), "disk_cache_create with "
                "MESA_GLSL_CACHE_DIR set");
 
-   check_directories_created(CACHE_TEST_TMP "/mesa-glsl-cache-dir/"
-                             CACHE_DIR_NAME);
+   asprintf(&path, "%s%s", CACHE_TEST_TMP "/mesa-glsl-cache-dir/",
+            cache_dir_name);
+   check_directories_created(path);
+   free(path);
 
    disk_cache_destroy(cache);
 }
 
 static void
-test_put_and_get(void)
+test_put_and_get(bool test_cache_size_limit)
 {
    struct disk_cache *cache;
    char blob[] = "This is a blob of thirty-seven bytes";
@@ -342,6 +346,9 @@ test_put_and_get(void)
    /* Set the cache size to 1KB and add a 1KB item to force an eviction. */
    disk_cache_destroy(cache);
 
+   if (!test_cache_size_limit)
+      return;
+
    setenv("MESA_GLSL_CACHE_MAX_SIZE", "1K", 1);
    cache = disk_cache_create("test", "make_check", 0);
 
@@ -519,20 +526,60 @@ test_put_key_and_get_key(void)
 }
 #endif /* ENABLE_SHADER_CACHE */
 
-int
-main(void)
+static void
+test_multi_file_cache(void)
+{
+   int err;
+
+   printf("Test multi file disk cache - Start\n");
+
+   test_disk_cache_create(CACHE_DIR_NAME);
+
+   test_put_and_get(true);
+
+   test_put_key_and_get_key();
+
+   printf("Test multi file disk cache - End\n");
+
+   err = rmrf_local(CACHE_TEST_TMP);
+   expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
+}
+
+static void
+test_single_file_cache(void)
 {
-#ifdef ENABLE_SHADER_CACHE
    int err;
 
-   test_disk_cache_create();
+   printf("Test single file disk cache - Start\n");
 
-   test_put_and_get();
+   setenv("MESA_DISK_CACHE_SINGLE_FILE", "true", 1);
+
+   test_disk_cache_create(CACHE_DIR_NAME_SF);
+
+   /* We skip testing cache size limit as the single file cache currently
+    * doesn't have any functionality to enforce cache size limits.
+    */
+   test_put_and_get(false);
 
    test_put_key_and_get_key();
 
+   setenv("MESA_DISK_CACHE_SINGLE_FILE", "false", 1);
+
+   printf("Test single file disk cache - End\n");
+
    err = rmrf_local(CACHE_TEST_TMP);
    expect_equal(err, 0, "Removing " CACHE_TEST_TMP " again");
+}
+
+int
+main(void)
+{
+#ifdef ENABLE_SHADER_CACHE
+
+   test_multi_file_cache();
+
+   test_single_file_cache();
+
 #endif /* ENABLE_SHADER_CACHE */
 
    return error ? 1 : 0;