extract FileBasedIndexSwitcher from FileBasedIndexPluginListener to use it in tests

GitOrigin-RevId: 946fb7bdef335bceab64ef51b684810a6e398478
This commit is contained in:
Dmitry Batkovich
2019-12-19 06:33:37 +00:00
committed by intellij-monorepo-bot
parent baeb68e827
commit 5632e066b2
3 changed files with 67 additions and 31 deletions
@@ -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();
@@ -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();
}
}
@@ -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();
}
}
}