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

jvmTest.flow.ExceptionTransparencyTest.kt Maven / Gradle / Ivy

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

package kotlinx.coroutines.flow

import kotlinx.coroutines.*
import kotlin.test.*

class ExceptionTransparencyTest : TestBase() {

    @Test
    fun testViolation() = runTest {
        val flow = flow {
            try {
                expect(1)
                emit(1)
                expectUnreached()
            } catch (e: CancellationException) {
                expect(3)
                emit(2)
            }
        }.take(1)

        assertFailsWith { flow.collect { expect(2) } }
        finish(4)
    }

    @Test
    fun testViolationResumeWith() = runTest {
        val flow = flow {
            try {
                expect(1)
                emit(1)
                yield()
                expectUnreached()
            } catch (e: CancellationException) {
                expect(3)
                emit(2)
            }
        }.take(1)

        assertFailsWith {
            flow.collect {
                yield()
                expect(2)
            }
        }
        finish(4)
    }

    @Test
    fun testViolationAfterInvariantVariation() = runTest {
        val flow = flow {
            coroutineScope {
                try {
                    expect(1)
                    launch {
                        expect(2)
                        emit(1)
                    }.join()
                    expectUnreached()
                } catch (e: Throwable) {
                    try {
                        emit(2)
                    } catch (e: IllegalStateException) {
                        assertTrue { e.message!!.contains("exception transparency") }
                        emit(3)
                    }
                }
            }
        }
        val e = assertFailsWith { flow.collect { expectUnreached() } }
        assertTrue { e.message!!.contains("channelFlow") }
        finish(3)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy