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
This commit is contained in:
parent
933558caf8
commit
8db0b67ce9
14 changed files with 173 additions and 13 deletions
8
coroutine-utils-testing/api/coroutine-utils-testing.api
Normal file
8
coroutine-utils-testing/api/coroutine-utils-testing.api
Normal file
|
@ -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;
|
||||
}
|
||||
|
14
coroutine-utils-testing/build.gradle.kts
Normal file
14
coroutine-utils-testing/build.gradle.kts
Normal file
|
@ -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)
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue