|
| 1 | +//===- ActionCacheTest.cpp ------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "llvm/CAS/ActionCache.h" |
| 10 | +#include "CASTestConfig.h" |
| 11 | +#include "llvm/CAS/ObjectStore.h" |
| 12 | +#include "llvm/Support/FileSystem.h" |
| 13 | +#include "llvm/Testing/Support/Error.h" |
| 14 | +#include "llvm/Testing/Support/SupportHelpers.h" |
| 15 | +#include "gtest/gtest.h" |
| 16 | + |
| 17 | +using namespace llvm; |
| 18 | +using namespace llvm::cas; |
| 19 | + |
| 20 | +TEST_P(CASTest, ActionCacheHit) { |
| 21 | + std::shared_ptr<ObjectStore> CAS = createObjectStore(); |
| 22 | + std::unique_ptr<ActionCache> Cache = createActionCache(); |
| 23 | + |
| 24 | + std::optional<ObjectProxy> ID; |
| 25 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID), |
| 26 | + Succeeded()); |
| 27 | + std::optional<CASID> ResultID; |
| 28 | + ASSERT_THAT_ERROR(Cache->put(*ID, *ID), Succeeded()); |
| 29 | + ASSERT_THAT_ERROR(Cache->get(*ID).moveInto(ResultID), Succeeded()); |
| 30 | + ASSERT_TRUE(ResultID); |
| 31 | + std::optional<ObjectRef> Result = CAS->getReference(*ResultID); |
| 32 | + ASSERT_TRUE(Result); |
| 33 | + ASSERT_EQ(*ID, *Result); |
| 34 | +} |
| 35 | + |
| 36 | +TEST_P(CASTest, ActionCacheMiss) { |
| 37 | + std::shared_ptr<ObjectStore> CAS = createObjectStore(); |
| 38 | + std::unique_ptr<ActionCache> Cache = createActionCache(); |
| 39 | + |
| 40 | + std::optional<ObjectProxy> ID1, ID2; |
| 41 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID1), |
| 42 | + Succeeded()); |
| 43 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "2").moveInto(ID2), |
| 44 | + Succeeded()); |
| 45 | + ASSERT_THAT_ERROR(Cache->put(*ID1, *ID2), Succeeded()); |
| 46 | + // This is a cache miss for looking up a key doesn't exist. |
| 47 | + std::optional<CASID> Result1; |
| 48 | + ASSERT_THAT_ERROR(Cache->get(*ID2).moveInto(Result1), Succeeded()); |
| 49 | + ASSERT_FALSE(Result1); |
| 50 | + |
| 51 | + ASSERT_THAT_ERROR(Cache->put(*ID2, *ID1), Succeeded()); |
| 52 | + // Cache hit after adding the value. |
| 53 | + std::optional<CASID> Result2; |
| 54 | + ASSERT_THAT_ERROR(Cache->get(*ID2).moveInto(Result2), Succeeded()); |
| 55 | + ASSERT_TRUE(Result2); |
| 56 | + std::optional<ObjectRef> Ref = CAS->getReference(*Result2); |
| 57 | + ASSERT_TRUE(Ref); |
| 58 | + ASSERT_EQ(*ID1, *Ref); |
| 59 | +} |
| 60 | + |
| 61 | +TEST_P(CASTest, ActionCacheRewrite) { |
| 62 | + std::shared_ptr<ObjectStore> CAS = createObjectStore(); |
| 63 | + std::unique_ptr<ActionCache> Cache = createActionCache(); |
| 64 | + |
| 65 | + std::optional<ObjectProxy> ID1, ID2; |
| 66 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID1), |
| 67 | + Succeeded()); |
| 68 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "2").moveInto(ID2), |
| 69 | + Succeeded()); |
| 70 | + ASSERT_THAT_ERROR(Cache->put(*ID1, *ID1), Succeeded()); |
| 71 | + // Writing to the same key with different value is error. |
| 72 | + ASSERT_THAT_ERROR(Cache->put(*ID1, *ID2), Failed()); |
| 73 | + // Writing the same value multiple times to the same key is fine. |
| 74 | + ASSERT_THAT_ERROR(Cache->put(*ID1, *ID1), Succeeded()); |
| 75 | +} |
| 76 | + |
| 77 | +#if LLVM_ENABLE_ONDISK_CAS |
| 78 | +TEST(OnDiskActionCache, ActionCacheResultInvalid) { |
| 79 | + unittest::TempDir Temp("on-disk-cache", /*Unique=*/true); |
| 80 | + std::unique_ptr<ObjectStore> CAS1 = createInMemoryCAS(); |
| 81 | + std::unique_ptr<ObjectStore> CAS2 = createInMemoryCAS(); |
| 82 | + |
| 83 | + std::optional<ObjectProxy> ID1, ID2, ID3; |
| 84 | + ASSERT_THAT_ERROR(CAS1->createProxy(std::nullopt, "1").moveInto(ID1), |
| 85 | + Succeeded()); |
| 86 | + ASSERT_THAT_ERROR(CAS1->createProxy(std::nullopt, "2").moveInto(ID2), |
| 87 | + Succeeded()); |
| 88 | + ASSERT_THAT_ERROR(CAS2->createProxy(std::nullopt, "1").moveInto(ID3), |
| 89 | + Succeeded()); |
| 90 | + |
| 91 | + std::unique_ptr<ActionCache> Cache1 = |
| 92 | + cantFail(createOnDiskActionCache(Temp.path())); |
| 93 | + // Test put and get. |
| 94 | + ASSERT_THAT_ERROR(Cache1->put(*ID1, *ID2), Succeeded()); |
| 95 | + std::optional<CASID> Result; |
| 96 | + ASSERT_THAT_ERROR(Cache1->get(*ID1).moveInto(Result), Succeeded()); |
| 97 | + ASSERT_TRUE(Result); |
| 98 | + |
| 99 | + // Create OnDiskCAS from the same location but a different underlying CAS. |
| 100 | + std::unique_ptr<ActionCache> Cache2 = |
| 101 | + cantFail(createOnDiskActionCache(Temp.path())); |
| 102 | + // Loading an key that points to an invalid object. |
| 103 | + std::optional<CASID> Result2; |
| 104 | + // Get will work but the resulting CASID doesn't exist in ObjectStore. |
| 105 | + ASSERT_THAT_ERROR(Cache2->get(*ID3).moveInto(Result2), Succeeded()); |
| 106 | + ASSERT_FALSE(CAS2->getReference(*Result2)); |
| 107 | + // Write a different value will cause error. |
| 108 | + ASSERT_THAT_ERROR(Cache2->put(*ID3, *ID3), Failed()); |
| 109 | +} |
| 110 | +#endif |
| 111 | + |
| 112 | +TEST_P(CASTest, ActionCacheAsync) { |
| 113 | + std::shared_ptr<ObjectStore> CAS = createObjectStore(); |
| 114 | + std::unique_ptr<ActionCache> Cache = createActionCache(); |
| 115 | + |
| 116 | + { |
| 117 | + std::optional<ObjectProxy> ID; |
| 118 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "1").moveInto(ID), |
| 119 | + Succeeded()); |
| 120 | + auto PutFuture = Cache->putFuture(*ID, *ID); |
| 121 | + ASSERT_THAT_ERROR(PutFuture.get().take(), Succeeded()); |
| 122 | + auto GetFuture = Cache->getFuture(*ID); |
| 123 | + std::optional<CASID> ResultID; |
| 124 | + ASSERT_THAT_ERROR(GetFuture.get().take().moveInto(ResultID), Succeeded()); |
| 125 | + ASSERT_TRUE(ResultID); |
| 126 | + } |
| 127 | + |
| 128 | + std::optional<ObjectProxy> ID2; |
| 129 | + ASSERT_THAT_ERROR(CAS->createProxy(std::nullopt, "2").moveInto(ID2), |
| 130 | + Succeeded()); |
| 131 | + { |
| 132 | + std::promise<AsyncErrorValue> Promise; |
| 133 | + auto Future = Promise.get_future(); |
| 134 | + Cache->putAsync(*ID2, *ID2, false, |
| 135 | + [Promise = std::move(Promise)](Error E) mutable { |
| 136 | + Promise.set_value(std::move(E)); |
| 137 | + }); |
| 138 | + ASSERT_THAT_ERROR(Future.get().take(), Succeeded()); |
| 139 | + } |
| 140 | + { |
| 141 | + std::promise<AsyncCASIDValue> Promise; |
| 142 | + auto Future = Promise.get_future(); |
| 143 | + Cache->getAsync(*ID2, false, |
| 144 | + [Promise = std::move(Promise)]( |
| 145 | + Expected<std::optional<CASID>> Value) mutable { |
| 146 | + Promise.set_value(std::move(Value)); |
| 147 | + }); |
| 148 | + std::optional<CASID> ResultID; |
| 149 | + ASSERT_THAT_ERROR(Future.get().take().moveInto(ResultID), Succeeded()); |
| 150 | + ASSERT_TRUE(ResultID); |
| 151 | + } |
| 152 | +} |
0 commit comments