Skip to content

Commit

Permalink
testing: Mitigate flaky test
Browse files Browse the repository at this point in the history
I think there were always potentially multiple annotations at the expected location, and depending on luck would get the one we were expecting first.
No idea why the totally unrelated change to WorkspaceFoldersTest in the preceding commit made this test fail 100% of the time (even when run in isolation),
but it did!
  • Loading branch information
ahmedneilhussain committed May 30, 2022
1 parent aa6b0c5 commit ae94504
Showing 1 changed file with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,12 @@ protected boolean condition() {
}.waitForCondition(Display.getCurrent(), 3000);

IAnnotationModel model = sourceViewer.getAnnotationModel();
Annotation annotation = findAnnotation(sourceViewer, 14);
Assert.assertNotNull(annotation);
// System.out.println("testLinkedEditingExitPolicy: annotation found at offset 14: " + annotation.getType());
assertTrue(annotation.getType().startsWith("org.eclipse.ui.internal.workbench.texteditor.link"));
List<Annotation> annotations = findAnnotations(sourceViewer, 14);
Assert.assertFalse(annotations.isEmpty());
assertTrue(annotations.stream().anyMatch(a -> a.getType().startsWith("org.eclipse.ui.internal.workbench.texteditor.link")));
Annotation masterAnnotation = findAnnotation(sourceViewer, "org.eclipse.ui.internal.workbench.texteditor.link.master");
assertNotNull(masterAnnotation);
assertEquals(annotation, masterAnnotation);
assertTrue(annotations.contains(masterAnnotation));
org.eclipse.jface.text.Position masterPosition = model.getPosition(masterAnnotation);
assertNotNull(masterPosition);
Annotation slaveAnnotation = findAnnotation(sourceViewer, "org.eclipse.ui.internal.workbench.texteditor.link.slave");
Expand All @@ -167,7 +166,6 @@ protected boolean condition() {
while (iterator.hasNext()) {
Annotation annotation = iterator.next();
if (annotation.getType().startsWith("org.eclipse.ui.internal.workbench.texteditor.link")) {
// System.out.println("testLinkedEditingExitPolicy: Wait for annotation succeeded: " + annotation.getType());
return true;
}
}
Expand All @@ -176,26 +174,26 @@ protected boolean condition() {
}.waitForCondition(Display.getCurrent(), 3000);

model = sourceViewer.getAnnotationModel();
annotation = findAnnotation(sourceViewer, 15);
annotations = findAnnotations(sourceViewer, 15);
// No "linked" annotation is to be found at this offset
if (annotation != null) {
// System.out.println("testLinkedEditingExitPolicy: annotation found at offset 15: " + annotation.getType());
assertFalse(annotation.getType().startsWith("org.eclipse.ui.internal.workbench.texteditor.link"));
if (!annotations.isEmpty()) {
assertFalse(annotations.stream().anyMatch(a -> a.getType().startsWith("org.eclipse.ui.internal.workbench.texteditor.link")));
}
}

private Annotation findAnnotation(ISourceViewer sourceViewer, int offset) {
private List<Annotation> findAnnotations(ISourceViewer sourceViewer, int offset) {
List<Annotation> annotations = new ArrayList<>();
IAnnotationModel model = sourceViewer.getAnnotationModel();
Iterator<Annotation> iterator = model.getAnnotationIterator();
while (iterator.hasNext()) {
Annotation annotation = iterator.next();
org.eclipse.jface.text.Position position = model.getPosition(annotation);
if (position != null && position.includes(offset)) {
return annotation;
annotations.add(annotation);
}
}

return null;
return annotations;
}

private Annotation findAnnotation(ISourceViewer sourceViewer, String type) {
Expand Down

0 comments on commit ae94504

Please sign in to comment.