Skip to content

Commit

Permalink
Make periodic task test more verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaGulya committed Jan 6, 2024
1 parent 64df02a commit f37282e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class MainSchedulerTest {
var executed = false
executor.submit(200.milliseconds) { executed = true }

return observableOf(
100.milliseconds, 300.milliseconds
)
return observableOf(100.milliseconds, 300.milliseconds)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { executed }
Expand All @@ -46,36 +44,31 @@ class MainSchedulerTest {
fun interval_task_executed_at_interval(): AsyncTestResult {
val scheduler = MainScheduler()
val executor = scheduler.newExecutor()
var counter = 0L
val items = mutableListOf<Long>()
var counter = 0
val items = mutableListOf<Int>()

val delayBetweenTasks = 50.milliseconds

executor.submit(period = delayBetweenTasks) {
executor.submit(period = 100.milliseconds) {
items.add(counter++)
}

val amountToTest = 10
val testTickShift = 25L
val checkTicks =
observableOf(50.milliseconds, 150.milliseconds, 250.milliseconds, 350.milliseconds, 450.milliseconds)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { items.toList() }
}

val testTicks = 1..amountToTest
val expectedItems = testTicks.map { tick ->
(0 until tick).map { it.toLong() }.toList()
}.toList()
val expectedResults =
listOf(
emptyList(),
listOf(0),
listOf(0, 1),
listOf(0, 1, 2),
listOf(0, 1, 2, 3),
)

return observableOf(
*testTicks
.map { it * 50L + testTickShift }
.map { it.milliseconds }
.toTypedArray()
)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { items.toList() }
}
.toList()
.testAwait { tickStates ->
assertEquals(expectedItems, tickStates)
}
return checkTicks.toList().testAwait { results ->
assertEquals(expectedResults, results)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ class Counter : DisposableScope by DisposableScope() {
return observableOfEmpty()
}


return when (event) {
is Event.Increment -> observableOf(Msg.Set(value = state.value + 5L))
is Event.Increment -> observableOf(Msg.Set(value = state.value + 1L))
is Event.Decrement -> observableOf(Msg.Set(value = state.value - 1L))
is Event.Reset -> observableOf(Msg.Set(value = 0L))
is Event.Fibonacci -> fibonacci(n = state.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.badoo.reaktive.base.Consumer
import com.badoo.reaktive.disposable.Disposable
import com.badoo.reaktive.disposable.scope.DisposableScope
import com.badoo.reaktive.observable.Observable
import com.badoo.reaktive.observable.flatMap
import com.badoo.reaktive.observable.map
import com.badoo.reaktive.observable.switchMap
import com.badoo.reaktive.subject.behavior.BehaviorObservable
Expand Down

0 comments on commit f37282e

Please sign in to comment.