Skip to content

Commit 17d617d

Browse files
authored
Merge pull request #922 from hangga/rxjava-to-coroutines
KTLN-524 - Convert RxJava Single to Kotlin Coroutine Deferred
2 parents 098b02c + 0f91eb8 commit 17d617d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

core-kotlin-modules/core-kotlin-concurrency-3/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,31 @@
1111
<version>1.0.0-SNAPSHOT</version>
1212
</parent>
1313

14+
<dependencies>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
<version>${junit.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>io.reactivex.rxjava3</groupId>
23+
<artifactId>rxkotlin</artifactId>
24+
<version>${rxkotlin.version}</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.jetbrains.kotlinx</groupId>
29+
<artifactId>kotlinx-coroutines-rx3</artifactId>
30+
<version>${rxcoroutines.version}</version>
31+
</dependency>
32+
</dependencies>
33+
34+
<properties>
35+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36+
<rxkotlin.version>3.0.1</rxkotlin.version>
37+
<rxcoroutines.version>1.8.1</rxcoroutines.version>
38+
<junit.version>4.13.2</junit.version>
39+
</properties>
40+
1441
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.singlerxjavatocoroutinedeferred
2+
3+
import io.reactivex.rxjava3.core.Single
4+
import io.reactivex.rxjava3.schedulers.Schedulers
5+
import kotlinx.coroutines.*
6+
import kotlinx.coroutines.rx3.await
7+
import org.assertj.core.api.Assertions.assertThat
8+
import org.junit.jupiter.api.Test
9+
import kotlin.coroutines.resume
10+
import kotlin.coroutines.resumeWithException
11+
import kotlin.coroutines.suspendCoroutine
12+
13+
class SingleRxJavaToCoroutineDeferredUnitTest {
14+
15+
data class Product(val id: Int, val name: String, val price: Double)
16+
17+
private val allProducts = listOf(
18+
Product(1, "Samsung", 1200.0),
19+
Product(2, "Oppo", 800.0),
20+
Product(3, "Nokia", 450.0),
21+
Product(4, "Lenovo", 550.0),
22+
Product(5, "ASUS", 400.0)
23+
)
24+
25+
private fun getFilteredProducts(): Single<List<Product>> {
26+
return Single.just(
27+
allProducts
28+
).map { products ->
29+
products.sortedBy { it.price }.filter { it.price > 500 }
30+
}.subscribeOn(Schedulers.io())
31+
}
32+
33+
private suspend fun Deferred<*>.assertOver500AndSorted() {
34+
assertThat(this.await() as List<*>).containsExactly(
35+
Product(4, "Lenovo", 550.0),
36+
Product(2, "Oppo", 800.0),
37+
Product(1, "Samsung", 1200.0)
38+
)
39+
}
40+
41+
@Test
42+
fun `using async and blockingGet`() = runBlocking {
43+
val deferred = async { getFilteredProducts().blockingGet() }
44+
deferred.assertOver500AndSorted()
45+
}
46+
47+
@Test
48+
fun `using subscribe and CompletableDeferred`() = runBlocking {
49+
val deferred = CompletableDeferred<List<Product>>()
50+
getFilteredProducts().subscribe(deferred::complete, deferred::completeExceptionally)
51+
deferred.assertOver500AndSorted()
52+
}
53+
54+
@Test
55+
fun `using suspendCoroutines`(): Unit = runBlocking {
56+
val deferred = async {
57+
suspendCoroutine { continuation ->
58+
getFilteredProducts().subscribe(continuation::resume, continuation::resumeWithException)
59+
}
60+
}
61+
deferred.assertOver500AndSorted()
62+
}
63+
64+
@Test
65+
fun `using suspendCancellableCoroutine`(): Unit = runBlocking {
66+
val deferred = async {
67+
suspendCancellableCoroutine { continuation ->
68+
getFilteredProducts().subscribe(continuation::resume, continuation::resumeWithException)
69+
}
70+
}
71+
deferred.assertOver500AndSorted()
72+
}
73+
74+
@Test
75+
fun `using Kotlin Coroutines Rx3`() = runBlocking {
76+
val deferred = async { getFilteredProducts().await() }
77+
deferred.assertOver500AndSorted()
78+
}
79+
80+
}

0 commit comments

Comments
 (0)