Skip to content

Commit 966e276

Browse files
committed
Add OLP_SDK_USE_STD_ANY handling and one test case
* Add handling of the OLP_SDK_USE_STD_ANY in CMakeFile to propagate it durintg the build. * Add one additional test case to improve code coverage. Relates-To: NLAM-157 Signed-off-by: Khomenko Denys <[email protected]>
1 parent 44008f2 commit 966e276

File tree

3 files changed

+89
-15
lines changed

3 files changed

+89
-15
lines changed

docs/get-started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ cmake --build . --target docs
131131
| `OLP_SDK_BUILD_EXTERNAL_DEPS` | Defaults to `ON`. If enabled, CMake downloads and compiles dependencies. |
132132
| `OLP_SDK_NO_EXCEPTION` | Defaults to `OFF`. If enabled, all libraries are built without exceptions. |
133133
| `OLP_SDK_USE_STD_OPTIONAL` | Defaults to `OFF`. If enabled, all libraries are built with std::optional type instead of boost::optional for C++17 and above. |
134+
| `OLP_SDK_USE_STD_ANY` | Defaults to `OFF`. If enabled, all libraries are built with std::any type instead of boost::any for C++17 and above. |
134135
| `OLP_SDK_BOOST_THROW_EXCEPTION_EXTERNAL` | Defaults to `OFF`. When `OLP_SDK_NO_EXCEPTION` is `ON`, `boost` requires `boost::throw_exception()` to be defined. If enabled, the external definition of `boost::throw_exception()` is used. Otherwise, the library uses own definition. |
135136
| `OLP_SDK_MSVC_PARALLEL_BUILD_ENABLE` (Windows Only) | Defaults to `ON`. If enabled, the `/MP` compilation flag is added to build the Data SDK using multiple cores. |
136137
| `OLP_SDK_DISABLE_DEBUG_LOGGING`| Defaults to `OFF`. If enabled, the debug and trace level log messages are not printed. |

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,11 @@ if (OLP_SDK_USE_STD_OPTIONAL)
451451
PUBLIC OLP_CPP_SDK_USE_STD_OPTIONAL)
452452
endif()
453453

