Skip to content

Commit 286908a

Browse files
committed
Make completion proposal calculation test time-independent #906 #907
The test cases for completion proposals in CompletionTest depend on specific timing behavior of a test proposal processor. On one hand, this is prone to errors if proper timing is not given on the test environment. On the other hand, the tests run unnecessarily long because of waiting long enough. This change replaces the fixed-time waiting of the LongRunningBarContentAssistProcessor with an explicit trigger that finalizes the content calculation as soon as some barrier is reached within the tests. Contributes to #906 and #907
1 parent 494178f commit 286908a

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

tests/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/CompletionTest.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,19 @@ public void testCompletionUsingViewerSelection() throws Exception {
125125
editor.selectAndReveal(0, 3);
126126
this.completionShell= openConentAssist();
127127
final Table completionProposalList = findCompletionSelectionControl(completionShell);
128-
waitForProposalRelatedCondition("Proposal list did not contain expected item: ABC", completionProposalList,
128+
waitForProposalRelatedCondition("Proposal list did not contain expected item 'ABC'", completionProposalList,
129129
() -> Arrays.stream(completionProposalList.getItems()).map(TableItem::getText).anyMatch("ABC"::equals), 5_000);
130130
}
131131

132-
private static void waitForProposalRelatedCondition(String errorMessage, Table completionProposalList, BooleanSupplier condition, int timeoutInMsec) {
133-
assertTrue(errorMessage, new DisplayHelper() {
132+
private static void waitForProposalRelatedCondition(String expectedListContentDescription, Table completionProposalList, BooleanSupplier condition, int timeoutInMsec) {
133+
boolean result = new DisplayHelper() {
134134
@Override
135135
protected boolean condition() {
136136
assertFalse("Completion proposal list was unexpectedly disposed", completionProposalList.isDisposed());
137137
return condition.getAsBoolean();
138138
}
139-
}.waitForCondition(completionProposalList.getDisplay(), timeoutInMsec));
139+
}.waitForCondition(completionProposalList.getDisplay(), timeoutInMsec);
140+
assertTrue(expectedListContentDescription + " but contained: " + Arrays.toString(completionProposalList.getItems()), result);
140141
}
141142

142143
@Test
@@ -177,7 +178,7 @@ private Shell openConentAssist(boolean expectShell) {
177178
*/
178179
private void checkCompletionContent(final Table completionProposalList) {
179180
// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait
180-
waitForProposalRelatedCondition("Proposal list did not show two initial items", completionProposalList,
181+
waitForProposalRelatedCondition("Proposal list should show two initial items", completionProposalList,
181182
() -> completionProposalList.getItemCount() == 2, 200);
182183
assertTrue("Missing computing info entry", isComputingInfoEntry(completionProposalList.getItem(0)));
183184
assertTrue("Missing computing info entry in proposal list", isComputingInfoEntry(completionProposalList.getItem(0)));
@@ -186,16 +187,17 @@ private void checkCompletionContent(final Table completionProposalList) {
186187
assertThat("Unexpected initial proposal item",
187188
BarContentAssistProcessor.PROPOSAL, endsWith(initialProposalString));
188189
completionProposalList.setSelection(initialProposalItem);
190+
191+
LongRunningBarContentAssistProcessor.finishComputation();
189192
// asynchronous
190-
waitForProposalRelatedCondition("Proposal list did not show two items after finishing computing", completionProposalList,
191-
() -> !isComputingInfoEntry(completionProposalList.getItem(0)) && completionProposalList.getItemCount() == 2,
192-
LongRunningBarContentAssistProcessor.DELAY + 200);
193+
waitForProposalRelatedCondition("Proposal list should contain two items", completionProposalList,
194+
() -> !isComputingInfoEntry(completionProposalList.getItem(0)) && completionProposalList.getItemCount() == 2, 5000);
193195
final TableItem firstCompletionProposalItem = completionProposalList.getItem(0);
194196
final TableItem secondCompletionProposalItem = completionProposalList.getItem(1);
195197
String firstCompletionProposalText = ((ICompletionProposal)firstCompletionProposalItem.getData()).getDisplayString();
196-
String secondCOmpletionProposalText = ((ICompletionProposal)secondCompletionProposalItem.getData()).getDisplayString();
198+
String secondCompletionProposalText = ((ICompletionProposal)secondCompletionProposalItem.getData()).getDisplayString();
197199
assertThat("Unexpected first proposal item", BarContentAssistProcessor.PROPOSAL, endsWith(firstCompletionProposalText));
198-
assertThat("Unexpected second proposal item", LongRunningBarContentAssistProcessor.PROPOSAL, endsWith(secondCOmpletionProposalText));
200+
assertThat("Unexpected second proposal item", LongRunningBarContentAssistProcessor.PROPOSAL, endsWith(secondCompletionProposalText));
199201
String selectedProposalString = ((ICompletionProposal)completionProposalList.getSelection()[0].getData()).getDisplayString();
200202
assertEquals("Addition of completion proposal should keep selection", initialProposalString, selectedProposalString);
201203
}
@@ -221,7 +223,7 @@ public void testCompletionFreeze_bug521484() throws Exception {
221223
this.completionShell=openConentAssist();
222224
final Table completionProposalList = findCompletionSelectionControl(this.completionShell);
223225
// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait
224-
waitForProposalRelatedCondition("Proposal list did not show two items", completionProposalList,
226+
waitForProposalRelatedCondition("Proposal list should show two items", completionProposalList,
225227
() -> completionProposalList.getItemCount() == 2, 200);
226228
assertTrue("Missing computing info entry", isComputingInfoEntry(completionProposalList.getItem(0)));
227229
// Some processors are long running, moving cursor can cause freeze (bug 521484)
@@ -230,7 +232,7 @@ public void testCompletionFreeze_bug521484() throws Exception {
230232
emulatePressLeftArrowKey();
231233
DisplayHelper.sleep(editor.getSite().getShell().getDisplay(), 200); //give time to process events
232234
long processingDuration = System.currentTimeMillis() - timestamp;
233-
assertTrue("UI Thread frozen for " + processingDuration + "ms", processingDuration < LongRunningBarContentAssistProcessor.DELAY);
235+
assertTrue("UI Thread frozen for " + processingDuration + "ms", processingDuration < LongRunningBarContentAssistProcessor.TIMEOUT_MSEC);
234236
}
235237

236238
@Test

tests/org.eclipse.ui.genericeditor.tests/src/org/eclipse/ui/genericeditor/tests/contributions/LongRunningBarContentAssistProcessor.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,38 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.genericeditor.tests.contributions;
1515

16+
import static org.junit.Assert.assertFalse;
17+
1618
import org.eclipse.jface.text.ITextViewer;
1719
import org.eclipse.jface.text.contentassist.ICompletionProposal;
1820

1921
public class LongRunningBarContentAssistProcessor extends BarContentAssistProcessor {
2022

2123
public static final String PROPOSAL = "bars are also good for soft drink cocktails.";
22-
public static final int DELAY = 2000;
23-
24+
public static final int TIMEOUT_MSEC = 10_000;
25+
private static boolean shouldComputationFinish = false;
26+
2427
public LongRunningBarContentAssistProcessor() {
2528
super(PROPOSAL);
2629
}
2730

2831
@Override
2932
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
33+
shouldComputationFinish = false;
3034
try {
31-
Thread.sleep(DELAY);
35+
long startExecutionTime = System.currentTimeMillis();
36+
while (!shouldComputationFinish && (System.currentTimeMillis() - startExecutionTime) < TIMEOUT_MSEC) {
37+
Thread.sleep(20);
38+
}
3239
} catch (InterruptedException e) {
33-
// TODO Auto-generated catch block
34-
e.printStackTrace();
40+
// Just finish on unexpected interrupt
3541
}
3642
return super.computeCompletionProposals(viewer, offset);
3743
}
44+
45+
public static void finishComputation() {
46+
assertFalse("Concurrent requests for finishing computation of long running content assist processor", shouldComputationFinish);
47+
shouldComputationFinish = true;
48+
}
49+
3850
}

0 commit comments

Comments
 (0)