-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FrayExtension for Junit Jupiter (#71)
- Loading branch information
Showing
14 changed files
with
235 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.pastalab.fray.junit | ||
|
||
import java.lang.reflect.Constructor | ||
import org.junit.jupiter.api.extension.* | ||
import org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled | ||
import org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled | ||
import org.pastalab.fray.core.RunContext | ||
import org.pastalab.fray.core.RuntimeDelegate | ||
import org.pastalab.fray.core.randomness.ControlledRandom | ||
import org.pastalab.fray.runtime.Delegate | ||
import org.pastalab.fray.runtime.Runtime | ||
|
||
class FrayExtension( | ||
val index: Int, | ||
val frayContext: RunContext, | ||
val frayJupiterContext: FrayJupiterContext | ||
) : BeforeEachCallback, AfterEachCallback, TestWatcher, ExecutionCondition, InvocationInterceptor { | ||
|
||
override fun <T : Any?> interceptTestClassConstructor( | ||
invocation: InvocationInterceptor.Invocation<T>, | ||
invocationContext: ReflectiveInvocationContext<Constructor<T>>, | ||
extensionContext: ExtensionContext | ||
): T { | ||
frayContext.config.currentIteration = index | ||
if (frayContext.config.currentIteration != 0) { | ||
frayContext.config.scheduler = frayContext.config.scheduler.nextIteration() | ||
frayContext.config.randomnessProvider = ControlledRandom() | ||
} | ||
Runtime.DELEGATE = RuntimeDelegate(frayContext) | ||
Runtime.start() | ||
val result = invocation.proceed() | ||
Runtime.onSkipMethod("JUnit internal") | ||
return result | ||
} | ||
|
||
override fun beforeEach(context: ExtensionContext?) { | ||
Runtime.onSkipMethodDone("JUnit internal") | ||
} | ||
|
||
override fun afterEach(context: ExtensionContext) { | ||
try { | ||
Runtime.onMainExit() | ||
} finally { | ||
Runtime.DELEGATE = Delegate() | ||
} | ||
if (frayContext.bugFound != null) { | ||
throw frayContext.bugFound!! | ||
} | ||
} | ||
|
||
override fun testFailed(context: ExtensionContext, cause: Throwable) { | ||
frayJupiterContext.bugFound = true | ||
} | ||
|
||
override fun evaluateExecutionCondition(context: ExtensionContext?): ConditionEvaluationResult { | ||
if (frayJupiterContext.bugFound) { | ||
return disabled("Bug found in previous iteration.") | ||
} | ||
return enabled("No bug found in previous iteration.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.pastalab.fray.junit | ||
|
||
class FrayJupiterContext { | ||
var bugFound = false | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.pastalab.fray.junit | ||
|
||
import java.lang.reflect.Method | ||
import java.nio.file.Paths | ||
import java.util.stream.Stream | ||
import kotlin.io.path.absolutePathString | ||
import kotlin.io.path.createTempDirectory | ||
import org.junit.jupiter.api.extension.ExtensionContext | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContext | ||
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider | ||
import org.junit.platform.commons.support.AnnotationSupport | ||
import org.junit.platform.commons.support.AnnotationSupport.isAnnotated | ||
import org.junit.platform.commons.util.Preconditions | ||
import org.pastalab.fray.core.RunContext | ||
import org.pastalab.fray.core.command.Configuration | ||
import org.pastalab.fray.core.command.ExecutionInfo | ||
import org.pastalab.fray.core.command.LambdaExecutor | ||
import org.pastalab.fray.core.randomness.ControlledRandom | ||
import org.pastalab.fray.core.scheduler.POSScheduler | ||
import org.pastalab.fray.junit.annotations.ConcurrencyTest | ||
|
||
class FrayTestExtension : TestTemplateInvocationContextProvider { | ||
val workDir = Paths.get(System.getProperty("fray.workDir", "fray/fray-report")) | ||
|
||
override fun supportsTestTemplate(context: ExtensionContext): Boolean { | ||
return isAnnotated(context.testMethod, ConcurrencyTest::class.java) | ||
} | ||
|
||
override fun provideTestTemplateInvocationContexts( | ||
context: ExtensionContext | ||
): Stream<TestTemplateInvocationContext> { | ||
val testMethod = context.requiredTestMethod | ||
val displayName = context.displayName | ||
val concurrencyTest: ConcurrencyTest = | ||
AnnotationSupport.findAnnotation( | ||
testMethod, | ||
ConcurrencyTest::class.java, | ||
) | ||
.get() | ||
val workDir = createTempDirectory(this.workDir).absolutePathString() | ||
val schedulerInfo = concurrencyTest.scheduler | ||
val config = | ||
Configuration( | ||
ExecutionInfo( | ||
LambdaExecutor {}, | ||
false, | ||
true, | ||
false, | ||
-1, | ||
), | ||
workDir, | ||
totalRepetition(concurrencyTest, testMethod), | ||
60, | ||
POSScheduler(), | ||
ControlledRandom(), | ||
false, | ||
false, | ||
true, | ||
false, | ||
false, | ||
true, | ||
) | ||
val frayContext = RunContext(config) | ||
val frayJupiterContext = FrayJupiterContext() | ||
return Stream.iterate(1, { it + 1 }) | ||
.sequential() | ||
.limit(totalRepetition(concurrencyTest, testMethod).toLong()) | ||
.map { FrayTestInvocationContext(it, displayName, frayContext, frayJupiterContext) } | ||
} | ||
|
||
private fun totalRepetition(concurrencyTest: ConcurrencyTest, method: Method): Int { | ||
val repetition = concurrencyTest.value | ||
Preconditions.condition(repetition > 0) { | ||
"Configuration error: @ConcurrencyTest on method [$method] must be declared with a positive 'value'." | ||
} | ||
return repetition | ||
} | ||
} |
Oops, something went wrong.