Skip to content

Commit c28216f

Browse files
mmollaverdisvc-squareup-copybara
authored andcommitted
Add helper methods to support running misk service tests in
parallel GitOrigin-RevId: cd048b38737715800dfdcb21650d3a8daa636f25
1 parent e528852 commit c28216f

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

misk-jdbc/api/misk-jdbc.api

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,11 @@ public final class misk/jdbc/MySqlTestDatabasePoolBackend : misk/jdbc/TestDataba
590590
public fun showDatabases ()Ljava/util/Set;
591591
}
592592

593+
public final class misk/jdbc/ParitionedDataSourceConfigKt {
594+
public static final fun updateForParallelTests (Lmisk/jdbc/DataSourceClusterConfig;)Lmisk/jdbc/DataSourceClusterConfig;
595+
public static final fun updateForParallelTests (Lmisk/jdbc/DataSourceConfig;)Lmisk/jdbc/DataSourceConfig;
596+
}
597+
593598
public final class misk/jdbc/PingDatabaseService : com/google/common/util/concurrent/AbstractIdleService {
594599
public fun <init> (Lmisk/jdbc/DataSourceConfig;Lwisp/deployment/Deployment;)V
595600
}

misk-jdbc/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
testFixturesImplementation(project(":misk"))
5757
testFixturesImplementation(project(":misk-core"))
5858
testFixturesImplementation(project(":misk-service"))
59+
testFixturesImplementation(project(":misk-testing"))
5960
testFixturesRuntimeOnly(libs.hsqldb)
6061

6162
testImplementation(libs.assertj)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package misk.jdbc
2+
3+
import misk.testing.updateForParallelTests
4+
5+
fun DataSourceConfig.updateForParallelTests(): DataSourceConfig = this.updateForParallelTests { config, partitionId ->
6+
config.copy(database = "${config.database}_$partitionId")
7+
}
8+
9+
fun DataSourceClusterConfig.updateForParallelTests(): DataSourceClusterConfig = this.copy(
10+
writer = writer.updateForParallelTests(),
11+
reader = reader?.updateForParallelTests()
12+
)

misk-testing/api/misk-testing.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ public final class misk/testing/MockTracingBackendModule : misk/inject/KAbstract
222222
public fun <init> ()V
223223
}
224224

225+
public final class misk/testing/ParallelTestsKt {
226+
public static final fun updateForParallelTests (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
227+
}
228+
225229
public final class misk/testing/TemporaryFolder {
226230
public fun <init> (Ljava/nio/file/Path;)V
227231
public final fun delete ()V
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package misk.testing
2+
3+
/**
4+
* Provides helper methods for supporting tests running in parallel.
5+
*/
6+
internal object ParallelTests {
7+
8+
/**
9+
* Can be used for determining if tests are running in parallel (with Gradle's `maxParallelForks`),
10+
* and returns a unique numeric partition ID for the current test process.
11+
*
12+
* This assumes that the environment variable `MAX_TEST_PARALLEL_FORKS` is set to the number of `maxParallelForks`.
13+
*/
14+
internal fun partitioned(): PartitionedTest {
15+
val maxTestPartitions = System.getenv("MAX_TEST_PARALLEL_FORKS")?.toInt()
16+
return if (maxTestPartitions != null && maxTestPartitions > 1) {
17+
val testWorkerId = System.getProperty("org.gradle.test.worker", "0").toInt()
18+
val partitionId = ((testWorkerId % maxTestPartitions) + 1)
19+
PartitionedTest.Partitioned(partitionId)
20+
} else {
21+
PartitionedTest.NotPartitioned
22+
}
23+
}
24+
}
25+
26+
internal sealed interface PartitionedTest {
27+
data object NotPartitioned : PartitionedTest
28+
data class Partitioned(val partitionId: Int) : PartitionedTest
29+
}
30+
31+
/**
32+
* Applies the `update` function to the given `T`, if the tests are running in parallel
33+
* (with Gradle's `maxParallelForks`). The `update` function is also passed the partition ID.
34+
*
35+
* This can be used for updating configurations for the purpose of providing isolation between
36+
* tests running across different parallel processes, such as appending the partition ID to the database name,
37+
* so that it's unique for each process.
38+
*
39+
* This assumes that the environment variable `MAX_TEST_PARALLEL_FORKS` is set to the number of `maxParallelForks`.
40+
*/
41+
fun <T> T.updateForParallelTests(update: (T, Int) -> T): T {
42+
return when (val partitionedTest = ParallelTests.partitioned()) {
43+
is PartitionedTest.Partitioned -> {
44+
update(this, partitionedTest.partitionId)
45+
}
46+
47+
is PartitionedTest.NotPartitioned -> this
48+
}
49+
}
50+

0 commit comments

Comments
 (0)