1 /*
<lambda>null2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
5 @file:Suppress("unused")
6
7 package kotlinx.coroutines
8
9 import kotlinx.coroutines.flow.*
10 import kotlin.coroutines.*
11 import kotlin.test.*
12
13 public expect val isStressTest: Boolean
14 public expect val stressTestMultiplier: Int
15
16 public expect open class TestBase constructor() {
17 public fun error(message: Any, cause: Throwable? = null): Nothing
18 public fun expect(index: Int)
19 public fun expectUnreached()
20 public fun finish(index: Int)
21 public fun ensureFinished() // Ensures that 'finish' was invoked
22 public fun reset() // Resets counter and finish flag. Workaround for parametrized tests absence in common
23
24 public fun runTest(
25 expected: ((Throwable) -> Boolean)? = null,
26 unhandled: List<(Throwable) -> Boolean> = emptyList(),
27 block: suspend CoroutineScope.() -> Unit
28 )
29 }
30
hangnull31 public suspend inline fun hang(onCancellation: () -> Unit) {
32 try {
33 suspendCancellableCoroutine<Unit> { }
34 } finally {
35 onCancellation()
36 }
37 }
38
assertFailsWithnull39 public inline fun <reified T : Throwable> assertFailsWith(block: () -> Unit) {
40 try {
41 block()
42 error("Should not be reached")
43 } catch (e: Throwable) {
44 assertTrue(e is T)
45 }
46 }
47
assertFailsWithnull48 public suspend inline fun <reified T : Throwable> assertFailsWith(flow: Flow<*>) {
49 try {
50 flow.collect()
51 fail("Should be unreached")
52 } catch (e: Throwable) {
53 assertTrue(e is T, "Expected exception ${T::class}, but had $e instead")
54 }
55 }
56
accnull57 public suspend fun Flow<Int>.sum() = fold(0) { acc, value -> acc + value }
accnull58 public suspend fun Flow<Long>.longSum() = fold(0L) { acc, value -> acc + value }
59
60
61 // data is added to avoid stacktrace recovery because CopyableThrowable is not accessible from common modules
62 public class TestException(message: String? = null, private val data: Any? = null) : Throwable(message)
63 public class TestException1(message: String? = null, private val data: Any? = null) : Throwable(message)
64 public class TestException2(message: String? = null, private val data: Any? = null) : Throwable(message)
65 public class TestException3(message: String? = null, private val data: Any? = null) : Throwable(message)
66 public class TestCancellationException(message: String? = null, private val data: Any? = null) : CancellationException(message)
67 public class TestRuntimeException(message: String? = null, private val data: Any? = null) : RuntimeException(message)
68 public class RecoverableTestException(message: String? = null) : RuntimeException(message)
69 public class RecoverableTestCancellationException(message: String? = null) : CancellationException(message)
70
wrapperDispatchernull71 public fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {
72 val dispatcher = context[ContinuationInterceptor] as CoroutineDispatcher
73 return object : CoroutineDispatcher() {
74 override fun isDispatchNeeded(context: CoroutineContext): Boolean =
75 dispatcher.isDispatchNeeded(context)
76 override fun dispatch(context: CoroutineContext, block: Runnable) =
77 dispatcher.dispatch(context, block)
78 }
79 }
80
wrapperDispatchernull81 public suspend fun wrapperDispatcher(): CoroutineContext = wrapperDispatcher(coroutineContext)
82
83 class BadClass {
84 override fun equals(other: Any?): Boolean = error("equals")
85 override fun hashCode(): Int = error("hashCode")
86 override fun toString(): String = error("toString")
87 }
88