| 1 | //===-- FileCacheTests.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 "support/FileCache.h" |
| 10 | |
| 11 | #include "TestFS.h" |
| 12 | #include "gmock/gmock.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | #include <atomic> |
| 15 | #include <chrono> |
| 16 | #include <optional> |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace clangd { |
| 20 | namespace config { |
| 21 | namespace { |
| 22 | |
| 23 | class TestCache : public FileCache { |
| 24 | MockFS FS; |
| 25 | mutable std::string Value; |
| 26 | |
| 27 | public: |
| 28 | TestCache() : FileCache(testPath("foo.cc")) {} |
| 29 | |
| 30 | void setContents(const char *C) { |
| 31 | if (C) |
| 32 | FS.Files[testPath("foo.cc")] = C; |
| 33 | else |
| 34 | FS.Files.erase(testPath("foo.cc")); |
| 35 | } |
| 36 | |
| 37 | std::string get(std::chrono::steady_clock::time_point FreshTime, |
| 38 | bool ExpectParse) const { |
| 39 | bool GotParse = false; |
| 40 | bool GotRead; |
| 41 | std::string Result; |
| 42 | read( |
| 43 | FS, FreshTime, |
| 44 | [&](std::optional<llvm::StringRef> Data) { |
| 45 | GotParse = true; |
| 46 | Value = Data.value_or("").str(); |
| 47 | }, |
| 48 | [&]() { |
| 49 | GotRead = true; |
| 50 | Result = Value; |
| 51 | }); |
| 52 | EXPECT_EQ(GotParse, ExpectParse); |
| 53 | EXPECT_TRUE(GotRead); |
| 54 | return Result; |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | TEST(FileCacheTest, Invalidation) { |
| 59 | TestCache C; |
| 60 | |
| 61 | auto StaleOK = std::chrono::steady_clock::now(); |
| 62 | auto MustBeFresh = StaleOK + std::chrono::hours(1); |
| 63 | |
| 64 | C.setContents("a"); |
| 65 | EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/true)) << "Parsed first time"; |
| 66 | EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)"; |
| 67 | EXPECT_EQ("a", C.get(MustBeFresh, /*ExpectParse=*/false)) << "Cached (stat)"; |
| 68 | C.setContents("bb"); |
| 69 | EXPECT_EQ("a", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)"; |
| 70 | EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Size changed"; |
| 71 | EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Cached (stat)"; |
| 72 | C.setContents(nullptr); |
| 73 | EXPECT_EQ("bb", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)"; |
| 74 | EXPECT_EQ("", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Stat failed"; |
| 75 | EXPECT_EQ("", C.get(MustBeFresh, /*ExpectParse=*/false)) << "Cached (404)"; |
| 76 | C.setContents("bb"); // Match the previous stat values! |
| 77 | EXPECT_EQ("", C.get(StaleOK, /*ExpectParse=*/false)) << "Cached (time)"; |
| 78 | EXPECT_EQ("bb", C.get(MustBeFresh, /*ExpectParse=*/true)) << "Size changed"; |
| 79 | } |
| 80 | |
| 81 | } // namespace |
| 82 | } // namespace config |
| 83 | } // namespace clangd |
| 84 | } // namespace clang |