2023-01-30 22:15:23 +01:00
|
|
|
#include "nar-info-disk-cache.hh"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <rapidcheck/gtest.h>
|
|
|
|
#include "sqlite.hh"
|
|
|
|
#include <sqlite3.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-01-17 19:56:06 +01:00
|
|
|
TEST(NarInfoDiskCacheImpl, create_and_read) {
|
|
|
|
int prio = 12345;
|
|
|
|
bool wantMassQuery = true;
|
|
|
|
|
2023-01-30 22:15:23 +01:00
|
|
|
Path tmpDir = createTempDir();
|
|
|
|
AutoDelete delTmpDir(tmpDir);
|
|
|
|
Path dbPath(tmpDir + "/test-narinfo-disk-cache.sqlite");
|
|
|
|
auto cache = getTestNarInfoDiskCache(dbPath);
|
|
|
|
|
2023-01-17 19:56:06 +01:00
|
|
|
{
|
|
|
|
auto bc1 = cache->createCache("https://bar", "/nix/storedir", wantMassQuery, prio);
|
|
|
|
auto bc2 = cache->createCache("https://xyz", "/nix/storedir", false, 12);
|
|
|
|
ASSERT_NE(bc1, bc2);
|
|
|
|
}
|
2023-01-30 22:15:23 +01:00
|
|
|
|
2023-01-17 19:56:06 +01:00
|
|
|
int savedId = cache->createCache("http://foo", "/nix/storedir", wantMassQuery, prio);;
|
2023-01-30 22:15:23 +01:00
|
|
|
{
|
2023-01-17 19:56:06 +01:00
|
|
|
auto r = cache->upToDateCacheExists("http://foo");
|
2023-01-30 22:15:23 +01:00
|
|
|
ASSERT_TRUE(r);
|
|
|
|
ASSERT_EQ(r->priority, prio);
|
|
|
|
ASSERT_EQ(r->wantMassQuery, wantMassQuery);
|
2023-01-17 19:56:06 +01:00
|
|
|
ASSERT_EQ(savedId, r->id);
|
2023-01-30 22:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SQLite db(dbPath);
|
|
|
|
SQLiteStmt getIds;
|
2023-01-17 19:56:06 +01:00
|
|
|
getIds.create(db, "SELECT id FROM BinaryCaches WHERE url = 'http://foo'");
|
2023-01-30 22:15:23 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
auto q(getIds.use());
|
|
|
|
ASSERT_TRUE(q.next());
|
2023-01-17 19:56:06 +01:00
|
|
|
ASSERT_EQ(savedId, q.getInt(0));
|
2023-01-30 22:15:23 +01:00
|
|
|
ASSERT_FALSE(q.next());
|
|
|
|
}
|
|
|
|
|
|
|
|
db.exec("UPDATE BinaryCaches SET timestamp = timestamp - 1 - 7 * 24 * 3600;");
|
|
|
|
|
|
|
|
// Relies on memory cache
|
|
|
|
{
|
2023-01-17 19:56:06 +01:00
|
|
|
auto r = cache->upToDateCacheExists("http://foo");
|
2023-01-30 22:15:23 +01:00
|
|
|
ASSERT_TRUE(r);
|
|
|
|
ASSERT_EQ(r->priority, prio);
|
|
|
|
ASSERT_EQ(r->wantMassQuery, wantMassQuery);
|
|
|
|
}
|
|
|
|
|
2023-01-17 19:56:06 +01:00
|
|
|
// We can't clear the in-memory cache, so we use a new cache object.
|
2023-01-30 22:15:23 +01:00
|
|
|
auto cache2 = getTestNarInfoDiskCache(dbPath);
|
|
|
|
|
|
|
|
{
|
2023-01-17 19:56:06 +01:00
|
|
|
auto r = cache2->upToDateCacheExists("http://foo");
|
2023-01-30 22:15:23 +01:00
|
|
|
ASSERT_FALSE(r);
|
|
|
|
}
|
|
|
|
|
2023-01-17 19:56:06 +01:00
|
|
|
// Update, same data, check that the id number is reused
|
|
|
|
cache2->createCache("http://foo", "/nix/storedir", wantMassQuery, prio);
|
2023-01-30 22:15:23 +01:00
|
|
|
|
|
|
|
{
|
2023-01-17 19:56:06 +01:00
|
|
|
auto r = cache2->upToDateCacheExists("http://foo");
|
2023-01-30 22:15:23 +01:00
|
|
|
ASSERT_TRUE(r);
|
|
|
|
ASSERT_EQ(r->priority, prio);
|
|
|
|
ASSERT_EQ(r->wantMassQuery, wantMassQuery);
|
2023-01-17 19:56:06 +01:00
|
|
|
ASSERT_EQ(r->id, savedId);
|
2023-01-30 22:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto q(getIds.use());
|
|
|
|
ASSERT_TRUE(q.next());
|
|
|
|
auto currentId = q.getInt(0);
|
|
|
|
ASSERT_FALSE(q.next());
|
2023-01-17 19:56:06 +01:00
|
|
|
ASSERT_EQ(currentId, savedId);
|
2023-01-30 22:15:23 +01:00
|
|
|
}
|
2023-01-17 19:56:06 +01:00
|
|
|
|
|
|
|
// Check that the fields can be modified
|
|
|
|
{
|
|
|
|
auto r0 = cache2->upToDateCacheExists("https://bar");
|
|
|
|
ASSERT_FALSE(r0);
|
|
|
|
|
|
|
|
cache2->createCache("https://bar", "/nix/storedir", !wantMassQuery, prio + 10);
|
|
|
|
auto r = cache2->upToDateCacheExists("https://bar");
|
|
|
|
ASSERT_EQ(r->wantMassQuery, !wantMassQuery);
|
|
|
|
ASSERT_EQ(r->priority, prio + 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// // Force update (no use case yet; we only retrieve cache metadata when stale based on timestamp)
|
|
|
|
// {
|
|
|
|
// cache2->createCache("https://bar", "/nix/storedir", wantMassQuery, prio + 20);
|
|
|
|
// auto r = cache2->upToDateCacheExists("https://bar");
|
|
|
|
// ASSERT_EQ(r->wantMassQuery, wantMassQuery);
|
|
|
|
// ASSERT_EQ(r->priority, prio + 20);
|
|
|
|
// }
|
|
|
|
|
2023-01-30 22:15:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|