Skip to content

Commit c265b2d

Browse files
Covered cache eviction in ConcurrentCacheXRayTest.simpleFlow
1 parent 61b91f3 commit c265b2d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/lrucache.h

+3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,11 @@ class lru_cache {
222222
return;
223223
}
224224
_current_cost += extra_cost;
225+
log_debug("_current_cost after increase: " << _current_cost);
225226
while (_current_cost > _max_cost && size() > 1) {
226227
dropLast();
227228
}
229+
log_debug("settled _current_cost: " << _current_cost);
228230
}
229231

230232
void decreaseCost(size_t costToRemove) {
@@ -236,6 +238,7 @@ class lru_cache {
236238
} else {
237239
_current_cost -= costToRemove;
238240
}
241+
log_debug("_current_cost after decrease: " << _current_cost);
239242
}
240243

241244
private: // functions

test/concurrentcache.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ TEST(ConcurrentCacheTest, handleException) {
4343

4444
TEST(ConcurrentCacheXRayTest, simpleFlow) {
4545
zim::Logging::logIntoMemory();
46-
zim::ConcurrentCache<int, int, zim::UnitCostEstimation> cache(10);
46+
zim::ConcurrentCache<int, int, zim::UnitCostEstimation> cache(1);
4747
EXPECT_EQ(cache.getOrPut(3, LazyValue(2025)), 2025);
4848
EXPECT_EQ(cache.getOrPut(3, LazyValue(123)), 2025);
4949
EXPECT_THROW(cache.getOrPut(2, ExceptionSource()), std::runtime_error);
50+
EXPECT_EQ(cache.getOrPut(2, LazyValue(123)), 123);
5051

5152
ASSERT_EQ(zim::Logging::getInMemLogContent(),
5253
R"(thread#0: ConcurrentCache::getOrPut(3) {
@@ -60,6 +61,8 @@ thread#0: It was a cache miss. Going to obtain the value...
6061
thread#0: Value was successfully obtained. Computing its cost...
6162
thread#0: cost=1. Committing to cache...
6263
thread#0: lru_cache::increaseCost(1) {
64+
thread#0: _current_cost after increase: 1
65+
thread#0: settled _current_cost: 1
6366
thread#0: }
6467
thread#0: Done. Cache cost is at 1
6568
thread#0: } (return value: 2025)
@@ -81,9 +84,32 @@ thread#0: Evaluation failed. Releasing the cache slot...
8184
thread#0: ConcurrentCache::drop(2) {
8285
thread#0: lru_cache::drop(2) {
8386
thread#0: lru_cache::decreaseCost(0) {
87+
thread#0: _current_cost after decrease: 1
8488
thread#0: }
8589
thread#0: }
8690
thread#0: }
8791
thread#0: }
92+
thread#0: ConcurrentCache::getOrPut(2) {
93+
thread#0: lru_cache::getOrPut(2) {
94+
thread#0: not in cache, adding...
95+
thread#0: lru_cache::increaseCost(0) {
96+
thread#0: }
97+
thread#0: }
98+
thread#0: Obtained the cache slot
99+
thread#0: It was a cache miss. Going to obtain the value...
100+
thread#0: Value was successfully obtained. Computing its cost...
101+
thread#0: cost=1. Committing to cache...
102+
thread#0: lru_cache::increaseCost(1) {
103+
thread#0: _current_cost after increase: 2
104+
thread#0: lru_cache::dropLast() {
105+
thread#0: evicting entry with key: 3
106+
thread#0: lru_cache::decreaseCost(1) {
107+
thread#0: _current_cost after decrease: 1
108+
thread#0: }
109+
thread#0: }
110+
thread#0: settled _current_cost: 1
111+
thread#0: }
112+
thread#0: Done. Cache cost is at 1
113+
thread#0: } (return value: 123)
88114
)");
89115
}

0 commit comments

Comments
 (0)