diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2021-12-09 10:07:54 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-09 04:37:54 +0000 |
commit | 8db0b67ce9ba4a5e56c04c1bea3a738eacb176cf (patch) | |
tree | 46700b7a3da32065e073bffbf22765c7e2f30d32 /coroutine-utils-testing | |
parent | 933558caf8266677dc26497d7c7b930254f4fb07 (diff) |
Refactor coroutine testing setup (#1583)
* coroutine-utils: init
* coroutine-utils-testing: init
* format-common: switch over to using DispatcherProvider
* Convert Binds method to an extension function
* Add Dispatcher module
Diffstat (limited to 'coroutine-utils-testing')
3 files changed, 67 insertions, 0 deletions
diff --git a/coroutine-utils-testing/api/coroutine-utils-testing.api b/coroutine-utils-testing/api/coroutine-utils-testing.api new file mode 100644 index 00000000..a90e209f --- /dev/null +++ b/coroutine-utils-testing/api/coroutine-utils-testing.api @@ -0,0 +1,8 @@ +public final class dev/msfjarvis/aps/test/CoroutineTestRule : org/junit/rules/TestWatcher { + public fun <init> ()V + public fun <init> (Lkotlinx/coroutines/test/TestDispatcher;)V + public synthetic fun <init> (Lkotlinx/coroutines/test/TestDispatcher;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getTestDispatcher ()Lkotlinx/coroutines/test/TestDispatcher; + public final fun getTestDispatcherProvider ()Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider; +} + diff --git a/coroutine-utils-testing/build.gradle.kts b/coroutine-utils-testing/build.gradle.kts new file mode 100644 index 00000000..96d87dc1 --- /dev/null +++ b/coroutine-utils-testing/build.gradle.kts @@ -0,0 +1,14 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ +plugins { + kotlin("jvm") + id("com.github.android-password-store.kotlin-library") +} + +dependencies { + implementation(projects.coroutineUtils) + implementation(libs.testing.junit) + implementation(libs.kotlin.coroutines.test) +} diff --git a/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt new file mode 100644 index 00000000..fa4a2d41 --- /dev/null +++ b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/CoroutineTestRule.kt @@ -0,0 +1,45 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.test + +import dev.msfjarvis.aps.util.coroutines.DispatcherProvider +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineScheduler +import kotlinx.coroutines.test.TestDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.rules.TestWatcher +import org.junit.runner.Description + +/** + * JUnit [TestWatcher] to correctly handle setting and resetting a given [testDispatcher] for tests. + */ +@ExperimentalCoroutinesApi +public class CoroutineTestRule( + public val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(TestCoroutineScheduler()), +) : TestWatcher() { + + public val testDispatcherProvider: DispatcherProvider = + object : DispatcherProvider { + override fun default(): CoroutineDispatcher = testDispatcher + override fun io(): CoroutineDispatcher = testDispatcher + override fun main(): CoroutineDispatcher = testDispatcher + override fun unconfined(): CoroutineDispatcher = testDispatcher + } + + override fun starting(description: Description?) { + super.starting(description) + Dispatchers.setMain(testDispatcher) + } + + override fun finished(description: Description?) { + super.finished(description) + Dispatchers.resetMain() + } +} |