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

jvmTest.FailFastOnStartTest.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0
Show newest version
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

@file:Suppress("DeferredResultUnused")

package kotlinx.coroutines

import kotlinx.coroutines.channels.*
import org.junit.*
import org.junit.Test
import org.junit.rules.*
import kotlin.test.*

class FailFastOnStartTest : TestBase() {

    @Rule
    @JvmField
    public val timeout: Timeout = Timeout.seconds(5)

    @Test
    fun testLaunch() = runTest(expected = ::mainException) {
        launch(Dispatchers.Main) {}
    }

    @Test
    fun testLaunchLazy() = runTest(expected = ::mainException) {
        val job = launch(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
        job.join()
    }

    @Test
    fun testLaunchUndispatched() = runTest(expected = ::mainException) {
        launch(Dispatchers.Main, start = CoroutineStart.UNDISPATCHED) {
            yield()
            fail()
        }
    }

    @Test
    fun testAsync() = runTest(expected = ::mainException) {
        async(Dispatchers.Main) {}
    }

    @Test
    fun testAsyncLazy() = runTest(expected = ::mainException) {
        val job = async(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
        job.await()
    }

    @Test
    fun testWithContext() = runTest(expected = ::mainException) {
        withContext(Dispatchers.Main) {
            fail()
        }
    }

    @Test
    fun testProduce() = runTest(expected = ::mainException) {
        produce(Dispatchers.Main) { fail() }
    }

    @Test
    fun testActor() = runTest(expected = ::mainException) {
        actor(Dispatchers.Main) { fail() }
    }

    @Test
    fun testActorLazy() = runTest(expected = ::mainException) {
        val actor = actor(Dispatchers.Main, start = CoroutineStart.LAZY) { fail() }
        actor.send(1)
    }

    private fun mainException(e: Throwable): Boolean {
        return e is IllegalStateException && e.message?.contains("Module with the Main dispatcher is missing") ?: false
    }

    @Test
    fun testProduceNonChild() = runTest(expected = ::mainException) {
        produce(Job() + Dispatchers.Main) { fail() }
    }

    @Test
    fun testAsyncNonChild() = runTest(expected = ::mainException) {
        async(Job() + Dispatchers.Main) { fail() }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy