Skip to content

Commit 6f0fb42

Browse files
committed
Try to remove job cancellation from mock listeners
1 parent 848ec13 commit 6f0fb42

12 files changed

Lines changed: 66 additions & 109 deletions

File tree

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,24 +1149,24 @@ open class TypeParameters(val parameters: List<ClassId> = emptyList())
11491149
class WildcardTypeParameter : TypeParameters(emptyList())
11501150

11511151
/**
1152-
* Additional data describing user project.
1152+
* A context to use when no specific data is required.
11531153
*/
1154-
interface ApplicationContext
1155-
1156-
/**
1157-
* A context to use when no additional data is required.
1158-
*/
1159-
object EmptyApplicationContext: ApplicationContext
1154+
open class ApplicationContext(
1155+
val mockFrameworkInstalled: Boolean = true,
1156+
val staticsMockingIsConfigured: Boolean = true,
1157+
)
11601158

11611159
/**
11621160
* Data we get from Spring application context
11631161
* to manage engine and code generator behaviour.
11641162
*
11651163
* @param beanQualifiedNames describes fqn of injected classes
11661164
*/
1167-
data class SpringApplicationContext(
1165+
class SpringApplicationContext(
1166+
mockInstalled: Boolean,
1167+
staticsMockingIsConfigured: Boolean,
11681168
val beanQualifiedNames: List<String> = emptyList(),
1169-
): ApplicationContext {
1169+
): ApplicationContext(mockInstalled, staticsMockingIsConfigured) {
11701170
private val springInjectedClasses: List<ClassId> by lazy {
11711171
beanQualifiedNames.map { fqn -> utContext.classLoader.loadClass(fqn).id }
11721172
}

utbot-framework/src/main/kotlin/org/utbot/engine/Mocks.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlinx.collections.immutable.persistentListOf
1919
import org.utbot.common.nameOfPackage
2020
import org.utbot.engine.types.OBJECT_TYPE
2121
import org.utbot.engine.util.mockListeners.MockListenerController
22+
import org.utbot.framework.plugin.api.ApplicationContext
2223
import org.utbot.framework.plugin.api.util.isInaccessibleViaReflection
2324
import soot.BooleanType
2425
import soot.RefType
@@ -156,13 +157,30 @@ class Mocker(
156157
private val hierarchy: Hierarchy,
157158
chosenClassesToMockAlways: Set<ClassId>,
158159
internal val mockListenerController: MockListenerController? = null,
160+
private val applicationContext: ApplicationContext,
159161
) {
160-
private val mocksDesired: Boolean = strategy != MockStrategy.NO_MOCKS
162+
private val mocksAreDesired: Boolean = strategy != MockStrategy.NO_MOCKS
163+
164+
fun construct(value: ObjectValue?, mockInfo: UtMockInfo): MockedObjectInfo {
165+
return value
166+
?.let {
167+
val mockingIsPossible = when (mockInfo) {
168+
is UtFieldMockInfo,
169+
is UtObjectMockInfo -> applicationContext.mockFrameworkInstalled
170+
is UtNewInstanceMockInfo,
171+
is UtStaticMethodMockInfo,
172+
is UtStaticObjectMockInfo -> applicationContext.staticsMockingIsConfigured
173+
}
174+
val mockingIsForcedAndPossible = mockAlways(it.type) && mockingIsPossible
161175

162-
fun construct(value: ObjectValue?): MockedObjectInfo =
163-
value
164-
?.let { if (mocksDesired || mockAlways(it.type)) ExpectedMock(it) else UnexpectedMock(it) }
176+
if (mocksAreDesired || mockingIsForcedAndPossible) {
177+
ExpectedMock(it)
178+
} else {
179+
UnexpectedMock(it)
180+
}
181+
}
165182
?: NoMock
183+
}
166184

167185
/**
168186
* Creates mocked instance of the [type] using mock info if it should be mocked by the mocker,
@@ -172,7 +190,7 @@ class Mocker(
172190
*/
173191
fun mock(type: RefType, mockInfo: UtMockInfo): MockedObjectInfo {
174192
val objectValue = if (shouldMock(type, mockInfo)) createMockObject(type, mockInfo) else null
175-
return construct(objectValue)
193+
return construct(objectValue, mockInfo)
176194
}
177195

178196
/**
@@ -183,7 +201,7 @@ class Mocker(
183201
mockListenerController?.onShouldMock(strategy, mockInfo)
184202

185203
val objectValue = createMockObject(type, mockInfo)
186-
return construct(objectValue)
204+
return construct(objectValue, mockInfo)
187205
}
188206

189207
/**

utbot-framework/src/main/kotlin/org/utbot/engine/Traverser.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,10 +1385,9 @@ class Traverser(
13851385

13861386
val mockedObject = mockedObjectInfo.value
13871387
if (mockedObjectInfo is UnexpectedMock) {
1388-
error("Wrong mocker configuration, it decided to mock $type object, although it is unexpected")
1388+
queuedSymbolicStateUpdates += nullEqualityConstraint.asHardConstraint()
13891389
}
13901390

1391-
13921391
if (mockedObject != null) {
13931392
queuedSymbolicStateUpdates += MemoryUpdate(mockInfos = persistentListOf(MockInfoEnriched(mockInfo)))
13941393

utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class UtBotSymbolicEngine(
105105
dependencyPaths: String,
106106
val mockStrategy: MockStrategy = NO_MOCKS,
107107
chosenClassesToMockAlways: Set<ClassId>,
108-
applicationContext: ApplicationContext?,
108+
applicationContext: ApplicationContext,
109109
private val solverTimeoutInMillis: Int = checkSolverTimeoutMillis
110110
) : UtContextInitializer() {
111111
private val graph = methodUnderTest.sootMethod.jimpleBody().apply {
@@ -129,7 +129,8 @@ class UtBotSymbolicEngine(
129129
classUnderTest,
130130
hierarchy,
131131
chosenClassesToMockAlways,
132-
MockListenerController(controller)
132+
MockListenerController(controller),
133+
applicationContext = applicationContext,
133134
)
134135

135136
fun attachMockListener(mockListener: MockListener) = mocker.mockListenerController?.attach(mockListener)

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/ForceMockListener.kt

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,17 @@ import org.utbot.framework.util.ConflictTriggers
88

99
/**
1010
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
11-
* If forced mock happened, cancels the engine job.
1211
*
1312
* Supposed to be created only if Mockito is not installed.
1413
*/
15-
class ForceMockListener private constructor(triggers: ConflictTriggers, private val shouldCancelJob: Boolean): MockListener(triggers) {
14+
class ForceMockListener private constructor(triggers: ConflictTriggers): MockListener(triggers) {
1615
override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
17-
// If force mocking happened -- сancel engine job
18-
if (shouldCancelJob) controller.job?.cancel(ForceMockCancellationException())
19-
2016
triggers[Conflict.ForceMockHappened] = true
2117
}
2218

2319
companion object {
24-
fun create(
25-
testCaseGenerator: TestCaseGenerator,
26-
conflictTriggers: ConflictTriggers,
27-
shouldCancelJob: Boolean = false,
28-
) : ForceMockListener {
29-
val listener = ForceMockListener(conflictTriggers, shouldCancelJob)
20+
fun create(testCaseGenerator: TestCaseGenerator, conflictTriggers: ConflictTriggers) : ForceMockListener {
21+
val listener = ForceMockListener(conflictTriggers)
3022
testCaseGenerator.engineActions.add { engine -> engine.attachMockListener(listener) }
3123

3224
return listener

utbot-framework/src/main/kotlin/org/utbot/engine/util/mockListeners/ForceStaticMockListener.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,17 @@ import org.utbot.framework.util.ConflictTriggers
1212

1313
/**
1414
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
15-
* If forced static mock happened, cancels the engine job.
1615
*
1716
* Supposed to be created only if Mockito inline is not installed.
1817
*/
19-
class ForceStaticMockListener private constructor(triggers: ConflictTriggers, private val shouldCancelJob: Boolean): MockListener(triggers) {
18+
class ForceStaticMockListener private constructor(triggers: ConflictTriggers): MockListener(triggers) {
2019
override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
21-
if (mockInfo is UtNewInstanceMockInfo
22-
|| mockInfo is UtStaticMethodMockInfo
23-
|| mockInfo is UtStaticObjectMockInfo) {
24-
// If force static mocking happened -- сancel engine job
25-
if (shouldCancelJob) controller.job?.cancel(ForceStaticMockCancellationException())
26-
2720
triggers[Conflict.ForceStaticMockHappened] = true
2821
}
29-
}
3022

3123
companion object {
32-
fun create(
33-
testCaseGenerator: TestCaseGenerator,
34-
conflictTriggers: ConflictTriggers,
35-
shouldCancelJob: Boolean = false,
36-
) : ForceStaticMockListener {
37-
val listener = ForceStaticMockListener(conflictTriggers, shouldCancelJob)
24+
fun create(testCaseGenerator: TestCaseGenerator, conflictTriggers: ConflictTriggers) : ForceStaticMockListener {
25+
val listener = ForceStaticMockListener(conflictTriggers)
3826
testCaseGenerator.engineActions.add { engine -> engine.attachMockListener(listener) }
3927

4028
return listener

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ open class TestCaseGenerator(
6161
val engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
6262
val isCanceled: () -> Boolean = { false },
6363
val forceSootReload: Boolean = true,
64-
val applicationContext: ApplicationContext? = null,
64+
val applicationContext: ApplicationContext = ApplicationContext(),
6565
) {
6666
private val logger: KLogger = KotlinLogging.logger {}
6767
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")
@@ -118,7 +118,7 @@ open class TestCaseGenerator(
118118
method,
119119
mockStrategy,
120120
chosenClassesToMockAlways,
121-
applicationContext = null,
121+
applicationContext,
122122
executionTimeEstimator,
123123
)
124124
engineActions.map { engine.apply(it) }
@@ -257,7 +257,7 @@ open class TestCaseGenerator(
257257
method: ExecutableId,
258258
mockStrategyApi: MockStrategyApi,
259259
chosenClassesToMockAlways: Set<ClassId>,
260-
applicationContext: ApplicationContext?,
260+
applicationContext: ApplicationContext,
261261
executionTimeEstimator: ExecutionTimeEstimator
262262
): UtBotSymbolicEngine {
263263
logger.debug("Starting symbolic execution for $method --$mockStrategyApi--")

utbot-framework/src/main/kotlin/org/utbot/framework/process/EngineProcessMain.kt

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import kotlinx.coroutines.runBlocking
77
import mu.KotlinLogging
88
import org.utbot.analytics.AnalyticsConfigureUtil
99
import org.utbot.common.*
10-
import org.utbot.engine.util.mockListeners.ForceMockListener
11-
import org.utbot.engine.util.mockListeners.ForceStaticMockListener
1210
import org.utbot.framework.codegen.*
1311
import org.utbot.framework.codegen.domain.HangingTestsTimeout
1412
import org.utbot.framework.codegen.domain.MockitoStaticMocking
@@ -26,7 +24,6 @@ import org.utbot.framework.plugin.api.util.jClass
2624
import org.utbot.framework.plugin.api.util.method
2725
import org.utbot.framework.plugin.services.JdkInfo
2826
import org.utbot.framework.process.generated.*
29-
import org.utbot.framework.util.ConflictTriggers
3027
import org.utbot.instrumentation.instrumentation.instrumenter.Instrumenter
3128
import org.utbot.instrumentation.util.KryoHelper
3229
import org.utbot.rd.IdleWatchdog
@@ -99,19 +96,9 @@ private fun EngineProcessModel.setup(kryoHelper: KryoHelper, watchdog: IdleWatch
9996
watchdog.measureTimeForActiveCall(generate, "Generating tests") { params ->
10097
val methods: List<ExecutableId> = kryoHelper.readObject(params.methods)
10198
logger.debug().measureTime({ "starting generation for ${methods.size} methods, starting with ${methods.first()}" }) {
102-
val mockFrameworkInstalled = params.mockInstalled
103-
val conflictTriggers = ConflictTriggers(kryoHelper.readObject(params.conflictTriggers))
104-
if (!mockFrameworkInstalled) {
105-
ForceMockListener.create(testGenerator, conflictTriggers, shouldCancelJob = true)
106-
}
107-
val staticsMockingConfigured = params.staticsMockingIsConfigureda
108-
if (!staticsMockingConfigured) {
109-
ForceStaticMockListener.create(testGenerator, conflictTriggers, shouldCancelJob = true)
110-
}
111-
11299
val generateFlow = when (testGenerator.applicationContext) {
113100
is SpringApplicationContext -> defaultSpringFlow(params.generationTimeout)
114-
is EmptyApplicationContext -> testFlow {
101+
is ApplicationContext -> testFlow {
115102
generationTimeout = params.generationTimeout
116103
isSymbolicEngineEnabled = params.isSymbolicEngineEnabled
117104
isFuzzingEnabled = params.isFuzzingEnabled

0 commit comments

Comments
 (0)