@@ -6,6 +6,7 @@ import kotlinx.coroutines.*
6
6
import kotlinx.coroutines.rx3.await
7
7
import org.assertj.core.api.Assertions.assertThat
8
8
import org.junit.jupiter.api.Test
9
+ import org.slf4j.LoggerFactory
9
10
import kotlin.coroutines.CoroutineContext
10
11
import kotlin.coroutines.resume
11
12
import kotlin.coroutines.resumeWithException
@@ -15,6 +16,8 @@ import kotlin.test.assertTrue
15
16
16
17
class SingleRxJavaToCoroutineDeferredUnitTest {
17
18
19
+ private val logger = LoggerFactory .getLogger(" " )
20
+
18
21
data class Product (val id : Int , val name : String , val price : Double )
19
22
20
23
private val allProducts = listOf (
@@ -33,7 +36,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
33
36
}.subscribeOn(Schedulers .io())
34
37
}
35
38
36
- private suspend fun Deferred <* >.assertResultsTrue () {
39
+ private suspend fun Deferred <* >.assertOver500AndSorted () {
37
40
assertTrue(actual = this is Deferred <* >)
38
41
39
42
assertThat(this .await() as List <* >).containsExactly(
@@ -46,7 +49,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
46
49
@Test
47
50
fun `using async direcly and blockingGet` () = runBlocking {
48
51
val deferred = async { getFilteredProducts().blockingGet() } // simple, but must be careful because blocking main thread
49
- deferred.assertResultsTrue () // assertion test
52
+ deferred.assertOver500AndSorted () // assertion test
50
53
}
51
54
52
55
// using async with extension
@@ -57,7 +60,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
57
60
@Test
58
61
fun `test using async with extension` () = runBlocking {
59
62
val deferredExt = getFilteredProducts().toDeferredAsync()
60
- deferredExt.assertResultsTrue ()
63
+ deferredExt.assertOver500AndSorted ()
61
64
}
62
65
63
66
// using CoroutineScope(context).async
@@ -67,7 +70,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
67
70
@Test
68
71
fun `test using CoroutineScope with context and async` (): Unit = runBlocking {
69
72
val deferred = getFilteredProducts().toDeferredWithContext(Dispatchers .IO )
70
- deferred.assertResultsTrue ()
73
+ deferred.assertOver500AndSorted ()
71
74
}
72
75
73
76
// using CompletableDeferred & subscribe
@@ -79,7 +82,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
79
82
}, { error ->
80
83
deferred.completeExceptionally(error)
81
84
})
82
- deferred.assertResultsTrue ()
85
+ deferred.assertOver500AndSorted ()
83
86
}
84
87
85
88
// using CompletableDeferred extension
@@ -107,15 +110,15 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
107
110
@Test
108
111
fun `test using CompletableDeferred extension` () = runBlocking {
109
112
val deferred = getFilteredProducts().toCompletableDeferred()
110
- deferred.assertResultsTrue ()
113
+ deferred.assertOver500AndSorted ()
111
114
}
112
115
113
116
@Test
114
117
fun `test using CompletableDeferred with callback` (): Unit = runBlocking {
115
118
getFilteredProducts().toCompletableDeferred(onSuccess = { deferredResult, _ ->
116
- launch { deferredResult.assertResultsTrue () }
119
+ launch { deferredResult.assertOver500AndSorted () }
117
120
}, onError = { _, error ->
118
- println (" Error: ${error.message} " )
121
+ logger.debug (" Error: ${error.message} " )
119
122
}).await()
120
123
}
121
124
@@ -129,7 +132,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
129
132
}
130
133
}
131
134
}
132
- defered.assertResultsTrue ()
135
+ defered.assertOver500AndSorted ()
133
136
}
134
137
135
138
// using suspendCoroutines with extension
@@ -148,7 +151,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
148
151
@Test
149
152
fun `test using suspendCoroutine extension` () = runBlocking {
150
153
val deferred = getFilteredProducts().toDeferredWithSuspend()
151
- deferred.assertResultsTrue ()
154
+ deferred.assertOver500AndSorted ()
152
155
}
153
156
154
157
// using suspendCancellableCoroutine
@@ -175,17 +178,17 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
175
178
@Test
176
179
fun `test using suspendCancellableCoroutine with custom callback` (): Unit = runBlocking {
177
180
getFilteredProducts().toDeferredWithSuspendCancellableCoroutine(onSuccess = { deferredResult ->
178
- launch { deferredResult.assertResultsTrue () }
181
+ launch { deferredResult.assertOver500AndSorted () }
179
182
}, onError = { error ->
180
- println (" Error: ${error.message} " )
183
+ logger.debug (" Error: ${error.message} " )
181
184
}).await()
182
185
}
183
186
184
187
// using rx3 directly
185
188
@Test
186
189
fun `using rx3 directly` () = runBlocking {
187
190
val deferred = async { getFilteredProducts().await() }
188
- deferred.assertResultsTrue ()
191
+ deferred.assertOver500AndSorted ()
189
192
}
190
193
191
194
// using rx3 ext
@@ -195,7 +198,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
195
198
@Test
196
199
fun `test using rx3 ext` () = runBlocking {
197
200
val deferred = getFilteredProducts().toDeferredRx3()
198
- deferred.assertResultsTrue ()
201
+ deferred.assertOver500AndSorted ()
199
202
}
200
203
201
204
// using rx3 with context
@@ -205,7 +208,7 @@ class SingleRxJavaToCoroutineDeferredUnitTest {
205
208
@Test
206
209
fun `test using rx3 with context` (): Unit = runBlocking {
207
210
val deferred = getFilteredProducts().toDeferredRx3WithContext(Dispatchers .IO )
208
- deferred.assertResultsTrue ()
211
+ deferred.assertOver500AndSorted ()
209
212
}
210
213
211
214
}
0 commit comments