/
TestSupport.kt
100 lines (82 loc) · 2.6 KB
/
TestSupport.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2019 David Karnok
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hu.akarnokd.kotlin.flow
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import org.junit.Assert.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
/**
* Execute a suspendable block with a single-threaded executor service.
*/
suspend fun withSingle(block: suspend (ExecutorService) -> Unit) {
val exec = Executors.newSingleThreadExecutor()
try {
block(exec)
} finally {
exec.shutdownNow()
}
}
suspend fun withParallels(parallelism: Int, block: suspend (List<CoroutineDispatcher>) -> Unit) {
val executors = Array(parallelism) { Executors.newSingleThreadExecutor() }
try {
block(executors.map { it.asCoroutineDispatcher() })
} finally {
executors.forEach { it.shutdownNow() }
}
}
suspend fun <T> Flow<T>.assertResult(vararg values: T) {
val list = ArrayList<T>()
this.collect {
list.add(it)
}
assertEquals(values.asList(), list)
}
suspend fun <T> Flow<T>.assertResultSet(vararg values: T) {
val set = HashSet<T>()
this.collect {
set.add(it)
}
assertEquals("Number of values differ", values.size, set.size)
values.forEach { assertTrue("Missing: " + it, set.contains(it)) }
}
suspend fun <T, E : Throwable> Flow<T>.assertFailure(errorClazz: Class<E>, vararg values: T) {
val list = ArrayList<T>()
var error : Throwable? = null
try {
this.collect {
list.add(it)
}
} catch (ex: Throwable) {
error = ex
}
assertEquals(values.asList(), list)
assertNotNull(error)
assertTrue("" + error, errorClazz.isInstance(error))
}
suspend fun <T, E : Throwable> Flow<T>.assertError(errorClazz: Class<E>) {
var error : Throwable? = null
try {
this.collect {
}
} catch (ex: Throwable) {
error = ex
}
assertNotNull(error)
assertTrue("" + error, errorClazz.isInstance(error))
}