Skip to content

Commit

Permalink
Ensure cluster cache is properly clean in case of exception in constr…
Browse files Browse the repository at this point in the history
…uctor.
  • Loading branch information
mgautierfr committed Feb 14, 2025
1 parent 93f6d40 commit 75e78c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/fileimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Grouping
{}
#endif

FileImpl::FileImpl(std::shared_ptr<FileCompound> _zimFile)
FileImpl::FileImpl(std::shared_ptr<FileCompound> _zimFile) try
: zimFile(_zimFile),
zimReader(makeFileReader(zimFile)),
direntReader(new DirentReader(zimReader)),
Expand Down Expand Up @@ -265,6 +265,9 @@ class Grouping
m_byTitleDirentLookup.reset(new ByTitleDirentLookup(mp_titleDirentAccessor.get()));

readMimeTypes();
} catch (...) {
getClusterCache().drop_all([=](const std::tuple<FileImpl*, cluster_index_type>& key) {return std::get<0>(key) == this;});
throw;
}

FileImpl::~FileImpl() {
Expand Down
6 changes: 5 additions & 1 deletion test/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class ZimArchive: public testing::Test {
zim::set_cluster_cache_max_size(CLUSTER_CACHE_SIZE);
ASSERT_EQ(zim::get_cluster_cache_current_size(), 0);
}
void TearDown() override {
ASSERT_EQ(zim::get_cluster_cache_current_size(), 0);
}
};

using TestContextImpl = std::vector<std::pair<std::string, std::string> >;
Expand Down Expand Up @@ -669,7 +672,8 @@ class CapturedStderr
#define EXPECT_BROKEN_ZIMFILE(ZIMPATH, EXPECTED_STDERROR_TEXT) \
CapturedStderr stderror; \
EXPECT_FALSE(zim::validate(ZIMPATH, checksToRun)); \
EXPECT_EQ(EXPECTED_STDERROR_TEXT, std::string(stderror)) << ZIMPATH;
EXPECT_EQ(EXPECTED_STDERROR_TEXT, std::string(stderror)) << ZIMPATH; \
ASSERT_EQ(zim::get_cluster_cache_current_size(), 0);

#define TEST_BROKEN_ZIM_NAME(ZIMNAME, EXPECTED) \
for(auto& testfile: getDataFilePath(ZIMNAME)) {EXPECT_BROKEN_ZIMFILE(testfile.path, EXPECTED)}
Expand Down

0 comments on commit 75e78c6

Please sign in to comment.