All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmTest.ExecutorAsCoroutineDispatcherDelayTest.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
/*
 * 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)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy