@@ -2,47 +2,80 @@ package by.jprof.telegram.bot.core
2
2
3
3
import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
4
4
import dev.inmo.tgbotapi.types.update.abstracts.Update
5
+ import java.time.Duration
6
+ import java.util.concurrent.atomic.AtomicBoolean
7
+ import kotlin.random.Random
5
8
import kotlinx.coroutines.delay
6
9
import 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
8
13
import org.junit.jupiter.api.Test
9
- import java.time.Duration
10
- import java.util.concurrent.atomic.AtomicBoolean
11
14
12
15
internal class UpdateProcessingPipelineTest {
13
16
@Test
14
17
fun process () {
15
18
val completionFlags = (1 .. 5 ).map { AtomicBoolean (false ) }
16
19
val sut = UpdateProcessingPipeline (
17
- completionFlags.mapIndexed { index, atomicBoolean ->
20
+ processors = completionFlags.mapIndexed { index, atomicBoolean ->
18
21
Delay ((index + 1 ) * 1000L , atomicBoolean)
19
- }
22
+ },
23
+ timeout = 10_000 ,
20
24
)
21
25
22
- Assertions . assertTimeout(Duration .ofMillis(6000 )) {
26
+ assertTimeout(Duration .ofMillis(6000 )) {
23
27
sut.process(UnknownUpdate (1L , " " , JsonNull ))
24
28
}
25
- Assertions . assertTrue(completionFlags.all { it.get() })
29
+ assertTrue(completionFlags.all { it.get() })
26
30
}
27
31
28
32
@Test
29
33
fun processWithBroken () {
30
34
val completionFlags = (1 .. 5 ).map { AtomicBoolean (false ) }
31
35
val sut = UpdateProcessingPipeline (
32
- completionFlags.mapIndexed { index, atomicBoolean ->
36
+ processors = completionFlags.mapIndexed { index, atomicBoolean ->
33
37
when (index % 2 ) {
34
38
0 -> Delay ((index + 1 ) * 1000L , atomicBoolean)
35
39
else -> Fail ()
36
40
}
37
- }
41
+ },
42
+ timeout = 10_000 ,
38
43
)
39
44
40
- Assertions . assertTimeout(Duration .ofMillis(6000 )) {
45
+ assertTimeout(Duration .ofMillis(6000 )) {
41
46
sut.process(UnknownUpdate (1L , " " , JsonNull ))
42
47
}
43
48
completionFlags.forEachIndexed { index, atomicBoolean ->
44
49
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())
46
79
}
47
80
}
48
81
}
0 commit comments