1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <string>
#include "eckit/container/CacheManager.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/log/Log.h"
#include "eckit/testing/Test.h"
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
namespace caching {
struct CacheTraits {
using value_type = std::string;
using Locker = eckit::CacheManagerFileFlock;
static const char* name() { return "test_cachemanager"; }
static const char* extension() { return ".ext"; }
static int version() { return 42; }
static void save(const eckit::CacheManagerBase&, const value_type&, const eckit::PathName& path) {
Log::info() << "CacheTraits::save(path='" << path << "')" << std::endl;
ASSERT(!path.exists());
path.touch();
ASSERT(path.exists());
}
static void load(const eckit::CacheManagerBase&, value_type& value, const eckit::PathName& path) {
Log::info() << "CacheTraits::load(path='" << path << "')" << std::endl;
ASSERT(path.exists());
value = "cached";
}
};
struct Manager : eckit::CacheManager<CacheTraits> {
Manager() : eckit::CacheManager<CacheTraits>("loader", ".", /*throwOnCacheMiss*/ false, /*maxCacheSize*/ 0) {}
private:
friend CacheTraits;
};
struct CacheCreator : Manager::CacheContentCreator {
CacheCreator() = default;
CacheCreator(const CacheCreator&) = delete;
CacheCreator& operator=(const CacheCreator&) = delete;
private:
void create(const eckit::PathName& path, CacheTraits::value_type& value, bool& saved) final {
Log::info() << "CacheCreator::create(path='" << path << "')" << std::endl;
path.touch();
saved = true;
value = "cached";
}
};
struct ManagerCantMiss : eckit::CacheManager<CacheTraits> {
ManagerCantMiss() :
eckit::CacheManager<CacheTraits>("loader", ".", /*throwOnCacheMiss*/ true, /*maxCacheSize*/ 0) {}
private:
friend CacheTraits;
};
struct CacheCreatorCantMiss : ManagerCantMiss::CacheContentCreator {
CacheCreatorCantMiss() = default;
CacheCreatorCantMiss(const CacheCreatorCantMiss&) = delete;
CacheCreatorCantMiss& operator=(const CacheCreatorCantMiss&) = delete;
private:
void create(const eckit::PathName&, CacheTraits::value_type&, bool&) final { NOTIMP; }
};
} // namespace caching
namespace {
void deldir(eckit::PathName& p) {
if (!p.exists()) {
return;
}
std::vector<eckit::PathName> files;
std::vector<eckit::PathName> dirs;
p.children(files, dirs);
for (auto& f : files) {
f.unlink();
}
for (auto& d : dirs) {
deldir(d);
}
p.rmdir();
}
} // namespace
//----------------------------------------------------------------------------------------------------------------------
CASE("test_cachemanager") {
PathName dir = caching::CacheTraits::name();
deldir(dir);
EXPECT(!dir.exists());
using value_t = caching::CacheTraits::value_type;
value_t reference = "cached";
auto cached_path = [&dir](caching::Manager::key_t& key) {
return dir / std::to_string(caching::CacheTraits::version()) / key + caching::CacheTraits::extension();
};
caching::Manager::key_t key = "key";
auto path = cached_path(key);
Log::info() << "path='" << path << "'" << std::endl;
static caching::Manager cache;
caching::CacheCreator creator;
EXPECT(!path.exists());
value_t saved;
cache.getOrCreate(key, creator, saved);
EXPECT(saved == reference);
EXPECT(path.exists());
value_t loaded;
cache.getOrCreate(key, creator, loaded);
EXPECT(loaded == reference);
caching::ManagerCantMiss::key_t missingKey = "missingKey";
auto missingPath = cached_path(missingKey);
Log::info() << "missingPath='" << missingPath << "'" << std::endl;
value_t another;
static caching::ManagerCantMiss strictCache;
caching::CacheCreatorCantMiss strictCreator;
EXPECT(!missingPath.exists());
EXPECT_THROWS(strictCache.getOrCreate(missingKey, strictCreator, another));
EXPECT(!missingPath.exists());
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
return eckit::testing::run_tests(argc, argv);
}
|