Disable WorkspaceEntitiesLifecycleActivity in unit tests by default

(cherry picked from commit 73934f5796d84186576ecc8b755f033dd24a7f9b)

GitOrigin-RevId: 03ca9b870763aead7cfea780c1a081db8ac2966f
This commit is contained in:
Elena Shaverdova
2024-10-16 23:18:00 +02:00
committed by intellij-monorepo-bot
parent 0ab15c65e6
commit cb73ccd751
3 changed files with 99 additions and 40 deletions

View File

@@ -87,7 +87,9 @@ import com.intellij.util.io.PersistentMapImpl;
import com.intellij.util.ref.GCUtil;
import com.intellij.util.ref.GCWatcher;
import com.intellij.util.ui.UIUtil;
import com.intellij.workspaceModel.ide.impl.WorkspaceEntityLifecycleSupporterUtils;
import com.siyeh.ig.JavaOverridingMethodUtil;
import kotlin.Unit;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -1438,7 +1440,8 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
});
}
public void test_indexes_should_be_wiped_after_scratch_removal() throws StorageException, IOException {
public void test_indexes_should_be_wiped_after_scratch_removal() {
WorkspaceEntityLifecycleSupporterUtils.INSTANCE.withAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(getProject(), () -> {
final VirtualFile file =
ScratchRootType.getInstance().createScratchFile(getProject(), "Foo.java", JavaLanguage.INSTANCE, "class Foo {}");
int fileId = ((VirtualFileWithId)file).getId();
@@ -1448,6 +1451,7 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
ID<Integer, Void> trigramId = TrigramIndex.INDEX_ID;
fileBasedIndex.ensureUpToDate(trigramId, getProject(), GlobalSearchScope.everythingScope(getProject()));
try {
assertNotEmpty(fileBasedIndex.getIndex(trigramId).getIndexedFileData(fileId).values());
WriteCommandAction.runWriteCommandAction(getProject(), (ThrowableComputable<?, IOException>)() -> {
@@ -1457,8 +1461,15 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
fileBasedIndex.ensureUpToDate(trigramId, getProject(), GlobalSearchScope.everythingScope(getProject()));
assertEmpty(fileBasedIndex.getIndex(trigramId).getIndexedFileData(fileId).values());
}
catch (StorageException | IOException e) {
throw new RuntimeException(e);
}
return Unit.INSTANCE;
});
}
public void test_requestReindex() {
WorkspaceEntityLifecycleSupporterUtils.INSTANCE.withAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(getProject(), () -> {
VirtualFile file = ScratchRootType.getInstance().createScratchFile(getProject(), "Foo.java", JavaLanguage.INSTANCE, "class Foo {}");
deleteOnTearDown(file);
@@ -1472,6 +1483,8 @@ public class IndexTest extends JavaCodeInsightFixtureTestCase {
FileBasedIndex.getInstance().getFileData(CountingFileBasedIndexExtension.getINDEX_ID(), file, getProject());
assertTrue(CountingFileBasedIndexExtension.getCOUNTER().get() > 0);
return Unit.INSTANCE;
});
}
public void test_modified_excluded_file_not_present_in_index() throws StorageException, IOException {

View File

@@ -2,6 +2,7 @@
package com.intellij.workspaceModel.ide.impl
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.extensions.ExtensionNotApplicableException
import com.intellij.openapi.extensions.ExtensionPointListener
import com.intellij.openapi.extensions.PluginDescriptor
import com.intellij.openapi.project.Project
@@ -13,13 +14,13 @@ import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
private class WorkspaceEntitiesLifecycleActivity : ProjectActivity {
override suspend fun execute(project: Project) {
init {
if (ApplicationManager.getApplication().isUnitTestMode) {
//todo GoGutterMarkerTest failed without it - ideally, we should setup project in tests without this activity
WorkspaceEntityLifecycleSupporterUtils.ensureAllEntitiesInWorkspaceAreAsProvidersDefined(project)
return
throw ExtensionNotApplicableException.create()
}
}
override suspend fun execute(project: Project) {
coroutineScope {
WorkspaceEntityLifecycleSupporter.EP_NAME.addExtensionPointListener(this, object : ExtensionPointListener<WorkspaceEntityLifecycleSupporter<out WorkspaceEntity, out WorkspaceEntity.Builder<out WorkspaceEntity>>> {
override fun extensionAdded(extension: WorkspaceEntityLifecycleSupporter<out WorkspaceEntity, out WorkspaceEntity.Builder<out WorkspaceEntity>>, pluginDescriptor: PluginDescriptor) {

View File

@@ -1,6 +1,7 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.workspaceModel.ide.impl
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.backgroundWriteAction
import com.intellij.openapi.components.serviceAsync
import com.intellij.openapi.project.Project
@@ -12,6 +13,7 @@ import com.intellij.platform.workspace.storage.MutableEntityStorage
import com.intellij.platform.workspace.storage.WorkspaceEntity
import com.intellij.platform.workspace.storage.toBuilder
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
@ApiStatus.Internal
object WorkspaceEntityLifecycleSupporterUtils {
@@ -35,6 +37,49 @@ object WorkspaceEntityLifecycleSupporterUtils {
builderRef.get()?.let { backgroundWriteAction { writeBuilder(workspaceModel, it) } }
}
@TestOnly
fun withAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(project: Project, block: () -> Unit) {
setUpAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(project)
try {
block()
}
finally {
tearDownAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(project)
}
}
@TestOnly
fun setUpAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(project: Project) {
val workspaceModel = WorkspaceModel.getInstance(project)
val snapshot = workspaceModel.currentSnapshot
val builderRef = Ref<MutableEntityStorage>()
WorkspaceEntityLifecycleSupporter.EP_NAME.forEachExtensionSafe { provider ->
ensureInitialized(project = project, provider = provider, snapshot = snapshot, builderRef = builderRef)
}
builderRef.get()?.let { ApplicationManager.getApplication().runWriteAction { writeBuilder(workspaceModel, it) } }
}
@TestOnly
fun tearDownAllEntitiesInWorkspaceFromProvidersDefinedOnEdt(project: Project) {
val workspaceModel = WorkspaceModel.getInstance(project)
val snapshot = workspaceModel.currentSnapshot
val builderRef = Ref<MutableEntityStorage>()
WorkspaceEntityLifecycleSupporter.EP_NAME.forEachExtensionSafe { provider ->
val entitiesIterator = snapshot.entities(provider.getEntityClass()).iterator()
if (entitiesIterator.hasNext()) {
var builder = builderRef.get()
if (builder == null) {
builder = snapshot.toBuilder()
builderRef.set(builder)
}
entitiesIterator.forEach { builder.removeEntity(it) }
}
}
builderRef.get()?.let { ApplicationManager.getApplication().runWriteAction { writeBuilder(workspaceModel, it) } }
}
private fun writeBuilder(workspaceModel: WorkspaceModel, builder: MutableEntityStorage) {
workspaceModel.updateProjectModel("ConstantEntitiesCheckActivity") {
it.applyChangesFrom(builder)