|
| 1 | +//===-- FifoFilesTest.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 "FifoFiles.h" |
| 10 | +#include "llvm/Support/FileSystem.h" |
| 11 | +#include "llvm/Testing/Support/Error.h" |
| 12 | +#include "gtest/gtest.h" |
| 13 | +#include <chrono> |
| 14 | +#include <thread> |
| 15 | + |
| 16 | +using namespace lldb_dap; |
| 17 | +using namespace llvm; |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +std::string MakeTempFifoPath() { |
| 22 | + llvm::SmallString<128> temp_path; |
| 23 | + llvm::sys::fs::createUniquePath("lldb-dap-fifo-%%%%%%", temp_path, |
| 24 | + /*MakeAbsolute=*/true); |
| 25 | + return temp_path.str().str(); |
| 26 | +} |
| 27 | + |
| 28 | +} // namespace |
| 29 | + |
| 30 | +TEST(FifoFilesTest, CreateAndDestroyFifoFile) { |
| 31 | + std::string fifo_path = MakeTempFifoPath(); |
| 32 | + auto fifo = CreateFifoFile(fifo_path); |
| 33 | + EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); |
| 34 | + |
| 35 | + // File should exist. |
| 36 | + EXPECT_TRUE(llvm::sys::fs::exists(fifo_path)); |
| 37 | + |
| 38 | + // Destructor should remove the file. |
| 39 | + fifo->reset(); |
| 40 | + EXPECT_FALSE(llvm::sys::fs::exists(fifo_path)); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(FifoFilesTest, SendAndReceiveJSON) { |
| 44 | + std::string fifo_path = MakeTempFifoPath(); |
| 45 | + auto fifo = CreateFifoFile(fifo_path); |
| 46 | + EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); |
| 47 | + |
| 48 | + FifoFileIO writer(fifo_path, "writer"); |
| 49 | + FifoFileIO reader(fifo_path, "reader"); |
| 50 | + |
| 51 | + llvm::json::Object obj; |
| 52 | + obj["foo"] = "bar"; |
| 53 | + obj["num"] = 42; |
| 54 | + |
| 55 | + // Writer thread. |
| 56 | + std::thread writer_thread([&]() { |
| 57 | + EXPECT_THAT_ERROR(writer.SendJSON(llvm::json::Value(std::move(obj)), |
| 58 | + std::chrono::milliseconds(500)), |
| 59 | + llvm::Succeeded()); |
| 60 | + }); |
| 61 | + |
| 62 | + // Reader thread. |
| 63 | + std::thread reader_thread([&]() { |
| 64 | + auto result = reader.ReadJSON(std::chrono::milliseconds(500)); |
| 65 | + EXPECT_THAT_EXPECTED(result, llvm::Succeeded()); |
| 66 | + auto *read_obj = result->getAsObject(); |
| 67 | + |
| 68 | + ASSERT_NE(read_obj, nullptr); |
| 69 | + EXPECT_EQ((*read_obj)["foo"].getAsString(), "bar"); |
| 70 | + EXPECT_EQ((*read_obj)["num"].getAsInteger(), 42); |
| 71 | + }); |
| 72 | + |
| 73 | + writer_thread.join(); |
| 74 | + reader_thread.join(); |
| 75 | +} |
| 76 | + |
| 77 | +TEST(FifoFilesTest, ReadTimeout) { |
| 78 | + std::string fifo_path = MakeTempFifoPath(); |
| 79 | + auto fifo = CreateFifoFile(fifo_path); |
| 80 | + EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); |
| 81 | + |
| 82 | + FifoFileIO reader(fifo_path, "reader"); |
| 83 | + |
| 84 | + // No writer, should timeout. |
| 85 | + auto result = reader.ReadJSON(std::chrono::milliseconds(100)); |
| 86 | + EXPECT_THAT_EXPECTED(result, llvm::Failed()); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(FifoFilesTest, WriteTimeout) { |
| 90 | + std::string fifo_path = MakeTempFifoPath(); |
| 91 | + auto fifo = CreateFifoFile(fifo_path); |
| 92 | + EXPECT_THAT_EXPECTED(fifo, llvm::Succeeded()); |
| 93 | + |
| 94 | + FifoFileIO writer(fifo_path, "writer"); |
| 95 | + |
| 96 | + // No reader, should timeout. |
| 97 | + llvm::json::Object obj; |
| 98 | + obj["foo"] = "bar"; |
| 99 | + EXPECT_THAT_ERROR(writer.SendJSON(llvm::json::Value(std::move(obj)), |
| 100 | + std::chrono::milliseconds(100)), |
| 101 | + llvm::Failed()); |
| 102 | +} |
0 commit comments