From d92572bc5279e31de64cd8dccfd8d8a66d2f5d5b Mon Sep 17 00:00:00 2001 From: Kris De Volder Date: Mon, 19 Oct 2020 14:27:03 -0700 Subject: [PATCH] Attempt to fix race condition boot ls tests See: https://www.pivotaltracker.com/story/show/175339205 --- .../languageserver/testharness/Editor.java | 14 +++---- .../testharness/LanguageServerHarness.java | 38 ++++--------------- .../test/ActiveProfilesHoverTest.java | 4 +- 3 files changed, 17 insertions(+), 39 deletions(-) diff --git a/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/Editor.java b/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/Editor.java index df368d8e4..a56e2bda8 100644 --- a/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/Editor.java +++ b/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/Editor.java @@ -11,9 +11,9 @@ package org.springframework.ide.vscode.languageserver.testharness; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness.HIGHLIGHTS_TIMEOUT; import static org.springframework.ide.vscode.languageserver.testharness.LanguageServerHarness.getDocString; import static org.springframework.ide.vscode.languageserver.testharness.TestAsserts.assertContains; import static org.springframework.ide.vscode.languageserver.testharness.TestAsserts.assertDoesNotContain; @@ -27,6 +27,8 @@ import java.util.Iterator; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -137,6 +139,7 @@ public class Editor { private int selectionStart; private Set ignoredTypes; private LanguageId languageId; + private Future highlightsFuture; public Editor(LanguageServerHarness harness, String contents, LanguageId languageId) throws Exception { this(harness, contents, languageId, null); @@ -150,6 +153,7 @@ public class Editor { this.selectionStart = state.selectionStart; this.selectionEnd = state.selectionEnd; this.ignoredTypes = new HashSet<>(); + this.highlightsFuture = harness.getHighlightsFuture(doc); } public Editor(LanguageServerHarness harness, TextDocumentInfo doc, String contents, LanguageId languageId) throws Exception { this.harness = harness; @@ -159,6 +163,7 @@ public class Editor { this.selectionStart = state.selectionStart; this.selectionEnd = state.selectionEnd; this.ignoredTypes = new HashSet<>(); + this.highlightsFuture = harness.getHighlightsFuture(doc); } /** @@ -215,7 +220,7 @@ public class Editor { public List assertHighlights(String... expectedHighlights) throws Exception { - HighlightParams highlights = harness.getHighlights(doc); + HighlightParams highlights = this.highlightsFuture.get(HIGHLIGHTS_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); List ranges = highlights != null ? highlights.getCodeLenses().stream().map(CodeLens::getRange).collect(Collectors.toList()) : ImmutableList.of(); Collections.sort(ranges, RANGE_COMPARATOR); List actualHighlights = ranges.stream() @@ -225,11 +230,6 @@ public class Editor { return ranges; } - public void assertNoHighlights() throws Exception { - HighlightParams highlights = harness.getHighlights(false, doc); - assertNull(highlights); - } - /** * Get the editor text, with cursor markers inserted (for easy textual comparison * after applying a proposal) diff --git a/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/LanguageServerHarness.java b/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/LanguageServerHarness.java index d48903b3a..0dbf23ff5 100644 --- a/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/LanguageServerHarness.java +++ b/headless-services/commons/language-server-test-harness/src/main/java/org/springframework/ide/vscode/languageserver/testharness/LanguageServerHarness.java @@ -104,10 +104,13 @@ import org.eclipse.lsp4j.WorkspaceEditCapabilities; import org.eclipse.lsp4j.WorkspaceSymbolParams; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.eclipse.lsp4j.services.LanguageClientAware; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits; import org.springframework.ide.vscode.commons.languageserver.util.LanguageServerTestListener; import org.springframework.ide.vscode.commons.languageserver.util.Settings; import org.springframework.ide.vscode.commons.languageserver.util.SimpleLanguageServer; +import org.springframework.ide.vscode.commons.languageserver.util.SimpleWorkspaceService; import org.springframework.ide.vscode.commons.protocol.CursorMovement; import org.springframework.ide.vscode.commons.protocol.HighlightParams; import org.springframework.ide.vscode.commons.protocol.ProgressParams; @@ -138,6 +141,8 @@ import reactor.core.publisher.Mono; public class LanguageServerHarness { //Warning this 'harness' is incomplete. Growing it as needed. + + private static Logger log = LoggerFactory.getLogger(LanguageServerHarness.class); private Random random = new Random(); @@ -161,7 +166,7 @@ public class LanguageServerHarness { this.server = server; } - public static final Duration HIGHLIGHTS_TIMEOUT = Duration.ofMillis(10_000L); //Why so long? + public static final Duration HIGHLIGHTS_TIMEOUT = Duration.ofMillis(5_000L); // public static LanguageServerHarness create(String extensionId, LanguageServerInitializer initializer) throws Exception { // Callable factory = () -> { @@ -224,6 +229,7 @@ public class LanguageServerHarness { } private void receiveHighlights(HighlightParams highlights) { + log.info("highlights received: {}", highlights); Collection>requestors = ImmutableList.of(); synchronized (this) { String uri = highlights.getDoc().getUri(); @@ -515,40 +521,12 @@ public class LanguageServerHarness { } public synchronized Future getHighlightsFuture(TextDocumentInfo doc) { + log.info("highlights requested"); CompletableFuture future = new CompletableFuture(); highlights.put(doc.getUri(), future); return future; } - public HighlightParams getHighlights(TextDocumentInfo doc) throws Exception { - return getHighlights(true, doc); - } - - /** - * Set expectServerHighlights to false if NO highlights are expected from the server (for example, test cases - * that test that no highlights are received from the server because there are no running apps). - * @param expectServerHighlights false if NOT expecting any highlights from the server - * @param doc - * @return highlights, if they are expected, or null if they are not expected. - * @throws Exception - */ - public HighlightParams getHighlights(boolean expectServerHighlights, TextDocumentInfo doc) throws Exception { - try { - return getHighlightsFuture(doc).get(HIGHLIGHTS_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS); - } catch (TimeoutException e) { - // highlight requestor will timeout if the server does not send any highlights. This - // is not always an error. For example, if there are no initial running apps, the server will - // NOT send highlights (see PT 156688501), so in this case we expect to time out as part of - // the expected behaviour - if (!expectServerHighlights) { - return null; - } - else { - throw e; - } - } - } - public static Condition isDiagnosticWithSeverity(DiagnosticSeverity severity) { return new Condition<>( (d) -> d.getSeverity()==severity, diff --git a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/livehover/test/ActiveProfilesHoverTest.java b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/livehover/test/ActiveProfilesHoverTest.java index 95fc0f3d3..2b1a52d09 100644 --- a/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/livehover/test/ActiveProfilesHoverTest.java +++ b/headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/livehover/test/ActiveProfilesHoverTest.java @@ -110,8 +110,8 @@ public class ActiveProfilesHoverTest { "\n" + "}" ); + editor.assertHighlights(/*NONE*/); editor.assertHoverContains("@Profile", "Consider adding `spring-boot-actuator` as a dependency"); - editor.assertNoHighlights(); } @Test @@ -162,7 +162,7 @@ public class ActiveProfilesHoverTest { "\n" + "}" ); + editor.assertHighlights(/*NONE*/); editor.assertNoHover("@Profile"); - editor.assertNoHighlights(); } }