Skip to content

Commit 08e9241

Browse files
committed
update
1 parent 4ad0e84 commit 08e9241

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package com.baeldung.parallelOperationsCollections
33
import io.reactivex.Observable
44
import io.reactivex.rxkotlin.toObservable
55
import io.reactivex.schedulers.Schedulers
6-
import kotlinx.coroutines.*
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import kotlinx.coroutines.async
8+
import kotlinx.coroutines.awaitAll
79
import kotlinx.coroutines.flow.*
10+
import kotlinx.coroutines.runBlocking
811
import org.assertj.core.api.Assertions.assertThat
912
import org.junit.jupiter.api.Test
1013
import java.text.SimpleDateFormat
@@ -31,60 +34,55 @@ class ParallelOperationCollectionsUnitTest {
3134

3235
private fun List<Person>.assertResultsTrue() {
3336
assertThat(this).containsExactly(
34-
Person("Bob", 16, false), Person("Alice", 30, true), Person("Charlie", 40, true), Person("Ahmad", 42, true)
37+
Person("Bob", 16, false),
38+
Person("Alice", 30, true),
39+
Person("Charlie", 40, true),
40+
Person("Ahmad", 42, true)
3541
)
3642
}
3743

38-
private val dateFormat = SimpleDateFormat("yyyy-MM-dd:HH:mm:ss:SSS")
44+
private val columnScheme = "%-25s %-45s %-40s"
3945

4046
private fun Person.printFormattedInfo() {
4147
println(
42-
"%-30s %-40s %s".format(
43-
dateFormat.format(Date.from(Instant.now())), Thread.currentThread().name, this
48+
columnScheme.format(
49+
SimpleDateFormat("yyyy-MM-dd:HH:mm:ss:SSS").format(Date.from(Instant.now())),
50+
this.toString(),
51+
Thread.currentThread().name
4452
)
4553
)
4654
}
4755

48-
private fun printHeader() {
49-
println(
50-
"%-30s %-40s %s".format(
51-
"Time", "Thread name", "Operation"
52-
)
53-
)
54-
println("-".repeat(115))
56+
private fun String.printAsHeader() {
57+
println("$this ${"-".repeat(100 - this.length)}\n${columnScheme.format("Time", "Operation", "Thread name")}")
5558
}
5659

57-
private fun Instant.printFooter() {
58-
val endTime = Instant.now()
59-
val duration = Duration.between(this, endTime)
60-
println("Total time taken: ${duration.toMillis()} ms")
61-
println()
60+
private fun Instant.printTotalTime() {
61+
println("Total time taken: ${Duration.between(this, Instant.now()).toMillis()} ms\n")
6262
}
6363

6464
@Test
6565
fun `using coroutines for parallel operations`() = runBlocking {
66-
printHeader()
66+
"Using Coroutines".printAsHeader()
6767
val startTime = Instant.now()
6868

6969
val filteredPeople = people.map { person ->
7070
async {
71-
launch {
72-
person.isAdult = person.age >= 18
73-
person.printFormattedInfo()
74-
}
71+
person.isAdult = person.age >= 18
72+
person.printFormattedInfo()
7573
person
7674
}
7775
}.awaitAll().filter { it.age > 15 }.sortedBy { it.age }
7876

79-
startTime.printFooter()
77+
startTime.printTotalTime()
8078

8179
filteredPeople.assertResultsTrue()
8280
}
8381

8482
@OptIn(ExperimentalCoroutinesApi::class)
8583
@Test
8684
fun `using coroutines for parallel operations with Flow`() = runBlocking {
87-
printHeader()
85+
"Using Kotlin Flow".printAsHeader()
8886
val startTime = Instant.now()
8987

9088
val filteredPeople = people.asFlow().flatMapMerge { person ->
@@ -97,14 +95,14 @@ class ParallelOperationCollectionsUnitTest {
9795
}
9896
}.filter { it.age > 15 }.toList().sortedBy { it.age }
9997

100-
startTime.printFooter()
98+
startTime.printTotalTime()
10199

102100
filteredPeople.assertResultsTrue()
103101
}
104102

105103
@Test
106104
fun `using RxJava for parallel operations`() { // Observable.class from io.reactivex;
107-
printHeader()
105+
"Using RxJava".printAsHeader()
108106
val startTime = Instant.now()
109107

110108
val observable = Observable.fromIterable(people).flatMap({
@@ -115,14 +113,14 @@ class ParallelOperationCollectionsUnitTest {
115113
}, people.size) // Uses maxConcurrency for the number of elements
116114
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
117115

118-
startTime.printFooter()
116+
startTime.printTotalTime()
119117

120118
observable.assertResultsTrue()
121119
}
122120

123121
@Test
124122
fun `using RxKotlin for parallel operations`() { // ObservableKt.kt.class from io.reactivex.rxkotlin
125-
printHeader()
123+
"Using RxKotlin".printAsHeader()
126124
val startTime = Instant.now()
127125

128126
val observable = people.toObservable().flatMap({
@@ -133,14 +131,14 @@ class ParallelOperationCollectionsUnitTest {
133131
}, people.size) // Uses maxConcurrency for the number of elements
134132
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
135133

136-
startTime.printFooter()
134+
startTime.printTotalTime()
137135

138136
observable.assertResultsTrue()
139137
}
140138

141139
@Test
142140
fun `using RxKotlin but still use 1 thread`() { // ObservableKt.kt.class from io.reactivex.rxkotlin
143-
printHeader()
141+
"Using RxKotlin 1 thread".printAsHeader()
144142
val startTime = Instant.now()
145143

146144
val observable =
@@ -149,14 +147,14 @@ class ParallelOperationCollectionsUnitTest {
149147
person.printFormattedInfo()
150148
}.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
151149

152-
startTime.printFooter()
150+
startTime.printTotalTime()
153151

154152
observable.assertResultsTrue()
155153
}
156154

157155
@Test
158156
fun `using parallelStream()`() {
159-
printHeader()
157+
"Using Stream API".printAsHeader()
160158
val startTime = Instant.now()
161159

162160
val filteredPeople = people.parallelStream().map { person ->
@@ -165,14 +163,14 @@ class ParallelOperationCollectionsUnitTest {
165163
person
166164
}.filter { it.age > 15 }.sorted { p1, p2 -> p1.age.compareTo(p2.age) }.collect(Collectors.toList())
167165

168-
startTime.printFooter()
166+
startTime.printTotalTime()
169167

170168
filteredPeople.assertResultsTrue()
171169
}
172170

173171
@Test
174-
fun `using ScheduledExecutorService for parallel operations`() {
175-
printHeader()
172+
fun `using ExecutorService for parallel operations`() {
173+
"Using ExecutorService".printAsHeader()
176174
val startTime = Instant.now()
177175

178176
val executor = Executors.newFixedThreadPool(people.size)
@@ -186,7 +184,7 @@ class ParallelOperationCollectionsUnitTest {
186184

187185
executor.shutdown()
188186

189-
startTime.printFooter()
187+
startTime.printTotalTime()
190188

191189
futures.assertResultsTrue()
192190
}

0 commit comments

Comments
 (0)