Skip to content

Commit 69c330e

Browse files
tomekl007pivovarit
authored andcommitted
BAEL-867 coroutines tests (eugenp#2011)
* BAEL-867 coroutines tests * BAEL-867 add two channels examples * BAEL-867 fix example * BAEL-867 remove channels * BAEL-867 optimize imports * BAEL-867 remove kotlin module
1 parent df782e4 commit 69c330e

3 files changed

Lines changed: 199 additions & 7 deletions

File tree

‎kotlin/pom.xml‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

66
<groupId>com.baeldung</groupId>
@@ -13,6 +13,15 @@
1313
<version>1.0.0-SNAPSHOT</version>
1414
</parent>
1515

16+
17+
<repositories>
18+
<repository>
19+
<id>central</id>
20+
<url>http://jcenter.bintray.com</url>
21+
</repository>
22+
</repositories>
23+
24+
1625
<dependencies>
1726
<dependency>
1827
<groupId>org.jetbrains.kotlin</groupId>
@@ -31,6 +40,11 @@
3140
<version>${kotlin-reflect.version}</version>
3241
<scope>test</scope>
3342
</dependency>
43+
<dependency>
44+
<groupId>org.jetbrains.kotlinx</groupId>
45+
<artifactId>kotlinx-coroutines-core</artifactId>
46+
<version>${kotlinx.version}</version>
47+
</dependency>
3448
</dependencies>
3549

3650

@@ -104,10 +118,11 @@
104118
</build>
105119

106120
<properties>
107-
<kotlin-maven-plugin.version>1.1.1</kotlin-maven-plugin.version>
108-
<kotlin-test-junit.version>1.1.1</kotlin-test-junit.version>
109-
<kotlin-stdlib.version>1.1.1</kotlin-stdlib.version>
110-
<kotlin-reflect.version>1.1.1</kotlin-reflect.version>
121+
<kotlin-maven-plugin.version>1.1.2</kotlin-maven-plugin.version>
122+
<kotlin-test-junit.version>1.1.2</kotlin-test-junit.version>
123+
<kotlin-stdlib.version>1.1.2</kotlin-stdlib.version>
124+
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
125+
<kotlinx.version>0.15</kotlinx.version>
111126
</properties>
112127

113128
</project>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package com.baeldung.kotlin
2+
3+
import kotlinx.coroutines.experimental.*
4+
import org.junit.Test
5+
import java.util.concurrent.atomic.AtomicInteger
6+
import kotlin.coroutines.experimental.buildSequence
7+
import kotlin.system.measureTimeMillis
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertTrue
10+
11+
12+
class CoroutinesTest {
13+
14+
@Test
15+
fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() {
16+
//given
17+
val fibonacciSeq = buildSequence {
18+
var a = 0
19+
var b = 1
20+
21+
yield(1)
22+
23+
while (true) {
24+
yield(a + b)
25+
26+
val tmp = a + b
27+
a = b
28+
b = tmp
29+
}
30+
}
31+
32+
//when
33+
val res = fibonacciSeq.take(5).toList()
34+
35+
//then
36+
assertEquals(res, listOf(1, 1, 2, 3, 5))
37+
}
38+
39+
@Test
40+
fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() {
41+
//given
42+
val lazySeq = buildSequence {
43+
print("START ")
44+
for (i in 1..5) {
45+
yield(i)
46+
print("STEP ")
47+
}
48+
print("END")
49+
}
50+
//when
51+
val res = lazySeq.take(10).toList()
52+
53+
//then
54+
assertEquals(res, listOf(1, 2, 3, 4, 5))
55+
}
56+
57+
@Test
58+
fun givenAsyncCoroutine_whenStartIt_thenShouldExecuteItInTheAsyncWay() {
59+
//given
60+
val res = mutableListOf<String>()
61+
62+
//when
63+
runBlocking<Unit> {
64+
val promise = launch(CommonPool) { expensiveComputation(res) }
65+
res.add("Hello,")
66+
promise.join()
67+
}
68+
69+
//then
70+
assertEquals(res, listOf("Hello,", "word!"))
71+
}
72+
73+
74+
suspend fun expensiveComputation(res: MutableList<String>) {
75+
delay(1000L)
76+
res.add("word!")
77+
}
78+
79+
@Test
80+
fun givenHugeAmountOfCoroutines_whenStartIt_thenShouldExecuteItWithoutOutOfMemory() {
81+
runBlocking<Unit> {
82+
//given
83+
val counter = AtomicInteger(0)
84+
val numberOfCoroutines = 100_000
85+
86+
//when
87+
val jobs = List(numberOfCoroutines) {
88+
launch(CommonPool) {
89+
delay(1L)
90+
counter.incrementAndGet()
91+
}
92+
}
93+
jobs.forEach { it.join() }
94+
95+
//then
96+
assertEquals(counter.get(), numberOfCoroutines)
97+
}
98+
}
99+
100+
@Test
101+
fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() {
102+
runBlocking<Unit> {
103+
//given
104+
val job = launch(CommonPool) {
105+
while (isActive) {
106+
println("is working")
107+
}
108+
}
109+
110+
delay(1300L)
111+
112+
//when
113+
job.cancel()
114+
115+
//then cancel successfully
116+
117+
}
118+
}
119+
120+
@Test(expected = CancellationException::class)
121+
fun givenAsyncAction_whenDeclareTimeout_thenShouldFinishWhenTimedOut() {
122+
runBlocking<Unit> {
123+
withTimeout(1300L) {
124+
repeat(1000) { i ->
125+
println("Some expensive computation $i ...")
126+
delay(500L)
127+
}
128+
}
129+
}
130+
}
131+
132+
@Test
133+
fun givenHaveTwoExpensiveAction_whenExecuteThemAsync_thenTheyShouldRunConcurrently() {
134+
runBlocking<Unit> {
135+
val delay = 1000L
136+
val time = measureTimeMillis {
137+
//given
138+
val one = async(CommonPool) { someExpensiveComputation(delay) }
139+
val two = async(CommonPool) { someExpensiveComputation(delay) }
140+
141+
//when
142+
runBlocking {
143+
one.await()
144+
two.await()
145+
}
146+
}
147+
148+
//then
149+
assertTrue(time < delay * 2)
150+
}
151+
}
152+
153+
@Test
154+
fun givenTwoExpensiveAction_whenExecuteThemLazy_thenTheyShouldNotConcurrently() {
155+
runBlocking<Unit> {
156+
val delay = 1000L
157+
val time = measureTimeMillis {
158+
//given
159+
val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
160+
val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) }
161+
162+
//when
163+
runBlocking {
164+
one.await()
165+
two.await()
166+
}
167+
}
168+
169+
//then
170+
assertTrue(time > delay * 2)
171+
}
172+
}
173+
174+
suspend fun someExpensiveComputation(delayInMilliseconds: Long) {
175+
delay(delayInMilliseconds)
176+
}
177+
178+
179+
}

‎pom.xml‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@
8888
<module>junit5</module>
8989
<module>jws</module>
9090

91-
<module>kotlin</module>
92-
9391
<module>libraries</module>
9492
<module>log-mdc</module>
9593
<module>log4j</module>

0 commit comments

Comments
 (0)