Skip to content

Commit 46d4573

Browse files
If utBotGenerationTimeoutInMillis value is out of allowed scope, Ille… (#1455)
* If utBotGenerationTimeoutInMillis value is out of allowed scope, IllegalArgumentException is thrown down to IDEA #1438 Implement API for settings properties ranges
1 parent df40a70 commit 46d4573

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

utbot-core/src/main/kotlin/org/utbot/common/AbstractSettings.kt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ package org.utbot.common
33
import java.io.FileInputStream
44
import java.io.IOException
55
import java.util.*
6+
import kotlin.Comparator
67
import mu.KLogger
78
import org.utbot.common.PathUtil.toPath
89
import kotlin.properties.PropertyDelegateProvider
910
import kotlin.properties.ReadWriteProperty
1011
import kotlin.reflect.KProperty
1112

1213
interface SettingsContainer {
13-
fun <T> settingFor(defaultValue: T, converter: (String) -> T): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>>
14+
fun <T> settingFor(
15+
defaultValue: T,
16+
range : Triple<T, T, Comparator<T>>? = null,
17+
converter: (String) -> T
18+
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>>
1419

1520
// Returns true iff some properties have non-default values
1621
fun isCustomized() = false
@@ -73,19 +78,25 @@ class PropertiesSettingsContainer(
7378

7479
override fun <T> settingFor(
7580
defaultValue: T,
81+
range : Triple<T, T, Comparator<T>>?,
7682
converter: (String) -> T
7783
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>> {
7884
return PropertyDelegateProvider { _, property ->
7985
SettingDelegate(property) {
8086
try {
8187
properties.getProperty(property.name)?.let {
82-
val parsedValue = converter.invoke(it)
88+
var parsedValue = converter.invoke(it)
89+
range?.let {
90+
// Coerce parsed value into the specified range
91+
parsedValue = maxOf(parsedValue, range.first, range.third)
92+
parsedValue = minOf(parsedValue, range.second, range.third)
93+
}
8394
customized = customized or (parsedValue != defaultValue)
8495
return@SettingDelegate parsedValue
8596
}
8697
defaultValue
8798
} catch (e: Throwable) {
88-
logger.info(e) { e.message }
99+
logger.warn("Cannot parse value for ${property.name}, default value [$defaultValue] will be used instead") { e }
89100
defaultValue
90101
}
91102
}
@@ -130,16 +141,26 @@ abstract class AbstractSettings(
130141

131142
fun areCustomized(): Boolean = container.isCustomized()
132143

144+
protected fun <T> getProperty(
145+
defaultValue: T,
146+
range : Triple<T, T, Comparator<T>>?,
147+
converter: (String) -> T
148+
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>> where T: Comparable<T> {
149+
return container.settingFor(defaultValue, range, converter)
150+
}
151+
133152
protected fun <T> getProperty(
134153
defaultValue: T,
135154
converter: (String) -> T
136155
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>> {
137-
return container.settingFor(defaultValue, converter)
156+
return container.settingFor(defaultValue, null, converter)
138157
}
139158

140-
protected fun getBooleanProperty(defaultValue: Boolean) = getProperty(defaultValue, String::toBoolean)
141-
protected fun getIntProperty(defaultValue: Int) = getProperty(defaultValue, String::toInt)
142-
protected fun getLongProperty(defaultValue: Long) = getProperty(defaultValue, String::toLong)
159+
protected fun getBooleanProperty(defaultValue: Boolean) = getProperty(defaultValue, converter = String::toBoolean)
160+
protected fun getIntProperty(defaultValue: Int) = getProperty(defaultValue, converter = String::toInt)
161+
protected fun getIntProperty(defaultValue: Int, minValue: Int, maxValue: Int) = getProperty(defaultValue, Triple(minValue, maxValue, Comparator(Integer::compare)), String::toInt)
162+
protected fun getLongProperty(defaultValue: Long) = getProperty(defaultValue, converter = String::toLong)
163+
protected fun getLongProperty(defaultValue: Long, minValue: Long, maxValue: Long) = getProperty(defaultValue, Triple(minValue, maxValue, Comparator(Long::compareTo)), String::toLong)
143164
protected fun getStringProperty(defaultValue: String) = getProperty(defaultValue) { it }
144165
protected inline fun <reified T : Enum<T>> getEnumProperty(defaultValue: T) =
145166
getProperty(defaultValue) { enumValueOf(it) }

utbot-framework-api/src/main/kotlin/org/utbot/framework/UtSettings.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
4747
*
4848
* Set it to 0 to disable timeout.
4949
*/
50-
var checkSolverTimeoutMillis: Int by getIntProperty(1000)
50+
var checkSolverTimeoutMillis: Int by getIntProperty(1000, 0, Int.MAX_VALUE)
5151

5252
/**
5353
* Timeout for symbolic execution
5454
*
5555
*/
56-
var utBotGenerationTimeoutInMillis by getLongProperty(60000L)
56+
var utBotGenerationTimeoutInMillis by getLongProperty(60000L, 1000L, Long.MAX_VALUE)
5757

5858
/**
5959
* Random seed in path selector.
@@ -223,7 +223,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
223223
* Test related files from the temp directory that are older than [daysLimitForTempFiles]
224224
* will be removed at the beginning of the test run.
225225
*/
226-
var daysLimitForTempFiles by getIntProperty(3)
226+
var daysLimitForTempFiles by getIntProperty(3, 0, 30)
227227

228228
/**
229229
* Enables soft constraints in the engine.
@@ -244,12 +244,12 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
244244
/**
245245
* Set the total attempts to improve coverage by fuzzer.
246246
*/
247-
var fuzzingMaxAttempts: Int by getIntProperty(Int.MAX_VALUE)
247+
var fuzzingMaxAttempts: Int by getIntProperty(Int.MAX_VALUE, 0, Int.MAX_VALUE)
248248

249249
/**
250250
* Fuzzer tries to generate and run tests during this time.
251251
*/
252-
var fuzzingTimeoutInMillis: Long by getLongProperty(3_000L)
252+
var fuzzingTimeoutInMillis: Long by getLongProperty(3_000L, 0, Long.MAX_VALUE)
253253

254254
/**
255255
* Generate tests that treat possible overflows in arithmetic operations as errors
@@ -316,7 +316,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
316316
* The instrumented process JDWP agent's port of the instrumented process.
317317
* A debugger attaches to the port in order to debug the process.
318318
*/
319-
var instrumentedProcessDebugPort by getIntProperty(5006)
319+
var instrumentedProcessDebugPort by getIntProperty(5006, 0, 65535)
320320

321321
/**
322322
* Value of the suspend mode for the JDWP agent of the instrumented process.
@@ -436,7 +436,7 @@ object UtSettings : AbstractSettings(logger, defaultKeyForSettingsPath, defaultS
436436
/**
437437
* Limit for number of generated tests per method (in each region)
438438
*/
439-
var maxTestsPerMethodInRegion by getIntProperty(50)
439+
var maxTestsPerMethodInRegion by getIntProperty(50, 1, Integer.MAX_VALUE)
440440

441441
/**
442442
* Max file length for generated test file

utbot-rd/src/main/kotlin/org/utbot/rd/RdSettingsContainer.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,27 @@ class RdSettingsContainer(val logger: KLogger, val key: String, val settingsMode
2323

2424
override fun <T> settingFor(
2525
defaultValue: T,
26+
range : Triple<T, T, Comparator<T>>?,
2627
converter: (String) -> T
2728
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>> {
2829
return PropertyDelegateProvider { _, _ ->
2930
object : ReadWriteProperty<Any?, T> {
3031
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
3132
val params = SettingForArgument(key, property.name)
3233
return settingsModel.settingFor.startBlocking(params).value?.let {
33-
converter(it)
34+
try {
35+
return range?.run {
36+
// Coerce parsed value into the specified range
37+
minOf(
38+
range.second,
39+
maxOf(converter(it), range.first, range.third),
40+
range.third
41+
)
42+
} ?: converter(it)
43+
} catch (e: Exception) {
44+
logger.warn("Cannot parse value for $key, default value [$defaultValue] will be used instead") { e }
45+
defaultValue
46+
}
3447
} ?: defaultValue
3548
}
3649

0 commit comments

Comments
 (0)