-
Notifications
You must be signed in to change notification settings - Fork 59
/
MainSchedulerTest.kt
77 lines (68 loc) · 2.45 KB
/
MainSchedulerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.badoo.reaktive.scheduler
import com.badoo.reaktive.observable.flatMapSingle
import com.badoo.reaktive.observable.observableOf
import com.badoo.reaktive.observable.toList
import com.badoo.reaktive.single.map
import com.badoo.reaktive.single.singleTimer
import com.badoo.reaktive.test.single.AsyncTestResult
import com.badoo.reaktive.test.single.testAwait
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
import kotlin.time.Duration.Companion.milliseconds
class MainSchedulerTest {
@Test
fun submits_task_to_executor() {
val scheduler = MainScheduler()
val executor = scheduler.newExecutor()
executor.submit {}
}
@Test
fun delayed_task_executed_with_delay(): AsyncTestResult {
val scheduler = MainScheduler()
val executor = scheduler.newExecutor()
var executed = false
executor.submit(200.milliseconds) { executed = true }
return observableOf(100.milliseconds, 300.milliseconds)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { executed }
}
.toList()
.testAwait { (before, after) ->
assertFalse(before)
assertTrue(after)
}
}
@Test
fun interval_task_executed_at_interval(): AsyncTestResult {
val scheduler = MainScheduler()
val executor = scheduler.newExecutor()
var counter = 0
val items = mutableListOf<Int>()
executor.submit(period = 100.milliseconds) {
items.add(counter++)
}
val checkTicks =
observableOf(50.milliseconds, 150.milliseconds, 250.milliseconds, 350.milliseconds, 450.milliseconds)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { items.toList() }
}
val expectedResults =
listOf(
emptyList(),
listOf(0),
listOf(0, 1),
listOf(0, 1, 2),
listOf(0, 1, 2, 3),
)
return checkTicks.toList().testAwait { results ->
assertEquals(expectedResults, results)
// Required to pass test on NodeJS environment since runtime waits
// for all tasks to cancel or finish their work.
executor.cancel()
}
}
}