diff --git a/ktor-client/ktor-client-tests/build.gradle.kts b/ktor-client/ktor-client-tests/build.gradle.kts index 575f41a802..7d882df44f 100644 --- a/ktor-client/ktor-client-tests/build.gradle.kts +++ b/ktor-client/ktor-client-tests/build.gradle.kts @@ -41,7 +41,7 @@ kotlin.sourceSets { dependencies { api(project(":ktor-client:ktor-client-core")) api(project(":ktor-client:ktor-client-mock")) - api(project(":ktor-client:ktor-client-tests:ktor-client-tests-dispatcher")) + api(project(":ktor-test-dispatcher")) api(project(":ktor-client:ktor-client-features:ktor-client-json:ktor-client-serialization")) } } diff --git a/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt b/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt index a57ed70723..449ca2c061 100644 --- a/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt +++ b/ktor-client/ktor-client-tests/common/src/io/ktor/client/tests/utils/CommonClientTestUtils.kt @@ -6,7 +6,7 @@ package io.ktor.client.tests.utils import io.ktor.client.* import io.ktor.client.engine.* -import io.ktor.client.tests.utils.dispatcher.* +import io.ktor.test.dispatcher.* import io.ktor.util.* import kotlinx.coroutines.* import io.ktor.utils.io.core.* diff --git a/ktor-io/build.gradle b/ktor-io/build.gradle index 90d450c09c..5606938568 100644 --- a/ktor-io/build.gradle +++ b/ktor-io/build.gradle @@ -11,4 +11,7 @@ kotlin { } } } + sourceSets.commonTest.dependencies { + api project(":ktor-test-dispatcher") + } } diff --git a/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt b/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt index 6ca97dac8a..27ae802504 100644 --- a/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt +++ b/ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt @@ -675,7 +675,9 @@ abstract class ByteChannelSequentialBase( protected suspend fun awaitFreeSpace() { afterWrite() - return notFull.await { flush() } + notFull.await { + flush() + } } final override suspend fun peekTo( diff --git a/ktor-io/common/src/io/ktor/utils/io/Coroutines.kt b/ktor-io/common/src/io/ktor/utils/io/Coroutines.kt index 1ebe7636f8..9cbf632325 100644 --- a/ktor-io/common/src/io/ktor/utils/io/Coroutines.kt +++ b/ktor-io/common/src/io/ktor/utils/io/Coroutines.kt @@ -113,7 +113,8 @@ private fun CoroutineScope.launchChannel( context: CoroutineContext, channel: ByteChannel, attachJob: Boolean, - block: suspend S.() -> Unit): ChannelJob { + block: suspend S.() -> Unit +): ChannelJob { val job = launch(context) { if (attachJob) { @@ -130,10 +131,14 @@ private fun CoroutineScope.launchChannel( return ChannelJob(job, channel) } -private class ChannelScope(delegate: CoroutineScope, - override val channel: ByteChannel) : ReaderScope, WriterScope, CoroutineScope by delegate +private class ChannelScope( + delegate: CoroutineScope, + override val channel: ByteChannel +) : ReaderScope, WriterScope, CoroutineScope by delegate private class ChannelJob( - delegate: Job, + private val delegate: Job, override val channel: ByteChannel -) : ReaderJob, WriterJob, Job by delegate +) : ReaderJob, WriterJob, Job by delegate { + override fun toString(): String = "ChannelJob[$delegate]" +} diff --git a/ktor-io/common/test/io/ktor/utils/io/tests/ByteChannelBuildersTest.kt b/ktor-io/common/test/io/ktor/utils/io/tests/ByteChannelBuildersTest.kt new file mode 100644 index 0000000000..d9d29dcfae --- /dev/null +++ b/ktor-io/common/test/io/ktor/utils/io/tests/ByteChannelBuildersTest.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. + */ + +package io.ktor.utils.io.tests + +import io.ktor.utils.io.* +import kotlinx.coroutines.* +import kotlin.test.* +import io.ktor.test.dispatcher.* + +class ByteChannelBuildersTest { + @Test + fun testWriterCancelledByChannel() = testSuspend { + val context = Job() + val scope = CoroutineScope(context) + + val task = scope.writer { + val data = ByteArray(8 * 1024) + while (true) { + channel.writeFully(data) + } + } + + context.complete() + task.channel.cancel() + task.join() + assertTrue(task.isCancelled) + assertTrue(context.isCompleted) + } +} diff --git a/ktor-io/js/src/io/ktor/utils/io/ByteChannelJS.kt b/ktor-io/js/src/io/ktor/utils/io/ByteChannelJS.kt index c069b4d955..8f7f4e958e 100644 --- a/ktor-io/js/src/io/ktor/utils/io/ByteChannelJS.kt +++ b/ktor-io/js/src/io/ktor/utils/io/ByteChannelJS.kt @@ -125,4 +125,6 @@ internal class ByteChannelJS(initial: IoBuffer, autoFlush: Boolean) : ByteChanne remaining -= rc } } + + override fun toString(): String = "ByteChannel[$attachedJob, ${hashCode()}]" } diff --git a/ktor-io/js/src/io/ktor/utils/io/ByteReadChannel.kt b/ktor-io/js/src/io/ktor/utils/io/ByteReadChannel.kt index 288768eadf..04a5f5185a 100644 --- a/ktor-io/js/src/io/ktor/utils/io/ByteReadChannel.kt +++ b/ktor-io/js/src/io/ktor/utils/io/ByteReadChannel.kt @@ -1,6 +1,6 @@ package io.ktor.utils.io -import io.ktor.utils.io.bits.Memory +import io.ktor.utils.io.bits.* import io.ktor.utils.io.core.* import org.khronos.webgl.* @@ -57,6 +57,7 @@ actual interface ByteReadChannel { * Suspends if not enough bytes available. */ actual suspend fun readFully(dst: ByteArray, offset: Int, length: Int) + actual suspend fun readFully(dst: IoBuffer, n: Int) suspend fun readFully(dst: ArrayBuffer, offset: Int, length: Int) diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/build.gradle.kts b/ktor-test-dispatcher/build.gradle.kts similarity index 100% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/build.gradle.kts rename to ktor-test-dispatcher/build.gradle.kts diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/common/src/TestCommon.kt b/ktor-test-dispatcher/common/src/TestCommon.kt similarity index 89% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/common/src/TestCommon.kt rename to ktor-test-dispatcher/common/src/TestCommon.kt index 49cad8d902..e995de9b27 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/common/src/TestCommon.kt +++ b/ktor-test-dispatcher/common/src/TestCommon.kt @@ -2,7 +2,7 @@ * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm32/src/TestIosArm32.kt b/ktor-test-dispatcher/iosArm32/src/TestIosArm32.kt similarity index 92% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm32/src/TestIosArm32.kt rename to ktor-test-dispatcher/iosArm32/src/TestIosArm32.kt index fbcdc0368b..fc8cc3c025 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm32/src/TestIosArm32.kt +++ b/ktor-test-dispatcher/iosArm32/src/TestIosArm32.kt @@ -1,6 +1,6 @@ @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm64/src/TestIosArm64.kt b/ktor-test-dispatcher/iosArm64/src/TestIosArm64.kt similarity index 92% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm64/src/TestIosArm64.kt rename to ktor-test-dispatcher/iosArm64/src/TestIosArm64.kt index fbcdc0368b..fc8cc3c025 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosArm64/src/TestIosArm64.kt +++ b/ktor-test-dispatcher/iosArm64/src/TestIosArm64.kt @@ -1,6 +1,6 @@ @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosX64/src/TestIosX64.kt b/ktor-test-dispatcher/iosX64/src/TestIosX64.kt similarity index 92% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosX64/src/TestIosX64.kt rename to ktor-test-dispatcher/iosX64/src/TestIosX64.kt index fbcdc0368b..fc8cc3c025 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/iosX64/src/TestIosX64.kt +++ b/ktor-test-dispatcher/iosX64/src/TestIosX64.kt @@ -1,6 +1,6 @@ @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/js/src/TestJs.kt b/ktor-test-dispatcher/js/src/TestJs.kt similarity index 89% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/js/src/TestJs.kt rename to ktor-test-dispatcher/js/src/TestJs.kt index 45817cf3c4..51cc8ef9c2 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/js/src/TestJs.kt +++ b/ktor-test-dispatcher/js/src/TestJs.kt @@ -2,7 +2,7 @@ * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/jvm/src/TestJvm.kt b/ktor-test-dispatcher/jvm/src/TestJvm.kt similarity index 88% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/jvm/src/TestJvm.kt rename to ktor-test-dispatcher/jvm/src/TestJvm.kt index 36cd497fe7..430e4600c6 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/jvm/src/TestJvm.kt +++ b/ktor-test-dispatcher/jvm/src/TestJvm.kt @@ -2,7 +2,7 @@ * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/linuxX64/src/TestLinuxX64.kt b/ktor-test-dispatcher/linuxX64/src/TestLinuxX64.kt similarity index 84% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/linuxX64/src/TestLinuxX64.kt rename to ktor-test-dispatcher/linuxX64/src/TestLinuxX64.kt index 535c8d9a85..c5566379f6 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/linuxX64/src/TestLinuxX64.kt +++ b/ktor-test-dispatcher/linuxX64/src/TestLinuxX64.kt @@ -1,4 +1,4 @@ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/macosX64/src/TestMacosX64.kt b/ktor-test-dispatcher/macosX64/src/TestMacosX64.kt similarity index 92% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/macosX64/src/TestMacosX64.kt rename to ktor-test-dispatcher/macosX64/src/TestMacosX64.kt index fbcdc0368b..fc8cc3c025 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/macosX64/src/TestMacosX64.kt +++ b/ktor-test-dispatcher/macosX64/src/TestMacosX64.kt @@ -1,6 +1,6 @@ @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/mingwX64/src/TestMingwX64.kt b/ktor-test-dispatcher/mingwX64/src/TestMingwX64.kt similarity index 84% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/mingwX64/src/TestMingwX64.kt rename to ktor-test-dispatcher/mingwX64/src/TestMingwX64.kt index 535c8d9a85..c5566379f6 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/mingwX64/src/TestMingwX64.kt +++ b/ktor-test-dispatcher/mingwX64/src/TestMingwX64.kt @@ -1,4 +1,4 @@ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/posix/src/.gitkeep b/ktor-test-dispatcher/posix/src/.gitkeep similarity index 100% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/posix/src/.gitkeep rename to ktor-test-dispatcher/posix/src/.gitkeep diff --git a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/posixIde/src/TestIde.kt b/ktor-test-dispatcher/posixIde/src/TestIde.kt similarity index 88% rename from ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/posixIde/src/TestIde.kt rename to ktor-test-dispatcher/posixIde/src/TestIde.kt index a820d9b3d3..22a2ac3677 100644 --- a/ktor-client/ktor-client-tests/ktor-client-tests-dispatcher/posixIde/src/TestIde.kt +++ b/ktor-test-dispatcher/posixIde/src/TestIde.kt @@ -2,7 +2,7 @@ * Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. */ -package io.ktor.client.tests.utils.dispatcher +package io.ktor.test.dispatcher import kotlinx.coroutines.* import kotlin.coroutines.* diff --git a/settings.gradle b/settings.gradle index df6aa190d4..7b6a02ec86 100644 --- a/settings.gradle +++ b/settings.gradle @@ -32,7 +32,6 @@ include ':ktor-server' include ':ktor-client' include ':ktor-client:ktor-client-core' include ':ktor-client:ktor-client-tests' -include ':ktor-client:ktor-client-tests:ktor-client-tests-dispatcher' include ':ktor-client:ktor-client-apache' include ':ktor-client:ktor-client-android' include ':ktor-client:ktor-client-cio' @@ -80,3 +79,4 @@ include ':ktor-network' include ':ktor-network:ktor-network-tls' include ':ktor-network:ktor-network-tls:ktor-network-tls-certificates' include ':ktor-bom' +include ':ktor-test-dispatcher'