Repository navigation
[mlir-lsp] Add transport unit tests - #89855
Conversation
Add unit tests for some aspects of the JSON transport and message handler. These will be expanded in future patches as behavior is modified.
|
@llvm/pr-subscribers-mlir Author: Brian Gesiak (modocache) ChangesAdd unit tests for some aspects of the JSON transport and message handler. These will be expanded in future patches as behavior is modified. 4 Files Affected:
diff --git a/mlir/unittests/CMakeLists.txt b/mlir/unittests/CMakeLists.txt
index 6fad249a0b2fba..6d8aa290e82f25 100644
--- a/mlir/unittests/CMakeLists.txt
+++ b/mlir/unittests/CMakeLists.txt
@@ -20,6 +20,7 @@ add_subdirectory(Support)
add_subdirectory(Rewrite)
add_subdirectory(TableGen)
add_subdirectory(Target)
+add_subdirectory(Tools)
add_subdirectory(Transforms)
if(MLIR_ENABLE_EXECUTION_ENGINE)
diff --git a/mlir/unittests/Tools/CMakeLists.txt b/mlir/unittests/Tools/CMakeLists.txt
new file mode 100644
index 00000000000000..a97588d9286685
--- /dev/null
+++ b/mlir/unittests/Tools/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(lsp-server-support)
diff --git a/mlir/unittests/Tools/lsp-server-support/CMakeLists.txt b/mlir/unittests/Tools/lsp-server-support/CMakeLists.txt
new file mode 100644
index 00000000000000..3aa8b9c4bc7735
--- /dev/null
+++ b/mlir/unittests/Tools/lsp-server-support/CMakeLists.txt
@@ -0,0 +1,6 @@
+add_mlir_unittest(MLIRLspServerSupportTests
+ Transport.cpp
+)
+target_link_libraries(MLIRLspServerSupportTests
+ PRIVATE
+ MLIRLspServerSupportLib)
diff --git a/mlir/unittests/Tools/lsp-server-support/Transport.cpp b/mlir/unittests/Tools/lsp-server-support/Transport.cpp
new file mode 100644
index 00000000000000..9877c12c3695be
--- /dev/null
+++ b/mlir/unittests/Tools/lsp-server-support/Transport.cpp
@@ -0,0 +1,65 @@
+//===- Transport.cpp - LSP JSON transport unit tests ----------------------===//
+//
+// 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 "mlir/Tools/lsp-server-support/Transport.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/FileSystem.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+using namespace mlir;
+using namespace mlir::lsp;
+using namespace testing;
+
+namespace {
+
+TEST(TransportTest, SendReply) {
+ std::string out;
+ llvm::raw_string_ostream os(out);
+ JSONTransport transport(nullptr, os);
+ MessageHandler handler(transport);
+
+ transport.reply(1989, nullptr);
+ EXPECT_THAT(out, HasSubstr("\"id\":1989"));
+ EXPECT_THAT(out, HasSubstr("\"result\":null"));
+}
+
+TEST(TransportTest, MethodNotFound) {
+ auto tempOr = llvm::sys::fs::TempFile::create("lsp-unittest-%%%%%%.json");
+ ASSERT_TRUE((bool)tempOr);
+ auto discardTemp =
+ llvm::make_scope_exit([&]() { ASSERT_FALSE((bool)tempOr->discard()); });
+
+ {
+ std::error_code ec;
+ llvm::raw_fd_ostream os(tempOr->TmpName, ec);
+ ASSERT_FALSE(ec);
+ os << "{\"jsonrpc\":\"2.0\",\"id\":29,\"method\":\"ack\"}\n";
+ os.close();
+ }
+
+ std::string out;
+ llvm::raw_string_ostream os(out);
+ std::FILE *in = std::fopen(tempOr->TmpName.c_str(), "r");
+ auto closeIn = llvm::make_scope_exit([&]() { std::fclose(in); });
+
+ JSONTransport transport(in, os, JSONStreamStyle::Delimited);
+ MessageHandler handler(transport);
+
+ bool gotEOF = false;
+ llvm::Error err = llvm::handleErrors(
+ transport.run(handler), [&](const llvm::ECError &ecErr) {
+ gotEOF = ecErr.convertToErrorCode() == std::errc::io_error;
+ });
+ llvm::consumeError(std::move(err));
+ EXPECT_TRUE(gotEOF);
+ EXPECT_THAT(out, HasSubstr("\"id\":29"));
+ EXPECT_THAT(out, HasSubstr("\"error\""));
+ EXPECT_THAT(out, HasSubstr("\"message\":\"method not found: ack\""));
+}
+} // namespace
|
River707
left a comment
There was a problem hiding this comment.
Thanks for adding some explicit tests for the underlying infra!
|
This broke Windows presubmit: https://lab.llvm.org/buildbot/#/builders/271/builds/6920 I'll revert. I suspect the follow-ups will need to be reverted as well |
|
Feel free to re-land these with a fix, probably best to actually wait for the premerge to complete if you're unsure about Windows. |
|
Gotcha, thanks @joker-eph!
Sorry, I received many buildbot emails yesterday, and missed the Windows one that brought up the test I added,
Sorry, I missed the pre-merge failures -- I haven't contributed since the migration to GitHub. I saw the buildkite status was being displayed as "pending" in the web UI, but I thought it was permanently stuck! I now realize I should have asked around to see if it would run -- sorry about that! I will reland with some changes, and will wait for the pre-merge checks to complete. Sorry for the inconvenience! |
This is fine: you did nothing wrong. The windows pre-merge is often too backed up and most people won't wait for it. |
|
Yup, totally understand. Thanks for your patience & the explanation! :) |
|
My reverts weren't enough :( |
|
I'm confused actually, I suspect some issues with caching in the build directory here... This test has been deleted! |
|
I like the idea of reverting to get back to a green state and then re-landing slowly with green pre-merge build bots. I can send reverts in the next 20 minutes. |
Yes, exactly, looking at this now, I think there's nothing I can revert, so I suspect it's something unrelated. Let me know if I can help! |
|
Can you log in https://lab.llvm.org/buildbot/#/builders/271 (top right, using GitHub login), and use the "force build" button, there should be a checkbox for "clean the build directory" I think. |
|
Done -- I think this is the link to the request I issued, for reference: https://lab.llvm.org/buildbot/#/buildrequests/12648012 |
This reverts the revert commit 6844c2f, which was comprised of the following commits: 1. f3f6f22 - [mlir-lsp] Initialize `Reply::method` member (llvm#89857) 2. 37e13d4 - [mlir-lsp] Log invalid notification params (llvm#89856) 3. ba1b52e - [mlir-lsp] Add `outgoingNotification` unit test 4. 84bc21f - [mlir-lsp] Add transport unit tests (llvm#89855) Of these, (4) specifically caused issues in Windows pre-merge buildbots, in the `TransportTest.MethodNotFound` unit test that it added. The failure was caused by a statement that asserted that opening a file stream on a newly created temporary file did not result in an error, but this assert failed on Windows. This patch adds additional error logging for failures, to make it clearer what went wrong when failures occur. This patch also addresses the Windows failure by ensuring temporary files are created in the system temporary directory.
This reverts the revert commit 6844c2f, which was comprised of the following commits: 1. f3f6f22 - [mlir-lsp] Initialize `Reply::method` member (#89857) 2. 37e13d4 - [mlir-lsp] Log invalid notification params (#89856) 3. ba1b52e - [mlir-lsp] Add `outgoingNotification` unit test 4. 84bc21f - [mlir-lsp] Add transport unit tests (#89855) Of these, (4) specifically caused issues in Windows pre-merge buildbots, in the `TransportTest.MethodNotFound` unit test that it added. The failure was caused by a statement that asserted that opening a file stream on a newly created temporary file did not result in an error, but this assert failed on Windows. This patch adds additional error logging for failures, to make it clearer what went wrong when failures occur. This patch also addresses the Windows failure by ensuring temporary files are created in the system temporary directory.
Add unit tests for some aspects of the JSON transport and message handler. These will be expanded in future patches as behavior is modified.
This reverts the revert commit 9ae7f0f, which was comprised of the following commits: 1. ef591c4 - [mlir-lsp] Initialize `Reply::method` member (llvm#89857) 2. f4fa512 - [mlir-lsp] Log invalid notification params (llvm#89856) 3. 658cf08 - [mlir-lsp] Add `outgoingNotification` unit test 4. 330476d - [mlir-lsp] Add transport unit tests (llvm#89855) Of these, (4) specifically caused issues in Windows pre-merge buildbots, in the `TransportTest.MethodNotFound` unit test that it added. The failure was caused by a statement that asserted that opening a file stream on a newly created temporary file did not result in an error, but this assert failed on Windows. This patch adds additional error logging for failures, to make it clearer what went wrong when failures occur. This patch also addresses the Windows failure by ensuring temporary files are created in the system temporary directory.
Stacked pull requests:
Add unit tests for some aspects of the JSON transport and message
handler. These will be expanded in future patches as behavior is
modified.