Skip to content

Commit b671eff

Browse files
committed
update
1 parent 42f343a commit b671eff

File tree

3 files changed

+17
-45
lines changed

3 files changed

+17
-45
lines changed

core-kotlin-modules/core-kotlin-concurrency-3/src/main/java/com/baeldung/schedulingrepeatingtask/UsingScheduledThreadPool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fun main(){
77
val scheduler = Executors.newSingleThreadScheduledExecutor()
88
scheduler.scheduleAtFixedRate({
99
println("Complex task completed!")
10-
}, 0L, 1L, TimeUnit.SECONDS)
10+
}, 0, 1, TimeUnit.SECONDS)
1111
}

core-kotlin-modules/core-kotlin-concurrency-3/src/main/java/com/baeldung/schedulingrepeatingtask/UsingTimer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlin.concurrent.schedule
55

66
fun main(){
77
val timer = Timer()
8-
timer.schedule(0L, 1000L) {
8+
timer.schedule(0, 1000) {
99
println("Timer ticked!")
1010
}
1111
}

core-kotlin-modules/core-kotlin-concurrency-3/src/test/java/com/baeldung/schedulingrepeatingtask/SchedulingRepeatingTaskUnitTest.kt

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.flow
55
import kotlinx.coroutines.flow.flowOf
66
import kotlinx.coroutines.flow.take
77
import kotlinx.coroutines.flow.transform
8+
import org.junit.jupiter.api.Assertions.assertEquals
89
import org.junit.jupiter.api.Test
910
import org.junit.jupiter.api.assertThrows
1011
import kotlin.time.Duration.Companion.milliseconds
@@ -13,10 +14,14 @@ class SchedulingRepeatingTaskUnitTest {
1314

1415
@Test
1516
fun `using repeat and delay`() = runBlocking {
17+
var count = 0
1618
repeat(10) {
17-
println("Timer ticked!")
18-
delay(1000)
19+
count++
20+
println("Timer ticked! $count")
21+
delay(500)
1922
}
23+
24+
assertEquals(10, count)
2025
}
2126

2227
@Test
@@ -33,56 +38,23 @@ class SchedulingRepeatingTaskUnitTest {
3338

3439
// Using flow & collect
3540
@Test
36-
fun `using flow collect`() = runBlocking {
37-
var count = 0
38-
val job = launch {
39-
flow {
40-
while (true) {
41-
emit(Unit)
42-
delay(1000.milliseconds)
43-
}
44-
}.collect {
45-
count++
46-
println("Task executed")
47-
if (count == 10) cancel() // cancel after 10 executions
48-
}
49-
}
50-
job.join()
51-
}
41+
fun `using flow collect take`() = runBlocking {
5242

53-
@Test
54-
fun `using flow and take`() = runBlocking {
55-
flow {
43+
val flow = flow {
5644
while (true) {
5745
emit(Unit)
5846
delay(1000.milliseconds)
5947
}
60-
}.take(10).collect {
61-
println("Task executed times")
6248
}
63-
}
6449

65-
// using flowOf & collect
66-
@Test
67-
fun `using flowOf`() = runBlocking {
68-
flowOf(1, 2, 3, 4, 5).collect {
69-
println("Received value: $it")
70-
delay(1000.milliseconds)
71-
}
72-
}
50+
var count = 0
7351

74-
// using flowOf, transform and collect
75-
@Test
76-
fun `using flowOf with transform`() = runBlocking {
77-
flowOf(1, 2, 3, 4, 5).transform { value ->
78-
emit("Task executed $value times")
79-
}.collect {
80-
println(it)
81-
delay(1000.milliseconds)
52+
flow.take(10).collect{
53+
count++
54+
println(count)
8255
}
83-
}
84-
85-
// Using flow, take & collect
8656

57+
assertEquals(10, count)
58+
}
8759

8860
}

0 commit comments

Comments
 (0)