Skip to content

Commit 2fc63ac

Browse files
Try another approach to mark hanging generated tests #371 (#554)
1 parent 9c8d0e0 commit 2fc63ac

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/Domain.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import org.utbot.framework.plugin.api.util.objectClassId
2727
import org.utbot.framework.plugin.api.util.shortArrayClassId
2828
import org.utbot.framework.plugin.api.util.voidClassId
2929
import java.io.File
30+
import org.utbot.framework.plugin.api.util.longClassId
31+
import org.utbot.framework.plugin.api.util.voidWrapperClassId
3032

3133
data class TestClassFile(val packageName: String, val imports: List<Import>, val testClass: String)
3234

@@ -425,6 +427,19 @@ object Junit5 : TestFramework("JUnit5") {
425427
simpleName = "TimeUnit"
426428
)
427429

430+
val durationClassId = BuiltinClassId(
431+
name = "Duration",
432+
canonicalName = "java.time.Duration",
433+
simpleName = "Duration"
434+
)
435+
436+
val ofMillis = builtinStaticMethodId(
437+
classId = durationClassId,
438+
name = "ofMillis",
439+
returnType = durationClassId,
440+
arguments = arrayOf(longClassId)
441+
)
442+
428443
override val testAnnotationId = BuiltinClassId(
429444
name = "$JUNIT5_PACKAGE.Test",
430445
canonicalName = "$JUNIT5_PACKAGE.Test",
@@ -462,6 +477,16 @@ object Junit5 : TestFramework("JUnit5") {
462477
)
463478
)
464479

480+
val assertTimeoutPreemptively = builtinStaticMethodId(
481+
classId = assertionsClass,
482+
name = "assertTimeoutPreemptively",
483+
returnType = voidWrapperClassId,
484+
arguments = arrayOf(
485+
durationClassId,
486+
executableClassId
487+
)
488+
)
489+
465490
val displayNameClassId = BuiltinClassId(
466491
name = "$JUNIT5_PACKAGE.DisplayName",
467492
canonicalName = "$JUNIT5_PACKAGE.DisplayName",

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,17 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
363363
return
364364
}
365365

366-
when (exception) {
367-
is TimeoutException -> {
368-
methodType = TIMEOUT
369-
writeWarningAboutTimeoutExceeding()
366+
if (shouldTestPassWithTimeoutException(execution, exception)) {
367+
writeWarningAboutTimeoutExceeding()
368+
testFrameworkManager.expectTimeout(hangingTestsTimeout.timeoutMs) {
369+
methodInvocationBlock()
370370
}
371+
methodType = TIMEOUT
372+
373+
return
374+
}
375+
376+
when (exception) {
371377
is ConcreteExecutionFailureException -> {
372378
methodType = CRASH
373379
writeWarningAboutCrash()
@@ -390,6 +396,10 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
390396
return exceptionRequiresAssert || exceptionIsExplicit
391397
}
392398

399+
private fun shouldTestPassWithTimeoutException(execution: UtExecution, exception: Throwable): Boolean {
400+
return execution.result is UtTimeoutException || exception is TimeoutException
401+
}
402+
393403
private fun writeWarningAboutTimeoutExceeding() {
394404
+CgMultilineComment(
395405
listOf(

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/TestFrameworkManager.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ internal abstract class TestFrameworkManager(val context: CgContext)
155155
// TestNg allows both approaches, we use similar to JUnit5
156156
abstract fun expectException(exception: ClassId, block: () -> Unit)
157157

158+
open fun expectTimeout(timeoutMs: Long, block: () -> Unit) {}
159+
158160
open fun setTestExecutionTimeout(timeoutMs: Long) {
159161
val timeout = CgNamedAnnotationArgument(
160162
name = timeoutArgumentName,
@@ -327,6 +329,14 @@ internal class Junit5Manager(context: CgContext) : TestFrameworkManager(context)
327329
+assertions[assertThrows](exception.toExceptionClass(), lambda)
328330
}
329331

332+
override fun expectTimeout(timeoutMs : Long, block: () -> Unit) {
333+
require(testFramework is Junit5) { "According to settings, JUnit5 was expected, but got: $testFramework" }
334+
val lambda = statementConstructor.lambda(testFramework.executableClassId) { block() }
335+
importIfNeeded(testFramework.durationClassId)
336+
val duration = CgMethodCall(null, testFramework.ofMillis, listOf(timeoutMs.resolve()))
337+
+assertions[testFramework.assertTimeoutPreemptively](duration, lambda)
338+
}
339+
330340
override fun addDisplayName(name: String) {
331341
require(testFramework is Junit5) { "According to settings, JUnit5 was expected, but got: $testFramework" }
332342
collectedTestMethodAnnotations += statementConstructor.annotation(testFramework.displayNameClassId, name)

0 commit comments

Comments
 (0)