From bff109cea5100ede4796bbceb963b6963d100b6e Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 28 Feb 2019 14:38:27 +0100 Subject: [PATCH] unify ChooseByNamePopup testing logic into one place, decrease max waiting time to avoid waiting for many hours when something goes wrong --- .../java/navigation/ChooseByNameTest.groovy | 22 ++------------- .../util/gotoByName/ChooseByNamePopup.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/java/navigation/ChooseByNameTest.groovy b/java/java-tests/testSrc/com/intellij/java/navigation/ChooseByNameTest.groovy index 4414dc578bcf..9cdfcf85190f 100644 --- a/java/java-tests/testSrc/com/intellij/java/navigation/ChooseByNameTest.groovy +++ b/java/java-tests/testSrc/com/intellij/java/navigation/ChooseByNameTest.groovy @@ -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 calcPopupElements(ChooseByNamePopup popup, String text, boolean checkboxState = false) { - List elements = ['empty'] - def semaphore = new Semaphore(1) - popup.scheduleCalcElements(text, checkboxState, ModalityState.NON_MODAL, SelectMostRelevant.INSTANCE, { set -> - elements = set as List - semaphore.up() - } as Consumer>) - 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 calcPopupElements(ChooseByNamePopup popup, String text, boolean checkboxState = false) { + return popup.calcPopupElements(text, checkboxState) } private ChooseByNamePopup createPopup(ChooseByNameModel model, PsiElement context = null) { diff --git a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/ChooseByNamePopup.java b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/ChooseByNamePopup.java index f16f840dcf99..0428c94ba078 100644 --- a/platform/lang-impl/src/com/intellij/ide/util/gotoByName/ChooseByNamePopup.java +++ b/platform/lang-impl/src/com/intellij/ide/util/gotoByName/ChooseByNamePopup.java @@ -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 calcPopupElements(@NotNull String text, boolean checkboxState) { + List 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; + } + } \ No newline at end of file