Skip to content

Commit d0900c8

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 7e942e6 commit d0900c8

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,22 @@ private void checkCompletionContent(final Table completionProposalList) {
170170
protected boolean condition() {
171171
return completionProposalList.getItemCount() == 2;
172172
}
173-
}.waitForCondition(completionProposalList.getDisplay(), 200));
173+
}.waitForCondition(completionProposalList.getDisplay(), 5000));
174174
final TableItem computingItem = completionProposalList.getItem(0);
175175
assertTrue("Missing computing info entry", computingItem.getText().contains("Computing")); //$NON-NLS-1$ //$NON-NLS-2$
176176
TableItem completionProposalItem = completionProposalList.getItem(1);
177177
final ICompletionProposal selectedProposal = (ICompletionProposal)completionProposalItem.getData();
178178
assertTrue("Incorrect proposal content", BarContentAssistProcessor.PROPOSAL.endsWith(selectedProposal .getDisplayString()));
179179
completionProposalList.setSelection(completionProposalItem);
180+
181+
LongRunningBarContentAssistProcessor.finishComputation();
180182
// asynchronous
181183
new DisplayHelper() {
182184
@Override
183185
protected boolean condition() {
184186
return completionProposalList.getItem(0) != computingItem && completionProposalList.getItemCount() == 2;
185187
}
186-
}.waitForCondition(completionProposalList.getDisplay(), LongRunningBarContentAssistProcessor.DELAY + 200);
188+
}.waitForCondition(completionProposalList.getDisplay(), 5000);
187189
completionProposalItem = completionProposalList.getItem(0);
188190
assertTrue("Proposal content seems incorrect", BarContentAssistProcessor.PROPOSAL.endsWith(((ICompletionProposal)completionProposalItem.getData()).getDisplayString()));
189191
TableItem otherProposalItem = completionProposalList.getItem(1);
@@ -223,15 +225,15 @@ protected boolean condition() {
223225
emulatePressLeftArrowKey();
224226
DisplayHelper.sleep(editor.getSite().getShell().getDisplay(), 200); //give time to process events
225227
long processingDuration = System.currentTimeMillis() - timestamp;
226-
assertTrue("UI Thread frozen for " + processingDuration + "ms", processingDuration < LongRunningBarContentAssistProcessor.DELAY);
228+
assertTrue("UI Thread frozen for " + processingDuration + "ms", processingDuration < LongRunningBarContentAssistProcessor.TIMEOUT_MSEC);
227229
}
228230

229231
@Test
230232
public void testMoveCaretBackUsesAllProcessors_bug522255() throws Exception {
231233
testCompletion();
232234
emulatePressLeftArrowKey();
233235
final Set<Shell> beforeShells = Arrays.stream(editor.getSite().getShell().getDisplay().getShells()).filter(Shell::isVisible).collect(Collectors.toSet());
234-
DisplayHelper.sleep(editor.getSite().getShell().getDisplay(), LongRunningBarContentAssistProcessor.DELAY + 500); // adding delay is a workaround for bug521484, use only 100ms without the bug
236+
DisplayHelper.sleep(editor.getSite().getShell().getDisplay(), 200);
235237
this.completionShell= findNewShell(beforeShells, editor.getSite().getShell().getDisplay(), true);
236238
final Table completionProposalList = findCompletionSelectionControl(this.completionShell);
237239
assertEquals("Missing proposals from a Processor", 2, completionProposalList.getItemCount()); // replace with line below when #5214894 is done

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) {
3033
try {
31-
Thread.sleep(DELAY);
34+
long startExecutionTime = System.currentTimeMillis();
35+
while (!shouldComputationFinish && (System.currentTimeMillis() - startExecutionTime) < TIMEOUT_MSEC) {
36+
Thread.sleep(20);
37+
}
3238
} catch (InterruptedException e) {
33-
// TODO Auto-generated catch block
34-
e.printStackTrace();
39+
// Just finish on unexpected interrupt
3540
}
41+
shouldComputationFinish = false;
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)