blob: cef74c1704395e01b0cd73c818bc79e9edeb369e [file] [log] [blame]
Roman Elizarova7db8ec2017-12-21 22:45:12 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarova7db8ec2017-12-21 22:45:12 +03003 */
4
Roman Elizarov0950dfa2018-07-13 10:33:25 +03005package kotlinx.coroutines
Roman Elizarov8bff72b2017-12-20 12:55:38 +03006
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +03007import kotlinx.coroutines.flow.*
Vsevolod Tolstopyatov78832e32018-11-04 15:32:34 +03008import kotlin.coroutines.*
Vsevolod Tolstopyatov675c30c2018-11-02 15:55:20 +03009import kotlinx.coroutines.internal.*
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +030010import kotlin.test.*
Vsevolod Tolstopyatov78832e32018-11-04 15:32:34 +030011
Roman Elizarov8bff72b2017-12-20 12:55:38 +030012public expect open class TestBase constructor() {
13 public val isStressTest: Boolean
14 public val stressTestMultiplier: Int
15
Roman Elizarov8bff72b2017-12-20 12:55:38 +030016 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 Tolstopyatovfe820ba2019-04-24 17:14:03 +030020 public fun ensureFinished() // Ensures that 'finish' was invoked
Vsevolod Tolstopyatov732474f2018-07-20 11:36:20 +030021 public fun reset() // Resets counter and finish flag. Workaround for parametrized tests absence in common
Roman Elizarov8bff72b2017-12-20 12:55:38 +030022
Roman Elizarov8bff72b2017-12-20 12:55:38 +030023 public fun runTest(
24 expected: ((Throwable) -> Boolean)? = null,
25 unhandled: List<(Throwable) -> Boolean> = emptyList(),
26 block: suspend CoroutineScope.() -> Unit
Roman Elizarov4b9ae982018-01-25 11:51:25 +030027 )
Roman Elizarov8bff72b2017-12-20 12:55:38 +030028}
Vsevolod Tolstopyatov0b886a32018-10-25 13:52:38 +030029
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +030030public suspend inline fun hang(onCancellation: () -> Unit) {
31 try {
32 suspendCancellableCoroutine<Unit> { }
33 } finally {
34 onCancellation()
35 }
36}
37
38public 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
47public suspend inline fun <reified T : Throwable> assertFailsWith(flow: Flow<*>) {
Vsevolod Tolstopyatovfe820ba2019-04-24 17:14:03 +030048 try {
49 flow.collect { /* Do nothing */ }
50 fail("Should be unreached")
51 } catch (e: Throwable) {
52 assertTrue(e is T)
53 }
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +030054}
55
56public suspend fun Flow<Int>.sum() = fold(0) { acc, value -> acc + value }
Vsevolod Tolstopyatov165fbaf2019-04-22 11:21:44 +030057public suspend fun Flow<Long>.longSum() = fold(0L) { acc, value -> acc + value }
Vsevolod Tolstopyatovd57bfa22019-04-04 14:25:13 +030058
Vsevolod Tolstopyatov675c30c2018-11-02 15:55:20 +030059public class TestException(message: String? = null) : Throwable(message), NonRecoverableThrowable
60public class TestException1(message: String? = null) : Throwable(message), NonRecoverableThrowable
61public class TestException2(message: String? = null) : Throwable(message), NonRecoverableThrowable
62public class TestException3(message: String? = null) : Throwable(message), NonRecoverableThrowable
Roman Elizarov0aad8f12019-03-01 12:08:43 +030063public class TestCancellationException(message: String? = null) : CancellationException(message), NonRecoverableThrowable
Vsevolod Tolstopyatov675c30c2018-11-02 15:55:20 +030064public class TestRuntimeException(message: String? = null) : RuntimeException(message), NonRecoverableThrowable
Vsevolod Tolstopyatov1032f582018-11-27 18:13:47 +030065public class RecoverableTestException(message: String? = null) : RuntimeException(message)
Roman Elizarov0aad8f12019-03-01 12:08:43 +030066public class RecoverableTestCancellationException(message: String? = null) : CancellationException(message)
Vsevolod Tolstopyatov78832e32018-11-04 15:32:34 +030067
Vsevolod Tolstopyatov78832e32018-11-04 15:32:34 +030068public 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 Tolstopyatovd57bfa22019-04-04 14:25:13 +030075}
76
Vsevolod Tolstopyatovc022ab62019-05-14 15:10:09 +030077public suspend fun wrapperDispatcher(): CoroutineContext = wrapperDispatcher(coroutineContext)
78