|
| 1 | +/** |
| 2 | + * @file tiledb/api/c_api/vfs/test/unit_capi_ls_recursive.cc |
| 3 | + * |
| 4 | + * @section LICENSE |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2023 TileDB, Inc. |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + * |
| 28 | + * @section DESCRIPTION |
| 29 | + * |
| 30 | + * Tests the C API for tiledb_vfs_ls_recursive. |
| 31 | + * |
| 32 | + * TODO: This test is built and ran as part of tiledb_unit. Once we're able to |
| 33 | + * execute these tests in CI, we should build this test as a separate unit. |
| 34 | + */ |
| 35 | + |
| 36 | +#include <test/support/tdb_catch.h> |
| 37 | +#include "test/support/src/vfs_helpers.h" |
| 38 | + |
| 39 | +using namespace tiledb::test; |
| 40 | + |
| 41 | +TEST_CASE("C API: ls_recursive callback", "[vfs][ls-recursive]") { |
| 42 | + using tiledb::sm::LsObjects; |
| 43 | + S3Test s3_test({10, 50}); |
| 44 | + if (!s3_test.is_supported()) { |
| 45 | + return; |
| 46 | + } |
| 47 | + auto expected = s3_test.expected_results(); |
| 48 | + |
| 49 | + vfs_config vfs_config; |
| 50 | + tiledb_ctx_t* ctx; |
| 51 | + tiledb_ctx_alloc(vfs_config.config, &ctx); |
| 52 | + tiledb_vfs_t* vfs; |
| 53 | + tiledb_vfs_alloc(ctx, vfs_config.config, &vfs); |
| 54 | + |
| 55 | + LsObjects data; |
| 56 | + tiledb_ls_callback_t cb = [](const char* path, |
| 57 | + size_t path_len, |
| 58 | + uint64_t object_size, |
| 59 | + void* data) -> int32_t { |
| 60 | + auto* ls_data = static_cast<LsObjects*>(data); |
| 61 | + ls_data->push_back({{path, path_len}, object_size}); |
| 62 | + return 1; |
| 63 | + }; |
| 64 | + |
| 65 | + // This callback will return 0 exactly once. Traversal should stop immediately |
| 66 | + // and not continue to the next object. |
| 67 | + SECTION("callback stops traversal") { |
| 68 | + cb = [](const char* path, |
| 69 | + size_t path_len, |
| 70 | + uint64_t object_size, |
| 71 | + void* data) -> int32_t { |
| 72 | + // There's no precheck here to push_back, so the vector size will match |
| 73 | + // the number of times the callback was executed. |
| 74 | + auto* ls_data = static_cast<LsObjects*>(data); |
| 75 | + ls_data->push_back({{path, path_len}, object_size}); |
| 76 | + // Stop traversal after we collect 10 results. |
| 77 | + return ls_data->size() != 10; |
| 78 | + }; |
| 79 | + expected.resize(10); |
| 80 | + } |
| 81 | + |
| 82 | + CHECK( |
| 83 | + tiledb_vfs_ls_recursive(ctx, vfs, s3_test.temp_dir_.c_str(), cb, &data) == |
| 84 | + TILEDB_OK); |
| 85 | + CHECK(data.size() == expected.size()); |
| 86 | + CHECK(data == expected); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("C API: ls_recursive throwing callback", "[vfs][ls-recursive]") { |
| 90 | + using tiledb::sm::LsObjects; |
| 91 | + S3Test s3_test({10, 50}); |
| 92 | + if (!s3_test.is_supported()) { |
| 93 | + return; |
| 94 | + } |
| 95 | + auto expected = s3_test.expected_results(); |
| 96 | + |
| 97 | + vfs_config vfs_config; |
| 98 | + tiledb_ctx_t* ctx; |
| 99 | + tiledb_ctx_alloc(vfs_config.config, &ctx); |
| 100 | + tiledb_vfs_t* vfs; |
| 101 | + tiledb_vfs_alloc(ctx, vfs_config.config, &vfs); |
| 102 | + |
| 103 | + LsObjects data; |
| 104 | + tiledb_ls_callback_t cb = |
| 105 | + [](const char*, size_t, uint64_t, void*) -> int32_t { |
| 106 | + throw std::runtime_error("Throwing callback"); |
| 107 | + }; |
| 108 | + |
| 109 | + CHECK( |
| 110 | + tiledb_vfs_ls_recursive(ctx, vfs, s3_test.temp_dir_.c_str(), cb, &data) == |
| 111 | + TILEDB_ERR); |
| 112 | + CHECK(data.empty()); |
| 113 | +} |
0 commit comments