In tests we need to be able to populate the cache with a deterministic
set of entries. This means we need to drop the contents of the cache
between runs to prevent spillage between test cases.
virFileCacheClear drops all entries from the hash table used for the
cache.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virfilecache.c | 16 ++++++++++++++++
src/util/virfilecache.h | 4 ++++
3 files changed, 21 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f1bed27ba7..1159889bd6 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2352,6 +2352,7 @@ virFindFileInPath;
# util/virfilecache.h
+virFileCacheClear;
virFileCacheGetPriv;
virFileCacheInsertData;
virFileCacheLookup;
diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c
index bad37c9f00..b9e06545c0 100644
--- a/src/util/virfilecache.c
+++ b/src/util/virfilecache.c
@@ -407,3 +407,19 @@ virFileCacheInsertData(virFileCache *cache,
return ret;
}
+
+
+/**
+ * virFileCacheClear:
+ * @cache: existing cache object
+ *
+ * Drops all entries from the cache. This is useful in tests to clean out
+ * previous usage.
+ */
+void
+virFileCacheClear(virFileCache *cache)
+{
+ virObjectLock(cache);
+ virHashRemoveAll(cache->table);
+ virObjectUnlock(cache);
+}
diff --git a/src/util/virfilecache.h b/src/util/virfilecache.h
index 81be8feef5..c3bc0f529c 100644
--- a/src/util/virfilecache.h
+++ b/src/util/virfilecache.h
@@ -135,3 +135,7 @@ int
virFileCacheInsertData(virFileCache *cache,
const char *name,
void *data);
+
+/* for testing usage */
+void
+virFileCacheClear(virFileCache *cache);
--
2.39.2