Skip to content

Commit

Permalink
Merge pull request #116 from JetBrains-Research/pderakhshanfar/improv…
Browse files Browse the repository at this point in the history
…ements/background-test-execution

Pderakhshanfar/improvements/background test execution
  • Loading branch information
arksap2002 authored Feb 12, 2024
2 parents 3b62320 + bbe54b8 commit eca88ea
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import java.awt.RenderingHints
import java.awt.Toolkit
import java.awt.datatransfer.Clipboard
import java.awt.datatransfer.StringSelection
import java.util.Queue
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.FocusManager
Expand All @@ -50,6 +51,7 @@ import javax.swing.ScrollPaneConstants
import javax.swing.SwingUtilities
import javax.swing.border.Border
import javax.swing.border.MatteBorder
import kotlin.collections.HashMap

class TestCasePanelFactory(
private val project: Project,
Expand Down Expand Up @@ -469,28 +471,52 @@ class TestCasePanelFactory(
* label in the test case upper panel, removes all highlighters from the language text field,
* and updates the UI.
*/
fun runTest() {
private fun runTest() {

if (isRemoved) return
if (!runTestButton.isEnabled) return

loadingLabel.isVisible = true
runTestButton.isEnabled = false
if (!runTestButton.isEnabled) return

SwingUtilities.invokeLater {
val newTestCase = project.service<TestStorageProcessingService>()
.processNewTestCase(
"${project.service<JavaClassBuilderService>().getClassFromTestCaseCode(testCase.testCode)}.java",
testCase.id,
testCase.testName,
testCase.testCode,
)
testCase.coveredLines = newTestCase.coveredLines
ProgressManager.getInstance()
.run(object : Task.Backgroundable(project, TestSparkBundle.message("sendingFeedback")) {
override fun run(indicator: ProgressIndicator) {
runTest(indicator)
}
})
}

testCaseCodeToListOfCoveredLines[testCase.testCode] = testCase.coveredLines
fun addTask(tasks: Queue<(ProgressIndicator) -> Unit>) {
if (isRemoved) return
if (!runTestButton.isEnabled) return

lastRunCodes[currentRequestNumber - 1] = testCase.testCode
loadingLabel.isVisible = false
loadingLabel.isVisible = true
if (!runTestButton.isEnabled) return

tasks.add { indicator ->
runTest(indicator)
}
}

private fun runTest(indicator: ProgressIndicator) {
indicator.text = "Executing ${testCase.testName}"

val newTestCase = project.service<TestStorageProcessingService>()
.processNewTestCase(
"${project.service<JavaClassBuilderService>().getClassFromTestCaseCode(testCase.testCode)}.java",
testCase.id,
testCase.testName,
testCase.testCode,
)

testCase.coveredLines = newTestCase.coveredLines

testCaseCodeToListOfCoveredLines[testCase.testCode] = testCase.coveredLines

lastRunCodes[currentRequestNumber - 1] = testCase.testCode
loadingLabel.isVisible = false
SwingUtilities.invokeLater {
updateUI()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.jetbrains.research.testspark.display

import com.intellij.openapi.components.service
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.project.Project
import org.jetbrains.research.testspark.bundles.TestSparkBundle
import org.jetbrains.research.testspark.bundles.TestSparkLabelsBundle
import org.jetbrains.research.testspark.services.TestCaseDisplayService
import java.awt.Dimension
import java.util.LinkedList
import java.util.Queue
import javax.swing.Box
import javax.swing.BoxLayout
import javax.swing.JButton
Expand Down Expand Up @@ -144,9 +149,29 @@ class TopButtonsPanelFactory(private val project: Project) {

runAllButton.isEnabled = false

// add each test generation task to queue
val tasks: Queue<(ProgressIndicator) -> Unit> = LinkedList()
for (testCasePanelFactory in testCasePanelFactories) {
// todo use doClick() function after removing JOptionPane
testCasePanelFactory.runTest()
testCasePanelFactory.addTask(tasks)
}
// run tasks one after each other
executeTasks(tasks)
}

private fun executeTasks(tasks: Queue<(ProgressIndicator) -> Unit>) {
val nextTask = tasks.poll()

nextTask?.let { task ->
ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Test execution") {
override fun run(indicator: ProgressIndicator) {
task(indicator)
}

override fun onFinished() {
super.onFinished()
executeTasks(tasks)
}
})
}
}

Expand Down

0 comments on commit eca88ea

Please sign in to comment.