From 13ade9e724003b2438917a64167607a95ab5f426 Mon Sep 17 00:00:00 2001 From: Patrick LeBlanc Date: Fri, 8 Mar 2019 10:35:36 -0600 Subject: [PATCH] Added more Cache tests, fixed a couple bugs. --- src/Cache.cpp | 8 ++++++-- src/Cache.h | 2 +- src/Downloader.cpp | 3 ++- src/unit_tests.cpp | 30 +++++++++++++++++++++++++----- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index c53f39924..122f4d087 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -189,7 +189,7 @@ void Cache::exists(const vector &keys, vector *out) out->resize(keys.size()); boost::unique_lock s(lru_mutex); for (int i = 0; i < keys.size(); i++) - (*out)[i] = (m_lru.find(keys[i]) == m_lru.end()); + (*out)[i] = (m_lru.find(keys[i]) != m_lru.end()); } void Cache::newObject(const string &key, size_t size) @@ -248,7 +248,7 @@ void Cache::makeSpace(size_t size) } /* - TODO: tell Synchronizer that this key will be evicted + tell Synchronizer that this key will be evicted delete the file remove it from our structs update current size @@ -264,6 +264,10 @@ void Cache::makeSpace(size_t size) } } +size_t Cache::getCurrentCacheSize() const +{ + return currentCacheSize; +} /* The helper classes */ diff --git a/src/Cache.h b/src/Cache.h index 12e751b75..44efea1d2 100644 --- a/src/Cache.h +++ b/src/Cache.h @@ -28,7 +28,7 @@ class Cache : public boost::noncopyable void newObject(const std::string &key, size_t size); void deletedObject(const std::string &key, size_t size); void setMaxCacheSize(size_t size); - size_t getCurrentCacheSize(); + size_t getCurrentCacheSize() const; // test helpers const boost::filesystem::path &getCachePath(); diff --git a/src/Downloader.cpp b/src/Downloader.cpp index ba28c2f7c..32158fa46 100644 --- a/src/Downloader.cpp +++ b/src/Downloader.cpp @@ -105,6 +105,7 @@ int Downloader::download(const vector &keys, vector *errnos if (dl->dl_errno != 0) { char buf[80]; + // not sure why yet, but valgrind complains about the call below logger->log(LOG_ERR, "Downloader: failed to download %s, got '%s'", keys[i]->c_str(), strerror_r(dl->dl_errno, buf, 80)); ret = -1; } @@ -122,7 +123,7 @@ inline const string & Downloader::getDownloadPath() const } /* The helper fcns */ -Downloader::Download::Download(const string *source, Downloader *dl) : key(source), dler(dl), dl_errno(0), size(0) +Downloader::Download::Download(const string *source, Downloader *dl) : dler(dl), key(source), dl_errno(0), size(0) { } diff --git a/src/unit_tests.cpp b/src/unit_tests.cpp index f0e42a8b2..0f8735eb5 100644 --- a/src/unit_tests.cpp +++ b/src/unit_tests.cpp @@ -482,21 +482,41 @@ bool cacheTest1() bf::path storagePath = ls->getPrefix(); bf::path cachePath = cache.getCachePath(); vector v_bogus; + vector exists; // make sure nothing shows up in the cache path for files that don't exist v_bogus.push_back("does-not-exist"); cache.read(v_bogus); assert(!bf::exists(cachePath / "does-not-exist")); + cache.exists(v_bogus, &exists); + assert(exists.size() == 1); + assert(!exists[0]); // make sure a file that does exist does show up in the cache path - bf::copy_file("storagemanager.cnf", storagePath / "storagemanager.cnf", bf::copy_option::overwrite_if_exists); - v_bogus[0] = "storagemanager.cnf"; + string realFile("storagemanager.cnf"); + bf::copy_file(realFile, storagePath / realFile, bf::copy_option::overwrite_if_exists); + v_bogus[0] = realFile; cache.read(v_bogus); - assert(bf::exists(cachePath / "storagemanager.cnf")); + assert(bf::exists(cachePath / realFile)); + exists.clear(); + cache.exists(v_bogus, &exists); + assert(exists.size() == 1); + assert(exists[0]); + ssize_t currentSize = cache.getCurrentCacheSize(); + assert(currentSize == bf::file_size(cachePath / realFile)); + + // lie about the file being deleted and then replaced + cache.deletedObject(realFile, currentSize); + assert(cache.getCurrentCacheSize() == 0); + cache.newObject(realFile, currentSize); + assert(cache.getCurrentCacheSize() == currentSize); + cache.exists(v_bogus, &exists); + assert(exists.size() == 1); + assert(exists[0]); // cleanup - bf::remove(cachePath / "storagemanager.cnf"); - bf::remove(storagePath / "storagemanager.cnf"); + bf::remove(cachePath / realFile); + bf::remove(storagePath / realFile); cout << "cache test 1 OK" << endl; }