|
| 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 | +} |
0 commit comments