Skip to content

[mlir-lsp] Add transport unit tests - #89855

Merged
modocache merged 1 commit into
llvm:mainfrom
modocache:lsp-1
Apr 24, 2024
Merged

modocache merged 1 commit into
llvm:mainfrom
modocache:lsp-1

Conversation

@modocache

@modocache modocache commented Apr 24, 2024 •

Copy link
Copy Markdown
Member

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.

Add unit tests for some aspects of the JSON transport and message
handler. These will be expanded in future patches as behavior is
modified.
@llvmbot

llvmbot commented Apr 24, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-mlir

Author: Brian Gesiak (modocache)

Changes

Add unit tests for some aspects of the JSON transport and message handler. These will be expanded in future patches as behavior is modified.


Full diff: https://github.com/llvm/llvm-project/pull/89855.diff

4 Files Affected:

  • (modified) mlir/unittests/CMakeLists.txt (+1)
  • (added) mlir/unittests/Tools/CMakeLists.txt (+1)
  • (added) mlir/unittests/Tools/lsp-server-support/CMakeLists.txt (+6)
  • (added) mlir/unittests/Tools/lsp-server-support/Transport.cpp (+65)
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

@walter-erquinigo walter-erquinigo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@River707 River707 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding some explicit tests for the underlying infra!

@modocache
modocache merged commit 84bc21f into llvm:main Apr 24, 2024
@modocache
modocache deleted the lsp-1 branch April 24, 2024 19:14
@joker-eph

joker-eph commented Apr 26, 2024 •

Copy link
Copy Markdown
Contributor

This broke Windows presubmit: https://lab.llvm.org/buildbot/#/builders/271/builds/6920
Didn't you get an email about it?

I'll revert. I suspect the follow-ups will need to be reverted as well

@joker-eph

Copy link
Copy Markdown
Contributor

6844c2f

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.

@modocache

modocache commented Apr 26, 2024 •

Copy link
Copy Markdown
Member Author

Gotcha, thanks @joker-eph!

Didn't you get an email about it?

Sorry, I received many buildbot emails yesterday, and missed the Windows one that brought up the test I added, TransportTest.MethodNotFound.

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.

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!

@joker-eph

Copy link
Copy Markdown
Contributor

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!

This is fine: you did nothing wrong. The windows pre-merge is often too backed up and most people won't wait for it.
My comment was in the sense: "before re-landing this particular change, if you are not sure you have the right fix for Windows and can't test it locally, it'll probably best to get confirmation from buildkite".

@modocache

Copy link
Copy Markdown
Member Author

Yup, totally understand. Thanks for your patience & the explanation! :)

@joker-eph

Copy link
Copy Markdown
Contributor

My reverts weren't enough :(
Things are still broken here: https://lab.llvm.org/buildbot/#/builders/271 ; do you think you'll have this fix quickly or should I look into reverting more?

@joker-eph

Copy link
Copy Markdown
Contributor

I'm confused actually, I suspect some issues with caching in the build directory here... This test has been deleted!

@modocache

Copy link
Copy Markdown
Member Author

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.

@modocache

Copy link
Copy Markdown
Member Author

This test has been deleted!

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!

@joker-eph

Copy link
Copy Markdown
Contributor

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.
(my login is broken)

@modocache

Copy link
Copy Markdown
Member Author

Done -- I think this is the link to the request I issued, for reference: https://lab.llvm.org/buildbot/#/buildrequests/12648012

modocache added a commit to modocache/llvm-project that referenced this pull request Apr 26, 2024
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.
modocache added a commit that referenced this pull request Apr 29, 2024
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.
paleauraaaa pushed a commit to paleauraaaa/llvm-project that referenced this pull request Oct 7, 2026
Add unit tests for some aspects of the JSON transport and message
handler. These will be expanded in future patches as behavior is
modified.
paleauraaaa pushed a commit to paleauraaaa/llvm-project that referenced this pull request Oct 7, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants