forked from Kotlin/kotlinx.coroutines
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document and tweak the contract of Executor.asCoroutineDispatcher and…
… ExecutorService.asCoroutineDispatcher (Kotlin#2727) * Get rid of ThreadPoolDispatcher and PoolThread classes * Reuse the same class for both asCoroutineDispatcher and newFixedThreadPoolContext * Replace 3-classes hierarchy by a single impl class * Copy the auto-closing logic to test source * Document and tweak the contract of Executor.asCoroutineDispatcher and ExecutorService.asCoroutineDispatcher * Document it properly * Make it more robust to signature changes and/or delegation (e.g. see the implementation of java.util.concurrent.Executors.newScheduledThreadPool) * Give a public way to reduce the memory pressure via ScheduledFuture.cancel Fixes Kotlin#2601
- Loading branch information
1 parent
78af220
commit 6953032
Showing
5 changed files
with
154 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines | ||
|
||
import org.junit.Test | ||
import java.lang.Runnable | ||
import java.util.concurrent.* | ||
import kotlin.test.* | ||
|
||
class ExecutorAsCoroutineDispatcherDelayTest : TestBase() { | ||
|
||
private var callsToSchedule = 0 | ||
|
||
private inner class STPE : ScheduledThreadPoolExecutor(1) { | ||
override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> { | ||
if (delay != 0L) ++callsToSchedule | ||
return super.schedule(command, delay, unit) | ||
} | ||
} | ||
|
||
private inner class SES : ScheduledExecutorService by STPE() | ||
|
||
@Test | ||
fun testScheduledThreadPool() = runTest { | ||
val executor = STPE() | ||
withContext(executor.asCoroutineDispatcher()) { | ||
delay(100) | ||
} | ||
executor.shutdown() | ||
assertEquals(1, callsToSchedule) | ||
} | ||
|
||
@Test | ||
fun testScheduledExecutorService() = runTest { | ||
val executor = SES() | ||
withContext(executor.asCoroutineDispatcher()) { | ||
delay(100) | ||
} | ||
executor.shutdown() | ||
assertEquals(1, callsToSchedule) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
kotlinx-coroutines-core/jvm/test/knit/ClosedAfterGuideTestExecutor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines // Trick to make guide tests use these declarations with executors that can be closed on our side implicitly | ||
|
||
import java.util.concurrent.* | ||
import java.util.concurrent.atomic.* | ||
import kotlin.coroutines.* | ||
|
||
internal fun newSingleThreadContext(name: String): ExecutorCoroutineDispatcher = ClosedAfterGuideTestDispatcher(1, name) | ||
|
||
internal fun newFixedThreadPoolContext(nThreads: Int, name: String): ExecutorCoroutineDispatcher = | ||
ClosedAfterGuideTestDispatcher(nThreads, name) | ||
|
||
internal class PoolThread( | ||
@JvmField val dispatcher: ExecutorCoroutineDispatcher, // for debugging & tests | ||
target: Runnable, name: String | ||
) : Thread(target, name) { | ||
init { | ||
isDaemon = true | ||
} | ||
} | ||
|
||
private class ClosedAfterGuideTestDispatcher( | ||
private val nThreads: Int, | ||
private val name: String | ||
) : ExecutorCoroutineDispatcher() { | ||
private val threadNo = AtomicInteger() | ||
|
||
override val executor: Executor = | ||
Executors.newScheduledThreadPool(nThreads, object : ThreadFactory { | ||
override fun newThread(target: java.lang.Runnable): Thread { | ||
return PoolThread( | ||
this@ClosedAfterGuideTestDispatcher, | ||
target, | ||
if (nThreads == 1) name else name + "-" + threadNo.incrementAndGet() | ||
) | ||
} | ||
}) | ||
|
||
override fun dispatch(context: CoroutineContext, block: Runnable) { | ||
executor.execute(wrapTask(block)) | ||
} | ||
|
||
override fun close() { | ||
(executor as ExecutorService).shutdown() | ||
} | ||
|
||
override fun toString(): String = "ThreadPoolDispatcher[$nThreads, $name]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters