summaryrefslogtreecommitdiff
path: root/coroutine-utils-testing
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-02-01 19:21:01 +0530
committerGitHub <noreply@github.com>2022-02-01 19:21:01 +0530
commitcf111f197856673185817921460025e7324ac512 (patch)
tree7a2b93ff755e6b55063f0b61fdd5506de59c8df9 /coroutine-utils-testing
parentcf92d8a5a3b15f8866ef9782c68298f59dcb8aed (diff)
Refactor PasswordEntry TOTP calculation into a cold flow (#1702)
Diffstat (limited to 'coroutine-utils-testing')
-rw-r--r--coroutine-utils-testing/build.gradle.kts1
-rw-r--r--coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/turbine.ext.kt37
2 files changed, 38 insertions, 0 deletions
diff --git a/coroutine-utils-testing/build.gradle.kts b/coroutine-utils-testing/build.gradle.kts
index 96d87dc1..c4f7400b 100644
--- a/coroutine-utils-testing/build.gradle.kts
+++ b/coroutine-utils-testing/build.gradle.kts
@@ -11,4 +11,5 @@ dependencies {
implementation(projects.coroutineUtils)
implementation(libs.testing.junit)
implementation(libs.kotlin.coroutines.test)
+ api(libs.testing.turbine)
}
diff --git a/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/turbine.ext.kt b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/turbine.ext.kt
new file mode 100644
index 00000000..596be1a1
--- /dev/null
+++ b/coroutine-utils-testing/src/main/kotlin/dev/msfjarvis/aps/test/turbine.ext.kt
@@ -0,0 +1,37 @@
+/*
+ * Copyright © 2014-2022 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.test
+
+import app.cash.turbine.FlowTurbine
+import app.cash.turbine.test
+import kotlin.coroutines.coroutineContext
+import kotlin.time.Duration
+import kotlin.time.Duration.Companion.seconds
+import kotlin.time.ExperimentalTime
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.flowOn
+import kotlinx.coroutines.test.TestCoroutineScheduler
+import kotlinx.coroutines.test.UnconfinedTestDispatcher
+
+/**
+ * Wrapper for [test] that implements compatibility with kotlinx.coroutines 1.6.0
+ *
+ * @see "https://github.com/cashapp/turbine/issues/42#issuecomment-1000317026"
+ */
+@ExperimentalTime
+@ExperimentalCoroutinesApi
+public suspend fun <T> Flow<T>.test2(
+ timeout: Duration = 1.seconds,
+ validate: suspend FlowTurbine<T>.() -> Unit,
+) {
+ val testScheduler = coroutineContext[TestCoroutineScheduler]
+ return if (testScheduler == null) {
+ test(timeout, validate)
+ } else {
+ flowOn(UnconfinedTestDispatcher(testScheduler)).test(timeout, validate)
+ }
+}