From 2d3accedcc9664364651208bca5e878e18a4d0bc Mon Sep 17 00:00:00 2001 From: Alfonso Lentini Date: Sun, 7 Oct 2018 13:08:12 +0200 Subject: [PATCH 1/4] Thread and Coroutines in Kotlin --- .../com/baeldung/thread/SimpleRunnable.kt | 8 +++ .../com/baeldung/thread/SimpleThread.kt | 8 +++ .../com/baeldung/thread/CoroutineUnitTest.kt | 68 +++++++++++++++++++ .../com/baeldung/thread/ThreadUnitTest.kt | 38 +++++++++++ 4 files changed, 122 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt new file mode 100644 index 0000000000..7bc0528d06 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleRunnable.kt @@ -0,0 +1,8 @@ +package com.baeldung.thread + +class SimpleRunnable: Runnable { + + override fun run() { + println("${Thread.currentThread()} has run.") + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt new file mode 100644 index 0000000000..2b2827ae02 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/thread/SimpleThread.kt @@ -0,0 +1,8 @@ +package com.baeldung.thread + +class SimpleThread: Thread() { + + override fun run() { + println("${Thread.currentThread()} has run.") + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt new file mode 100644 index 0000000000..215fa6710f --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt @@ -0,0 +1,68 @@ +package com.baeldung.thread + +import kotlinx.coroutines.experimental.* +import org.junit.jupiter.api.Test + +class CoroutineUnitTest { + + @Test + fun whenCreateLaunchCoroutineWithoutContext_thenRun() { + + val job = launch { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithDefaultContext_thenRun() { + + val job = launch(DefaultDispatcher) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithUnconfinedContext_thenRun() { + + val job = launch(Unconfined) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateLaunchCoroutineWithDedicatedThread_thenRun() { + + val job = launch(newSingleThreadContext("dedicatedThread")) { + println("${Thread.currentThread()} has run.") + } + + runBlocking { + job.join() + } + } + + @Test + fun whenCreateAsyncCoroutine_thenRun() { + + val deferred = async(Unconfined) { + return@async "${Thread.currentThread()} has run." + } + + runBlocking { + val result = deferred.await() + println(result) + } + } +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt new file mode 100644 index 0000000000..fa2f1edc36 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/ThreadUnitTest.kt @@ -0,0 +1,38 @@ +package com.baeldung.thread + +import org.junit.jupiter.api.Test +import kotlin.concurrent.thread + +class ThreadUnitTest { + + @Test + fun whenCreateThread_thenRun() { + + val thread = SimpleThread() + thread.start() + } + + @Test + fun whenCreateThreadWithRunnable_thenRun() { + + val threadWithRunnable = Thread(SimpleRunnable()) + threadWithRunnable.start() + } + + @Test + fun whenCreateThreadWithSAMConversions_thenRun() { + + val thread = Thread { + println("${Thread.currentThread()} has run.") + } + thread.start() + } + + @Test + fun whenCreateThreadWithMethodExtension_thenRun() { + + thread(start = true) { + println("${Thread.currentThread()} has run.") + } + } +} \ No newline at end of file From c315ce5975b1c0e719e580a1e03e3ff7abe78396 Mon Sep 17 00:00:00 2001 From: Alfonso Lentini Date: Sat, 13 Oct 2018 18:36:01 +0200 Subject: [PATCH 2/4] new coroutone API Koltin version 1.3-RC --- .../kotlin/com/baeldung/filter/SliceTest.kt | 16 +++---- .../com/baeldung/fuel/FuelHttpUnitTest.kt | 48 +++++++------------ .../com/baeldung/kotlin/CoroutinesUnitTest.kt | 9 ++-- .../com/baeldung/thread/CoroutineUnitTest.kt | 39 +++++---------- parent-kotlin/pom.xml | 21 +++++++- 5 files changed, 61 insertions(+), 72 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt index dca167928d..db2bfed947 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt @@ -24,14 +24,14 @@ internal class SliceTest { assertIterableEquals(expected, actual) } - @Test - fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() { - val original = arrayOf(12, 3, 34, 4) - val actual = original.slice(3..8) - val expected = listOf(4, null, null, null, null, null) - - assertIterableEquals(expected, actual) - } +// @Test +// fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() { +// val original = arrayOf(12, 3, 34, 4) +// val actual = original.slice(3..8) +// val expected = listOf(4, null, null, null, null, null) +// +// assertIterableEquals(expected, actual) +// } @Test fun whenSlicingBeyondRangeOfArrayWithStep_thenOutOfBoundsException() { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt index f0f9267618..74b2dd9fa1 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt @@ -1,16 +1,12 @@ package com.baeldung.fuel -import awaitObjectResult -import awaitStringResponse import com.github.kittinunf.fuel.Fuel import com.github.kittinunf.fuel.core.FuelManager -import com.github.kittinunf.fuel.core.Request import com.github.kittinunf.fuel.core.interceptors.cUrlLoggingRequestInterceptor import com.github.kittinunf.fuel.gson.responseObject import com.github.kittinunf.fuel.httpGet import com.github.kittinunf.fuel.rx.rx_object import com.google.gson.Gson -import kotlinx.coroutines.experimental.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test import java.io.File @@ -226,32 +222,24 @@ internal class FuelHttpUnitTest { } - @Test - fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() { - - runBlocking { - val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse() - - result.fold({ data -> - Assertions.assertEquals(200, response.statusCode) - - }, { error -> }) - } - - } - - @Test - fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() { - - - runBlocking { - Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer()) - .fold({ data -> - Assertions.assertEquals(1, data.get(0).userId) - }, { error -> }) - } - - } +// @Test +// fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() = runBlocking { +// val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse() +// +// result.fold({ data -> +// Assertions.assertEquals(200, response.statusCode) +// +// }, { error -> }) +// } +// +// +// @Test +// fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() = runBlocking { +// Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer()) +// .fold({ data -> +// Assertions.assertEquals(1, data.get(0).userId) +// }, { error -> }) +// } @Test fun whenMakeGETPostRequestUsingRoutingAPI_thenDeserializeResponse() { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt index d724933654..b6f28a4903 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt @@ -1,9 +1,8 @@ package com.baeldung.kotlin -import kotlinx.coroutines.experimental.* +import kotlinx.coroutines.* 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 @@ -14,7 +13,7 @@ class CoroutinesTest { @Test fun givenBuildSequence_whenTakeNElements_thenShouldReturnItInALazyWay() { //given - val fibonacciSeq = buildSequence { + val fibonacciSeq = sequence { var a = 0 var b = 1 @@ -39,7 +38,7 @@ class CoroutinesTest { @Test fun givenLazySeq_whenTakeNElements_thenShouldReturnAllElements() { //given - val lazySeq = buildSequence { + val lazySeq = sequence { print("START ") for (i in 1..5) { yield(i) @@ -60,7 +59,7 @@ class CoroutinesTest { val res = mutableListOf() //when - runBlocking { + runBlocking { val promise = launch(CommonPool) { expensiveComputation(res) } res.add("Hello,") promise.join() diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt index 215fa6710f..f87409253f 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt @@ -1,68 +1,53 @@ package com.baeldung.thread -import kotlinx.coroutines.experimental.* +import kotlinx.coroutines.* import org.junit.jupiter.api.Test class CoroutineUnitTest { @Test - fun whenCreateLaunchCoroutineWithoutContext_thenRun() { + fun whenCreateCoroutineWithLaunchWithoutContext_thenRun() = runBlocking { val job = launch { println("${Thread.currentThread()} has run.") } - runBlocking { - job.join() - } } @Test - fun whenCreateLaunchCoroutineWithDefaultContext_thenRun() { + fun whenCreateCoroutineWithLaunchWithDefaultContext_thenRun() = runBlocking { - val job = launch(DefaultDispatcher) { + val job = launch(Dispatchers.Default) { println("${Thread.currentThread()} has run.") } - - runBlocking { - job.join() - } } @Test - fun whenCreateLaunchCoroutineWithUnconfinedContext_thenRun() { + fun whenCreateCoroutineWithLaunchWithUnconfinedContext_thenRun() = runBlocking { - val job = launch(Unconfined) { + val job = launch(Dispatchers.Unconfined) { println("${Thread.currentThread()} has run.") } - - runBlocking { - job.join() - } } @Test - fun whenCreateLaunchCoroutineWithDedicatedThread_thenRun() { + fun whenCreateCoroutineWithLaunchWithDedicatedThread_thenRun() = runBlocking { val job = launch(newSingleThreadContext("dedicatedThread")) { println("${Thread.currentThread()} has run.") } - runBlocking { - job.join() - } } @Test - fun whenCreateAsyncCoroutine_thenRun() { + fun whenCreateAsyncCoroutine_thenRun() = runBlocking { - val deferred = async(Unconfined) { + val deferred = async(Dispatchers.IO) { return@async "${Thread.currentThread()} has run." } - runBlocking { - val result = deferred.await() - println(result) - } + + val result = deferred.await() + println(result) } } \ No newline at end of file diff --git a/parent-kotlin/pom.xml b/parent-kotlin/pom.xml index 7fd18e4fa4..0a04da7dc2 100644 --- a/parent-kotlin/pom.xml +++ b/parent-kotlin/pom.xml @@ -22,7 +22,18 @@ kotlin-ktor https://dl.bintray.com/kotlin/ktor/ + + kotlin-eap + http://dl.bintray.com/kotlin/kotlin-eap + + + + + kotlin-eap + http://dl.bintray.com/kotlin/kotlin-eap + + @@ -42,6 +53,11 @@ kotlin-stdlib-jdk8 ${kotlin.version} + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + org.jetbrains.kotlin kotlin-reflect @@ -157,6 +173,7 @@ + org.apache.maven.plugins maven-failsafe-plugin ${maven-failsafe-plugin.version} @@ -185,8 +202,8 @@ - 1.2.61 - 0.25.0 + 1.3.0-rc-146 + 0.26.1-eap13 0.9.3 3.11.0 1.2.0 From e3b1152c5e05e38baa91a781727c5ed5bffd5c41 Mon Sep 17 00:00:00 2001 From: Alfonso Lentini Date: Sun, 14 Oct 2018 10:50:00 +0200 Subject: [PATCH 3/4] CommonPool deprecated --- .../com/baeldung/kotlin/CoroutinesUnitTest.kt | 14 +++++++------- .../com/baeldung/thread/CoroutineUnitTest.kt | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt index b6f28a4903..324cf1109b 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/CoroutinesUnitTest.kt @@ -60,7 +60,7 @@ class CoroutinesTest { //when runBlocking { - val promise = launch(CommonPool) { expensiveComputation(res) } + val promise = launch(Dispatchers.Default) { expensiveComputation(res) } res.add("Hello,") promise.join() } @@ -84,7 +84,7 @@ class CoroutinesTest { //when val jobs = List(numberOfCoroutines) { - launch(CommonPool) { + launch(Dispatchers.Default) { delay(1L) counter.incrementAndGet() } @@ -100,7 +100,7 @@ class CoroutinesTest { fun givenCancellableJob_whenRequestForCancel_thenShouldQuit() { runBlocking { //given - val job = launch(CommonPool) { + val job = launch(Dispatchers.Default) { while (isActive) { //println("is working") } @@ -134,8 +134,8 @@ class CoroutinesTest { val delay = 1000L val time = measureTimeMillis { //given - val one = async(CommonPool) { someExpensiveComputation(delay) } - val two = async(CommonPool) { someExpensiveComputation(delay) } + val one = async(Dispatchers.Default) { someExpensiveComputation(delay) } + val two = async(Dispatchers.Default) { someExpensiveComputation(delay) } //when runBlocking { @@ -155,8 +155,8 @@ class CoroutinesTest { val delay = 1000L val time = measureTimeMillis { //given - val one = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } - val two = async(CommonPool, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + val one = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) } + val two = async(Dispatchers.Default, CoroutineStart.LAZY) { someExpensiveComputation(delay) } //when runBlocking { diff --git a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt index f87409253f..1f1609b06b 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/thread/CoroutineUnitTest.kt @@ -46,7 +46,6 @@ class CoroutineUnitTest { return@async "${Thread.currentThread()} has run." } - val result = deferred.await() println(result) } From a828ae21377d86727c598c56a7f4a4af11f788bd Mon Sep 17 00:00:00 2001 From: Alfonso Lentini Date: Tue, 23 Oct 2018 23:40:46 +0200 Subject: [PATCH 4/4] Added comments --- .../src/test/kotlin/com/baeldung/filter/SliceTest.kt | 1 + .../src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt index db2bfed947..793fe68427 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/filter/SliceTest.kt @@ -24,6 +24,7 @@ internal class SliceTest { assertIterableEquals(expected, actual) } +// From the 1.3 version of Kotlin APIs, slice doesn't return array of nulls but throw IndexOutOfBoundsException // @Test // fun whenSlicingBeyondTheRangeOfTheArray_thenContainManyNulls() { // val original = arrayOf(12, 3, 34, 4) diff --git a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt index 74b2dd9fa1..b7993c5f43 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/fuel/FuelHttpUnitTest.kt @@ -222,6 +222,8 @@ internal class FuelHttpUnitTest { } + +// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library // @Test // fun whenMakeGETRequestUsingCoroutines_thenResponseStatusCode200() = runBlocking { // val (request, response, result) = Fuel.get("http://httpbin.org/get").awaitStringResponse() @@ -231,8 +233,8 @@ internal class FuelHttpUnitTest { // // }, { error -> }) // } -// -// + +// The new 1.3 coroutine APIs, aren't implemented yet in Fuel Library // @Test // fun whenMakeGETRequestUsingCoroutines_thenDeserializeResponse() = runBlocking { // Fuel.get("https://jsonplaceholder.typicode.com/posts?id=1").awaitObjectResult(Post.Deserializer())