Skip to content

Commit

Permalink
Stabilize two test that seem to often fail on the CI
Browse files Browse the repository at this point in the history
- Use a timeout instead of static wait times
- retry diagnostics
- use a copy of the current state

Signed-off-by: Christoph Läubrich <[email protected]>
  • Loading branch information
laeubi committed Nov 10, 2024
1 parent 0aadd16 commit c1f7c79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.lemminx.customservice.ActionableNotification;
import org.eclipse.lemminx.customservice.XMLLanguageClientAPI;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class MockXMLLanguageClient implements XMLLanguageClientAPI {
private final List<MessageParams> logMessages;

public MockXMLLanguageClient() {
publishDiagnostics = new ArrayList<>();
publishDiagnostics = new CopyOnWriteArrayList<>();
showMessages = new ArrayList<>();
logMessages = new ArrayList<>();
actionableNotifications = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ public static void testCompletionItemResolveFor(XMLLanguageService xmlLanguageSe
CompletionList resolved = new CompletionList(
list.getItems().stream() //
.map((item) -> {
return (CompletionItem) xmlLanguageService.resolveCompletionItem(item, htmlDoc,
return xmlLanguageService.resolveCompletionItem(item, htmlDoc,
sharedSettings,
() -> {
});
Expand Down Expand Up @@ -860,6 +860,24 @@ public static void testPublishDiagnosticsFor(String xml, String fileURI, XMLLang
testPublishDiagnosticsFor(xml, fileURI, null, xmlLanguageService, expected);
}

public static void testPublishDiagnosticsFor(long timeout, String xml, String fileURI,
XMLValidationRootSettings validationSettings, XMLLanguageService xmlLanguageService,
PublishDiagnosticsParams... expected) {
long deadline = System.currentTimeMillis() + timeout;
while (true) {
try {
testPublishDiagnosticsFor(xml, fileURI, validationSettings, xmlLanguageService, expected);
return;
} catch (AssertionError e) {
if (System.currentTimeMillis() < deadline) {
Thread.yield();
continue;
}
throw e;
}
}
}

public static void testPublishDiagnosticsFor(String xml, String fileURI,
XMLValidationRootSettings validationSettings,
XMLLanguageService xmlLanguageService, PublishDiagnosticsParams... expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.eclipse.lemminx.MockXMLLanguageServer;
import org.eclipse.lemminx.XMLLanguageServer;
Expand Down Expand Up @@ -81,7 +83,7 @@ public void externalDTDTest() throws InterruptedException, IOException {

Thread.sleep(threadSleepMs);

List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
assertEquals(1, actualDiagnostics.size());
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());

Expand Down Expand Up @@ -134,21 +136,30 @@ public void externalXSDTest() throws InterruptedException, IOException {

clientOpenFile(languageServer, xmlTextDocument);

Thread.sleep(threadSleepMs);

List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
assertEquals(1, actualDiagnostics.size());
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());

editFile(testXsd, 12, " maxOccurs=\"2\"/>");
didChangedWatchedFiles(languageServer, testXsd);

Thread.sleep(threadSleepMs);

actualDiagnostics = getDiagnostic(2);
assertEquals(2, actualDiagnostics.size());
assertEquals("cvc-complex-type.2.4.f", actualDiagnostics.get(1).getDiagnostics().get(0).getCode().getLeft());
}

private List<PublishDiagnosticsParams> getDiagnostic(int wanted) {
long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
while (System.currentTimeMillis() < deadline) {
List<PublishDiagnosticsParams> list = new ArrayList<>(languageServer.getPublishDiagnostics());
if (list.size() >= wanted) {
return list;
}
Thread.yield();
}
fail("Did not recived at laest " + wanted + " diagnostics withing time frame!");
return new ArrayList<>();
}

private TextDocumentItem getXMLTextDocumentItem(String filename, String xmlContents) {
String languageId = "xml";
int version = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void entityRefDownloadProblem() throws Exception {
String dtdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.dtd").toString();
String fileURI = "test.xml";
// Downloading...
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"The resource 'http://localhost:8080/sample.dtd' is downloading in the cache path '"
Expand All @@ -177,10 +177,8 @@ public void entityRefDownloadProblem() throws Exception {
new Diagnostic(r(6, 1, 6, 7), "The entity \"abcd\" was referenced, but not declared.",
DiagnosticSeverity.Error, "xml", DTDErrorCode.EntityNotDeclared.getCode())));

TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines

// Downloaded error
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
pd(fileURI,
new Diagnostic(r(2, 32, 2, 64),
"Error while downloading 'http://localhost:8080/sample.dtd' to '" + dtdCachePath
Expand Down

0 comments on commit c1f7c79

Please sign in to comment.