diff options
author | therealbush <therealbush@users.noreply.github.com> | 2022-04-23 15:22:18 -0700 |
---|---|---|
committer | therealbush <therealbush@users.noreply.github.com> | 2022-04-23 15:22:18 -0700 |
commit | c12696bd528e891b9da126d3b92f3db089b4b6e6 (patch) | |
tree | 6683284c29f6dd7b9d65fae881527e05b54b3a84 /src/test/kotlin | |
parent | 1cb96200f8844e9192f55037cfc11bfb42e1d94f (diff) | |
download | eventbus-kotlin-c12696bd528e891b9da126d3b92f3db089b4b6e6.tar.gz eventbus-kotlin-c12696bd528e891b9da126d3b92f3db089b4b6e6.tar.bz2 eventbus-kotlin-c12696bd528e891b9da126d3b92f3db089b4b6e6.zip |
some changes
Diffstat (limited to 'src/test/kotlin')
-rw-r--r-- | src/test/kotlin/KotlinTest.kt | 76 |
1 files changed, 64 insertions, 12 deletions
diff --git a/src/test/kotlin/KotlinTest.kt b/src/test/kotlin/KotlinTest.kt index 3670703..136e7c8 100644 --- a/src/test/kotlin/KotlinTest.kt +++ b/src/test/kotlin/KotlinTest.kt @@ -1,7 +1,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking -import me.bush.illnamethislater.* +import kotlinx.coroutines.launch +import me.bush.eventbuskotlin.* import org.apache.logging.log4j.Level import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.core.config.Configurator @@ -9,6 +9,7 @@ import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance +import java.util.concurrent.atomic.AtomicInteger import kotlin.random.Random /** @@ -41,7 +42,7 @@ class KotlinTest { //////////////////////////////////////////////////////////////////////////////////////////////////////////////// @Test - fun `test listener priority and ability to cancel events or receive cancelled events`() { + fun `test priority and ability to cancel events or receive cancelled events`() { eventBus.subscribe(this) val event = SimpleEvent() eventBus.post(event) @@ -130,25 +131,75 @@ class KotlinTest { @Test fun `test parallel event posting`() { - runBlocking { - sus() + val listeners = mutableListOf<Listener>() + repeat(10) { + // Not sure what to test + listeners += listener<Unit>(parallel = true) { + logger.info("Thread:" + Thread.currentThread().name) + } } - } + listeners.forEach { eventBus.register(it) } + eventBus.post(Unit) + listeners.forEach { eventBus.unregister(it) } - suspend fun sus() { - println() - } + /* I'm not sure what else to test for this, but I'm also not really happy with parallel event posting yet. TODO + + [DefaultDispatcher-worker-5 @coroutine#5] INFO Kotlin Test - DefaultDispatcher-worker-5 @coroutine#5 + [DefaultDispatcher-worker-2 @coroutine#2] INFO Kotlin Test - DefaultDispatcher-worker-2 @coroutine#2 + [DefaultDispatcher-worker-1 @coroutine#1] INFO Kotlin Test - DefaultDispatcher-worker-1 @coroutine#1 + [DefaultDispatcher-worker-4 @coroutine#4] INFO Kotlin Test - DefaultDispatcher-worker-4 @coroutine#4 + [DefaultDispatcher-worker-7 @coroutine#7] INFO Kotlin Test - DefaultDispatcher-worker-7 @coroutine#7 + [DefaultDispatcher-worker-6 @coroutine#6] INFO Kotlin Test - DefaultDispatcher-worker-6 @coroutine#6 + [DefaultDispatcher-worker-8 @coroutine#8] INFO Kotlin Test - DefaultDispatcher-worker-8 @coroutine#8 + [DefaultDispatcher-worker-9 @coroutine#9] INFO Kotlin Test - DefaultDispatcher-worker-9 @coroutine#9 + [DefaultDispatcher-worker-3 @coroutine#3] INFO Kotlin Test - DefaultDispatcher-worker-3 @coroutine#3 + [DefaultDispatcher-worker-10 @coroutine#10] INFO Kotlin Test - DefaultDispatcher-worker-10 @coroutine#10 */ - fun sussy() { - println() } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// @Test - fun `call every method on multiple threads concurrently to ensure no CME is thrown`() { + fun `test thread safety`() { + val listeners = mutableListOf<Listener>() + repeat(10) { + listeners += listener<Any>(parallel = false) { + doStuff() + } + listeners += listener<Any>(parallel = true) { + doStuff() + } + } + listeners.forEach { eventBus.register(it) } + eventBus.debug() + CoroutineScope(Dispatchers.Default).launch { + repeat(100) { + launch { + doStuff() + eventBus.post(Any()) + } + } + } + Thread.sleep(2000) + Assertions.assertEquals(2100, counter.get()) + listeners.forEach { eventBus.unregister(it) } + eventBus.unregister(dummy) + eventBus.unsubscribe(this) + // Should be empty + eventBus.debug() + } + + private val dummy = listener<Unit> {} + + private var counter = AtomicInteger() + private fun doStuff() { + eventBus.unsubscribe(this) + eventBus.subscribe(this) + eventBus.unregister(dummy) + eventBus.register(dummy) + counter.getAndIncrement() } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -181,6 +232,7 @@ class KotlinTest { eventBus.subscribe(this) eventBus.post(Unit) Assertions.assertTrue(called) + eventBus.unsubscribe(this) } var called = false |