-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lldb-dap] Add unit test for FifoFiles #140480
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//===-- FifoFilesTest.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 "FifoFiles.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Testing/Support/Error.h" | ||
#include "gtest/gtest.h" | ||
#include <chrono> | ||
#include <thread> | ||
|
||
using namespace lldb_dap; | ||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
std::string MakeTempFifoPath() { | ||
llvm::SmallString<128> temp_path; | ||
llvm::sys::fs::createUniquePath("lldb-dap-fifo-%%%%%%", temp_path, | ||
/*MakeAbsolute=*/true); | ||
return temp_path.str().str(); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(FifoFilesTest, CreateAndDestroyFifoFile) { | ||
std::string fifo_path = MakeTempFifoPath(); | ||
auto fifo = CreateFifoFile(fifo_path); | ||
EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); | ||
|
||
// File should exist. | ||
EXPECT_TRUE(llvm::sys::fs::exists(fifo_path)); | ||
|
||
// Destructor should remove the file. | ||
fifo->reset(); | ||
EXPECT_FALSE(llvm::sys::fs::exists(fifo_path)); | ||
} | ||
|
||
TEST(FifoFilesTest, SendAndReceiveJSON) { | ||
std::string fifo_path = MakeTempFifoPath(); | ||
auto fifo = CreateFifoFile(fifo_path); | ||
EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); | ||
|
||
FifoFileIO writer(fifo_path, "writer"); | ||
FifoFileIO reader(fifo_path, "reader"); | ||
|
||
llvm::json::Object obj; | ||
obj["foo"] = "bar"; | ||
obj["num"] = 42; | ||
|
||
// Writer thread. | ||
std::thread writer_thread([&]() { | ||
EXPECT_THAT_ERROR(writer.SendJSON(llvm::json::Value(std::move(obj)), | ||
std::chrono::milliseconds(500)), | ||
llvm::Succeeded()); | ||
}); | ||
|
||
// Reader thread. | ||
std::thread reader_thread([&]() { | ||
auto result = reader.ReadJSON(std::chrono::milliseconds(500)); | ||
EXPECT_THAT_EXPECTED(result, llvm::Succeeded()); | ||
auto *read_obj = result->getAsObject(); | ||
|
||
ASSERT_NE(read_obj, nullptr); | ||
EXPECT_EQ((*read_obj)["foo"].getAsString(), "bar"); | ||
EXPECT_EQ((*read_obj)["num"].getAsInteger(), 42); | ||
}); | ||
|
||
writer_thread.join(); | ||
reader_thread.join(); | ||
} | ||
|
||
TEST(FifoFilesTest, ReadTimeout) { | ||
std::string fifo_path = MakeTempFifoPath(); | ||
auto fifo = CreateFifoFile(fifo_path); | ||
EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); | ||
|
||
FifoFileIO reader(fifo_path, "reader"); | ||
|
||
// No writer, should timeout. | ||
auto result = reader.ReadJSON(std::chrono::milliseconds(100)); | ||
EXPECT_THAT_EXPECTED(result, llvm::Failed()); | ||
} | ||
|
||
TEST(FifoFilesTest, WriteTimeout) { | ||
std::string fifo_path = MakeTempFifoPath(); | ||
auto fifo = CreateFifoFile(fifo_path); | ||
EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); | ||
|
||
FifoFileIO writer(fifo_path, "writer"); | ||
|
||
// No reader, should timeout. | ||
llvm::json::Object obj; | ||
obj["foo"] = "bar"; | ||
EXPECT_THAT_ERROR(writer.SendJSON(llvm::json::Value(std::move(obj)), | ||
std::chrono::milliseconds(100)), | ||
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. Same as above |
||
llvm::Failed()); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since we're expecting this to fail, should we shorten this timeout to like 1 ms?
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.
My reasoning was that too short of a timeout could lead to false negatives and a 100ms is short enough to be hardly noticeable while also accounting for load on the bots where something like 1ms can easily become 10-50ms. But I'm happy to make it shorter if you're worried about test slowdown.
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.
Sounds reasonable to me