From 15e89b78de4c8e11d27c9ce0726cec44e2c5cc5e Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 4 Aug 2021 07:36:32 +0900 Subject: [PATCH] Add a partial page_list unit test (#1735) --- .gitignore | 1 + src/Makefile.am | 9 +++++ src/common.h | 2 + src/curl.h | 6 +++ src/fdcache_page.h | 5 ++- src/fdcache_untreated.h | 3 ++ src/test_page_list.cpp | 82 +++++++++++++++++++++++++++++++++++++++++ src/types.h | 1 + 8 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/test_page_list.cpp diff --git a/.gitignore b/.gitignore index a4fb681..5c4a86e 100644 --- a/.gitignore +++ b/.gitignore @@ -74,6 +74,7 @@ missing default_commit_hash src/s3fs src/test_curl_util +src/test_page_list src/test_string_util test/chaos-http-proxy-* test/s3proxy-* diff --git a/src/Makefile.am b/src/Makefile.am index 985b5b7..78af0a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -68,6 +68,7 @@ s3fs_LDADD = $(DEPS_LIBS) noinst_PROGRAMS = \ test_curl_util \ + test_page_list \ test_string_util test_curl_util_SOURCES = common_auth.cpp curl_util.cpp string_util.cpp test_curl_util.cpp s3fs_global.cpp s3fs_logger.cpp @@ -83,10 +84,18 @@ endif test_curl_util_LDADD = $(DEPS_LIBS) +test_page_list_SOURCES = \ + fdcache_page.cpp \ + s3fs_global.cpp \ + s3fs_logger.cpp \ + string_util.cpp \ + test_page_list.cpp + test_string_util_SOURCES = string_util.cpp test_string_util.cpp s3fs_logger.cpp TESTS = \ test_curl_util \ + test_page_list \ test_string_util clang-tidy: diff --git a/src/common.h b/src/common.h index 73bef38..8536987 100644 --- a/src/common.h +++ b/src/common.h @@ -21,6 +21,8 @@ #ifndef S3FS_COMMON_H_ #define S3FS_COMMON_H_ +#include + #include "../config.h" #include "types.h" #include "s3fs_logger.h" diff --git a/src/curl.h b/src/curl.h index ee8a3a9..c8936ed 100644 --- a/src/curl.h +++ b/src/curl.h @@ -21,8 +21,14 @@ #ifndef S3FS_CURL_H_ #define S3FS_CURL_H_ +#include #include +#include +#include +#include +#include +#include "common.h" #include "curl_handlerpool.h" #include "bodydata.h" #include "psemaphore.h" diff --git a/src/fdcache_page.h b/src/fdcache_page.h index f3a8b75..e99f4b8 100644 --- a/src/fdcache_page.h +++ b/src/fdcache_page.h @@ -21,6 +21,9 @@ #ifndef S3FS_FDCACHE_PAGE_H_ #define S3FS_FDCACHE_PAGE_H_ +#include +#include + #include "fdcache_stat.h" //------------------------------------------------ @@ -89,7 +92,6 @@ class PageList static bool CheckAreaInSparseFile(const struct fdpage& checkpage, const fdpage_list_t& sparse_list, int fd, fdpage_list_t& err_area_list, fdpage_list_t& warn_area_list); void Clear(); - bool Compress(); bool Parse(off_t new_pos); public: @@ -115,6 +117,7 @@ class PageList bool IsModified() const; bool ClearAllModified(); + bool Compress(); bool Serialize(CacheFileStat& file, bool is_output, ino_t inode); void Dump() const; bool CompareSparseFile(int fd, size_t file_size, fdpage_list_t& err_area_list, fdpage_list_t& warn_area_list); diff --git a/src/fdcache_untreated.h b/src/fdcache_untreated.h index 349541e..89738c3 100644 --- a/src/fdcache_untreated.h +++ b/src/fdcache_untreated.h @@ -21,6 +21,9 @@ #ifndef S3FS_FDCACHE_UNTREATED_H_ #define S3FS_FDCACHE_UNTREATED_H_ +#include "common.h" +#include "types.h" + //------------------------------------------------ // Class UntreatedParts //------------------------------------------------ diff --git a/src/test_page_list.cpp b/src/test_page_list.cpp new file mode 100644 index 0000000..3c449cd --- /dev/null +++ b/src/test_page_list.cpp @@ -0,0 +1,82 @@ +/* + * s3fs - FUSE-based file system backed by Amazon S3 + * + * Copyright(C) 2021 Andrew Gaul + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include +#include +#include + +#include "fdcache.h" +#include "test_util.h" + +bool CacheFileStat::Open() { return false; } + +void test_compress() +{ + PageList list; + ASSERT_EQUALS(off_t(0), list.Size()); + + list.Init(42, /*is_loaded=*/ false, /*is_modified=*/ false); + ASSERT_EQUALS(off_t(42), list.Size()); + ASSERT_FALSE(list.IsPageLoaded(0, 1)); + + list.SetPageLoadedStatus(0, 1, /*pstatus=*/ PageList::PAGE_LOADED); + ASSERT_TRUE(list.IsPageLoaded(0, 1)); + ASSERT_FALSE(list.IsPageLoaded(0, 2)); + + off_t start = 0; + off_t size = 0; + ASSERT_TRUE(list.FindUnloadedPage(0, start, size)); + ASSERT_EQUALS(off_t(1), start); + ASSERT_EQUALS(off_t(41), size); + + // test adding subsequent page then compressing + list.SetPageLoadedStatus(1, 3, /*pstatus=*/ PageList::PAGE_LOADED); + list.Compress(); + ASSERT_TRUE(list.IsPageLoaded(0, 3)); + + ASSERT_TRUE(list.FindUnloadedPage(0, start, size)); + ASSERT_EQUALS(off_t(4), start); + ASSERT_EQUALS(off_t(38), size); + + // test adding non-contiguous page then compressing + list.SetPageLoadedStatus(5, 1, /*pstatus=*/ PageList::PAGE_LOADED); + list.Compress(); + + ASSERT_TRUE(list.FindUnloadedPage(0, start, size)); + ASSERT_EQUALS(off_t(4), start); + ASSERT_EQUALS(off_t(1), size); + list.Dump(); + printf("\n"); + + // test adding page between two pages then compressing + list.SetPageLoadedStatus(4, 1, /*pstatus=*/ PageList::PAGE_LOADED); + list.Compress(); + + list.Dump(); + ASSERT_TRUE(list.FindUnloadedPage(0, start, size)); + ASSERT_EQUALS(off_t(6), start); + ASSERT_EQUALS(off_t(36), size); +} + +int main(int argc, char *argv[]) +{ + test_compress(); + return 0; +} diff --git a/src/types.h b/src/types.h index 8005ac6..887fd3b 100644 --- a/src/types.h +++ b/src/types.h @@ -21,6 +21,7 @@ #ifndef S3FS_TYPES_H_ #define S3FS_TYPES_H_ +#include #include #include #include