Skip to content

Commit

Permalink
Merge pull request #393 from JetBrains-Research/pderakhshanfar/bug-fi…
Browse files Browse the repository at this point in the history
…x/390-better-generation-process-monitor

Fix Issue 390: Better generation process monitor
  • Loading branch information
pderakhshanfar authored Oct 15, 2024
2 parents 298662a + efbed23 commit 67ba61d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface CustomProgressIndicator {
fun isCanceled(): Boolean
fun start()
fun stop()
fun isRunning(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import com.intellij.openapi.project.Project
import org.jetbrains.research.testspark.bundles.plugin.PluginMessagesBundle
import org.jetbrains.research.testspark.core.monitor.DefaultErrorMonitor
import org.jetbrains.research.testspark.core.monitor.ErrorMonitor
import org.jetbrains.research.testspark.core.progress.CustomProgressIndicator

/**
* Manager used for monitoring the unit test generation process.
* It also limits TestSpark to generate tests only once at a time.
*/
class TestGenerationController {
private var isRunning: Boolean = false
var indicator: CustomProgressIndicator? = null

// errorMonitor is passed in many places in the project
// and reflects if any bug happened in the test generation process
Expand All @@ -26,6 +27,7 @@ class TestGenerationController {
private fun showGenerationRunningNotification(project: Project) {
val terminateButton: AnAction = object : AnAction("Terminate") {
override fun actionPerformed(e: AnActionEvent) {
indicator?.stop()
errorMonitor.notifyErrorOccurrence()
}
}
Expand All @@ -44,7 +46,11 @@ class TestGenerationController {
}

fun finished() {
isRunning = false
if (indicator != null &&
indicator!!.isRunning()
) {
indicator?.stop()
}
}

/**
Expand All @@ -53,11 +59,15 @@ class TestGenerationController {
* @return true if it is already running
*/
fun isGeneratorRunning(project: Project): Boolean {
if (isRunning) {
// If indicator is null, we have never initiated an indicator before and there is no running test generation
if (indicator == null) {
return false
}

if (indicator!!.isRunning()) {
showGenerationRunningNotification(project)
return true
}
isRunning = true
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.serviceContainer.AlreadyDisposedException
import com.intellij.ui.content.ContentManager
import org.jetbrains.research.testspark.bundles.plugin.PluginLabelsBundle
import org.jetbrains.research.testspark.bundles.plugin.PluginMessagesBundle
Expand Down Expand Up @@ -94,7 +95,8 @@ class TestSparkDisplayManager {
}
}
}

toolWindow?.hide()
try {
toolWindow?.hide()
} catch (_: AlreadyDisposedException) {} // Make sure the process continues if the tool window is already closed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class IJProgressIndicator(private val indicator: ProgressIndicator) : CustomProg

override fun isCanceled(): Boolean = indicator.isCanceled

override fun isRunning(): Boolean = indicator.isRunning

override fun start() {
indicator.start()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.research.testspark.display.generatedTests
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.serviceContainer.AlreadyDisposedException
import com.intellij.ui.content.ContentFactory
import com.intellij.ui.content.ContentManager
import org.jetbrains.research.testspark.bundles.plugin.PluginLabelsBundle
Expand Down Expand Up @@ -233,9 +234,11 @@ class GeneratedTestsTabBuilder(
* Closes the tool window by removing the content and hiding the window.
*/
private fun closeToolWindow() {
generatedTestsTabData.contentManager?.removeContent(generatedTestsTabData.content!!, true)
ToolWindowManager.getInstance(project).getToolWindow("TestSpark")?.hide()
coverageVisualisationTabBuilder.closeToolWindowTab()
try {
generatedTestsTabData.contentManager?.removeContent(generatedTestsTabData.content!!, true)
ToolWindowManager.getInstance(project).getToolWindow("TestSpark")?.hide()
coverageVisualisationTabBuilder.closeToolWindowTab()
} catch (_: AlreadyDisposedException) {} // Make sure the process continues if the tool window is already closed
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class HeadlessProgressIndicator : CustomProgressIndicator {

override fun isCanceled(): Boolean = false

override fun isRunning(): Boolean = true

override fun start() {}

override fun stop() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Pipeline(
override fun run(indicator: ProgressIndicator) {
try {
val ijIndicator = IJProgressIndicator(indicator)
testGenerationController.indicator = ijIndicator

if (ToolUtils.isProcessStopped(testGenerationController.errorMonitor, ijIndicator)) return

Expand Down

0 comments on commit 67ba61d

Please sign in to comment.