From 5632e066b2d2169982c54e130a558bdfdd95ff1b Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Wed, 18 Dec 2019 15:38:49 +0300 Subject: [PATCH] extract FileBasedIndexSwitcher from FileBasedIndexPluginListener to use it in tests GitOrigin-RevId: 946fb7bdef335bceab64ef51b684810a6e398478 --- .../util/indexing/FileBasedIndexImpl.java | 2 +- .../FileBasedIndexPluginListener.java | 36 ++--------- .../util/indexing/FileBasedIndexSwitcher.java | 60 +++++++++++++++++++ 3 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexSwitcher.java diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java index 4bc99c58bb37..9793e730e39d 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexImpl.java @@ -367,7 +367,7 @@ public final class FileBasedIndexImpl extends FileBasedIndex { myRegisteredIndexes.initializeIndexes(new FileIndexDataInitialization()); } - private void waitUntilIndicesAreInitialized() { + void waitUntilIndicesAreInitialized() { if (myRegisteredIndexes == null) { // interrupt all calculation while plugin reload throw new ProcessCanceledException(); diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexPluginListener.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexPluginListener.java index 41749c119db6..a1032e9f7b24 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexPluginListener.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexPluginListener.java @@ -1,22 +1,17 @@ // Copyright 2000-2019 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.util.indexing; -import com.intellij.ide.IdeBundle; import com.intellij.ide.plugins.DynamicPluginListener; import com.intellij.ide.plugins.IdeaPluginDescriptor; -import com.intellij.openapi.progress.ProgressIndicator; -import com.intellij.openapi.project.DumbModeTask; -import com.intellij.openapi.project.DumbService; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectManager; -import com.intellij.util.concurrency.Semaphore; import org.jetbrains.annotations.NotNull; class FileBasedIndexPluginListener implements DynamicPluginListener { @NotNull - private final FileBasedIndexImpl myFileBasedIndex; + private final FileBasedIndexSwitcher mySwitcher; - FileBasedIndexPluginListener(@NotNull FileBasedIndexImpl index) {myFileBasedIndex = index;} + FileBasedIndexPluginListener(@NotNull FileBasedIndexImpl index) { + mySwitcher = new FileBasedIndexSwitcher(index); + } @Override public void beforePluginLoaded(@NotNull IdeaPluginDescriptor pluginDescriptor) { @@ -38,30 +33,11 @@ class FileBasedIndexPluginListener implements DynamicPluginListener { afterPluginSetChanged(); } - private final Semaphore mySemaphore = new Semaphore(); - private void beforePluginSetChanged() { - FileBasedIndexImpl.LOG.assertTrue(mySemaphore.isUp()); - mySemaphore.down(); - - for (Project project : ProjectManager.getInstance().getOpenProjects()) { - DumbService.getInstance(project).queueTask(new DumbModeTask() { - @Override - public void performInDumbMode(@NotNull ProgressIndicator indicator) { - indicator.setText(IdeBundle.message("progress.indexing.reload")); - mySemaphore.waitFor(); - } - }); - } - - myFileBasedIndex.performShutdown(true); - myFileBasedIndex.dropRegisteredIndexes(); + mySwitcher.turnOff(); } private void afterPluginSetChanged() { - myFileBasedIndex.initComponent(); - - FileBasedIndexImpl.LOG.assertTrue(!mySemaphore.isUp()); - mySemaphore.up(); + mySwitcher.turnOn(); } } diff --git a/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexSwitcher.java b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexSwitcher.java new file mode 100644 index 000000000000..70c47d5fadf5 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/util/indexing/FileBasedIndexSwitcher.java @@ -0,0 +1,60 @@ +// Copyright 2000-2019 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.util.indexing; + +import com.intellij.ide.IdeBundle; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.progress.ProgressIndicator; +import com.intellij.openapi.project.DumbModeTask; +import com.intellij.openapi.project.DumbService; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.project.ProjectManager; +import com.intellij.util.concurrency.Semaphore; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.TestOnly; + +public class FileBasedIndexSwitcher { + @NotNull + private final FileBasedIndexImpl myFileBasedIndex; + @NotNull + private final Semaphore mySemaphore = new Semaphore(); + + @TestOnly + public FileBasedIndexSwitcher() { + this(((FileBasedIndexImpl) FileBasedIndex.getInstance())); + } + + public FileBasedIndexSwitcher(@NotNull FileBasedIndexImpl index) { + myFileBasedIndex = index; + } + + public void turnOff() { + assert ApplicationManager.getApplication().isDispatchThread(); + FileBasedIndexImpl.LOG.assertTrue(mySemaphore.isUp()); + mySemaphore.down(); + + for (Project project : ProjectManager.getInstance().getOpenProjects()) { + DumbService.getInstance(project).queueTask(new DumbModeTask() { + @Override + public void performInDumbMode(@NotNull ProgressIndicator indicator) { + indicator.setText(IdeBundle.message("progress.indexing.reload")); + mySemaphore.waitFor(); + } + }); + } + + myFileBasedIndex.performShutdown(true); + myFileBasedIndex.dropRegisteredIndexes(); + } + + public void turnOn() { + assert ApplicationManager.getApplication().isDispatchThread(); + myFileBasedIndex.initComponent(); + + FileBasedIndexImpl.LOG.assertTrue(!mySemaphore.isUp()); + mySemaphore.up(); + + if (ApplicationManager.getApplication().isUnitTestMode()) { + myFileBasedIndex.waitUntilIndicesAreInitialized(); + } + } +}