Skip to content

Commit 6f5e5c3

Browse files
authored
[KTLN-763] Add samples (#1092)
* [KTLN-177] Add samples * [KTLN-177] Extract strings to properties
1 parent e32234b commit 6f5e5c3

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

core-kotlin-modules/core-kotlin-concurrency-4/pom.xml

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,29 @@
1111

1212
<artifactId>core-kotlin-concurrency-4</artifactId>
1313

14-
</project>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.assertj</groupId>
17+
<artifactId>assertj-core</artifactId>
18+
<version>${assertj.version}</version>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.jetbrains.kotlinx</groupId>
23+
<artifactId>kotlinx-coroutines-core</artifactId>
24+
<version>${kotlinx.coroutines.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.jetbrains.kotlinx</groupId>
28+
<artifactId>kotlinx-coroutines-test</artifactId>
29+
<version>${kotlinx.coroutines.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
34+
<properties>
35+
<project.version>1.0.0-SNAPSHOT</project.version>
36+
<assertj.version>3.24.2</assertj.version>
37+
<kotlinx.coroutines.version>1.7.3</kotlinx.coroutines.version>
38+
</properties>
39+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.schedulertimer
2+
3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.TimeoutCancellationException
6+
import kotlinx.coroutines.delay
7+
import kotlinx.coroutines.isActive
8+
import kotlinx.coroutines.launch
9+
import kotlinx.coroutines.runBlocking
10+
import kotlinx.coroutines.withTimeout
11+
import org.assertj.core.api.Assertions.assertThat
12+
import org.assertj.core.api.Assertions.within
13+
import org.junit.jupiter.api.Assertions.assertTrue
14+
import org.junit.jupiter.api.Test
15+
import org.junit.jupiter.api.assertThrows
16+
17+
fun CoroutineScope.startTimer(delay: Long = 1000L, task: suspend () -> Unit): Job {
18+
return launch {
19+
delay(delay)
20+
task()
21+
}
22+
}
23+
24+
fun CoroutineScope.startInfiniteScheduler(interval: Long = 1000L, task: suspend () -> Unit): Job {
25+
return launch {
26+
while (isActive) {
27+
task()
28+
delay(interval)
29+
}
30+
}
31+
}
32+
33+
suspend fun runWithTimeout(timeout: Long, task: suspend () -> Unit) {
34+
withTimeout(timeout) {
35+
task()
36+
}
37+
}
38+
39+
40+
class SchedulerTimerUnitTest {
41+
42+
@Test
43+
fun `timer executes task after delay`() = runBlocking {
44+
var taskExecuted = false
45+
val task: suspend () -> Unit = { taskExecuted = true }
46+
47+
startTimer(delay = 1000L, task = task)
48+
delay(1500L)
49+
50+
assertTrue(taskExecuted)
51+
}
52+
53+
@Test
54+
fun `infinite scheduler stops when scope is canceled`(): Unit = runBlocking {
55+
var taskExecutionCount = 0
56+
val task: suspend () -> Unit = { taskExecutionCount++ }
57+
58+
val schedulerJob = startInfiniteScheduler(interval = 500L, task = task)
59+
delay(1500L)
60+
61+
schedulerJob.cancel()
62+
schedulerJob.join()
63+
64+
assertThat(taskExecutionCount).isCloseTo(3, within(1))
65+
}
66+
67+
@Test
68+
fun `runWithTimeout throws TimeoutCancellationException if task exceeds timeout`(): Unit = runBlocking {
69+
val task: suspend () -> Unit = { delay(2000L) }
70+
assertThrows<TimeoutCancellationException> {
71+
runWithTimeout(timeout = 1000L, task = task)
72+
}
73+
}
74+
75+
}

0 commit comments

Comments
 (0)