|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#include <gtest/gtest.h> |
| 25 | +#include "cache/cc/dump_queue.h" |
| 26 | +#include "detail/data_generator.h" |
| 27 | +#include "detail/mock_store.h" |
| 28 | +#include "detail/random.h" |
| 29 | +#include "detail/types_helper.h" |
| 30 | + |
| 31 | +class UCCacheDumpQueueTest : public testing::Test { |
| 32 | +public: |
| 33 | + UC::Test::Detail::Random rd; |
| 34 | + static UC::Detail::TaskHandle NextId() |
| 35 | + { |
| 36 | + static std::atomic<size_t> id{1}; |
| 37 | + return id.fetch_add(1, std::memory_order_relaxed); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +TEST_F(UCCacheDumpQueueTest, DumpOneBlock) |
| 42 | +{ |
| 43 | + using namespace UC::CacheStore; |
| 44 | + UC::Test::Detail::MockStore backend; |
| 45 | + EXPECT_CALL(backend, Dump).WillOnce(testing::Invoke(NextId)); |
| 46 | + UC::Latch finish{}; |
| 47 | + finish.Up(); |
| 48 | + EXPECT_CALL(backend, Wait).WillOnce(testing::Invoke([&finish]() { |
| 49 | + finish.Done(); |
| 50 | + return UC::Status::OK(); |
| 51 | + })); |
| 52 | + UC::HashSet<UC::Detail::TaskHandle> failureSet; |
| 53 | + Config config; |
| 54 | + config.backend = (uintptr_t)(void*)&backend; |
| 55 | + config.tensorSize = 32768; |
| 56 | + config.shardSize = config.tensorSize; |
| 57 | + config.blockSize = config.shardSize; |
| 58 | + config.deviceId = 0; |
| 59 | + config.bufferSize = config.blockSize * 2048; |
| 60 | + config.engineId = rd.RandomString(10); |
| 61 | + config.shareBufferEnable = true; |
| 62 | + TransBuffer buffer; |
| 63 | + DumpQueue dumpQ; |
| 64 | + auto s = buffer.Setup(config); |
| 65 | + ASSERT_EQ(s, UC::Status::OK()); |
| 66 | + s = dumpQ.Setup(config, &failureSet, &buffer); |
| 67 | + ASSERT_EQ(s, UC::Status::OK()); |
| 68 | + auto blockId = UC::Test::Detail::TypesHelper::MakeBlockId("a1b2c3d4e5f6789012345678901234ab"); |
| 69 | + constexpr size_t shardIdx = 0; |
| 70 | + UC::Test::Detail::DataGenerator data{1, config.blockSize}; |
| 71 | + data.Generate(); |
| 72 | + UC::Detail::TaskDesc desc{ |
| 73 | + {blockId, shardIdx, {data.Buffer()}} |
| 74 | + }; |
| 75 | + auto task = std::make_shared<TransTask>(TransTask::Type::DUMP, desc); |
| 76 | + auto waiter = std::make_shared<UC::Latch>(); |
| 77 | + dumpQ.Submit(task, waiter); |
| 78 | + waiter->Wait(); |
| 79 | + ASSERT_FALSE(failureSet.Contains(task->id)); |
| 80 | + finish.Wait(); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(UCCacheDumpQueueTest, DumpBlockWhileBackendSubmitFailed) |
| 84 | +{ |
| 85 | + using namespace UC::CacheStore; |
| 86 | + UC::Test::Detail::MockStore backend; |
| 87 | + EXPECT_CALL(backend, Dump).WillOnce(testing::Return(UC::Status::Error())); |
| 88 | + UC::HashSet<UC::Detail::TaskHandle> failureSet; |
| 89 | + Config config; |
| 90 | + config.backend = (uintptr_t)(void*)&backend; |
| 91 | + config.tensorSize = 32768; |
| 92 | + config.shardSize = config.tensorSize; |
| 93 | + config.blockSize = config.shardSize; |
| 94 | + config.deviceId = 0; |
| 95 | + config.bufferSize = config.blockSize * 2048; |
| 96 | + config.engineId = rd.RandomString(10); |
| 97 | + config.shareBufferEnable = true; |
| 98 | + TransBuffer buffer; |
| 99 | + DumpQueue dumpQ; |
| 100 | + auto s = buffer.Setup(config); |
| 101 | + ASSERT_EQ(s, UC::Status::OK()); |
| 102 | + s = dumpQ.Setup(config, &failureSet, &buffer); |
| 103 | + ASSERT_EQ(s, UC::Status::OK()); |
| 104 | + auto blockId = UC::Test::Detail::TypesHelper::MakeBlockId("a1b2c3d4e5f6789012345678901234ab"); |
| 105 | + constexpr size_t shardIdx = 0; |
| 106 | + UC::Test::Detail::DataGenerator data{1, config.blockSize}; |
| 107 | + data.Generate(); |
| 108 | + UC::Detail::TaskDesc desc{ |
| 109 | + {blockId, shardIdx, {data.Buffer()}} |
| 110 | + }; |
| 111 | + auto task = std::make_shared<TransTask>(TransTask::Type::DUMP, desc); |
| 112 | + auto waiter = std::make_shared<UC::Latch>(); |
| 113 | + dumpQ.Submit(task, waiter); |
| 114 | + waiter->Wait(); |
| 115 | + ASSERT_TRUE(failureSet.Contains(task->id)); |
| 116 | +} |
0 commit comments