aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt')
-rw-r--r--kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt44
1 files changed, 44 insertions, 0 deletions
diff --git a/kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt b/kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt
new file mode 100644
index 00000000..dbe9cb37
--- /dev/null
+++ b/kotlinx-coroutines-core/jvm/test/ExecutorAsCoroutineDispatcherDelayTest.kt
@@ -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)
+ }
+}