|
24 | 24 | #include <gtest/gtest.h> |
25 | 25 | #include "cache/cc/load_queue.h" |
26 | 26 | #include "detail/data_generator.h" |
| 27 | +#include "detail/mock_store.h" |
27 | 28 | #include "detail/random.h" |
28 | 29 | #include "detail/types_helper.h" |
29 | 30 |
|
30 | 31 | class UCCacheLoadQueueTest : public testing::Test { |
31 | | -public: |
32 | | - class StoreStub : public UC::Store { |
33 | | - private: |
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 | | - public: |
41 | | - UC::Expected<std::vector<uint8_t>> Lookup(const UC::Detail::BlockId* blocks, |
42 | | - size_t num) override |
43 | | - { |
44 | | - std::vector<uint8_t> founds(num); |
45 | | - for (size_t i = 0; i < num; i++) { founds[i] = true; } |
46 | | - return founds; |
47 | | - } |
48 | | - void Prefetch(const UC::Detail::BlockId* blocks, size_t num) override {} |
49 | | - UC::Expected<UC::Detail::TaskHandle> Load(UC::Detail::TaskDesc task) override |
50 | | - { |
51 | | - return NextId(); |
52 | | - } |
53 | | - UC::Expected<UC::Detail::TaskHandle> Dump(UC::Detail::TaskDesc task) override |
54 | | - { |
55 | | - return NextId(); |
56 | | - } |
57 | | - UC::Expected<bool> Check(UC::Detail::TaskHandle taskId) override { return true; } |
58 | | - UC::Status Wait(UC::Detail::TaskHandle taskId) override { return UC::Status::OK(); } |
59 | | - }; |
60 | | - |
61 | 32 | public: |
62 | 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 | + } |
63 | 39 | }; |
64 | 40 |
|
65 | 41 | TEST_F(UCCacheLoadQueueTest, LoadSameBlockTwice) |
66 | 42 | { |
67 | 43 | using namespace UC::CacheStore; |
| 44 | + UC::Test::Detail::MockStore backend; |
| 45 | + EXPECT_CALL(backend, Load).WillOnce(testing::Invoke(NextId)); |
| 46 | + EXPECT_CALL(backend, Wait).WillOnce(testing::Return(UC::Status::OK())); |
68 | 47 | UC::HashSet<UC::Detail::TaskHandle> failureSet; |
69 | | - UCCacheLoadQueueTest::StoreStub backend; |
70 | 48 | Config config; |
71 | 49 | config.backend = (uintptr_t)(void*)&backend; |
72 | 50 | config.tensorSize = 32768; |
@@ -100,3 +78,76 @@ TEST_F(UCCacheLoadQueueTest, LoadSameBlockTwice) |
100 | 78 | waiter2->Wait(); |
101 | 79 | ASSERT_FALSE(failureSet.Contains(task2->id)); |
102 | 80 | } |
| 81 | + |
| 82 | +TEST_F(UCCacheLoadQueueTest, LoadWhileBackendSubmitFailed) |
| 83 | +{ |
| 84 | + using namespace UC::CacheStore; |
| 85 | + using namespace testing; |
| 86 | + UC::Test::Detail::MockStore backend; |
| 87 | + EXPECT_CALL(backend, Load).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 | + LoadQueue loadQ; |
| 100 | + auto s = buffer.Setup(config); |
| 101 | + ASSERT_EQ(s, UC::Status::OK()); |
| 102 | + s = loadQ.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::LOAD, desc); |
| 112 | + auto waiter = std::make_shared<UC::Latch>(); |
| 113 | + loadQ.Submit(task, waiter); |
| 114 | + waiter->Wait(); |
| 115 | + ASSERT_TRUE(failureSet.Contains(task->id)); |
| 116 | +} |
| 117 | + |
| 118 | +TEST_F(UCCacheLoadQueueTest, LoadWhileBackendWaitFailed) |
| 119 | +{ |
| 120 | + using namespace UC::CacheStore; |
| 121 | + using namespace testing; |
| 122 | + UC::Test::Detail::MockStore backend; |
| 123 | + EXPECT_CALL(backend, Load).WillOnce(testing::Invoke(NextId)); |
| 124 | + EXPECT_CALL(backend, Wait).WillOnce(testing::Return(UC::Status::Error())); |
| 125 | + UC::HashSet<UC::Detail::TaskHandle> failureSet; |
| 126 | + Config config; |
| 127 | + config.backend = (uintptr_t)(void*)&backend; |
| 128 | + config.tensorSize = 32768; |
| 129 | + config.shardSize = config.tensorSize; |
| 130 | + config.blockSize = config.shardSize; |
| 131 | + config.deviceId = 0; |
| 132 | + config.bufferSize = config.blockSize * 2048; |
| 133 | + config.engineId = rd.RandomString(10); |
| 134 | + config.shareBufferEnable = true; |
| 135 | + TransBuffer buffer; |
| 136 | + LoadQueue loadQ; |
| 137 | + auto s = buffer.Setup(config); |
| 138 | + ASSERT_EQ(s, UC::Status::OK()); |
| 139 | + s = loadQ.Setup(config, &failureSet, &buffer); |
| 140 | + ASSERT_EQ(s, UC::Status::OK()); |
| 141 | + auto blockId = UC::Test::Detail::TypesHelper::MakeBlockId("a1b2c3d4e5f6789012345678901234ab"); |
| 142 | + constexpr size_t shardIdx = 0; |
| 143 | + UC::Test::Detail::DataGenerator data{1, config.blockSize}; |
| 144 | + data.Generate(); |
| 145 | + UC::Detail::TaskDesc desc{ |
| 146 | + {blockId, shardIdx, {data.Buffer()}} |
| 147 | + }; |
| 148 | + auto task = std::make_shared<TransTask>(TransTask::Type::LOAD, desc); |
| 149 | + auto waiter = std::make_shared<UC::Latch>(); |
| 150 | + loadQ.Submit(task, waiter); |
| 151 | + waiter->Wait(); |
| 152 | + ASSERT_TRUE(failureSet.Contains(task->id)); |
| 153 | +} |
0 commit comments