Skip to content

Commit

Permalink
Move common test dispatcher to top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Nov 19, 2019
1 parent 951f1e3 commit 3ce3906
Show file tree
Hide file tree
Showing 21 changed files with 64 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ktor-client/ktor-client-tests/build.gradle.kts
Expand Up @@ -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"))
}
}
Expand Down
Expand Up @@ -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.*
Expand Down
3 changes: 3 additions & 0 deletions ktor-io/build.gradle
Expand Up @@ -11,4 +11,7 @@ kotlin {
}
}
}
sourceSets.commonTest.dependencies {
api project(":ktor-test-dispatcher")
}
}
4 changes: 3 additions & 1 deletion ktor-io/common/src/io/ktor/utils/io/ByteChannelSequential.kt
Expand Up @@ -675,7 +675,9 @@ abstract class ByteChannelSequentialBase(

protected suspend fun awaitFreeSpace() {
afterWrite()
return notFull.await { flush() }
notFull.await {
flush()
}
}

final override suspend fun peekTo(
Expand Down
15 changes: 10 additions & 5 deletions ktor-io/common/src/io/ktor/utils/io/Coroutines.kt
Expand Up @@ -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) {
Expand All @@ -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]"
}
@@ -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)
}
}
2 changes: 2 additions & 0 deletions ktor-io/js/src/io/ktor/utils/io/ByteChannelJS.kt
Expand Up @@ -125,4 +125,6 @@ internal class ByteChannelJS(initial: IoBuffer, autoFlush: Boolean) : ByteChanne
remaining -= rc
}
}

override fun toString(): String = "ByteChannel[$attachedJob, ${hashCode()}]"
}
3 changes: 2 additions & 1 deletion 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.*

Expand Down Expand Up @@ -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)
Expand Down
File renamed without changes.
Expand Up @@ -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.*
Expand Down
@@ -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.*
Expand Down
@@ -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.*
Expand Down
@@ -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.*
Expand Down
Expand Up @@ -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.*
Expand Down
Expand Up @@ -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.*
Expand Down
@@ -1,4 +1,4 @@
package io.ktor.client.tests.utils.dispatcher
package io.ktor.test.dispatcher

import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down
@@ -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.*
Expand Down
@@ -1,4 +1,4 @@
package io.ktor.client.tests.utils.dispatcher
package io.ktor.test.dispatcher

import kotlinx.coroutines.*
import kotlin.coroutines.*
Expand Down
File renamed without changes.
Expand Up @@ -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.*
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Expand Up @@ -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'
Expand Down Expand Up @@ -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'

0 comments on commit 3ce3906

Please sign in to comment.