From 8db0b67ce9ba4a5e56c04c1bea3a738eacb176cf Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 9 Dec 2021 10:07:54 +0530 Subject: 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 --- coroutine-utils/api/coroutine-utils.api | 22 ++++++++++++++++++++++ coroutine-utils/build.gradle.kts | 13 +++++++++++++ .../aps/util/coroutines/DispatcherProvider.kt | 22 ++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 coroutine-utils/api/coroutine-utils.api create mode 100644 coroutine-utils/build.gradle.kts create mode 100644 coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/DispatcherProvider.kt (limited to 'coroutine-utils') diff --git a/coroutine-utils/api/coroutine-utils.api b/coroutine-utils/api/coroutine-utils.api new file mode 100644 index 00000000..000dde65 --- /dev/null +++ b/coroutine-utils/api/coroutine-utils.api @@ -0,0 +1,22 @@ +public final class dev/msfjarvis/aps/util/coroutines/DefaultDispatcherProvider : dev/msfjarvis/aps/util/coroutines/DispatcherProvider { + public fun ()V + public fun default ()Lkotlinx/coroutines/CoroutineDispatcher; + public fun io ()Lkotlinx/coroutines/CoroutineDispatcher; + public fun main ()Lkotlinx/coroutines/CoroutineDispatcher; + public fun unconfined ()Lkotlinx/coroutines/CoroutineDispatcher; +} + +public abstract interface class dev/msfjarvis/aps/util/coroutines/DispatcherProvider { + public abstract fun default ()Lkotlinx/coroutines/CoroutineDispatcher; + public abstract fun io ()Lkotlinx/coroutines/CoroutineDispatcher; + public abstract fun main ()Lkotlinx/coroutines/CoroutineDispatcher; + public abstract fun unconfined ()Lkotlinx/coroutines/CoroutineDispatcher; +} + +public final class dev/msfjarvis/aps/util/coroutines/DispatcherProvider$DefaultImpls { + public static fun default (Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider;)Lkotlinx/coroutines/CoroutineDispatcher; + public static fun io (Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider;)Lkotlinx/coroutines/CoroutineDispatcher; + public static fun main (Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider;)Lkotlinx/coroutines/CoroutineDispatcher; + public static fun unconfined (Ldev/msfjarvis/aps/util/coroutines/DispatcherProvider;)Lkotlinx/coroutines/CoroutineDispatcher; +} + diff --git a/coroutine-utils/build.gradle.kts b/coroutine-utils/build.gradle.kts new file mode 100644 index 00000000..2a20df08 --- /dev/null +++ b/coroutine-utils/build.gradle.kts @@ -0,0 +1,13 @@ +/* + * 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(libs.kotlin.coroutines.core) + implementation(libs.dagger.hilt.core) +} diff --git a/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/DispatcherProvider.kt b/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/DispatcherProvider.kt new file mode 100644 index 00000000..a7c4530d --- /dev/null +++ b/coroutine-utils/src/main/kotlin/dev/msfjarvis/aps/util/coroutines/DispatcherProvider.kt @@ -0,0 +1,22 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.util.coroutines + +import javax.inject.Inject +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers + +/** Interface to allow abstracting individual [CoroutineDispatcher]s out of class dependencies. */ +public interface DispatcherProvider { + + public fun main(): CoroutineDispatcher = Dispatchers.Main + public fun default(): CoroutineDispatcher = Dispatchers.Default + public fun io(): CoroutineDispatcher = Dispatchers.IO + public fun unconfined(): CoroutineDispatcher = Dispatchers.Unconfined +} + +/** Concrete type for [DispatcherProvider] with all the defaults from the class. */ +public class DefaultDispatcherProvider @Inject constructor() : DispatcherProvider -- cgit v1.2.3