Skip to content

Commit 49fe36d

Browse files
Merge pull request #974 from openzim/separate_concurrent_cache_unit_test
A separate unit-test for ConcurrentCache
2 parents 0787eb0 + 72010c4 commit 49fe36d

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

test/concurrentcache.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2025 Veloman Yunkan
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License as
6+
* published by the Free Software Foundation; either version 2 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
11+
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
12+
* NON-INFRINGEMENT. See the GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
*/
19+
20+
#include "concurrent_cache.h"
21+
#include "gtest/gtest.h"
22+
23+
struct LazyValue
24+
{
25+
const int value;
26+
explicit LazyValue(int v) : value(v) {}
27+
int operator()() const { return value; }
28+
};
29+
30+
struct ExceptionSource
31+
{
32+
int operator()() const { throw std::runtime_error("oops"); return 0; }
33+
};
34+
35+
TEST(ConcurrentCacheTest, handleException) {
36+
zim::ConcurrentCache<int, int> cache(1);
37+
EXPECT_EQ(cache.getOrPut(7, LazyValue(777)), 777);
38+
EXPECT_THROW(cache.getOrPut(8, ExceptionSource()), std::runtime_error);
39+
EXPECT_EQ(cache.getOrPut(8, LazyValue(888)), 888);
40+
}

test/lrucache.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131

3232
#include "lrucache.h"
33-
#include "concurrent_cache.h"
3433
#include "gtest/gtest.h"
3534

3635
const int NUM_OF_TEST2_RECORDS = 100;
@@ -131,12 +130,3 @@ TEST(CacheTest1, ChangeCacheCapacity) {
131130
EXPECT_RANGE_MISSING_FROM_CACHE(cache_lru, 0, (NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY))
132131
EXPECT_RANGE_FULLY_IN_CACHE(cache_lru, (NUM_OF_TEST2_RECORDS - TEST2_CACHE_CAPACITY), NUM_OF_TEST2_RECORDS, 1000)
133132
}
134-
135-
TEST(ConcurrentCacheTest, handleException) {
136-
zim::ConcurrentCache<int, int> cache(1);
137-
auto val = cache.getOrPut(7, []() { return 777; });
138-
EXPECT_EQ(val, 777);
139-
EXPECT_THROW(cache.getOrPut(8, []() { throw std::runtime_error("oups"); return 0; }), std::runtime_error);
140-
val = cache.getOrPut(8, []() { return 888; });
141-
EXPECT_EQ(val, 888);
142-
}

test/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
tests = [
22
'lrucache',
3+
'concurrentcache',
34
'cluster',
45
'creator',
56
'dirent',

0 commit comments

Comments
 (0)