Skip to content

Commit b87b2a4

Browse files
committed
Stabilize two test that seem to often fail on the CI
Signed-off-by: azerr <azerr@redhat.com>
1 parent 3ee1cfd commit b87b2a4

6 files changed

Lines changed: 58 additions & 10 deletions

File tree

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/model/ContentModelManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,8 @@ public void forceDownloadExternalResource(String url) {
546546
cacheResolverExtension.forceDownloadExternalResource(url);
547547
}
548548

549+
public void waitForDownload(String resourceUri) {
550+
cacheResolverExtension.waitForDownload(resourceUri);
551+
}
552+
549553
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/uriresolver/XMLCacheResolverExtension.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,8 @@ public void evictCache() throws IOException {
172172
public void forceDownloadExternalResource(String url) {
173173
cacheResourcesManager.forceDownloadExternalResource(url);
174174
}
175+
176+
public void waitForDownload(String resourceUri) {
177+
cacheResourcesManager.waitForDownload(resourceUri);
178+
}
175179
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLLanguageService.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public void checkCanceled() {
9696
private final XMLLinkedEditing linkedEditing;
9797
private final XMLDocumentColor documentColor;
9898

99+
private boolean retriggerValidationWhenDownloadError;
100+
99101
public XMLLanguageService() {
100102
this.formatter = new XMLFormatter(this);
101103
this.highlighting = new XMLHighlighting(this);
@@ -115,6 +117,7 @@ public XMLLanguageService() {
115117
this.rename = new XMLRename(this);
116118
this.selectionRanges = new XMLSelectionRanges();
117119
this.linkedEditing = new XMLLinkedEditing(this);
120+
setRetriggerValidationWhenDownloadError(true);
118121
}
119122

120123
@Override
@@ -208,8 +211,15 @@ public CompletableFuture<Path> publishDiagnostics(DOMDocument xmlDocument,
208211

209212
// If there are some XSD, DTD which are downloading, wait for all download and
210213
// re-trigger the validation.
214+
if (retriggerValidationWhenDownloadError) {
211215
List<CompletableFuture<?>> futures = diagnostics.getFutures();
212216
if (!futures.isEmpty()) {
217+
try {
218+
Thread.sleep(100);
219+
} catch (InterruptedException e) {
220+
// TODO Auto-generated catch block
221+
e.printStackTrace();
222+
}
213223
CompletableFuture<Void> allFutures = CompletableFuture
214224
.allOf(futures.toArray(new CompletableFuture[futures.size()]));
215225
allFutures.thenAccept(Void -> {
@@ -219,6 +229,7 @@ public CompletableFuture<Path> publishDiagnostics(DOMDocument xmlDocument,
219229
return null;
220230
});
221231
}
232+
}
222233
return null;
223234
}
224235

@@ -334,5 +345,9 @@ public LinkedEditingRanges findLinkedEditingRanges(DOMDocument xmlDocument, Posi
334345
CancelChecker cancelChecker) {
335346
return linkedEditing.findLinkedEditingRanges(xmlDocument, position, cancelChecker);
336347
}
348+
349+
public void setRetriggerValidationWhenDownloadError(boolean retriggerValidationWhenDownloadError) {
350+
this.retriggerValidationWhenDownloadError = retriggerValidationWhenDownloadError;
351+
}
337352

338353
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/uriresolver/CacheResourcesManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Map;
3232
import java.util.Set;
3333
import java.util.concurrent.CompletableFuture;
34+
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.TimeUnit;
3536
import java.util.logging.Level;
3637
import java.util.logging.Logger;
@@ -492,4 +493,16 @@ public void forceDownloadExternalResource(String url) {
492493
private boolean isForceDownloadExternalResource(String url) {
493494
return forceDownloadExternalResources.getIfPresent(url) != null;
494495
}
496+
497+
public void waitForDownload(String resourceUri) {
498+
CompletableFuture<Path> downloadedFile = resourcesLoading.get(resourceUri);
499+
if (downloadedFile != null) {
500+
try {
501+
downloadedFile.get();
502+
} catch (InterruptedException e) {
503+
Thread.currentThread().interrupt();
504+
} catch (ExecutionException e) {
505+
}
506+
}
507+
}
495508
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ public void docTypeDownloadProblem() throws Exception {
7878
validation.setResolveExternalEntities(true);
7979

8080
XMLLanguageService ls = new XMLLanguageService();
81+
ls.setRetriggerValidationWhenDownloadError(false);
8182

8283
String xml = "<!DOCTYPE root-element PUBLIC \"public-id\" \"http://localhost:8080/sample.dtd\">\r\n" + //
8384
"<root-element>\r\n" + //
8485
"</root-element>";
8586

86-
String dtdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.dtd").toString();
87+
String resourceUri = "http://localhost:8080/sample.dtd";
88+
String dtdCachePath = CacheResourcesManager.getResourceCachePath(resourceUri).toString();
8789
String fileURI = "test.xml";
8890
// Downloading...
8991
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
@@ -96,7 +98,8 @@ public void docTypeDownloadProblem() throws Exception {
9698
new Diagnostic(r(1, 1, 1, 13), "Element type \"root-element\" must be declared.",
9799
DiagnosticSeverity.Error, "xml", DTDErrorCode.MSG_ELEMENT_NOT_DECLARED.getCode())));
98100

99-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
101+
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
102+
contentModelManager.waitForDownload(resourceUri);
100103

101104
// Downloaded error
102105
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
@@ -154,6 +157,7 @@ public void entityRefDownloadProblem() throws Exception {
154157
validation.setResolveExternalEntities(true);
155158

156159
XMLLanguageService ls = new XMLLanguageService();
160+
ls.setRetriggerValidationWhenDownloadError(false);
157161

158162
String xml = "<!DOCTYPE root-element [\r\n" + //
159163
" <!ELEMENT root-element (#PCDATA)>\r\n" + //
@@ -164,7 +168,8 @@ public void entityRefDownloadProblem() throws Exception {
164168
" &abcd;\r\n" + //
165169
"</root-element>";
166170

167-
String dtdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.dtd").toString();
171+
String resourceUri = "http://localhost:8080/sample.dtd";
172+
String dtdCachePath = CacheResourcesManager.getResourceCachePath(resourceUri).toString();
168173
String fileURI = "test.xml";
169174
// Downloading...
170175
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,
@@ -177,7 +182,8 @@ public void entityRefDownloadProblem() throws Exception {
177182
new Diagnostic(r(6, 1, 6, 7), "The entity \"abcd\" was referenced, but not declared.",
178183
DiagnosticSeverity.Error, "xml", DTDErrorCode.EntityNotDeclared.getCode())));
179184

180-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
185+
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
186+
contentModelManager.waitForDownload(resourceUri);
181187

182188
// Downloaded error
183189
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls,

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ public void noNamespaceSchemaLocationDownloadProblem() throws Exception {
8181
validation.setResolveExternalEntities(true);
8282

8383
XMLLanguageService ls = new XMLLanguageService();
84-
84+
ls.setRetriggerValidationWhenDownloadError(false);
85+
8586
String xml = "<root-element\r\n" + //
8687
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
8788
" xsi:noNamespaceSchemaLocation=\"http://localhost:8080/sample.xsd\">\r\n" + //
8889
" \r\n" + //
8990
"</root-element>";
9091

91-
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
92+
String resourceUri = "http://localhost:8080/sample.xsd";
93+
String xsdCachePath = CacheResourcesManager.getResourceCachePath(resourceUri).toString();
9294
String fileURI = "test.xml";
9395
// Downloading...
9496
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
@@ -99,7 +101,8 @@ public void noNamespaceSchemaLocationDownloadProblem() throws Exception {
99101
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
100102
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));
101103

102-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
104+
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
105+
contentModelManager.waitForDownload(resourceUri);
103106

104107
// Downloaded error
105108
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
@@ -154,15 +157,17 @@ public void schemaLocationDownloadProblem() throws Exception {
154157
validation.setResolveExternalEntities(true);
155158

156159
XMLLanguageService ls = new XMLLanguageService();
157-
160+
ls.setRetriggerValidationWhenDownloadError(false);
161+
158162
String xml = "<root-element xmlns=\"https://github.com/eclipse/lemminx\"\r\n" + //
159163
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
160164
" xsi:schemaLocation=\"\r\n" + //
161165
" https://github.com/eclipse/lemminx http://localhost:8080/sample.xsd\">\r\n" + //
162166
" \r\n" + //
163167
"</root-element>";
164168

165-
String xsdCachePath = CacheResourcesManager.getResourceCachePath("http://localhost:8080/sample.xsd").toString();
169+
String resourceUri = "http://localhost:8080/sample.xsd";
170+
String xsdCachePath = CacheResourcesManager.getResourceCachePath(resourceUri).toString();
166171
String fileURI = "test.xml";
167172
// Downloading...
168173
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,
@@ -173,7 +178,8 @@ public void schemaLocationDownloadProblem() throws Exception {
173178
new Diagnostic(r(0, 1, 0, 13), "cvc-elt.1.a: Cannot find the declaration of element 'root-element'.",
174179
DiagnosticSeverity.Error, "xml", XMLSchemaErrorCode.cvc_elt_1_a.getCode())));
175180

176-
TimeUnit.SECONDS.sleep(5); // HACK: to make the timing work on slow machines
181+
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class);
182+
contentModelManager.waitForDownload(resourceUri);
177183

178184
// Downloaded error
179185
XMLAssert.testPublishDiagnosticsFor(xml, fileURI, validation, ls, pd(fileURI,

0 commit comments

Comments
 (0)