Skip to content

Commit 60e28ad

Browse files
laeubicursoragent
authored andcommitted
Stabilize two tests that seem to often fail on the CI
- Use a timeout instead of static wait times - retry diagnostics - use a copy of the current state Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 7512bc2 commit 60e28ad

5 files changed

Lines changed: 52 additions & 23 deletions

File tree

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/MockXMLLanguageClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.ArrayList;
1515
import java.util.List;
1616
import java.util.concurrent.CompletableFuture;
17+
import java.util.concurrent.CopyOnWriteArrayList;
1718

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

4546
public MockXMLLanguageClient() {
46-
publishDiagnostics = new ArrayList<>();
47+
publishDiagnostics = new CopyOnWriteArrayList<>();
4748
showMessages = new ArrayList<>();
4849
logMessages = new ArrayList<>();
4950
actionableNotifications = new ArrayList<>();

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/XMLAssert.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public static void testCompletionItemResolveFor(XMLLanguageService xmlLanguageSe
634634
CompletionList resolved = new CompletionList(
635635
list.getItems().stream() //
636636
.map((item) -> {
637-
return (CompletionItem) xmlLanguageService.resolveCompletionItem(item, htmlDoc,
637+
return xmlLanguageService.resolveCompletionItem(item, htmlDoc,
638638
sharedSettings,
639639
() -> {
640640
});
@@ -860,6 +860,24 @@ public static void testPublishDiagnosticsFor(String xml, String fileURI, XMLLang
860860
testPublishDiagnosticsFor(xml, fileURI, null, xmlLanguageService, expected);
861861
}
862862

863+
public static void testPublishDiagnosticsFor(long timeout, String xml, String fileURI,
864+
XMLValidationRootSettings validationSettings, XMLLanguageService xmlLanguageService,
865+
PublishDiagnosticsParams... expected) {
866+
long deadline = System.currentTimeMillis() + timeout;
867+
while (true) {
868+
try {
869+
testPublishDiagnosticsFor(xml, fileURI, validationSettings, xmlLanguageService, expected);
870+
return;
871+
} catch (AssertionError e) {
872+
if (System.currentTimeMillis() < deadline) {
873+
Thread.yield();
874+
continue;
875+
}
876+
throw e;
877+
}
878+
}
879+
}
880+
863881
public static void testPublishDiagnosticsFor(String xml, String fileURI,
864882
XMLValidationRootSettings validationSettings,
865883
XMLLanguageService xmlLanguageService, PublishDiagnosticsParams... expected) {

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLExternalTest.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@
1313

1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.fail;
1618

1719
import java.io.File;
1820
import java.io.IOException;
1921
import java.util.ArrayList;
2022
import java.util.List;
23+
import java.util.concurrent.TimeUnit;
2124

2225
import org.eclipse.lemminx.MockXMLLanguageServer;
2326
import org.eclipse.lemminx.XMLLanguageServer;
27+
import org.eclipse.lsp4j.Diagnostic;
2428
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
2529
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
2630
import org.eclipse.lsp4j.FileChangeType;
@@ -81,15 +85,15 @@ public void externalDTDTest() throws InterruptedException, IOException {
8185

8286
Thread.sleep(threadSleepMs);
8387

84-
List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
88+
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
8589
assertEquals(1, actualDiagnostics.size());
8690
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());
8791

8892
editFile(testDtd, 2, "");
8993
didChangedWatchedFiles(languageServer, testDtd);
9094

9195
Thread.sleep(threadSleepMs);
92-
96+
actualDiagnostics = getDiagnostic(2);
9397
assertEquals(2, actualDiagnostics.size());
9498
assertFalse(actualDiagnostics.get(1).getDiagnostics().isEmpty());
9599
assertEquals("MSG_ELEMENT_NOT_DECLARED", actualDiagnostics.get(1).getDiagnostics().get(0).getCode().getLeft());
@@ -134,19 +138,31 @@ public void externalXSDTest() throws InterruptedException, IOException {
134138

135139
clientOpenFile(languageServer, xmlTextDocument);
136140

137-
Thread.sleep(threadSleepMs);
138-
139-
List<PublishDiagnosticsParams> actualDiagnostics = languageServer.getPublishDiagnostics();
141+
List<PublishDiagnosticsParams> actualDiagnostics = getDiagnostic(1);
140142
assertEquals(1, actualDiagnostics.size());
141143
assertEquals(0, actualDiagnostics.get(0).getDiagnostics().size());
142144

143145
editFile(testXsd, 12, " maxOccurs=\"2\"/>");
144146
didChangedWatchedFiles(languageServer, testXsd);
145-
146-
Thread.sleep(threadSleepMs);
147-
147+
actualDiagnostics = getDiagnostic(2);
148148
assertEquals(2, actualDiagnostics.size());
149-
assertEquals("cvc-complex-type.2.4.f", actualDiagnostics.get(1).getDiagnostics().get(0).getCode().getLeft());
149+
PublishDiagnosticsParams params = actualDiagnostics.get(1);
150+
List<Diagnostic> diagnostics = params.getDiagnostics();
151+
assertTrue(diagnostics.size() > 0);
152+
assertEquals("cvc-complex-type.2.4.f", diagnostics.get(0).getCode().getLeft());
153+
}
154+
155+
private List<PublishDiagnosticsParams> getDiagnostic(int wanted) {
156+
long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
157+
while (System.currentTimeMillis() < deadline) {
158+
List<PublishDiagnosticsParams> list = new ArrayList<>(languageServer.getPublishDiagnostics());
159+
if (list.size() >= wanted) {
160+
return list;
161+
}
162+
Thread.yield();
163+
}
164+
fail("Did not receive at least " + wanted + " diagnostics within time frame!");
165+
return new ArrayList<>();
150166
}
151167

152168
private TextDocumentItem getXMLTextDocumentItem(String filename, String xmlContents) {

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnDTDTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void entityRefDownloadProblem() throws Exception {
167167
String dtdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.dtd").toString();
168168
String fileURI = "test.xml";
169169
// Downloading...
170-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
170+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
171171
pd(fileURI,
172172
new Diagnostic(r(2, 32, 2, 64),
173173
"The resource 'http://localhost:8080/sample.dtd' is downloading in the cache path '"
@@ -177,10 +177,8 @@ public void entityRefDownloadProblem() throws Exception {
177177
new Diagnostic(r(6, 1, 6, 7), "The entity \"abcd\" was referenced, but not declared.",
178178
DiagnosticSeverity.Error, "xml", DTDErrorCode.EntityNotDeclared.getCode())));
179179

180-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
181-
182180
// Downloaded error
183-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
181+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls,
184182
pd(fileURI,
185183
new Diagnostic(r(2, 32, 2, 64),
186184
"Error while downloading 'http://localhost:8080/sample.dtd' to '" + dtdCachePath

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLValidationExternalResourcesBasedOnXSDTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.eclipse.lemminx.extensions.contentmodel.participants.XMLSchemaErrorCode;
2727
import org.eclipse.lemminx.extensions.contentmodel.participants.codeactions.DownloadDisabledResourceCodeAction;
2828
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
29-
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationRootSettings;
3029
import org.eclipse.lemminx.services.XMLLanguageService;
3130
import org.eclipse.lemminx.uriresolver.CacheResourcesManager;
3231
import org.eclipse.lsp4j.Diagnostic;
@@ -91,18 +90,17 @@ public void noNamespaceSchemaLocationDownloadProblem() throws Exception {
9190
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
9291
String fileURI = "test.xml";
9392
// Downloading...
94-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
93+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
9594
new Diagnostic(r(2, 32, 2, 64),
9695
"The resource 'http://localhost:8080/sample.xsd' is downloading in the cache path '"
9796
+ xsdCachePath + "'.",
9897
DiagnosticSeverity.Information, "xml", ExternalResourceErrorCode.DownloadingResource.getCode()),
9998
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
10099
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));
101100

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

104102
// Downloaded error
105-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
103+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
106104
new Diagnostic(r(2, 32, 2, 64),
107105
"Error while downloading 'http://localhost:8080/sample.xsd' to '" + xsdCachePath
108106
+ "' : '[java.net.ConnectException] Connection refused'.",
@@ -165,18 +163,16 @@ public void schemaLocationDownloadProblem() throws Exception {
165163
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
166164
String fileURI = "test.xml";
167165
// Downloading...
168-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
166+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
169167
new Diagnostic(r(3, 37, 3, 69),
170168
"The resource 'http://localhost:8080/sample.xsd' is downloading in the cache path '"
171169
+ xsdCachePath + "'.",
172170
DiagnosticSeverity.Information, "xml", ExternalResourceErrorCode.DownloadingResource.getCode()),
173171
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
174172
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));
175173

176-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
177-
178174
// Downloaded error
179-
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
175+
XMLAssert.testPublishDiagnosticsFor(TimeUnit.SECONDS.toMillis(5), xml, fileURI, validation, ls, pd(fileURI,
180176
new Diagnostic(r(3, 37, 3, 69),
181177
"Error while downloading 'http://localhost:8080/sample.xsd' to '" + xsdCachePath
182178
+ "' : '[java.net.ConnectException] Connection refused'.",

0 commit comments

Comments
 (0)