From cb0aa603f13c30964f0d9dda366baa2e4c48ddc1 Mon Sep 17 00:00:00 2001 From: Maksim Pelevin Date: Mon, 4 Jul 2022 09:11:10 +0300 Subject: [PATCH] Enable smart fuzzer for UTBot by default for a plugin #66 --- .../kotlin/org/utbot/framework/UtSettings.kt | 9 +++++++-- .../org/utbot/engine/UtBotSymbolicEngine.kt | 16 +++++++++++++--- .../plugin/api/UtBotTestCaseGenerator.kt | 5 ++++- .../examples/AbstractTestCaseGeneratorTest.kt | 1 + utbot-intellij/src/main/resources/log4j2.xml | 7 ++++++- 5 files changed, 31 insertions(+), 7 deletions(-) diff --git a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt index abc0005402..6eee9a2d39 100644 --- a/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt +++ b/utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt @@ -246,12 +246,17 @@ object UtSettings { /** * Set to true to start fuzzing if symbolic execution haven't return anything */ - var useFuzzing: Boolean by getBooleanProperty(false) + var useFuzzing: Boolean by getBooleanProperty(true) /** * Set the total attempts to improve coverage by fuzzer. */ - var fuzzingMaxAttemps: Int by getIntProperty(Int.MAX_VALUE) + var fuzzingMaxAttempts: Int by getIntProperty(Int.MAX_VALUE) + + /** + * Fuzzer tries to generate and run tests during this time. + */ + var fuzzingTimeoutInMillis: Int by getIntProperty(3_000) /** * Generate tests that treat possible overflows in arithmetic operations as errors diff --git a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt index 5e47512389..02c793d8f4 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/engine/UtBotSymbolicEngine.kt @@ -574,8 +574,13 @@ class UtBotSymbolicEngine( } - //Simple fuzzing - fun fuzzing(modelProvider: (ModelProvider) -> ModelProvider = { it }) = flow { + /** + * Run fuzzing flow. + * + * @param until is used by fuzzer to cancel all tasks if the current time is over this value + * @param modelProvider provides model values for a method + */ + fun fuzzing(until: Long = Long.MAX_VALUE, modelProvider: (ModelProvider) -> ModelProvider = { it }) = flow { val executableId = if (methodUnderTest.isConstructor) { methodUnderTest.javaConstructor!!.executableId } else { @@ -618,8 +623,13 @@ class UtBotSymbolicEngine( } val modelProviderWithFallback = modelProvider(defaultModelProviders { nextDefaultModelId++ }).withFallback(fallbackModelProvider::toModel) val coveredInstructionTracker = mutableSetOf() - var attempts = UtSettings.fuzzingMaxAttemps + var attempts = UtSettings.fuzzingMaxAttempts fuzz(methodUnderTestDescription, modelProviderWithFallback).forEach { values -> + if (System.currentTimeMillis() >= until) { + logger.info { "Fuzzing overtime: $methodUnderTest" } + return@flow + } + val initialEnvironmentModels = EnvironmentModels(thisInstance, values.map { it.model }, mapOf()) try { diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/UtBotTestCaseGenerator.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/UtBotTestCaseGenerator.kt index ac41c25900..9c078dd7e2 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/UtBotTestCaseGenerator.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/UtBotTestCaseGenerator.kt @@ -219,7 +219,10 @@ object UtBotTestCaseGenerator : TestCaseGenerator { private fun createDefaultFlow(engine: UtBotSymbolicEngine): Flow { var flow = engine.traverse() if (UtSettings.useFuzzing) { - flow = flowOf(flow, engine.fuzzing()).flattenConcat() + flow = flowOf( + engine.fuzzing(System.currentTimeMillis() + UtSettings.fuzzingTimeoutInMillis), + flow, + ).flattenConcat() } return flow } diff --git a/utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt b/utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt index ca3c345871..1ab652db79 100644 --- a/utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt +++ b/utbot-framework/src/test/kotlin/org/utbot/examples/AbstractTestCaseGeneratorTest.kt @@ -91,6 +91,7 @@ abstract class AbstractTestCaseGeneratorTest( UtSettings.substituteStaticsWithSymbolicVariable = true UtSettings.useAssembleModelGenerator = true UtSettings.saveRemainingStatesForConcreteExecution = false + UtSettings.useFuzzing = false } // checks paramsBefore and result diff --git a/utbot-intellij/src/main/resources/log4j2.xml b/utbot-intellij/src/main/resources/log4j2.xml index 4530b73674..47234d7c78 100644 --- a/utbot-intellij/src/main/resources/log4j2.xml +++ b/utbot-intellij/src/main/resources/log4j2.xml @@ -9,6 +9,11 @@ - + + + + + + \ No newline at end of file