aboutsummaryrefslogtreecommitdiff
path: root/src/libstore
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-01-30 22:15:23 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-02-07 23:34:36 +0100
commit2ceece3ef384385d886f6aed5311d9b6dbbdd6dd (patch)
treeae598195b919806ce65a35ca885fdeb4354a1f51 /src/libstore
parent79f62d2dda8603c1f2f471ce20557548db932296 (diff)
NarInfoDiskCache: Prepare reproducer for #3898
Diffstat (limited to 'src/libstore')
-rw-r--r--src/libstore/nar-info-disk-cache.cc6
-rw-r--r--src/libstore/nar-info-disk-cache.hh3
-rw-r--r--src/libstore/tests/nar-info-disk-cache.cc87
3 files changed, 96 insertions, 0 deletions
diff --git a/src/libstore/nar-info-disk-cache.cc b/src/libstore/nar-info-disk-cache.cc
index c460100de..494318af9 100644
--- a/src/libstore/nar-info-disk-cache.cc
+++ b/src/libstore/nar-info-disk-cache.cc
@@ -207,6 +207,7 @@ public:
if (!cache)
return std::nullopt;
return CacheInfo {
+ .id = cache->id,
.wantMassQuery = cache->wantMassQuery,
.priority = cache->priority
};
@@ -370,4 +371,9 @@ ref<NarInfoDiskCache> getNarInfoDiskCache()
return cache;
}
+ref<NarInfoDiskCache> getTestNarInfoDiskCache(Path dbPath)
+{
+ return make_ref<NarInfoDiskCacheImpl>(dbPath);
+}
+
}
diff --git a/src/libstore/nar-info-disk-cache.hh b/src/libstore/nar-info-disk-cache.hh
index c185ca5e4..adc14f3bc 100644
--- a/src/libstore/nar-info-disk-cache.hh
+++ b/src/libstore/nar-info-disk-cache.hh
@@ -18,6 +18,7 @@ public:
struct CacheInfo
{
+ int id;
bool wantMassQuery;
int priority;
};
@@ -45,4 +46,6 @@ public:
multiple threads. */
ref<NarInfoDiskCache> getNarInfoDiskCache();
+ref<NarInfoDiskCache> getTestNarInfoDiskCache(Path dbPath);
+
}
diff --git a/src/libstore/tests/nar-info-disk-cache.cc b/src/libstore/tests/nar-info-disk-cache.cc
new file mode 100644
index 000000000..1e9cbaa78
--- /dev/null
+++ b/src/libstore/tests/nar-info-disk-cache.cc
@@ -0,0 +1,87 @@
+#include "nar-info-disk-cache.hh"
+
+#include <gtest/gtest.h>
+#include <rapidcheck/gtest.h>
+#include "sqlite.hh"
+#include <sqlite3.h>
+
+
+namespace nix {
+
+RC_GTEST_PROP(
+ NarInfoDiskCacheImpl,
+ create_and_read,
+ (int prio, bool wantMassQuery)
+ )
+{
+ Path tmpDir = createTempDir();
+ AutoDelete delTmpDir(tmpDir);
+ Path dbPath(tmpDir + "/test-narinfo-disk-cache.sqlite");
+ auto cache = getTestNarInfoDiskCache(dbPath);
+
+ cache->createCache("other://uri", "/nix/storedir", wantMassQuery, prio);
+ cache->createCache("other://uri-2", "/nix/storedir", wantMassQuery, prio);
+
+ cache->createCache("the://uri", "/nix/storedir", wantMassQuery, prio);
+
+ {
+ auto r = cache->upToDateCacheExists("the://uri");
+ ASSERT_TRUE(r);
+ ASSERT_EQ(r->priority, prio);
+ ASSERT_EQ(r->wantMassQuery, wantMassQuery);
+ }
+
+ SQLite db(dbPath);
+ SQLiteStmt getIds;
+ getIds.create(db, "SELECT id FROM BinaryCaches WHERE url = 'the://uri'");
+
+ int savedId;
+ {
+ auto q(getIds.use());
+ ASSERT_TRUE(q.next());
+ savedId = q.getInt(0);
+ ASSERT_FALSE(q.next());
+ }
+
+
+ db.exec("UPDATE BinaryCaches SET timestamp = timestamp - 1 - 7 * 24 * 3600;");
+
+ // Relies on memory cache
+ {
+ auto r = cache->upToDateCacheExists("the://uri");
+ ASSERT_TRUE(r);
+ ASSERT_EQ(r->priority, prio);
+ ASSERT_EQ(r->wantMassQuery, wantMassQuery);
+ }
+
+ auto cache2 = getTestNarInfoDiskCache(dbPath);
+
+ {
+ auto r = cache2->upToDateCacheExists("the://uri");
+ ASSERT_FALSE(r);
+ }
+
+ cache2->createCache("the://uri", "/nix/storedir", wantMassQuery, prio);
+
+ {
+ auto r = cache->upToDateCacheExists("the://uri");
+ ASSERT_TRUE(r);
+ ASSERT_EQ(r->priority, prio);
+ ASSERT_EQ(r->wantMassQuery, wantMassQuery);
+ // FIXME, reproduces #3898
+ // ASSERT_EQ(r->id, savedId);
+ (void) savedId;
+ }
+
+ {
+ auto q(getIds.use());
+ ASSERT_TRUE(q.next());
+ auto currentId = q.getInt(0);
+ ASSERT_FALSE(q.next());
+ // FIXME, reproduces #3898
+ // ASSERT_EQ(currentId, savedId);
+ (void) currentId;
+ }
+}
+
+}