@@ -2,47 +2,80 @@ package by.jprof.telegram.bot.core
22
33import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
44import dev.inmo.tgbotapi.types.update.abstracts.Update
5+ import java.time.Duration
6+ import java.util.concurrent.atomic.AtomicBoolean
7+ import kotlin.random.Random
58import kotlinx.coroutines.delay
69import kotlinx.serialization.json.JsonNull
7- import org.junit.jupiter.api.Assertions
10+ import org.junit.jupiter.api.Assertions.assertFalse
11+ import org.junit.jupiter.api.Assertions.assertTimeout
12+ import org.junit.jupiter.api.Assertions.assertTrue
813import org.junit.jupiter.api.Test
9- import java.time.Duration
10- import java.util.concurrent.atomic.AtomicBoolean
1114
1215internal class UpdateProcessingPipelineTest {
1316 @Test
1417 fun process () {
1518 val completionFlags = (1 .. 5 ).map { AtomicBoolean (false ) }
1619 val sut = UpdateProcessingPipeline (
17- completionFlags.mapIndexed { index, atomicBoolean ->
20+ processors = completionFlags.mapIndexed { index, atomicBoolean ->
1821 Delay ((index + 1 ) * 1000L , atomicBoolean)
19- }
22+ },
23+ timeout = 10_000 ,
2024 )
2125
22- Assertions . assertTimeout(Duration .ofMillis(6000 )) {
26+ assertTimeout(Duration .ofMillis(6000 )) {
2327 sut.process(UnknownUpdate (1L , " " , JsonNull ))
2428 }
25- Assertions . assertTrue(completionFlags.all { it.get() })
29+ assertTrue(completionFlags.all { it.get() })
2630 }
2731
2832 @Test
2933 fun processWithBroken () {
3034 val completionFlags = (1 .. 5 ).map { AtomicBoolean (false ) }
3135 val sut = UpdateProcessingPipeline (
32- completionFlags.mapIndexed { index, atomicBoolean ->
36+ processors = completionFlags.mapIndexed { index, atomicBoolean ->
3337 when (index % 2 ) {
3438 0 -> Delay ((index + 1 ) * 1000L , atomicBoolean)
3539 else -> Fail ()
3640 }
37- }
41+ },
42+ timeout = 10_000 ,
3843 )
3944
40- Assertions . assertTimeout(Duration .ofMillis(6000 )) {
45+ assertTimeout(Duration .ofMillis(6000 )) {
4146 sut.process(UnknownUpdate (1L , " " , JsonNull ))
4247 }
4348 completionFlags.forEachIndexed { index, atomicBoolean ->
4449 if (index % 2 == 0 ) {
45- Assertions .assertTrue(atomicBoolean.get())
50+ assertTrue(atomicBoolean.get())
51+ }
52+ }
53+ }
54+
55+ @Test
56+ fun processWithTimeout () {
57+ val completionFlags = (1 .. 6 ).map { AtomicBoolean (false ) }
58+ val sut = UpdateProcessingPipeline (
59+ processors = completionFlags.mapIndexed { index, atomicBoolean ->
60+ when (index % 3 ) {
61+ 0 -> Delay (Long .MAX_VALUE , atomicBoolean)
62+ 1 -> Delay (Random .nextLong(0 , 100 ), atomicBoolean)
63+ else -> Fail ()
64+ }
65+ },
66+ timeout = 1000 ,
67+ )
68+
69+ assertTimeout(Duration .ofMillis(2000 )) {
70+ sut.process(UnknownUpdate (1L , " " , JsonNull ))
71+ }
72+ completionFlags.forEachIndexed { index, atomicBoolean ->
73+ if (index % 3 == 1 ) {
74+ assertTrue(atomicBoolean.get())
75+ }
76+ when (index % 3 ) {
77+ 1 -> assertTrue(atomicBoolean.get())
78+ else -> assertFalse(atomicBoolean.get())
4679 }
4780 }
4881 }
0 commit comments