From 673844a05922a85d95d0698e5692f00687d8cea4 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 16 Nov 2018 23:31:33 +0300 Subject: [PATCH] Move coroutine tests to other stdlib-jvm tests --- .../jvm/test/CoroutineContextTest.kt | 100 ------------------ libraries/stdlib/jvm/build.gradle | 1 + .../coroutines}/CoroutinesMigrationTest.kt | 4 +- .../test/coroutines}/CoroutinesTest.kt | 7 +- .../test/coroutines}/DebugMetadataTest.kt | 4 +- .../test/coroutines}/TestDispatcher.kt | 2 +- .../test/coroutines/CoroutineContextTest.kt | 90 ++++++++++++++++ 7 files changed, 99 insertions(+), 109 deletions(-) delete mode 100644 libraries/stdlib/coroutines/jvm/test/CoroutineContextTest.kt rename libraries/stdlib/{coroutines/jvm/test => jvm/test/coroutines}/CoroutinesMigrationTest.kt (95%) rename libraries/stdlib/{coroutines/jvm/test => jvm/test/coroutines}/CoroutinesTest.kt (91%) rename libraries/stdlib/{coroutines/jvm/test => jvm/test/coroutines}/DebugMetadataTest.kt (97%) rename libraries/stdlib/{coroutines/jvm/test => jvm/test/coroutines}/TestDispatcher.kt (97%) diff --git a/libraries/stdlib/coroutines/jvm/test/CoroutineContextTest.kt b/libraries/stdlib/coroutines/jvm/test/CoroutineContextTest.kt deleted file mode 100644 index 52f276aba2b..00000000000 --- a/libraries/stdlib/coroutines/jvm/test/CoroutineContextTest.kt +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.kotlin.coroutines - -import kotlin.coroutines.* -import kotlin.test.* - -class CoroutineContextTest { - @Test - fun testBasicOperations() { - checkContents(EmptyCoroutineContext) - val de1 = DataElement(1) - val de2 = DataElement(2) - checkContents(de1, de1) - checkContents(de2, de2) - checkContents(de1 + de2, de2) - checkContents(de2 + de1, de1) - val oe3 = OtherElement(3) - val oe4 = OtherElement(4) - val de1oe3 = de1 + oe3 - checkContents(de1oe3, de1, oe3) - checkContents(de1oe3 + de2, de2, oe3) - checkContents(de1oe3 + oe4, de1, oe4) - checkContents(de1oe3.minusKey(DataElement), oe3) - checkContents(de1oe3.minusKey(OtherElement), de1) - } - - @Test - fun testWrapperEquality() { - val we1 = WrapperElement("1") - val we2 = WrapperElement("2") - checkContents(we1, we1) - checkContents(we2, we2) - val we1we2 = we1 + we2 - checkContents(we1we2, we1, we2) - checkContents(we1we2.minusKey(WrapperKey("1")), we2) - checkContents(we1we2.minusKey(WrapperKey("2")), we1) - } - - @Test - fun testInterceptor() { - val de1 = DataElement(1) - val oe2 = OtherElement(2) - val we3 = WrapperElement("3") - val cci = CustomContinuationInterceptor() - // Make sure it works properly with any position of ContinuationInterceptor in the context - checkContentsAndRemoves(de1 + oe2 + we3 + cci, de1, oe2, we3, cci) - checkContentsAndRemoves(de1 + oe2 + cci + we3, de1, oe2, we3, cci) - checkContentsAndRemoves(de1 + cci + oe2 + we3, de1, oe2, we3, cci) - checkContentsAndRemoves(cci + de1 + oe2 + we3, de1, oe2, we3, cci) - } - - private fun checkContentsAndRemoves(context: CoroutineContext, vararg es: CoroutineContext.Element) { - checkContents(context, *es) - for (e in es) { - checkContents(context.minusKey(e.key), *(es.toSet() - e).toTypedArray()) - } - } - - private fun checkContents(context: CoroutineContext, vararg es: CoroutineContext.Element) { - val size = context.fold(0) { c, _ -> c + 1} - assertEquals(es.size, size) - for (e in es) { - val key = e.key - assertSame(e, context[key]) - } - val set = mutableSetOf() - context.fold(set) { s, e -> s.apply { add(e) } } - assertEquals(set, es.toSet()) - when (es.size) { - 0 -> assertSame(context, EmptyCoroutineContext) - 1 -> assertSame(context, es[0]) - } - } - - class DataElement(val data: Int) : AbstractCoroutineContextElement(Key) { - companion object Key : CoroutineContext.Key - } - - class OtherElement(val data: Int) : AbstractCoroutineContextElement(Key) { - companion object Key : CoroutineContext.Key - } - - data class WrapperKey(val key: String) : CoroutineContext.Key - - class WrapperElement(key: String) : CoroutineContext.Element { - override val key = WrapperKey(key) - } - - class CustomContinuationInterceptor : ContinuationInterceptor { - override val key: CoroutineContext.Key<*> - get() = ContinuationInterceptor - - override fun interceptContinuation(continuation: Continuation): Continuation = - continuation - } -} \ No newline at end of file diff --git a/libraries/stdlib/jvm/build.gradle b/libraries/stdlib/jvm/build.gradle index 4a7c7ad2c85..714c7c34b0a 100644 --- a/libraries/stdlib/jvm/build.gradle +++ b/libraries/stdlib/jvm/build.gradle @@ -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 diff --git a/libraries/stdlib/coroutines/jvm/test/CoroutinesMigrationTest.kt b/libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt similarity index 95% rename from libraries/stdlib/coroutines/jvm/test/CoroutinesMigrationTest.kt rename to libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt index 99886f12785..e14fc61e5d9 100644 --- a/libraries/stdlib/coroutines/jvm/test/CoroutinesMigrationTest.kt +++ b/libraries/stdlib/jvm/test/coroutines/CoroutinesMigrationTest.kt @@ -3,9 +3,9 @@ * that can be found in the license/LICENSE.txt file. */ -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +@file:Suppress("ObsoleteExperimentalCoroutines") -package test.kotlin.coroutines.experimental.migration +package test.coroutines.experimental.migration import kotlin.coroutines.experimental.buildSequence as experimentalBuildSequence import kotlin.coroutines.experimental.SequenceBuilder as ExperimentalSequenceBuilder diff --git a/libraries/stdlib/coroutines/jvm/test/CoroutinesTest.kt b/libraries/stdlib/jvm/test/coroutines/CoroutinesTest.kt similarity index 91% rename from libraries/stdlib/coroutines/jvm/test/CoroutinesTest.kt rename to libraries/stdlib/jvm/test/coroutines/CoroutinesTest.kt index bda8fb1db55..a5617ad2353 100644 --- a/libraries/stdlib/coroutines/jvm/test/CoroutinesTest.kt +++ b/libraries/stdlib/jvm/test/coroutines/CoroutinesTest.kt @@ -3,11 +3,10 @@ * that can be found in the license/LICENSE.txt file. */ -package test.kotlin.coroutines +@file:Suppress("ObsoleteExperimentalCoroutines") + +package test.coroutines -import java.io.Closeable -import java.util.concurrent.ExecutorService -import java.util.concurrent.Executors import java.util.concurrent.Semaphore import kotlin.coroutines.* import kotlin.coroutines.experimental.startCoroutine diff --git a/libraries/stdlib/coroutines/jvm/test/DebugMetadataTest.kt b/libraries/stdlib/jvm/test/coroutines/DebugMetadataTest.kt similarity index 97% rename from libraries/stdlib/coroutines/jvm/test/DebugMetadataTest.kt rename to libraries/stdlib/jvm/test/coroutines/DebugMetadataTest.kt index 55f0027368d..5b7f8c600f7 100644 --- a/libraries/stdlib/coroutines/jvm/test/DebugMetadataTest.kt +++ b/libraries/stdlib/jvm/test/coroutines/DebugMetadataTest.kt @@ -5,9 +5,9 @@ @file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER") -package test.kotlin.coroutines +package test.coroutines -import org.junit.Test +import kotlin.test.Test import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.jvm.internal.* diff --git a/libraries/stdlib/coroutines/jvm/test/TestDispatcher.kt b/libraries/stdlib/jvm/test/coroutines/TestDispatcher.kt similarity index 97% rename from libraries/stdlib/coroutines/jvm/test/TestDispatcher.kt rename to libraries/stdlib/jvm/test/coroutines/TestDispatcher.kt index 41ccf8f2235..bc880272c0e 100644 --- a/libraries/stdlib/coroutines/jvm/test/TestDispatcher.kt +++ b/libraries/stdlib/jvm/test/coroutines/TestDispatcher.kt @@ -3,7 +3,7 @@ * that can be found in the license/LICENSE.txt file. */ -package test.kotlin.coroutines +package test.coroutines import java.io.Closeable import java.util.concurrent.ExecutorService diff --git a/libraries/stdlib/test/coroutines/CoroutineContextTest.kt b/libraries/stdlib/test/coroutines/CoroutineContextTest.kt index 5d31464edfe..15dd3992add 100644 --- a/libraries/stdlib/test/coroutines/CoroutineContextTest.kt +++ b/libraries/stdlib/test/coroutines/CoroutineContextTest.kt @@ -160,4 +160,94 @@ class CoroutineContextTest { for (elem in elements) assertTrue(ctx[elem.key] == elem) } + + + @Test + fun testBasicOperations() { + checkContents(EmptyCoroutineContext) + val de1 = DataElement(1) + val de2 = DataElement(2) + checkContents(de1, de1) + checkContents(de2, de2) + checkContents(de1 + de2, de2) + checkContents(de2 + de1, de1) + val oe3 = OtherElement(3) + val oe4 = OtherElement(4) + val de1oe3 = de1 + oe3 + checkContents(de1oe3, de1, oe3) + checkContents(de1oe3 + de2, de2, oe3) + checkContents(de1oe3 + oe4, de1, oe4) + checkContents(de1oe3.minusKey(DataElement), oe3) + checkContents(de1oe3.minusKey(OtherElement), de1) + } + + @Test + fun testWrapperEquality() { + val we1 = WrapperElement("1") + val we2 = WrapperElement("2") + checkContents(we1, we1) + checkContents(we2, we2) + val we1we2 = we1 + we2 + checkContents(we1we2, we1, we2) + checkContents(we1we2.minusKey(WrapperKey("1")), we2) + checkContents(we1we2.minusKey(WrapperKey("2")), we1) + } + + @Test + fun testInterceptor() { + val de1 = DataElement(1) + val oe2 = OtherElement(2) + val we3 = WrapperElement("3") + val cci = CustomContinuationInterceptor() + // Make sure it works properly with any position of ContinuationInterceptor in the context + checkContentsAndRemoves(de1 + oe2 + we3 + cci, de1, oe2, we3, cci) + checkContentsAndRemoves(de1 + oe2 + cci + we3, de1, oe2, we3, cci) + checkContentsAndRemoves(de1 + cci + oe2 + we3, de1, oe2, we3, cci) + checkContentsAndRemoves(cci + de1 + oe2 + we3, de1, oe2, we3, cci) + } + + private fun checkContentsAndRemoves(context: CoroutineContext, vararg es: CoroutineContext.Element) { + checkContents(context, *es) + for (e in es) { + checkContents(context.minusKey(e.key), *(es.toSet() - e).toTypedArray()) + } + } + + private fun checkContents(context: CoroutineContext, vararg es: CoroutineContext.Element) { + val size = context.fold(0) { c, _ -> c + 1 } + assertEquals(es.size, size) + for (e in es) { + val key = e.key + assertSame(e, context[key]) + } + val set = mutableSetOf() + context.fold(set) { s, e -> s.apply { add(e) } } + assertEquals(set, es.toSet()) + when (es.size) { + 0 -> assertSame(context, EmptyCoroutineContext) + 1 -> assertSame(context, es[0]) + } + } + + class DataElement(val data: Int) : AbstractCoroutineContextElement(Key) { + companion object Key : CoroutineContext.Key + } + + class OtherElement(val data: Int) : AbstractCoroutineContextElement(Key) { + companion object Key : CoroutineContext.Key + } + + data class WrapperKey(val key: String) : CoroutineContext.Key + + class WrapperElement(key: String) : CoroutineContext.Element { + override val key = WrapperKey(key) + } + + class CustomContinuationInterceptor : ContinuationInterceptor { + override val key: CoroutineContext.Key<*> + get() = ContinuationInterceptor + + override fun interceptContinuation(continuation: Continuation): Continuation = + continuation + } }