Examples for article on Kovenant (#5011)
* Examples of Reflection in Kotlin * Some article updates to make better use of Assert * Replaced printlines with log statements * Added @Ignore to so tests with no assertions * Examples for Kovenant * Fixed an issue where tests fail depending on the order they are run * Test for timeouts on promises
This commit is contained in:
parent
ba32ee32c6
commit
57b6480e73
|
@ -55,6 +55,12 @@
|
|||
<artifactId>fuel-coroutines</artifactId>
|
||||
<version>${fuel.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.komponents.kovenant</groupId>
|
||||
<artifactId>kovenant</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,191 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
import nl.komponents.kovenant.*
|
||||
import nl.komponents.kovenant.Kovenant.deferred
|
||||
import nl.komponents.kovenant.combine.and
|
||||
import nl.komponents.kovenant.combine.combine
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
class KovenantTest {
|
||||
@Before
|
||||
fun setupTestMode() {
|
||||
Kovenant.testMode { error ->
|
||||
println("An unexpected error occurred")
|
||||
Assert.fail(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSuccessfulDeferred() {
|
||||
val def = deferred<Long, Exception>()
|
||||
Assert.assertFalse(def.promise.isDone())
|
||||
|
||||
def.resolve(1L)
|
||||
Assert.assertTrue(def.promise.isDone())
|
||||
Assert.assertTrue(def.promise.isSuccess())
|
||||
Assert.assertFalse(def.promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFailedDeferred() {
|
||||
val def = deferred<Long, Exception>()
|
||||
Assert.assertFalse(def.promise.isDone())
|
||||
|
||||
def.reject(RuntimeException())
|
||||
Assert.assertTrue(def.promise.isDone())
|
||||
Assert.assertFalse(def.promise.isSuccess())
|
||||
Assert.assertTrue(def.promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testResolveDeferredTwice() {
|
||||
val def = deferred<Long, Exception>()
|
||||
def.resolve(1L)
|
||||
try {
|
||||
def.resolve(1L)
|
||||
} catch (e: AssertionError) {
|
||||
// Expected.
|
||||
// This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSuccessfulTask() {
|
||||
val promise = task { 1L }
|
||||
Assert.assertTrue(promise.isDone())
|
||||
Assert.assertTrue(promise.isSuccess())
|
||||
Assert.assertFalse(promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFailedTask() {
|
||||
val promise = task { throw RuntimeException() }
|
||||
Assert.assertTrue(promise.isDone())
|
||||
Assert.assertFalse(promise.isSuccess())
|
||||
Assert.assertTrue(promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCallbacks() {
|
||||
val promise = task { 1L }
|
||||
|
||||
promise.success {
|
||||
println("This was a success")
|
||||
Assert.assertEquals(1L, it)
|
||||
}
|
||||
|
||||
promise.fail {
|
||||
println(it)
|
||||
Assert.fail("This shouldn't happen")
|
||||
}
|
||||
|
||||
promise.always {
|
||||
println("This always happens")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testGetValues() {
|
||||
val promise = task { 1L }
|
||||
Assert.assertEquals(1L, promise.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllSucceed() {
|
||||
val numbers = all(
|
||||
task { 1L },
|
||||
task { 2L },
|
||||
task { 3L }
|
||||
)
|
||||
|
||||
Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOneFails() {
|
||||
val runtimeException = RuntimeException()
|
||||
|
||||
val numbers = all(
|
||||
task { 1L },
|
||||
task { 2L },
|
||||
task { throw runtimeException }
|
||||
)
|
||||
|
||||
Assert.assertEquals(runtimeException, numbers.getError())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAnySucceeds() {
|
||||
val promise = any(
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(3)
|
||||
1L
|
||||
},
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(2)
|
||||
2L
|
||||
},
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(1)
|
||||
3L
|
||||
}
|
||||
)
|
||||
|
||||
Assert.assertTrue(promise.isDone())
|
||||
Assert.assertTrue(promise.isSuccess())
|
||||
Assert.assertFalse(promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAllFails() {
|
||||
val runtimeException = RuntimeException()
|
||||
val ioException = IOException()
|
||||
val illegalStateException = IllegalStateException()
|
||||
val promise = any(
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(3)
|
||||
throw runtimeException
|
||||
},
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(2)
|
||||
throw ioException
|
||||
},
|
||||
task {
|
||||
TimeUnit.SECONDS.sleep(1)
|
||||
throw illegalStateException
|
||||
}
|
||||
)
|
||||
|
||||
Assert.assertTrue(promise.isDone())
|
||||
Assert.assertFalse(promise.isSuccess())
|
||||
Assert.assertTrue(promise.isFailure())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSimpleCombine() {
|
||||
val promise = task { 1L } and task { "Hello" }
|
||||
val result = promise.get()
|
||||
|
||||
Assert.assertEquals(1L, result.first)
|
||||
Assert.assertEquals("Hello", result.second)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLongerCombine() {
|
||||
val promise = combine(
|
||||
task { 1L },
|
||||
task { "Hello" },
|
||||
task { Currency.getInstance("USD") }
|
||||
)
|
||||
val result = promise.get()
|
||||
|
||||
Assert.assertEquals(1L, result.first)
|
||||
Assert.assertEquals("Hello", result.second)
|
||||
Assert.assertEquals(Currency.getInstance("USD"), result.third)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.kotlin
|
||||
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.any
|
||||
import nl.komponents.kovenant.task
|
||||
import org.junit.Assert
|
||||
import org.junit.Ignore
|
||||
import org.junit.Test
|
||||
|
||||
@Ignore
|
||||
// Note that this can not run in the same test run if KovenantTest has already been executed
|
||||
class KovenantTimeoutTest {
|
||||
@Test
|
||||
fun testTimeout() {
|
||||
val promise = timedTask(1000) { "Hello" }
|
||||
val result = promise.get()
|
||||
Assert.assertEquals("Hello", result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testTimeoutExpired() {
|
||||
val promise = timedTask(1000) {
|
||||
Thread.sleep(3000)
|
||||
"Hello"
|
||||
}
|
||||
val result = promise.get()
|
||||
Assert.assertNull(result)
|
||||
}
|
||||
|
||||
fun <T> timedTask(millis: Long, body: () -> T) : Promise<T?, List<Exception>> {
|
||||
val timeoutTask = task {
|
||||
Thread.sleep(millis)
|
||||
null
|
||||
}
|
||||
val activeTask = task(body = body)
|
||||
return any(activeTask, timeoutTask)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue