Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | a7db8ec | 2017-12-21 22:45:12 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 6 | |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.flow.* |
Vsevolod Tolstopyatov | 78832e3 | 2018-11-04 15:32:34 +0300 | [diff] [blame] | 8 | import kotlin.coroutines.* |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 9 | import kotlinx.coroutines.internal.* |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 10 | import kotlin.test.* |
Vsevolod Tolstopyatov | 78832e3 | 2018-11-04 15:32:34 +0300 | [diff] [blame] | 11 | |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 12 | public expect open class TestBase constructor() { |
| 13 | public val isStressTest: Boolean |
| 14 | public val stressTestMultiplier: Int |
| 15 | |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 16 | public fun error(message: Any, cause: Throwable? = null): Nothing |
| 17 | public fun expect(index: Int) |
| 18 | public fun expectUnreached() |
| 19 | public fun finish(index: Int) |
Vsevolod Tolstopyatov | fe820ba | 2019-04-24 17:14:03 +0300 | [diff] [blame] | 20 | public fun ensureFinished() // Ensures that 'finish' was invoked |
Vsevolod Tolstopyatov | 732474f | 2018-07-20 11:36:20 +0300 | [diff] [blame] | 21 | public fun reset() // Resets counter and finish flag. Workaround for parametrized tests absence in common |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 22 | |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 23 | public fun runTest( |
| 24 | expected: ((Throwable) -> Boolean)? = null, |
| 25 | unhandled: List<(Throwable) -> Boolean> = emptyList(), |
| 26 | block: suspend CoroutineScope.() -> Unit |
Roman Elizarov | 4b9ae98 | 2018-01-25 11:51:25 +0300 | [diff] [blame] | 27 | ) |
Roman Elizarov | 8bff72b | 2017-12-20 12:55:38 +0300 | [diff] [blame] | 28 | } |
Vsevolod Tolstopyatov | 0b886a3 | 2018-10-25 13:52:38 +0300 | [diff] [blame] | 29 | |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 30 | public suspend inline fun hang(onCancellation: () -> Unit) { |
| 31 | try { |
| 32 | suspendCancellableCoroutine<Unit> { } |
| 33 | } finally { |
| 34 | onCancellation() |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public inline fun <reified T : Throwable> assertFailsWith(block: () -> Unit) { |
| 39 | try { |
| 40 | block() |
| 41 | error("Should not be reached") |
| 42 | } catch (e: Throwable) { |
| 43 | assertTrue(e is T) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public suspend inline fun <reified T : Throwable> assertFailsWith(flow: Flow<*>) { |
Vsevolod Tolstopyatov | fe820ba | 2019-04-24 17:14:03 +0300 | [diff] [blame] | 48 | try { |
| 49 | flow.collect { /* Do nothing */ } |
| 50 | fail("Should be unreached") |
| 51 | } catch (e: Throwable) { |
| 52 | assertTrue(e is T) |
| 53 | } |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public suspend fun Flow<Int>.sum() = fold(0) { acc, value -> acc + value } |
Vsevolod Tolstopyatov | 165fbaf | 2019-04-22 11:21:44 +0300 | [diff] [blame] | 57 | public suspend fun Flow<Long>.longSum() = fold(0L) { acc, value -> acc + value } |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 58 | |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 59 | public class TestException(message: String? = null) : Throwable(message), NonRecoverableThrowable |
| 60 | public class TestException1(message: String? = null) : Throwable(message), NonRecoverableThrowable |
| 61 | public class TestException2(message: String? = null) : Throwable(message), NonRecoverableThrowable |
| 62 | public class TestException3(message: String? = null) : Throwable(message), NonRecoverableThrowable |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 63 | public class TestCancellationException(message: String? = null) : CancellationException(message), NonRecoverableThrowable |
Vsevolod Tolstopyatov | 675c30c | 2018-11-02 15:55:20 +0300 | [diff] [blame] | 64 | public class TestRuntimeException(message: String? = null) : RuntimeException(message), NonRecoverableThrowable |
Vsevolod Tolstopyatov | 1032f58 | 2018-11-27 18:13:47 +0300 | [diff] [blame] | 65 | public class RecoverableTestException(message: String? = null) : RuntimeException(message) |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 66 | public class RecoverableTestCancellationException(message: String? = null) : CancellationException(message) |
Vsevolod Tolstopyatov | 78832e3 | 2018-11-04 15:32:34 +0300 | [diff] [blame] | 67 | |
Vsevolod Tolstopyatov | 78832e3 | 2018-11-04 15:32:34 +0300 | [diff] [blame] | 68 | public fun wrapperDispatcher(context: CoroutineContext): CoroutineContext { |
| 69 | val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher |
| 70 | return object : CoroutineDispatcher() { |
| 71 | override fun dispatch(context: CoroutineContext, block: Runnable) { |
| 72 | dispatcher.dispatch(context, block) |
| 73 | } |
| 74 | } |
Vsevolod Tolstopyatov | d57bfa2 | 2019-04-04 14:25:13 +0300 | [diff] [blame] | 75 | } |
| 76 | |
Vsevolod Tolstopyatov | c022ab6 | 2019-05-14 15:10:09 +0300 | [diff] [blame^] | 77 | public suspend fun wrapperDispatcher(): CoroutineContext = wrapperDispatcher(coroutineContext) |
| 78 | |