Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wasmJs target #771

Merged
merged 10 commits into from
Jan 11, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
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
Expand All @@ -9,4 +20,62 @@ class MainSchedulerTest {
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
IlyaGulya marked this conversation as resolved.
Show resolved Hide resolved
)
.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 = 0L
val items = mutableListOf<Long>()

val delayBetweenTasks = 50.milliseconds
IlyaGulya marked this conversation as resolved.
Show resolved Hide resolved

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

val amountToTest = 10
val testTickShift = 25L
IlyaGulya marked this conversation as resolved.
Show resolved Hide resolved

val testTicks = 1..amountToTest
val expectedItems = testTicks.map { tick ->
(0 until tick).map { it.toLong() }.toList()
}.toList()

return observableOf(
*testTicks
.map { it * 50L + testTickShift }
IlyaGulya marked this conversation as resolved.
Show resolved Hide resolved
.map { it.milliseconds }
.toTypedArray()
)
.flatMapSingle { timeout ->
singleTimer(timeout, scheduler = scheduler)
.map { items.toList() }
}
.toList()
.testAwait { tickStates ->
assertEquals(expectedItems, tickStates)
IlyaGulya marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
3 changes: 2 additions & 1 deletion sample-js-browser-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* :wasmJsBrowserProductionRun - release mode WASM-JS
* :wasmJsBrowserDevelopmentRun - debug mode WASM-JS
*/
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl

plugins {
id("kotlin-multiplatform")
Expand All @@ -16,6 +16,7 @@ kotlin {
browser()
binaries.executable()
}
@OptIn(ExperimentalWasmDsl::class)
wasmJs {
binaries.executable()
browser()
Expand Down