Skip to content

Commit 8fd774c

Browse files
committed
fix tests
1 parent 6d7e57b commit 8fd774c

File tree

12 files changed

+41
-16
lines changed

12 files changed

+41
-16
lines changed

clion-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ tasks {
196196
setScanForTestClasses(false)
197197
// Only run tests from classes that end with "Test"
198198
include("**/*Test.class")
199-
exclude("**/*BaseGenerationTestCaseTest.class")
199+
exclude("**/*BaseGenerationTestCaseTest.class", "**/*BaseBuildingTest.class")
200200
systemProperty("idea.log.debug.categories", true)
201201
systemProperty("idea.log.trace.categories", true)
202202

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/UTBotStartupActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.intellij.ide.util.RunOnceUtil
44
import com.intellij.openapi.components.service
55
import com.intellij.openapi.project.Project
66
import com.intellij.openapi.startup.StartupActivity
7+
import org.tinylog.kotlin.Logger
78
import org.utbot.cpp.clion.plugin.client.ManagedClient
89
import org.utbot.cpp.clion.plugin.settings.settings
910

@@ -12,6 +13,7 @@ class UTBotStartupActivity : StartupActivity {
1213
// We initialize Client here, so that initialization will not happen
1314
// when user issues first generation request which would cause a UI freeze.
1415
if (project.settings.storedSettings.isPluginEnabled) {
16+
Logger.info("startup activity is run: Initializing client")
1517
initializeClient(project)
1618
guessPathsOnFirstOpen(project)
1719
}

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/actions/generate/BaseGenerateTestsAction.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package org.utbot.cpp.clion.plugin.actions.generate
22

33
import com.intellij.openapi.actionSystem.AnActionEvent
44
import org.utbot.cpp.clion.plugin.actions.UTBotBaseAction
5+
import org.utbot.cpp.clion.plugin.client.ManagedClient
6+
import org.utbot.cpp.clion.plugin.utils.activeProject
57
import org.utbot.cpp.clion.plugin.utils.client
68

79
abstract class BaseGenerateTestsAction : UTBotBaseAction() {
@@ -10,7 +12,7 @@ abstract class BaseGenerateTestsAction : UTBotBaseAction() {
1012
val isDefined: Boolean = isDefined(e)
1113

1214
e.presentation.isVisible = isDefined
13-
e.presentation.isEnabled = isDefined && e.client.isServerAvailable()
15+
e.presentation.isEnabled = isDefined && ManagedClient.isConnectedToServer(e.activeProject())
1416
}
1517

1618
/**

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/client/ManagedClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.utbot.cpp.clion.plugin.client
22

33
import com.intellij.openapi.Disposable
44
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.components.serviceIfCreated
56
import com.intellij.openapi.project.Project
67
import com.intellij.openapi.util.Disposer
78
import org.utbot.cpp.clion.plugin.client.channels.GTestLogChannelImpl
@@ -26,6 +27,7 @@ class ManagedClient(val project: Project) : Disposable {
2627
private val clientId = generateClientID()
2728
private val loggingChannels = listOf<LogChannel>(GTestLogChannelImpl(project), ServerLogChannelImpl(project))
2829
val isPluginEnabled: Boolean get() = project.settings.storedSettings.isPluginEnabled
30+
2931
// if plugin is disabled then Client is null
3032
private var client: Client? =
3133
if (isPluginEnabled) createNewClient() else null
@@ -107,6 +109,8 @@ class ManagedClient(val project: Project) : Disposable {
107109
}
108110

109111
companion object {
112+
fun isConnectedToServer(project: Project) =
113+
project.serviceIfCreated<ManagedClient>()?.isServerAvailable() ?: false
110114
const val RANDOM_SEQUENCE_MAX_VALUE = 10
111115
const val RANDOM_SEQUENCE_LENGTH = 5
112116
}

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/client/channels/LogChannelImpl.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ abstract class LogChannelImpl(val project: Project) : LogChannel {
1818
abstract val name: String
1919
abstract val logLevel: String
2020

21-
val console: UTBotConsole by lazy { createConsole() }
21+
// because this function is called asynchronously (we call it from invokeOnEdt which is async)
22+
// it may be called when project is disposed or other services are disposed -- this is very unlikely in production
23+
// but possible in tests, so we do nothing if this happens
24+
private val console: UTBotConsole? by lazy { if (project.isDisposed) null else createConsole() }
2225

2326
abstract suspend fun open(stub: TestsGenServiceGrpcKt.TestsGenServiceCoroutineStub): Flow<Testgen.LogEntry>
2427
abstract suspend fun close(stub: TestsGenServiceGrpcKt.TestsGenServiceCoroutineStub)
2528

26-
abstract fun createConsole(): UTBotConsole
29+
abstract fun createConsole(): UTBotConsole?
2730

2831
override fun toString(): String = name
2932

@@ -37,6 +40,6 @@ abstract class LogChannelImpl(val project: Project) : LogChannel {
3740

3841
open(stub)
3942
.catch { cause -> logger.error { "Exception in log channel: $name \n$cause" } }
40-
.collect { invokeOnEdt { console.info(it.message) } }
43+
.collect { invokeOnEdt { console?.info(it.message) } }
4144
}
4245
}

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/client/handlers/StreamHandler.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.Flow
55
import kotlinx.coroutines.flow.cancellable
66
import kotlinx.coroutines.flow.collect
77
import kotlinx.coroutines.flow.onCompletion
8+
import org.tinylog.kotlin.Logger
89

910
/**
1011
* Base class for handling stream of server responses

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/services/OutputProvider.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ class OutputProvider(val project: Project) : Disposable {
6565
}
6666
}
6767
}
68+
69+
companion object {
70+
71+
}
6872
}

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/utbotToolWindow/UTBotToolWindowFactory.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.openapi.wm.ToolWindowFactory
99
import org.utbot.cpp.clion.plugin.UTBot
1010
import org.utbot.cpp.clion.plugin.ui.utbotToolWindow.logsToolWindow.ConsoleToolWindow
1111
import org.utbot.cpp.clion.plugin.ui.utbotToolWindow.targetToolWindow.UTBotTargetsController
12+
import org.utbot.cpp.clion.plugin.utils.invokeOnEdt
1213

1314
class UTBotToolWindowFactory : ToolWindowFactory, DumbAware {
1415
private val logger = Logger.getInstance(this::class.java)

clion-plugin/src/main/kotlin/org/utbot/cpp/clion/plugin/ui/utbotToolWindow/targetToolWindow/UTBotTargetsController.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class UTBotTargetsController(val project: Project) {
2626

2727
private var areTargetsUpToDate = false
2828
private val targetsUiModel = CollectionListModel(mutableListOf<UTBotTarget>())
29-
private val client = project.service<ManagedClient>()
3029
val targetsToolWindow: UTBotTargetsToolWindow by lazy { UTBotTargetsToolWindow(targetsUiModel, this) }
3130

3231
val targets: List<UTBotTarget>
@@ -39,12 +38,14 @@ class UTBotTargetsController(val project: Project) {
3938
fun isTargetUpToDate(path: String): Boolean = areTargetsUpToDate && targets.find { it.path == path } != null
4039

4140
fun requestTargetsFromServer() {
41+
invokeOnEdt { requestTargetsEdt() }
42+
}
43+
44+
private fun requestTargetsEdt() {
4245
areTargetsUpToDate = false
4346

44-
invokeOnEdt {
45-
targetsUiModel.removeAll()
46-
targetsToolWindow.setBusy(true)
47-
}
47+
targetsUiModel.removeAll()
48+
targetsToolWindow.setBusy(true)
4849
ProjectTargetsRequest(
4950
GrpcRequestBuilderFactory(project).createProjectTargetsRequestBuilder(),
5051
project,
@@ -76,14 +77,14 @@ class UTBotTargetsController(val project: Project) {
7677
targetsToolWindow.setBusy(false)
7778
}
7879
}).let { targetsRequest ->
79-
//todo: throw exception from client and handle it here
80+
val client = project.service<ManagedClient>()
81+
8082
if (!client.isServerAvailable()) {
8183
logger.error { "Could not request targets from server: server is unavailable!" }
82-
invokeOnEdt {
83-
targetsToolWindow.setBusy(false)
84-
}
84+
targetsToolWindow.setBusy(false)
8585
return
8686
}
87+
8788
logger.trace { "Requesting project targets from server!" }
8889
client.executeRequest(targetsRequest)
8990
}
@@ -110,7 +111,7 @@ class UTBotTargetsController(val project: Project) {
110111
if (newStatus != oldStatus) {
111112
// todo: remove this, when ci is fixed
112113
if (!ApplicationManager.getApplication().isUnitTestMode)
113-
requestTargetsFromServer()
114+
invokeOnEdt(::requestTargetsFromServer)
114115
}
115116
}
116117
}

clion-plugin/src/test/kotlin/org/utbot/cpp/clion/plugin/tests/buildingRequestsTests/BaseBuildingTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
44
import com.intellij.openapi.actionSystem.ex.ActionUtil
55
import com.intellij.openapi.project.Project
66
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
7+
import org.junit.jupiter.api.AfterEach
78
import org.junit.jupiter.api.extension.ExtendWith
89
import org.utbot.cpp.clion.plugin.SwingEdtInterceptor
910
import org.utbot.cpp.clion.plugin.actions.generate.GenerateForLineAction
@@ -41,6 +42,11 @@ open class BaseBuildingTest {
4142
}
4243
}
4344

45+
@AfterEach
46+
fun tearDown() {
47+
fixture.tearDown()
48+
}
49+
4450
// HELPER METHODS:
4551

4652
protected fun createExpectedProjectContext(remotePath: String): Testgen.ProjectContext {

clion-plugin/src/test/kotlin/org/utbot/cpp/clion/plugin/tests/integrationTests/BaseGenerationTestCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ abstract class BaseGenerationTestCase {
7474
}
7575

7676
protected fun waitForConnection(timeout: Long = 10000L) {
77+
Logger.info { "Waiting for connection to server!" }
7778
runBlocking {
7879
try {
7980
withTimeout(timeout) {
8081
while (!client.isServerAvailable()) {
8182
delay(1000L)
82-
Logger.info { "Waiting for connection to server!" }
8383
}
8484
}
8585
} catch (_: TimeoutCancellationException) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
writer = console
2+
writer.level = ${PLUGIN_LOG_LEVEL:info}
23
writer.format = {date: HH:mm:ss.SSS} | {file}: {line} [{thread}] {pipe}{level}{pipe} {message}

0 commit comments

Comments
 (0)