Move common test dispatcher to top-level

This commit is contained in:
Leonid Stashevsky
2019-11-18 17:09:12 +03:00
committed by Leonid Stashevsky
parent 951f1e33f1
commit 3ce3906c2b
21 changed files with 64 additions and 20 deletions

View File

@@ -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"))
}
}

View File

@@ -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.*

View File

@@ -11,4 +11,7 @@ kotlin {
}
}
}
sourceSets.commonTest.dependencies {
api project(":ktor-test-dispatcher")
}
}

View File

@@ -675,7 +675,9 @@ abstract class ByteChannelSequentialBase(
protected suspend fun awaitFreeSpace() {
afterWrite()
return notFull.await { flush() }
notFull.await {
flush()
}
}
final override suspend fun peekTo(

View File

@@ -113,7 +113,8 @@ private fun <S : CoroutineScope> 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 <S : CoroutineScope> 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]"
}

View File

@@ -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)
}
}

View File

@@ -125,4 +125,6 @@ internal class ByteChannelJS(initial: IoBuffer, autoFlush: Boolean) : ByteChanne
remaining -= rc
}
}
override fun toString(): String = "ByteChannel[$attachedJob, ${hashCode()}]"
}

View File

@@ -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)

View File

@@ -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.*

View File

@@ -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.*

View File

@@ -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.*

View File

@@ -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.*

View File

@@ -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.*

View File

@@ -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.*

View File

@@ -1,4 +1,4 @@
package io.ktor.client.tests.utils.dispatcher
package io.ktor.test.dispatcher
import kotlinx.coroutines.*
import kotlin.coroutines.*

View File

@@ -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.*

View File

@@ -1,4 +1,4 @@
package io.ktor.client.tests.utils.dispatcher
package io.ktor.test.dispatcher
import kotlinx.coroutines.*
import kotlin.coroutines.*

View File

@@ -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.*

View File

@@ -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'