Skip to content

Commit 57b6480

Browse files
sazzerpivovarit
authored andcommitted
Examples for article on Kovenant (eugenp#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
1 parent ba32ee3 commit 57b6480

3 files changed

Lines changed: 235 additions & 0 deletions

File tree

‎core-kotlin/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
<artifactId>fuel-coroutines</artifactId>
5656
<version>${fuel.version}</version>
5757
</dependency>
58+
<dependency>
59+
<groupId>nl.komponents.kovenant</groupId>
60+
<artifactId>kovenant</artifactId>
61+
<version>3.3.0</version>
62+
<type>pom</type>
63+
</dependency>
5864
</dependencies>
5965

6066
<properties>
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.baeldung.kotlin
2+
3+
import nl.komponents.kovenant.*
4+
import nl.komponents.kovenant.Kovenant.deferred
5+
import nl.komponents.kovenant.combine.and
6+
import nl.komponents.kovenant.combine.combine
7+
import org.junit.Assert
8+
import org.junit.Before
9+
import org.junit.Test
10+
import java.io.IOException
11+
import java.util.*
12+
import java.util.concurrent.TimeUnit
13+
14+
class KovenantTest {
15+
@Before
16+
fun setupTestMode() {
17+
Kovenant.testMode { error ->
18+
println("An unexpected error occurred")
19+
Assert.fail(error.message)
20+
}
21+
}
22+
23+
@Test
24+
fun testSuccessfulDeferred() {
25+
val def = deferred<Long, Exception>()
26+
Assert.assertFalse(def.promise.isDone())
27+
28+
def.resolve(1L)
29+
Assert.assertTrue(def.promise.isDone())
30+
Assert.assertTrue(def.promise.isSuccess())
31+
Assert.assertFalse(def.promise.isFailure())
32+
}
33+
34+
@Test
35+
fun testFailedDeferred() {
36+
val def = deferred<Long, Exception>()
37+
Assert.assertFalse(def.promise.isDone())
38+
39+
def.reject(RuntimeException())
40+
Assert.assertTrue(def.promise.isDone())
41+
Assert.assertFalse(def.promise.isSuccess())
42+
Assert.assertTrue(def.promise.isFailure())
43+
}
44+
45+
@Test
46+
fun testResolveDeferredTwice() {
47+
val def = deferred<Long, Exception>()
48+
def.resolve(1L)
49+
try {
50+
def.resolve(1L)
51+
} catch (e: AssertionError) {
52+
// Expected.
53+
// This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode()
54+
}
55+
}
56+
57+
@Test
58+
fun testSuccessfulTask() {
59+
val promise = task { 1L }
60+
Assert.assertTrue(promise.isDone())
61+
Assert.assertTrue(promise.isSuccess())
62+
Assert.assertFalse(promise.isFailure())
63+
}
64+
65+
@Test
66+
fun testFailedTask() {
67+
val promise = task { throw RuntimeException() }
68+
Assert.assertTrue(promise.isDone())
69+
Assert.assertFalse(promise.isSuccess())
70+
Assert.assertTrue(promise.isFailure())
71+
}
72+
73+
@Test
74+
fun testCallbacks() {
75+
val promise = task { 1L }
76+
77+
promise.success {
78+
println("This was a success")
79+
Assert.assertEquals(1L, it)
80+
}
81+
82+
promise.fail {
83+
println(it)
84+
Assert.fail("This shouldn't happen")
85+
}
86+
87+
promise.always {
88+
println("This always happens")
89+
}
90+
}
91+
92+
@Test
93+
fun testGetValues() {
94+
val promise = task { 1L }
95+
Assert.assertEquals(1L, promise.get())
96+
}
97+
98+
@Test
99+
fun testAllSucceed() {
100+
val numbers = all(
101+
task { 1L },
102+
task { 2L },
103+
task { 3L }
104+
)
105+
106+
Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get())
107+
}
108+
109+
@Test
110+
fun testOneFails() {
111+
val runtimeException = RuntimeException()
112+
113+
val numbers = all(
114+
task { 1L },
115+
task { 2L },
116+
task { throw runtimeException }
117+
)
118+
119+
Assert.assertEquals(runtimeException, numbers.getError())
120+
}
121+
122+
@Test
123+
fun testAnySucceeds() {
124+
val promise = any(
125+
task {
126+
TimeUnit.SECONDS.sleep(3)
127+
1L
128+
},
129+
task {
130+
TimeUnit.SECONDS.sleep(2)
131+
2L
132+
},
133+
task {
134+
TimeUnit.SECONDS.sleep(1)
135+
3L
136+
}
137+
)
138+
139+
Assert.assertTrue(promise.isDone())
140+
Assert.assertTrue(promise.isSuccess())
141+
Assert.assertFalse(promise.isFailure())
142+
}
143+
144+
@Test
145+
fun testAllFails() {
146+
val runtimeException = RuntimeException()
147+
val ioException = IOException()
148+
val illegalStateException = IllegalStateException()
149+
val promise = any(
150+
task {
151+
TimeUnit.SECONDS.sleep(3)
152+
throw runtimeException
153+
},
154+
task {
155+
TimeUnit.SECONDS.sleep(2)
156+
throw ioException
157+
},
158+
task {
159+
TimeUnit.SECONDS.sleep(1)
160+
throw illegalStateException
161+
}
162+
)
163+
164+
Assert.assertTrue(promise.isDone())
165+
Assert.assertFalse(promise.isSuccess())
166+
Assert.assertTrue(promise.isFailure())
167+
}
168+
169+
@Test
170+
fun testSimpleCombine() {
171+
val promise = task { 1L } and task { "Hello" }
172+
val result = promise.get()
173+
174+
Assert.assertEquals(1L, result.first)
175+
Assert.assertEquals("Hello", result.second)
176+
}
177+
178+
@Test
179+
fun testLongerCombine() {
180+
val promise = combine(
181+
task { 1L },
182+
task { "Hello" },
183+
task { Currency.getInstance("USD") }
184+
)
185+
val result = promise.get()
186+
187+
Assert.assertEquals(1L, result.first)
188+
Assert.assertEquals("Hello", result.second)
189+
Assert.assertEquals(Currency.getInstance("USD"), result.third)
190+
}
191+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.kotlin
2+
3+
import nl.komponents.kovenant.Promise
4+
import nl.komponents.kovenant.any
5+
import nl.komponents.kovenant.task
6+
import org.junit.Assert
7+
import org.junit.Ignore
8+
import org.junit.Test
9+
10+
@Ignore
11+
// Note that this can not run in the same test run if KovenantTest has already been executed
12+
class KovenantTimeoutTest {
13+
@Test
14+
fun testTimeout() {
15+
val promise = timedTask(1000) { "Hello" }
16+
val result = promise.get()
17+
Assert.assertEquals("Hello", result)
18+
}
19+
20+
@Test
21+
fun testTimeoutExpired() {
22+
val promise = timedTask(1000) {
23+
Thread.sleep(3000)
24+
"Hello"
25+
}
26+
val result = promise.get()
27+
Assert.assertNull(result)
28+
}
29+
30+
fun <T> timedTask(millis: Long, body: () -> T) : Promise<T?, List<Exception>> {
31+
val timeoutTask = task {
32+
Thread.sleep(millis)
33+
null
34+
}
35+
val activeTask = task(body = body)
36+
return any(activeTask, timeoutTask)
37+
}
38+
}

0 commit comments

Comments
 (0)