-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathExceptions.kt
73 lines (62 loc) · 2.31 KB
/
Exceptions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package kotlinx.coroutines.exceptions
import kotlinx.coroutines.*
import java.io.*
import java.util.*
import kotlin.coroutines.*
import kotlin.test.*
/**
* Proxy for [Throwable.getSuppressed] for tests, which are compiled for both JDK 1.6 and JDK 1.8,
* but run only under JDK 1.8
*/
@Suppress("ConflictingExtensionProperty")
actual val Throwable.suppressed: Array<Throwable> get() {
val method = this::class.java.getMethod("getSuppressed") ?: error("This test can only be run using JDK 1.7")
@Suppress("UNCHECKED_CAST")
return method.invoke(this) as Array<Throwable>
}
internal inline fun <reified T : Throwable> checkException(exception: Throwable): Boolean {
assertTrue(exception is T)
assertTrue(exception.suppressed.isEmpty())
assertNull(exception.cause)
return true
}
internal fun checkCycles(t: Throwable) {
val sw = StringWriter()
t.printStackTrace(PrintWriter(sw))
assertFalse(sw.toString().contains("CIRCULAR REFERENCE"))
}
class CapturingHandler : AbstractCoroutineContextElement(CoroutineExceptionHandler),
CoroutineExceptionHandler
{
private var unhandled: ArrayList<Throwable>? = ArrayList()
override fun tryHandleException(context: CoroutineContext, exception: Throwable) = synchronized(this) {
unhandled!!.add(exception)
}
fun getExceptions(): List<Throwable> = synchronized(this) {
return unhandled!!.also { unhandled = null }
}
fun getException(): Throwable = synchronized(this) {
val size = unhandled!!.size
assert(size == 1) { "Expected one unhandled exception, but have $size: $unhandled" }
return unhandled!![0].also { unhandled = null }
}
}
internal fun captureExceptionsRun(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
): Throwable {
val handler = CapturingHandler()
runBlocking(context + handler, block = block)
return handler.getException()
}
internal fun captureMultipleExceptionsRun(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> Unit
): List<Throwable> {
val handler = CapturingHandler()
runBlocking(context + handler, block = block)
return handler.getExceptions()
}