All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.MergedEventSourceTest.kt Maven / Gradle / Ivy

package kt.mobius

import kt.mobius.disposables.Disposable
import kt.mobius.functions.Consumer
import kotlin.test.Test
import kotlin.test.assertTrue

class MergedEventSourceTest {

    @Test
    fun composesMultipleEventSources() {
        val s1 = TestEventSource()
        val s2 = TestEventSource()
        val s3 = TestEventSource()
        val s4 = TestEventSource()

        val mergedSource = MergedEventSource.from(s1, s2, s3, s4)
        val consumer = RecordingConsumer()
        val disposable = mergedSource.subscribe(consumer)

        s1.send("Hello")
        s3.send("World!")
        s2.send("We")
        s4.send("are")
        s1.send("all")
        s2.send("one")
        s3.send("event")
        s1.send("source")

        consumer.assertValues("Hello", "World!", "We", "are", "all", "one", "event", "source")
        disposable.dispose()
        assertTrue(s1.disposed)
        assertTrue(s2.disposed)
        assertTrue(s3.disposed)
        assertTrue(s4.disposed)
    }

    private class TestEventSource : EventSource {

        private var eventConsumer: Consumer? = null
        var disposed: Boolean = false
            private set

        override fun subscribe(eventConsumer: Consumer): Disposable {
            this.eventConsumer = eventConsumer;
            return Disposable {
                disposed = true
                this.eventConsumer = null
            }
        }

        fun send(s: T) {
            eventConsumer!!.accept(s)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy