-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Modify the localCache API to require an explicit commit on CachedFile… #115331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3fdba46
7782900
8f99856
861e889
46b18d8
2d38a85
987f307
c8ed684
d4cd9ea
46660c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
//===- Caching.cpp --------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Support/Caching.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Support/Path.h" | ||
#include "llvm/Testing/Support/Error.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
#define ASSERT_NO_ERROR(x) \ | ||
if (std::error_code ASSERT_NO_ERROR_ec = x) { \ | ||
SmallString<128> MessageStorage; \ | ||
raw_svector_ostream Message(MessageStorage); \ | ||
Message << #x ": did not return errc::success.\n" \ | ||
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \ | ||
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \ | ||
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \ | ||
} else { \ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the empty else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a standard defensive programming technique with multiline macros to cause a compile error when someone writes "ASSERT_NO_ERROR(...) else { ... }" by mistake. |
||
|
||
char data[] = "some data"; | ||
|
||
TEST(Caching, Normal) { | ||
SmallString<256> TempDir; | ||
sys::path::system_temp_directory(true, TempDir); | ||
SmallString<256> CacheDir; | ||
sys::path::append(CacheDir, TempDir, "llvm_test_cache"); | ||
|
||
sys::fs::remove_directories(CacheDir.str()); | ||
|
||
std::unique_ptr<MemoryBuffer> CachedBuffer; | ||
auto AddBuffer = [&CachedBuffer](unsigned Task, const Twine &ModuleName, | ||
std::unique_ptr<MemoryBuffer> M) { | ||
CachedBuffer = std::move(M); | ||
}; | ||
auto CacheOrErr = | ||
localCache("LLVMTestCache", "LLVMTest", CacheDir, AddBuffer); | ||
ASSERT_TRUE(bool(CacheOrErr)); | ||
|
||
FileCache &Cache = *CacheOrErr; | ||
|
||
{ | ||
auto AddStreamOrErr = Cache(1, "foo", ""); | ||
ASSERT_TRUE(bool(AddStreamOrErr)); | ||
|
||
AddStreamFn &AddStream = *AddStreamOrErr; | ||
ASSERT_TRUE(AddStream); | ||
|
||
auto FileOrErr = AddStream(1, ""); | ||
ASSERT_TRUE(bool(FileOrErr)); | ||
|
||
CachedFileStream *CFS = FileOrErr->get(); | ||
(*CFS->OS).write(data, sizeof(data)); | ||
ASSERT_THAT_ERROR(CFS->commit(), Succeeded()); | ||
} | ||
|
||
{ | ||
auto AddStreamOrErr = Cache(1, "foo", ""); | ||
ASSERT_TRUE(bool(AddStreamOrErr)); | ||
|
||
AddStreamFn &AddStream = *AddStreamOrErr; | ||
ASSERT_FALSE(AddStream); | ||
|
||
ASSERT_TRUE(CachedBuffer->getBufferSize() == sizeof(data)); | ||
StringRef readData = CachedBuffer->getBuffer(); | ||
ASSERT_TRUE(memcmp(data, readData.data(), sizeof(data)) == 0); | ||
} | ||
|
||
ASSERT_NO_ERROR(sys::fs::remove_directories(CacheDir.str())); | ||
} | ||
|
||
TEST(Caching, WriteAfterCommit) { | ||
SmallString<256> TempDir; | ||
sys::path::system_temp_directory(true, TempDir); | ||
SmallString<256> CacheDir; | ||
sys::path::append(CacheDir, TempDir, "llvm_test_cache"); | ||
|
||
sys::fs::remove_directories(CacheDir.str()); | ||
|
||
std::unique_ptr<MemoryBuffer> CachedBuffer; | ||
auto AddBuffer = [&CachedBuffer](unsigned Task, const Twine &ModuleName, | ||
std::unique_ptr<MemoryBuffer> M) { | ||
CachedBuffer = std::move(M); | ||
}; | ||
auto CacheOrErr = | ||
localCache("LLVMTestCache", "LLVMTest", CacheDir, AddBuffer); | ||
ASSERT_TRUE(bool(CacheOrErr)); | ||
|
||
FileCache &Cache = *CacheOrErr; | ||
|
||
auto AddStreamOrErr = Cache(1, "foo", ""); | ||
ASSERT_TRUE(bool(AddStreamOrErr)); | ||
|
||
AddStreamFn &AddStream = *AddStreamOrErr; | ||
ASSERT_TRUE(AddStream); | ||
|
||
auto FileOrErr = AddStream(1, ""); | ||
ASSERT_TRUE(bool(FileOrErr)); | ||
|
||
CachedFileStream *CFS = FileOrErr->get(); | ||
(*CFS->OS).write(data, sizeof(data)); | ||
ASSERT_THAT_ERROR(CFS->commit(), Succeeded()); | ||
|
||
EXPECT_DEATH( | ||
{ (*CFS->OS).write(data, sizeof(data)); }, "") | ||
<< "Write after commit did not cause abort"; | ||
|
||
ASSERT_NO_ERROR(sys::fs::remove_directories(CacheDir.str())); | ||
} | ||
|
||
TEST(Caching, NoCommit) { | ||
SmallString<256> TempDir; | ||
sys::path::system_temp_directory(true, TempDir); | ||
SmallString<256> CacheDir; | ||
sys::path::append(CacheDir, TempDir, "llvm_test_cache"); | ||
|
||
sys::fs::remove_directories(CacheDir.str()); | ||
|
||
std::unique_ptr<MemoryBuffer> CachedBuffer; | ||
auto AddBuffer = [&CachedBuffer](unsigned Task, const Twine &ModuleName, | ||
std::unique_ptr<MemoryBuffer> M) { | ||
CachedBuffer = std::move(M); | ||
}; | ||
auto CacheOrErr = | ||
localCache("LLVMTestCache", "LLVMTest", CacheDir, AddBuffer); | ||
ASSERT_TRUE(bool(CacheOrErr)); | ||
|
||
FileCache &Cache = *CacheOrErr; | ||
|
||
auto AddStreamOrErr = Cache(1, "foo", ""); | ||
ASSERT_TRUE(bool(AddStreamOrErr)); | ||
|
||
AddStreamFn &AddStream = *AddStreamOrErr; | ||
ASSERT_TRUE(AddStream); | ||
|
||
auto FileOrErr = AddStream(1, ""); | ||
ASSERT_TRUE(bool(FileOrErr)); | ||
|
||
CachedFileStream *CFS = FileOrErr->get(); | ||
(*CFS->OS).write(data, sizeof(data)); | ||
ASSERT_THAT_ERROR(CFS->commit(), Succeeded()); | ||
|
||
EXPECT_DEATH( | ||
{ | ||
auto FileOrErr = AddStream(1, ""); | ||
ASSERT_TRUE(bool(FileOrErr)); | ||
|
||
CachedFileStream *CFS = FileOrErr->get(); | ||
(*CFS->OS).write(data, sizeof(data)); | ||
}, | ||
"") | ||
<< "destruction without commit did not cause error"; | ||
|
||
ASSERT_NO_ERROR(sys::fs::remove_directories(CacheDir.str())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you test the error when there is no commit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - I have added a test in my latest commit.