Skip to content

Commit 6d20245

Browse files
committed
RandomSeedTestRule.kt added
1 parent 9c9c43d commit 6d20245

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

firebase-dataconnect/androidTestutil/androidTestutil.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies {
5858
implementation(libs.androidx.test.core)
5959
implementation(libs.androidx.test.junit)
6060
implementation(libs.auth0.jwt)
61+
implementation(libs.kotest.property)
6162
implementation(libs.kotlinx.coroutines.core)
6263
implementation(libs.kotlinx.serialization.core)
6364
implementation(libs.kotlinx.serialization.json)

firebase-dataconnect/androidTestutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/DataConnectIntegrationTestBase.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.google.firebase.dataconnect.testutil
1919
import androidx.test.ext.junit.runners.AndroidJUnit4
2020
import com.google.firebase.dataconnect.ConnectorConfig
2121
import com.google.firebase.util.nextAlphanumericString
22+
import io.kotest.property.RandomSource
2223
import kotlin.random.Random
2324
import org.junit.Rule
2425
import org.junit.rules.TestName
@@ -35,6 +36,10 @@ abstract class DataConnectIntegrationTestBase {
3536

3637
@get:Rule val dataConnectFactory = TestDataConnectFactory(firebaseAppFactory)
3738

39+
@get:Rule(order = Int.MIN_VALUE) val randomSeedTestRule = RandomSeedTestRule()
40+
41+
val rs: RandomSource by randomSeedTestRule.rs
42+
3843
companion object {
3944
val testConnectorConfig: ConnectorConfig
4045
get() =

firebase-dataconnect/src/androidTest/kotlin/com/google/firebase/dataconnect/AuthIntegrationTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import io.kotest.matchers.collections.shouldNotContainNull
4141
import io.kotest.matchers.nulls.shouldNotBeNull
4242
import io.kotest.matchers.shouldBe
4343
import io.kotest.property.Arb
44-
import io.kotest.property.RandomSource
4544
import io.kotest.property.arbitrary.next
4645
import java.util.concurrent.CopyOnWriteArrayList
4746
import kotlin.random.Random
@@ -60,7 +59,6 @@ import org.junit.runner.RunWith
6059
class AuthIntegrationTest : DataConnectIntegrationTestBase() {
6160

6261
private val key = "e6w33rw36t"
63-
private val rs = RandomSource.default()
6462

6563
@get:Rule val inProcessDataConnectGrpcServer = InProcessDataConnectGrpcServer()
6664

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.firebase.dataconnect.testutil
17+
18+
import io.kotest.property.RandomSource
19+
import org.junit.rules.TestRule
20+
import org.junit.runner.Description
21+
import org.junit.runners.model.Statement
22+
23+
/**
24+
* A JUnit test rule that prints the seed of a [RandomSource] if the test fails, enabling replaying
25+
* the test with the same seed to investigate failures. If the [Lazy] is never initialized, then the
26+
* random seed is _not_ printed.
27+
*/
28+
class RandomSeedTestRule(val rs: Lazy<RandomSource>) : TestRule {
29+
30+
constructor() : this(lazy(LazyThreadSafetyMode.PUBLICATION) { RandomSource.default() })
31+
32+
override fun apply(base: Statement, description: Description) =
33+
object : Statement() {
34+
override fun evaluate() {
35+
val result = base.runCatching { evaluate() }
36+
result.onFailure {
37+
if (rs.isInitialized()) {
38+
println(
39+
"55negqf33k Test ${description.displayName} failed using " +
40+
"RandomSource with seed=${rs.value.seed}"
41+
)
42+
}
43+
}
44+
result.getOrThrow()
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)