Fix state conversion, fix tests

This commit is contained in:
Ilya Chernikov
2017-02-06 17:18:42 +01:00
parent c5bc58ad32
commit 7b2ea001c1
12 changed files with 83 additions and 91 deletions
@@ -74,6 +74,11 @@ open class AggregatedReplStageState<T1, T2>(val state1: IReplStageState<T1>, val
{
override val history: IReplStageHistory<Pair<T1, T2>> = AggregatedReplStateHistory(state1.history, state2.history, lock)
override fun <StateT : IReplStageState<*>> asState(): StateT = (state1 as? StateT) ?: (state2 as StateT) ?: super.asState()
override fun <StateT : IReplStageState<*>> asState(target: Class<out StateT>): StateT =
when {
target.isAssignableFrom(state1::class.java) -> state1 as StateT
target.isAssignableFrom(state2::class.java) -> state2 as StateT
else -> super.asState(target)
}
}
@@ -32,7 +32,7 @@ class GenericReplCompilingEvaluator(val compiler: ReplCompiler,
override fun compileAndEval(state: IReplStageState<*>, codeLine: ReplCodeLine, scriptArgs: ScriptArgsWithTypes?, invokeWrapper: InvokeWrapper?): ReplEvalResult {
return state.lock.write {
val aggregatedState = state.asState<AggregatedReplStageState<*, *>>()
val aggregatedState = state.asState(AggregatedReplStageState::class.java)
val compiled = compiler.compile(state, codeLine)
when (compiled) {
is ReplCompileResult.Error -> ReplEvalResult.Error.CompileTime(compiled.message, compiled.location)
@@ -47,7 +47,7 @@ open class GenericReplEvaluator(val baseClasspath: Iterable<File>,
scriptArgs: ScriptArgsWithTypes?,
invokeWrapper: InvokeWrapper?): ReplEvalResult {
state.lock.write {
val evalState = state.asState<GenericReplEvaluatorState>()
val evalState = state.asState(GenericReplEvaluatorState::class.java)
// val verifyHistory = compileResult.state.dropLast(1)
// val defaultHistoryActor = HistoryActions(
// effectiveHistory = evalState.history.map { it.item },
@@ -29,10 +29,11 @@ import kotlin.reflect.full.safeCast
@Suppress("unused") // used externally (kotlin.script.utils)
interface KotlinJsr223JvmInvocableScriptEngine : Invocable {
val replScriptEvaluator: ReplEvaluatorExposedInternalHistory
val state: IReplStageState<*> // The Invokable interface do not allow Context/Bindings substitution, so state is supplied via property
private fun prioritizedHistory(receiverClass: KClass<*>?, receiverInstance: Any?): List<EvalClassWithInstanceAndLoader> {
return replScriptEvaluator.lastEvaluatedScripts.map { it.second }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history ->
val evalState = state.asState(GenericReplEvaluatorState::class.java)
return evalState.history.map { it.item }.filter { it.instance != null }.reversed().ensureNotEmpty("no script ").let { history ->
if (receiverInstance != null) {
val receiverKlass = receiverClass ?: receiverInstance.javaClass.kotlin
val receiverInHistory = history.find { it.instance == receiverInstance } ?:
@@ -94,7 +95,7 @@ interface KotlinJsr223JvmInvocableScriptEngine : Invocable {
}
private fun <T : Any> proxyInterface(thiz: Any?, clasz: Class<T>?): T? {
replScriptEvaluator.lastEvaluatedScripts.ensureNotEmpty("no script")
if (state.history.size == 0) throw IllegalStateException("no script")
val priority = prioritizedHistory(thiz?.javaClass?.kotlin, thiz)
if (clasz == null) throw IllegalArgumentException("class object cannot be null")
@@ -77,7 +77,9 @@ interface IReplStageState<T> {
val lock: ReentrantReadWriteLock
fun <StateT : IReplStageState<*>> asState(): StateT = (this as? StateT) ?: throw IllegalArgumentException("$this is not an expected instance of IReplStageState")
fun <StateT : IReplStageState<*>> asState(target: Class<out StateT>): StateT =
if (target.isAssignableFrom(this::class.java)) this as StateT
else throw IllegalArgumentException("$this is not an expected instance of IReplStageState")
}
@@ -70,7 +70,7 @@ open class GenericReplChecker(
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult {
state.lock.write {
val checkerState = state.asState<GenericReplCheckerState>()
val checkerState = state.asState(GenericReplCheckerState::class.java)
val scriptFileName = makeScriptBaseName(codeLine, checkerState.generation.get())
val virtualFile =
LightVirtualFile("$scriptFileName${KotlinParserDefinition.STD_SCRIPT_EXT}", KotlinLanguage.INSTANCE, StringUtil.convertLineSeparators(codeLine.code)).apply {
@@ -47,7 +47,7 @@ open class GenericReplCompiler(disposable: Disposable,
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult {
state.lock.write {
val compilerState = state.asState<GenericReplCompilerState>()
val compilerState = state.asState(GenericReplCompilerState::class.java)
val currentGeneration = compilerState.generation.get()
@@ -26,7 +26,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock
// TODO: reduce number of ports used then SOCKET_ANY_FREE_PORT is passed (same problem with other calls)
open class KotlinRemoteReplClient(
open class KotlinRemoteReplCompilerClient(
protected val compileService: CompileService,
clientAliveFlagFile: File?,
targetPlatform: CompileService.TargetPlatform,
@@ -68,8 +68,8 @@ open class KotlinRemoteReplClient(
RemoteReplCompilerState(compileService.replCreateState(sessionId).get(), lock)
override fun check(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCheckResult =
compileService.replCheck(sessionId, state.asState<RemoteReplCompilerState>().replStateFacade.id, codeLine).get()
compileService.replCheck(sessionId, state.asState(RemoteReplCompilerState::class.java).replStateFacade.id, codeLine).get()
override fun compile(state: IReplStageState<*>, codeLine: ReplCodeLine): ReplCompileResult =
compileService.replCompile(sessionId, state.asState<RemoteReplCompilerState>().replStateFacade.id, codeLine).get()
compileService.replCompile(sessionId, state.asState(RemoteReplCompilerState::class.java).replStateFacade.id, codeLine).get()
}
@@ -151,7 +151,7 @@ open class KotlinJvmReplService(
fun createRemoteState(port: Int = portForServers): RemoteReplStateFacadeServer = statesLock.write {
val id = getValidId(stateIdCounter) { id -> states.none { it.key.id == id} }
val stateFacade = RemoteReplStateFacadeServer(id, createState().asState<GenericReplCompilerState>(), port)
val stateFacade = RemoteReplStateFacadeServer(id, createState().asState(GenericReplCompilerState::class.java), port)
states.put(stateFacade, true)
stateFacade
}
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.test.TestJdkKind
import org.junit.Test
import java.io.File
import java.net.URLClassLoader
import java.util.concurrent.locks.ReentrantReadWriteLock
class GenericReplTest : TestCase() {
@Test
@@ -44,37 +45,39 @@ class GenericReplTest : TestCase() {
listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")),
"kotlin.script.templates.standard.ScriptTemplateWithArgs")
val res1 = repl.replCompiler?.check(ReplCodeLine(0, "val x ="))
val state = repl.createState()
val res1 = repl.replCompiler.check(state, ReplCodeLine(0, "val x ="))
TestCase.assertTrue("Unexpected check results: $res1", res1 is ReplCheckResult.Incomplete)
val codeLine0 = ReplCodeLine(0, "val l1 = listOf(1 + 2)\nl1.first()")
val res2 = repl.replCompiler?.compile(codeLine0, emptyList())
val res2 = repl.replCompiler.compile(state, codeLine0)
val res2c = res2 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res2", res2c)
val res21 = repl.compiledEvaluator.eval(res2c!!)
val res21 = repl.compiledEvaluator.eval(state, res2c!!)
val res21e = res21 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res21", res21e)
TestCase.assertEquals(3, res21e!!.value)
val codeLine1 = ReplCodeLine(1, "val x = 5")
val res3 = repl.replCompiler?.compile(codeLine1, listOf(codeLine0))
val res3 = repl.replCompiler.compile(state, codeLine1)
val res3c = res3 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res3", res3c)
val res31 = repl.compiledEvaluator.eval(res3c!!)
val res31 = repl.compiledEvaluator.eval(state, res3c!!)
val res31e = res31 as? ReplEvalResult.UnitResult
TestCase.assertNotNull("Unexpected eval result: $res31", res31e)
val codeLine2 = ReplCodeLine(2, "x + 2")
val res4x = repl.replCompiler?.compile(codeLine2, listOf(codeLine1))
val res4x = repl.replCompiler.compile(state, codeLine2)
TestCase.assertNotNull("Unexpected compile result: $res4x", res4x as? ReplCompileResult.HistoryMismatch)
val res4 = repl.replCompiler?.compile(codeLine2, listOf(codeLine0, codeLine1))
val res4 = repl.replCompiler.compile(state, codeLine2)
val res4c = res4 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res4", res4c)
val res41 = repl.compiledEvaluator.eval(res4c!!)
val res41 = repl.compiledEvaluator.eval(state, res4c!!)
val res41e = res41 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res41", res41e)
TestCase.assertEquals(7, res41e!!.value)
@@ -108,22 +111,24 @@ class GenericReplTest : TestCase() {
listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")),
"kotlin.script.templates.standard.ScriptTemplateWithArgs")
val state = repl.createState()
val codeLine1 = ReplCodeLine(0, "package mypackage\n\nval x = 1\nx+2")
val res1 = repl.replCompiler?.compile(codeLine1, emptyList())
val res1 = repl.replCompiler.compile(state, codeLine1)
val res1c = res1 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res1", res1c)
val res11 = repl.compiledEvaluator.eval(res1c!!)
val res11 = repl.compiledEvaluator.eval(state, res1c!!)
val res11e = res11 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res11", res11e)
TestCase.assertEquals(3, res11e!!.value)
val codeLine2 = ReplCodeLine(1, "x+4")
val res2 = repl.replCompiler?.compile(codeLine2, listOf(codeLine1))
val res2 = repl.replCompiler.compile(state, codeLine2)
val res2c = res2 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res2", res2c)
val res21 = repl.compiledEvaluator.eval(res2c!!)
val res21 = repl.compiledEvaluator.eval(state, res2c!!)
val res21e = res21 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res21", res21e)
TestCase.assertEquals(5, res21e!!.value)
@@ -151,12 +156,15 @@ internal class TestRepl(
private val scriptDef = makeScriptDefinition(templateClasspath, templateClassName)
val replCompiler : GenericReplCompiler? by lazy {
val replCompiler : GenericReplCompiler by lazy {
GenericReplCompiler(disposable, scriptDef, configuration, PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false))
}
val compiledEvaluator: ReplEvaluator by lazy {
GenericReplEvaluator(configuration.jvmClasspathRoots, null, emptyScriptArgs, ReplRepeatingMode.NONE)
}
fun createState(lock: ReentrantReadWriteLock = ReentrantReadWriteLock()): IReplStageState<*> =
AggregatedReplStageState(replCompiler.createState(lock), compiledEvaluator.createState(lock), lock)
}
@@ -25,8 +25,6 @@ import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase
import org.jetbrains.kotlin.progress.CompilationCanceledStatus
import org.jetbrains.kotlin.script.*
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.testFramework.KtUsefulTestCase
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.net.URL
@@ -111,7 +109,7 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
}
}
assertTrue("Expecting that compilation 1 ($compileTime1 ms) is at least two times longer than compilation 2 ($compileTime2 ms)",
compileTime1 > compileTime2 * 2)
compileTime1 > compileTime2 * 2)
logFile.delete()
run("hello.run", "-cp", jar, "Hello.HelloKt")
}
@@ -536,10 +534,12 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
fun testDaemonReplLocalEvalNoParams() {
withDaemon { daemon ->
val repl = KotlinRemoteReplCompiler(daemon, null, CompileService.TargetPlatform.JVM,
classpathFromClassloader(),
ScriptWithNoParam::class.qualifiedName!!,
System.err)
val repl = KotlinRemoteReplCompilerClient(daemon!!, null, CompileService.TargetPlatform.JVM,
emptyArray(),
TestMessageCollector(),
classpathFromClassloader(),
ScriptWithNoParam::class.qualifiedName!!,
ScriptArgsWithTypes(emptyArray(), emptyArray()))
val localEvaluator = GenericReplEvaluator(emptyList(), Thread.currentThread().contextClassLoader)
@@ -550,10 +550,11 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
fun testDaemonReplLocalEvalStandardTemplate() {
withDaemon { daemon ->
val repl = KotlinRemoteReplCompiler(daemon, null, CompileService.TargetPlatform.JVM,
classpathFromClassloader(),
"kotlin.script.templates.standard.ScriptTemplateWithArgs",
System.err)
val repl = KotlinRemoteReplCompilerClient(disposable, daemon!!, null, CompileService.TargetPlatform.JVM, emptyArray(),
TestMessageCollector(),
classpathFromClassloader(),
"kotlin.script.templates.standard.ScriptTemplateWithArgs",
ScriptArgsWithTypes(emptyArray(), emptyArray()))
val localEvaluator = GenericReplEvaluator(emptyList(), Thread.currentThread().contextClassLoader,
ScriptArgsWithTypes(arrayOf(emptyArray<String>()), arrayOf(Array<String>::class)))
@@ -563,66 +564,37 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
}
}
private fun doReplTestWithLocalEval(repl: KotlinRemoteReplCompiler, localEvaluator: ReplEvaluator) {
val res0 = repl.check(ReplCodeLine(0, "val x ="))
private fun doReplTestWithLocalEval(replCompiler: KotlinRemoteReplCompilerClient, localEvaluator: ReplEvaluator) {
val compilerState = replCompiler.createState()
val evaluatorState = localEvaluator.createState()
val res0 = replCompiler.check(compilerState, ReplCodeLine(0, "val x ="))
TestCase.assertTrue("Unexpected check results: $res0", res0 is ReplCheckResult.Incomplete)
val codeLine1 = ReplCodeLine(1, "val lst = listOf(1)\nval x = 5")
val res1 = repl.compile(codeLine1, emptyList())
val res1 = replCompiler.compile(compilerState, codeLine1)
val res1c = res1 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res1", res1c)
val res11 = localEvaluator.eval(res1c!!)
val res11 = localEvaluator.eval(evaluatorState, res1c!!)
val res11e = res11 as? ReplEvalResult.UnitResult
TestCase.assertNotNull("Unexpected eval result: $res11", res11e)
val codeLine2 = ReplCodeLine(2, "x + 2")
val res2x = repl.compile(codeLine2, listOf(codeLine2))
val res2x = replCompiler.compile(compilerState, codeLine2)
TestCase.assertNotNull("Unexpected compile result: $res2x", res2x as? ReplCompileResult.HistoryMismatch)
val res2 = repl.compile(codeLine2, listOf(codeLine1))
val res2 = replCompiler.compile(compilerState, codeLine2)
val res2c = res2 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res2", res2c)
val res21 = localEvaluator.eval(res2c!!)
val res21 = localEvaluator.eval(evaluatorState, res2c!!)
val res21e = res21 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res21", res21e)
TestCase.assertEquals(7, res21e!!.value)
}
fun testDaemonReplRemoteEval() {
withDaemon { daemon ->
val evalOut = ByteArrayOutputStream()
val evalErr = ByteArrayOutputStream()
val evalIn = ByteArrayInputStream("42\n".toByteArray())
val repl = KotlinRemoteReplEvaluator(daemon, null, CompileService.TargetPlatform.JVM,
listOf(File(KotlinIntegrationTestBase.getCompilerLib(), "kotlin-runtime.jar")),
"kotlin.script.templates.standard.ScriptTemplateWithArgs",
arrayOf(emptyArray<String>()),
arrayOf(Array<String>::class.java),
System.err, evalOut, evalErr, evalIn)
val res0 = repl.check(ReplCodeLine(0, "val x ="))
TestCase.assertTrue("Unexpected check results: $res0", res0 is ReplCheckResult.Incomplete)
val codeLine1 = ReplCodeLine(1, "val x = 5")
val res1 = repl.compileAndEval(codeLine1, verifyHistory = emptyList())
val res1e = res1 as? ReplEvalResult.UnitResult
TestCase.assertNotNull("Unexpected eval result: $res1", res1e)
val codeLine2 = ReplCodeLine(2, "x + 2")
val res2x = repl.compileAndEval(codeLine2, verifyHistory = listOf(codeLine2))
TestCase.assertNotNull("Unexpected compile result: $res2x", res2x as? ReplEvalResult.HistoryMismatch)
val res2 = repl.compileAndEval(codeLine2, verifyHistory = listOf(codeLine1))
val res2e = res2 as? ReplEvalResult.ValueResult
TestCase.assertNotNull("Unexpected eval result: $res2", res2e)
TestCase.assertEquals(7, res2e!!.value)
repl.dispose()
}
}
fun testDaemonReplAutoshutdownOnIdle() {
withFlagFile(getTestName(true), ".alive") { flagFile ->
val daemonOptions = DaemonOptions(autoshutdownIdleSeconds = 1, autoshutdownUnusedSeconds = 1, shutdownDelayMilliseconds = 1, runFilesPath = File(tmpdir, getTestName(true)).absolutePath)
@@ -636,15 +608,19 @@ class CompilerDaemonTest : KotlinIntegrationTestBase() {
val daemon = KotlinCompilerClient.connectToCompileService(compilerId, flagFile, daemonJVMOptions, daemonOptions, DaemonReportingTargets(out = System.err), autostart = true)
assertNotNull("failed to connect daemon", daemon)
val repl = KotlinRemoteReplCompiler(daemon!!, null, CompileService.TargetPlatform.JVM,
classpathFromClassloader(),
ScriptWithNoParam::class.qualifiedName!!,
System.err)
val replCompiler = KotlinRemoteReplCompilerClient(daemon!!, null, CompileService.TargetPlatform.JVM,
emptyArray(),
TestMessageCollector(),
classpathFromClassloader(),
ScriptWithNoParam::class.qualifiedName!!,
ScriptArgsWithTypes(emptyArray(), emptyArray()))
val compilerState = replCompiler.createState()
// use repl compiler for >> 1s, making sure that idle/unused timeouts are not firing
for (attempts in 1..10) {
val codeLine1 = ReplCodeLine(attempts, "3 + 5")
val res1 = repl.compile(codeLine1)
val res1 = replCompiler.compile(compilerState, codeLine1)
val res1c = res1 as? ReplCompileResult.CompiledClasses
TestCase.assertNotNull("Unexpected compile result: $res1", res1c)
Thread.sleep(200)
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.cli.common.repl.*
import org.jetbrains.kotlin.daemon.client.DaemonReportMessage
import org.jetbrains.kotlin.daemon.client.DaemonReportingTargets
import org.jetbrains.kotlin.daemon.client.KotlinCompilerClient
import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplClient
import org.jetbrains.kotlin.daemon.client.KotlinRemoteReplCompilerClient
import org.jetbrains.kotlin.daemon.common.*
import org.jetbrains.kotlin.utils.PathUtil
import java.io.File
@@ -59,14 +59,14 @@ class KotlinJsr223JvmScriptEngine4Idea(
override val replCompiler: ReplCompiler by lazy {
daemon.let {
KotlinRemoteReplClient(it,
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
CompileService.TargetPlatform.JVM,
emptyArray(),
messageCollector,
templateClasspath,
templateClassName,
ScriptArgsWithTypes(emptyArray(), emptyArray()))
KotlinRemoteReplCompilerClient(it,
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
CompileService.TargetPlatform.JVM,
emptyArray(),
messageCollector,
templateClasspath,
templateClassName,
ScriptArgsWithTypes(emptyArray(), emptyArray()))
}
}