Move coroutine tests to other stdlib-jvm tests
This commit is contained in:
@@ -61,6 +61,7 @@ dependencies {
|
||||
compile group: 'org.jetbrains', name: 'annotations', version:'13.0'
|
||||
|
||||
testCompile project(':kotlin-test:kotlin-test-junit')
|
||||
testCompile sourceSets.coroutines.output
|
||||
|
||||
coroutinesCompile sourceSets.main.output
|
||||
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("ObsoleteExperimentalCoroutines")
|
||||
|
||||
package test.coroutines.experimental.migration
|
||||
|
||||
import kotlin.coroutines.experimental.buildSequence as experimentalBuildSequence
|
||||
import kotlin.coroutines.experimental.SequenceBuilder as ExperimentalSequenceBuilder
|
||||
import kotlin.coroutines.experimental.Continuation as ExperimentalContinuation
|
||||
import kotlin.coroutines.experimental.CoroutineContext as ExperimentalCoroutineContext
|
||||
import kotlin.coroutines.experimental.AbstractCoroutineContextElement as ExperimentalAbstractCoroutineContextElement
|
||||
import kotlin.coroutines.experimental.EmptyCoroutineContext as ExperimentalEmptyCoroutineContext
|
||||
import kotlin.coroutines.experimental.ContinuationInterceptor as ExperimentalContinuationInterceptor
|
||||
import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED as EXPERIMENTAL_COROUTINE_SUSPENDED
|
||||
import kotlin.coroutines.experimental.migration.*
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.test.*
|
||||
|
||||
/**
|
||||
* Tests on coroutines migrations utilities.
|
||||
*/
|
||||
class CoroutinesMigrationTest {
|
||||
@Test
|
||||
fun testContextMigration() {
|
||||
assertTrue(EmptyCoroutineContext === ExperimentalEmptyCoroutineContext.toCoroutineContext())
|
||||
assertTrue(ExperimentalEmptyCoroutineContext === EmptyCoroutineContext.toExperimentalCoroutineContext())
|
||||
MyElement().let { e ->
|
||||
assertTrue(e === e.toExperimentalCoroutineContext().toCoroutineContext())
|
||||
val ee = MyExperimentalElement()
|
||||
val ctx = (e.toExperimentalCoroutineContext() + ee).toCoroutineContext()
|
||||
assertTrue(e === ctx[MyElement.Key])
|
||||
assertTrue(ee === ctx.toExperimentalCoroutineContext()[MyExperimentalElement.Key])
|
||||
}
|
||||
MyExperimentalElement().let { ee ->
|
||||
assertTrue(ee === ee.toCoroutineContext().toExperimentalCoroutineContext())
|
||||
val e = MyElement()
|
||||
val ctx = (ee.toCoroutineContext() + e).toExperimentalCoroutineContext()
|
||||
assertTrue(ee === ctx[MyExperimentalElement.Key])
|
||||
assertTrue(e === ctx.toCoroutineContext()[MyElement.Key])
|
||||
}
|
||||
}
|
||||
|
||||
class MyElement : AbstractCoroutineContextElement(Key) {
|
||||
companion object Key : CoroutineContext.Key<MyElement>
|
||||
}
|
||||
|
||||
class MyExperimentalElement : ExperimentalAbstractCoroutineContextElement(Key) {
|
||||
companion object Key : ExperimentalCoroutineContext.Key<MyExperimentalElement>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("ObsoleteExperimentalCoroutines")
|
||||
|
||||
package test.coroutines
|
||||
|
||||
import java.util.concurrent.Semaphore
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.coroutines.experimental.startCoroutine
|
||||
import kotlin.test.*
|
||||
|
||||
/**
|
||||
* Tests on coroutines standard library functions.
|
||||
*/
|
||||
class CoroutinesTest {
|
||||
/**
|
||||
* Makes sure that using [startCoroutine] with suspending references properly establishes intercepted context.
|
||||
*/
|
||||
@Test
|
||||
fun testStartInterceptedSuspendReference() {
|
||||
val done = Semaphore(0)
|
||||
TestDispatcher("Result").use { resumeDispatcher ->
|
||||
TestDispatcher("Context").use { contextDispatcher ->
|
||||
val switcher = DispatcherSwitcher(contextDispatcher, resumeDispatcher)
|
||||
val ref = switcher::run // callable reference
|
||||
ref.startCoroutine(Continuation(contextDispatcher) { result ->
|
||||
contextDispatcher.assertThread()
|
||||
assertEquals(42, result.getOrThrow())
|
||||
done.release()
|
||||
})
|
||||
done.acquire()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DispatcherSwitcher(
|
||||
private val contextDispatcher: TestDispatcher,
|
||||
private val resumeDispatcher: TestDispatcher
|
||||
) {
|
||||
suspend fun run(): Int = suspendCoroutine { cont ->
|
||||
contextDispatcher.assertThread()
|
||||
resumeDispatcher.executor.execute {
|
||||
resumeDispatcher.assertThread()
|
||||
cont.resume(42)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER")
|
||||
|
||||
package test.coroutines
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
import kotlin.coroutines.EmptyCoroutineContext
|
||||
import kotlin.coroutines.jvm.internal.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@DebugMetadata(
|
||||
sourceFile = "test.kt",
|
||||
lineNumbers = [10, 122, 11],
|
||||
indexToLabel = [0, 0, 1, 1, 2],
|
||||
localNames = ["a", "b", "b", "c", "c"],
|
||||
spilled = ["L$1", "L$2", "L$1", "L$2", "L$1"],
|
||||
methodName = "testMethod",
|
||||
className = "SomeClass"
|
||||
)
|
||||
private class MyContinuation : BaseContinuationImpl(null) {
|
||||
override val context: CoroutineContext
|
||||
get() = EmptyCoroutineContext
|
||||
|
||||
var label = 0
|
||||
|
||||
override fun invokeSuspend(result: Result<Any?>): Any? = null
|
||||
}
|
||||
|
||||
class DebugMetadataTest {
|
||||
@Test
|
||||
fun testRuntimeDebugMetadata() {
|
||||
val myContinuation = MyContinuation()
|
||||
|
||||
myContinuation.label = 1
|
||||
assertEquals(
|
||||
StackTraceElement("SomeClass", "testMethod", "test.kt", 10),
|
||||
myContinuation.getStackTraceElement()
|
||||
)
|
||||
assertEquals(listOf("L$1", "a", "L$2", "b"), myContinuation.getSpilledVariableFieldMapping()!!.toList())
|
||||
myContinuation.label = 2
|
||||
assertEquals(
|
||||
StackTraceElement("SomeClass", "testMethod", "test.kt", 122),
|
||||
myContinuation.getStackTraceElement()
|
||||
)
|
||||
assertEquals(listOf("L$1", "b", "L$2", "c"), myContinuation.getSpilledVariableFieldMapping()!!.toList())
|
||||
myContinuation.label = 3
|
||||
assertEquals(
|
||||
StackTraceElement("SomeClass", "testMethod", "test.kt", 11),
|
||||
myContinuation.getStackTraceElement()
|
||||
)
|
||||
assertEquals(listOf("L$1", "c"), myContinuation.getSpilledVariableFieldMapping()!!.toList())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package test.coroutines
|
||||
|
||||
import java.io.Closeable
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.*
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class TestDispatcher(
|
||||
private val name: String
|
||||
) : AbstractCoroutineContextElement(ContinuationInterceptor), ContinuationInterceptor, Closeable {
|
||||
private lateinit var thread: Thread
|
||||
|
||||
val executor: ExecutorService = Executors.newSingleThreadExecutor { runnable ->
|
||||
Thread(runnable, name).also { thread = it }
|
||||
}
|
||||
|
||||
fun assertThread() {
|
||||
assertEquals(thread, Thread.currentThread())
|
||||
}
|
||||
|
||||
override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
|
||||
DispatchedContinuation(continuation)
|
||||
|
||||
override fun close() {
|
||||
executor.shutdown()
|
||||
}
|
||||
|
||||
suspend fun yield() = suspendCoroutine<Unit> { cont ->
|
||||
executor.execute {
|
||||
assertThread()
|
||||
cont.resume(Unit)
|
||||
}
|
||||
}
|
||||
|
||||
inner class DispatchedContinuation<T>(val delegate: Continuation<T>) : Continuation<T> {
|
||||
override val context: CoroutineContext = delegate.context
|
||||
|
||||
override fun resumeWith(result: Result<T>) {
|
||||
executor.execute {
|
||||
delegate.resumeWith(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user