Skip to content

Commit

Permalink
handle exceptions in RunTracker
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Moiseenko <[email protected]>
  • Loading branch information
eupp committed Jul 24, 2023
1 parent 166941d commit 6911607
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ internal data class LincheckOptionsImpl(
reporter.logIteration(iteration + 1, scenario)
}

override fun iterationEnd(iteration: Int, failure: LincheckFailure?) {
override fun iterationEnd(iteration: Int, failure: LincheckFailure?, exception: Throwable?) {
statistics?.apply {
reporter.logIterationStatistics(
invocations = iterationsStatistics[iteration].totalInvocationsCount,
Expand Down
83 changes: 54 additions & 29 deletions src/jvm/main/org/jetbrains/kotlinx/lincheck/RunTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,83 @@ import org.jetbrains.kotlinx.lincheck.strategy.LincheckFailure

interface RunTracker {
fun runStart(name: String, options: LincheckOptions) {}
fun runEnd(name: String, failure: LincheckFailure? = null, statistics: Statistics? = null) {}

fun runEnd(
name: String,
failure: LincheckFailure? = null,
exception: Throwable? = null,
statistics: Statistics? = null
) {}

fun iterationStart(iteration: Int, scenario: ExecutionScenario) {}
fun iterationEnd(iteration: Int, failure: LincheckFailure? = null) {}

fun iterationEnd(
iteration: Int,
failure: LincheckFailure? = null,
exception: Throwable? = null,
) {}

fun invocationStart(invocation: Int) {}
fun invocationEnd(invocation: Int, failure: LincheckFailure? = null) {}

fun invocationEnd(
invocation: Int,
failure: LincheckFailure? = null,
exception: Throwable? = null,
) {}

}

inline fun RunTracker?.trackRun(
name: String,
options: LincheckOptions,
block: () -> Pair<LincheckFailure?, Statistics>
): Pair<LincheckFailure?, Statistics> {
var failure: LincheckFailure? = null
var exception: Throwable? = null
var statistics: Statistics? = null
this?.runStart(name, options)
try {
val (failure, statistics) = block()
this?.runEnd(name, failure, statistics)
return (failure to statistics)
} catch (exception: Throwable) {
// TODO: once https://github.com/JetBrains/lincheck/issues/170 is implemented,
// we can put `check(false)` here instead
this?.runEnd(name)
throw exception
return block().also {
failure = it.first
statistics = it.second
}
} catch (throwable: Throwable) {
exception = throwable
throw throwable
} finally {
this?.runEnd(name, failure, exception, statistics)
}
}

inline fun RunTracker?.trackIteration(iteration: Int, scenario: ExecutionScenario, block: () -> LincheckFailure?): LincheckFailure? {
var failure: LincheckFailure? = null
var exception: Throwable? = null
this?.iterationStart(iteration, scenario)
try {
return block().also {
this?.iterationEnd(iteration, failure = it)
failure = it
}
} catch (exception: Throwable) {
// TODO: once https://github.com/JetBrains/lincheck/issues/170 is implemented,
// we can put `check(false)` here instead
this?.iterationEnd(iteration)
throw exception
} catch (throwable: Throwable) {
exception = throwable
throw throwable
} finally {
this?.iterationEnd(iteration, failure, exception)
}
}

inline fun RunTracker?.trackInvocation(invocation: Int, block: () -> LincheckFailure?): LincheckFailure? {
var failure: LincheckFailure? = null
var exception: Throwable? = null
this?.invocationStart(invocation)
try {
return block().also {
this?.invocationEnd(invocation, failure = it)
failure = it
}
} catch (exception: Throwable) {
// TODO: once https://github.com/JetBrains/lincheck/issues/170 is implemented,
// we can put `check(false)` here instead
this?.invocationEnd(invocation)
throw exception
} catch (throwable: Throwable) {
exception = throwable
throw throwable
} finally {
this?.invocationEnd(invocation, failure, exception)
}
}

Expand All @@ -91,9 +116,9 @@ fun trackersList(trackers: List<RunTracker>): RunTracker? =
}
}

override fun runEnd(name: String, failure: LincheckFailure?, statistics: Statistics?) {
override fun runEnd(name: String, failure: LincheckFailure?, exception: Throwable?, statistics: Statistics?) {
for (tracker in trackers) {
tracker.runEnd(name, failure, statistics)
tracker.runEnd(name, failure, exception, statistics)
}
}

Expand All @@ -103,9 +128,9 @@ fun trackersList(trackers: List<RunTracker>): RunTracker? =
}
}

override fun iterationEnd(iteration: Int, failure: LincheckFailure?) {
override fun iterationEnd(iteration: Int, failure: LincheckFailure?, exception: Throwable?) {
for (tracker in trackers) {
tracker.iterationEnd(iteration, failure)
tracker.iterationEnd(iteration, failure, exception)
}
}

Expand All @@ -115,9 +140,9 @@ fun trackersList(trackers: List<RunTracker>): RunTracker? =
}
}

override fun invocationEnd(invocation: Int, failure: LincheckFailure?) {
override fun invocationEnd(invocation: Int, failure: LincheckFailure?, exception: Throwable?) {
for (tracker in trackers) {
tracker.invocationEnd(invocation, failure)
tracker.invocationEnd(invocation, failure, exception)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/jvm/main/org/jetbrains/kotlinx/lincheck/Statistics.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ internal class StatisticsTracker : Statistics, RunTracker {
warmUpFlag = false
}

override fun iterationEnd(iteration: Int, failure: LincheckFailure?) {
override fun iterationEnd(iteration: Int, failure: LincheckFailure?, exception: Throwable?) {
invocation = -1
}

Expand All @@ -181,7 +181,7 @@ internal class StatisticsTracker : Statistics, RunTracker {
lastInvocationStartTimeNano = System.nanoTime()
}

override fun invocationEnd(invocation: Int, failure: LincheckFailure?) {
override fun invocationEnd(invocation: Int, failure: LincheckFailure?, exception: Throwable?) {
val invocationTimeNano = System.nanoTime() - lastInvocationStartTimeNano
check(invocationTimeNano >= 0)
if (warmUpFlag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ abstract class AbstractLincheckTest(
runOptions[name] = options
}

override fun runEnd(name: String, failure: LincheckFailure?, statistics: Statistics?) {
override fun runEnd(name: String, failure: LincheckFailure?, exception: Throwable?, statistics: Statistics?) {
check(statistics != null)
runStatistics[name] = statistics
}
Expand Down

0 comments on commit 6911607

Please sign in to comment.