unify ChooseByNamePopup testing logic into one place, decrease max waiting time

to avoid waiting for many hours when something goes wrong
This commit is contained in:
peter
2019-02-28 15:02:19 +01:00
parent 7899acf5ab
commit bff109cea5
2 changed files with 30 additions and 20 deletions
@@ -6,12 +6,8 @@ package com.intellij.java.navigation
import com.intellij.codeInsight.JavaProjectCodeInsightSettings
import com.intellij.ide.util.gotoByName.*
import com.intellij.lang.java.JavaLanguage
import com.intellij.openapi.application.ModalityState
import com.intellij.psi.*
import com.intellij.testFramework.PlatformTestUtil
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.util.Consumer
import com.intellij.util.concurrency.Semaphore
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
import static com.intellij.testFramework.EdtTestUtil.runInEdtAndWait
@@ -541,22 +537,8 @@ class Intf {
return calcPopupElements(createPopup(model), text, checkboxState)
}
static ArrayList<Object> calcPopupElements(ChooseByNamePopup popup, String text, boolean checkboxState = false) {
List<Object> elements = ['empty']
def semaphore = new Semaphore(1)
popup.scheduleCalcElements(text, checkboxState, ModalityState.NON_MODAL, SelectMostRelevant.INSTANCE, { set ->
elements = set as List<Object>
semaphore.up()
} as Consumer<Set<?>>)
def start = System.currentTimeMillis()
while (!semaphore.waitFor(10) && System.currentTimeMillis() - start < 10_000_000) {
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue()
}
if (!semaphore.waitFor(10)) {
printThreadDump()
fail()
}
return elements
static List<Object> calcPopupElements(ChooseByNamePopup popup, String text, boolean checkboxState = false) {
return popup.calcPopupElements(text, checkboxState)
}
private ChooseByNamePopup createPopup(ChooseByNameModel model, PsiElement context = null) {
@@ -1,6 +1,8 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.util.gotoByName;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import com.intellij.diagnostic.PerformanceWatcher;
import com.intellij.featureStatistics.FeatureUsageTracker;
import com.intellij.ide.ui.UISettings;
import com.intellij.openapi.Disposable;
@@ -18,11 +20,15 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.statistics.StatisticsInfo;
import com.intellij.psi.statistics.StatisticsManager;
import com.intellij.ui.ScreenUtil;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import javax.swing.*;
import java.awt.*;
@@ -465,4 +471,26 @@ public class ChooseByNamePopup extends ChooseByNameBase implements ChooseByNameP
myProject.putUserData(CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY, null);
}
}
@NotNull
@TestOnly
public List<Object> calcPopupElements(@NotNull String text, boolean checkboxState) {
List<Object> elements = ContainerUtil.newArrayList("empty");
Semaphore semaphore = new Semaphore(1);
scheduleCalcElements(text, checkboxState, ModalityState.NON_MODAL, SelectMostRelevant.INSTANCE, set -> {
elements.clear();
elements.addAll(set);
semaphore.up();
});
long start = System.currentTimeMillis();
while (!semaphore.waitFor(10) && System.currentTimeMillis() - start < 1_000_000) {
UIUtil.dispatchAllInvocationEvents();
}
if (!semaphore.waitFor(10)) {
PerformanceWatcher.dumpThreadsToConsole("Thread dump:");
throw new UncheckedTimeoutException("Too long background calculation");
}
return elements;
}
}