454+
if (OLP_SDK_USE_STD_ANY)
455+
target_compile_definitions(${PROJECT_NAME}
456+
PUBLIC OLP_SDK_USE_STD_ANY)
457+
endif()
458+
454459
target_include_directories(${PROJECT_NAME} PUBLIC
455460
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
456461
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>

olp-cpp-sdk-core/tests/cache/DefaultCacheImplTest.cpp

Lines changed: 83 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20+
#include <dirent.h>
2021
#include <chrono>
22+
#include <fstream>
2123
#include <thread>
2224

2325
#include <gtest/gtest.h>
@@ -31,6 +33,9 @@
3133
#define WIN32_LEAN_AND_MEAN
3234
#include <Windows.h>
3335
#undef max
36+
#else
37+
#include <sys/stat.h>
38+
#include <sys/types.h>
3439
#endif
3540

3641
#include "Helpers.h"
@@ -1399,21 +1404,23 @@ class DefaultCacheImplOpenTest
13991404
public testing::WithParamInterface<OpenTestParameters> {};
14001405

14011406
TEST_P(DefaultCacheImplOpenTest, ReadOnlyDir) {
1402-
const auto setup_dir = [&](const olp::porting::optional<std::string>& cache_path) {
1403-
if (cache_path) {
1404-
if (olp::utils::Dir::Exists(*cache_path)) {
1405-
ASSERT_TRUE(olp::utils::Dir::Remove(*cache_path));
1406-
}
1407-
ASSERT_TRUE(olp::utils::Dir::Create(*cache_path));
1408-
ASSERT_TRUE(SetRights(*cache_path, true));
1409-
}
1410-
};
1411-
1412-
const auto reset_dir = [&](const olp::porting::optional<std::string>& cache_path) {
1413-
if (cache_path) {
1414-
ASSERT_TRUE(olp::utils::Dir::Remove(*cache_path));
1415-
}
1416-
};
1407+
const auto setup_dir =
1408+
[&](const olp::porting::optional<std::string>& cache_path) {
1409+
if (cache_path) {
1410+
if (olp::utils::Dir::Exists(*cache_path)) {
1411+
ASSERT_TRUE(olp::utils::Dir::Remove(*cache_path));
1412+
}
1413+
ASSERT_TRUE(olp::utils::Dir::Create(*cache_path));
1414+
ASSERT_TRUE(SetRights(*cache_path, true));
1415+
}
1416+
};
1417+
1418+
const auto reset_dir =
1419+
[&](const olp::porting::optional<std::string>& cache_path) {
1420+
if (cache_path) {
1421+
ASSERT_TRUE(olp::utils::Dir::Remove(*cache_path));
1422+
}
1423+
};
14171424

14181425
const OpenTestParameters test_params = GetParam();
14191426

@@ -1447,4 +1454,65 @@ std::vector<OpenTestParameters> DefaultCacheImplOpenParams() {
14471454
INSTANTIATE_TEST_SUITE_P(, DefaultCacheImplOpenTest,
14481455
testing::ValuesIn(DefaultCacheImplOpenParams()));
14491456

1457+
TEST_F(DefaultCacheImplTest, ProtectedCacheIOErrorFallbackToReadOnly) {
1458+
SCOPED_TRACE("IOError fallback to read-only for protected cache");
1459+
1460+
const std::string ioerror_path =
1461+
olp::utils::Dir::TempDirectory() + "/unittest_ioerror_fallback";
1462+
1463+
if (olp::utils::Dir::Exists(ioerror_path)) {
1464+
helpers::MakeDirectoryAndContentReadonly(ioerror_path, false);
1465+
ASSERT_TRUE(olp::utils::Dir::Remove(ioerror_path));
1466+
}
1467+
1468+
ASSERT_TRUE(olp::utils::Dir::Create(ioerror_path));
1469+
1470+
{
1471+
cache::CacheSettings temp_settings;
1472+
temp_settings.disk_path_protected = ioerror_path;
1473+
DefaultCacheImplHelper temp_cache(temp_settings);
1474+
ASSERT_EQ(temp_cache.Open(),
1475+
cache::DefaultCache::StorageOpenResult::Success);
1476+
temp_cache.Close();
1477+
}
1478+
1479+
// Make all the database files read-only, but keep directory writable.
1480+
// This way Dir::IsReadOnly(directory) returns false (directory is writable),
1481+
// but LevelDB gets IOError when trying to write to the read-only DB files.
1482+
DIR* dir = opendir(ioerror_path.c_str());
1483+
ASSERT_TRUE(dir != nullptr);
1484+
struct dirent* entry;
1485+
while ((entry = readdir(dir)) != nullptr) {
1486+
if (entry->d_type == DT_REG) {
1487+
std::string file_path = ioerror_path + "/" + entry->d_name;
1488+
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
1489+
continue;
1490+
ASSERT_EQ(chmod(file_path.c_str(), S_IRUSR | S_IRGRP | S_IROTH), 0);
1491+
}
1492+
}
1493+
closedir(dir);
1494+
1495+
chmod(ioerror_path.c_str(),
1496+
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1497+
ASSERT_FALSE(olp::utils::Dir::IsReadOnly(ioerror_path));
1498+
1499+
cache::CacheSettings settings;
1500+
settings.disk_path_protected = ioerror_path;
1501+
settings.openOptions = cache::OpenOptions::Default;
1502+
DefaultCacheImplHelper cache(settings);
1503+
1504+
// Open should attempt R/W first, get IOError because files are read-only,
1505+
// then retry in read-only mode.
1506+
auto open_result = cache.Open();
1507+
EXPECT_TRUE(
1508+
open_result == cache::DefaultCache::StorageOpenResult::Success ||
1509+
open_result ==
1510+
cache::DefaultCache::StorageOpenResult::ProtectedCacheCorrupted ||
1511+
open_result ==
1512+
cache::DefaultCache::StorageOpenResult::OpenDiskPathFailure);
1513+
1514+
helpers::MakeDirectoryAndContentReadonly(ioerror_path, false);
1515+
ASSERT_TRUE(olp::utils::Dir::Remove(ioerror_path));
1516+
}
1517+
14501518
} // namespace

0 commit comments

Comments
 (0)