diff --git a/kotlin/pom.xml b/kotlin/pom.xml index 7b45e9809b..bcf5b36385 100644 --- a/kotlin/pom.xml +++ b/kotlin/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung @@ -13,6 +13,15 @@ 1.0.0-SNAPSHOT + + + + central + http://jcenter.bintray.com + + + + org.jetbrains.kotlin @@ -31,6 +40,11 @@ ${kotlin-reflect.version} test + + org.jetbrains.kotlinx + kotlinx-coroutines-core + ${kotlinx.version} + @@ -104,10 +118,11 @@ - 1.1.1 - 1.1.1 - 1.1.1 - 1.1.1 + 1.1.2 + 1.1.2 + 1.1.2 + 1.1.2 + 0.15 \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt new file mode 100644 index 0000000000..54fafdb3e1 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesTest.kt @@ -0,0 +1,179 @@ +package com.baeldung.kotlin + +import kotlinx.coroutines.experimental.* +import org.junit.Test +import java.util.concurrent.atomic.AtomicInteger +import kotlin.coroutines.experimental.buildSequence +import kotlin.system.measureTimeMillis +import kotlin.test.assertEquals +import kotlin.test.assertTrue + + +class CoroutinesTest { + + @Test + fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() { + //given + val fibonacciSeq = buildSequence { + var a = 0 + var b = 1 + + yield(1) + + while (true) { + yield(a + b) + + val tmp = a + b + a = b + b = tmp + } + } + + //when + val res = fibonacciSeq.take(5).toList() + + //then + assertEquals(res, listOf(1, 1, 2, 3, 5)) + } + + @Test + fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() { + //given + val lazySeq = buildSequence { + print("START ") + for (i in 1..5) { + yield(i) + print("STEP ") + } + print("END") + } + //when + val res = lazySeq.take(10).toList() + + //then + assertEquals(res, listOf(1, 2, 3, 4, 5)) + } + + @Test + fun givenAsyncCoroutine_whenStartIt_thenShouldExecuteItInTheAsyncWay() { + //given + val res = mutableListOf() + + //when + runBlocking { + val promise = launch(CommonPool) { expensiveComputation(res) } + res.add("Hello,") + promise.join() + } + + //then + assertEquals(res, listOf("Hello,", "word!")) + } + + + suspend fun expensiveComputation(res: MutableList) { + delay(1000L) + res.add("word!") + } + + @Test + fun givenHugeAmountOfCoroutines_whenStartIt_thenShouldExecuteItWithoutOutOfMemory() { + runBlocking { + //given + val counter = AtomicInteger(0) + val numberOfCoroutines = 100_000 + + //when + val jobs = List(numberOfCoroutines) { + launch(CommonPool) { + delay(1L) + counter.incrementAndGet() + } + } + jobs.forEach { it.join() } + + //then + assertEquals(counter.get(), numberOfCoroutines) + } + } + + @Test + fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() { + runBlocking { + //given + val job = launch(CommonPool) { + while (isActive) { + println("is working") + } + } + + delay(1300L) + + //when + job.cancel() + + //then cancel successfully + + } + } + + @Test(expected = CancellationException::class) + fun givenAsyncAction_whenDeclareTimeout_thenShouldFinishWhenTimedOut() { + runBlocking { + withTimeout(1300L) { + repeat(1000) { i -> + println("Some expensive computation $i ...") + delay(500L) + } + } + } + } + + @Test + fun givenHaveTwoExpensiveAction_whenExecuteThemAsync_thenTheyShouldRunConcurrently() { + runBlocking { + val delay = 1000L + val time = measureTimeMillis { + //given + val one = async(CommonPool) { someExpensiveComputation(delay) } + val two = async(CommonPool) { someExpensiveComputation(delay) } + + //when + runBlocking { + one.await() + two.await() + } + } + + //then + assertTrue(time < delay * 2) + } + } + + @Test + fun givenTwoExpensiveAction_whenExecuteThemLazy_thenTheyShouldNotConcurrently() { + runBlocking { + val delay = 1000L + val time = measureTimeMillis { + //given + val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + + //when + runBlocking { + one.await() + two.await() + } + } + + //then + assertTrue(time > delay * 2) + } + } + + suspend fun someExpensiveComputation(delayInMilliseconds: Long) { + delay(delayInMilliseconds) + } + + +} diff --git a/pom.xml b/pom.xml index 91d7abd5ca..43f31d5c98 100644 --- a/pom.xml +++ b/pom.xml @@ -88,8 +88,6 @@ junit5 jws - kotlin - libraries log-mdc log4j